Skip to content

Commit 3b292fa

Browse files
committed
Use spk directly for create ptrs and other fixes
1 parent 49ae602 commit 3b292fa

File tree

7 files changed

+29
-34
lines changed

7 files changed

+29
-34
lines changed

client/src/bin/space-cli.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ use spaces_client::{
3838
use spaces_client::rpc::{CommitParams, CreatePtrParams, TransferPtrParams};
3939
use spaces_client::store::Sha256;
4040
use spaces_protocol::bitcoin::{Amount, FeeRate, OutPoint, Txid};
41-
use spaces_protocol::bitcoin::params::Params;
4241
use spaces_protocol::slabel::SLabel;
4342
use spaces_ptr::sptr::Sptr;
44-
use spaces_wallet::{bitcoin, bitcoin::secp256k1::schnorr::Signature, export::WalletExport, nostr::{NostrEvent, NostrTag}, Listing};
43+
use spaces_wallet::{bitcoin::secp256k1::schnorr::Signature, export::WalletExport, nostr::{NostrEvent, NostrTag}, Listing};
4544
use spaces_wallet::address::SpaceAddress;
4645
use spaces_wallet::bitcoin::hashes::sha256;
4746
use spaces_wallet::bitcoin::ScriptBuf;
@@ -1062,15 +1061,11 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
10621061
let spk = ScriptBuf::from(hex::decode(spk)
10631062
.map_err(|_| ClientError::Custom("Invalid spk hex".to_string()))?);
10641063

1065-
let addr = bitcoin::address::Address::from_script(spk.as_script(),
1066-
Params::new(cli.network.fallback_network()))
1067-
.map_err(|_| ClientError::Custom("Invalid spk".to_string()))?;
1068-
1069-
let sptr = Sptr::from_spk::<Sha256>(spk);
1064+
let sptr = Sptr::from_spk::<Sha256>(spk.clone());
10701065
println!("Creating sptr: {}", sptr);
10711066
cli.send_request(
10721067
Some(RpcWalletRequest::CreatePtr(CreatePtrParams {
1073-
address: addr.to_string(),
1068+
spk: hex::encode(spk.as_bytes()),
10741069
})),
10751070
None,
10761071
fee_rate,
@@ -1129,26 +1124,27 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
11291124
return Err(ClientError::Custom("space is already operational".to_string()));
11301125
}
11311126

1132-
let sptr_addr = cli.client.wallet_get_new_address(&cli.wallet, AddressKind::Space).await?;
1133-
let sptr = Sptr::from_spk::<Sha256>(SpaceAddress::from_str(&sptr_addr)
1134-
.expect("valid").script_pubkey());
1127+
let address = cli.client.wallet_get_new_address(&cli.wallet, AddressKind::Space).await?;
1128+
let address = SpaceAddress::from_str(&address)
1129+
.expect("valid");
1130+
let spk = address.script_pubkey();
1131+
let sptr = Sptr::from_spk::<Sha256>(spk.clone());
11351132

11361133
println!("Assigning space to sptr {}", sptr);
11371134
cli.send_request(
11381135
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
11391136
spaces: vec![space],
1140-
to: Some(sptr_addr.clone()),
1137+
to: Some(address.to_string()),
11411138
})),
11421139
None,
11431140
fee_rate,
11441141
false,
11451142
)
11461143
.await?;
1147-
let addr = SpaceAddress::from_str(&sptr_addr).expect("valid");
11481144
println!("Creating UTXO for sptr {}", sptr);
11491145
cli.send_request(
11501146
Some(RpcWalletRequest::CreatePtr(CreatePtrParams {
1151-
address: addr.0.to_string(),
1147+
spk: hex::encode(spk.as_bytes()),
11521148
})),
11531149
None,
11541150
fee_rate,

client/src/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ impl Client {
286286
},
287287
});
288288
}
289+
289290
self.apply_ptrs_tx(chain, tx, ptrs_validated);
290291
}
291292
}

client/src/rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ pub struct TransferPtrParams {
448448

449449
#[derive(Clone, Serialize, Deserialize)]
450450
pub struct CreatePtrParams {
451-
pub address: String,
451+
pub spk: String,
452452
}
453453

454454
#[derive(Clone, Serialize, Deserialize)]

client/src/spaces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl Spaced {
176176
pub fn ptr_genesis(network: ExtendedNetwork) -> ChainAnchor {
177177
match network {
178178
ExtendedNetwork::Testnet4 => ChainAnchor::PTR_TESTNET4(),
179-
ExtendedNetwork::Regtest => ChainAnchor::PTR_TESTNET4(),
179+
ExtendedNetwork::Regtest => ChainAnchor::PTR_REGTEST(),
180180
_ => panic!("unsupported network"),
181181
}
182182
}

client/src/wallets.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use tokio::{
3636
time::Instant,
3737
};
3838
use spaces_protocol::bitcoin::address::ParseError;
39-
use spaces_protocol::bitcoin::Network;
39+
use spaces_protocol::bitcoin::{Network, ScriptBuf};
4040
use spaces_ptr::sptr::{Sptr, SptrParseError, SPTR_HRP};
4141
use spaces_wallet::builder::{CommitmentRequest, PtrRequest, PtrTransfer};
4242
use crate::{calc_progress, checker::TxChecker, client::BlockSource, config::ExtendedNetwork, rpc::{RpcWalletRequest, RpcWalletTxBuilder, WalletLoadRequest}, source::{
@@ -1275,10 +1275,12 @@ impl RpcWallet {
12751275
}
12761276
}
12771277
RpcWalletRequest::CreatePtr(params) => {
1278-
let addr = Address::from_str(&params.address)
1279-
.map_err(|e| anyhow!("transferptr: invalid address for ptr {}: {:?}", params.address, e))?;
1278+
let spk_raw = hex::decode(params.spk)
1279+
.map_err(|e| anyhow!("transferptr: invalid spk: {:?}", e))?;
1280+
1281+
let spk = ScriptBuf::from(spk_raw);
12801282
builder = builder.add_ptr(PtrRequest {
1281-
ptr: addr,
1283+
spk,
12821284
})
12831285
}
12841286
RpcWalletRequest::Commit(params) => {

client/tests/ptr_tests.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@ use spaces_ptr::transcript_hash;
1616
use spaces_testutil::TestRig;
1717
use spaces_wallet::{export::WalletExport};
1818
use spaces_wallet::address::SpaceAddress;
19-
use spaces_wallet::bitcoin::{Network, ScriptBuf};
2019

2120
const ALICE: &str = "wallet_99";
2221
const BOB: &str = "wallet_98";
2322
const EVE: &str = "wallet_93";
2423

2524
async fn it_should_create_sptrs(rig: &TestRig) -> anyhow::Result<()> {
26-
27-
println!("sptr example: {}", Sptr::from_spk::<Sha256>(ScriptBuf::from(hex::decode("5120d3c3196cb3ed7fa79c882ed62f8e5942e546130d5ae5983da67dbb6c9bdd2e79").expect("valid"))));
28-
2925
rig.wait_until_wallet_synced(ALICE).await?;
3026

3127
// 1) Create ptr bound to addr0 (spk0)
3228
let addr0 = rig.spaced.client.wallet_get_new_address(ALICE, AddressKind::Coin).await?;
29+
let addr0_spk = bitcoin::address::Address::from_str(&addr0)
30+
.expect("valid").assume_checked()
31+
.script_pubkey();
32+
let addr0_spk_string = hex::encode(addr0_spk.as_bytes());
33+
3334
let create0 = wallet_do(
3435
rig,
3536
ALICE,
36-
vec![RpcWalletRequest::CreatePtr(CreatePtrParams { address: addr0.clone() })],
37+
vec![RpcWalletRequest::CreatePtr(CreatePtrParams { spk: addr0_spk_string.clone() })],
3738
false,
3839
).await.expect("CreatePtr addr0");
3940
assert!(wallet_res_err(&create0).is_ok(), "CreatePtr(addr0) must not error");
@@ -86,7 +87,7 @@ async fn it_should_create_sptrs(rig: &TestRig) -> anyhow::Result<()> {
8687
let dup = wallet_do(
8788
rig,
8889
ALICE,
89-
vec![RpcWalletRequest::CreatePtr(CreatePtrParams { address: addr0.clone() })],
90+
vec![RpcWalletRequest::CreatePtr(CreatePtrParams { spk: addr0_spk_string })],
9091
false,
9192
).await.expect("duplicate CreatePtr(addr0)");
9293
assert!(wallet_res_err(&dup).is_ok(), "duplicate CreatePtr should not error");
@@ -171,15 +172,11 @@ async fn it_should_operate_space(rig: &TestRig) -> anyhow::Result<()> {
171172
assert_eq!(current_spk, spk_after, "space spk must remain the same after renewal");
172173

173174
// --- Create/bind an SPTR using the SAME scriptPubKey as the space ---
174-
let space_addr = bitcoin::address::Address::from_script(&current_spk, Network::Regtest)
175-
.expect("derive address from space spk")
176-
.to_string();
177-
178175
let create_ptr_res = wallet_do(
179176
rig,
180177
ALICE,
181178
vec![RpcWalletRequest::CreatePtr(CreatePtrParams {
182-
address: space_addr.clone(),
179+
spk: hex::encode(current_spk.as_bytes()),
183180
})],
184181
false,
185182
)

wallet/src/builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use bitcoin::{
2121
Amount, FeeRate, Network, OutPoint, Psbt, Script, ScriptBuf, Sequence, Transaction, TxOut,
2222
Txid, Weight, Witness,
2323
};
24-
use bitcoin::address::NetworkUnchecked;
2524
use spaces_protocol::{
2625
bitcoin::absolute::Height,
2726
constants::{BID_PSBT_INPUT_SEQUENCE, BID_PSBT_TX_VERSION},
@@ -124,7 +123,7 @@ pub struct RegisterRequest {
124123

125124
#[derive(Debug, Clone)]
126125
pub struct PtrRequest {
127-
pub ptr: Address<NetworkUnchecked>,
126+
pub spk: ScriptBuf,
128127
}
129128

130129
#[derive(Debug, Clone)]
@@ -978,7 +977,7 @@ impl Builder {
978977
// Add any binds last to not mess with input/output order for transfers
979978
for ptr in params.binds {
980979
builder.add_recipient(
981-
ptr.ptr.assume_checked().script_pubkey(),
980+
ptr.spk,
982981
ptr_utxo_dust(Amount::from_sat(1000)),
983982
);
984983
}

0 commit comments

Comments
 (0)