Skip to content

Commit 66243d0

Browse files
committed
test: Add sp createtx test
1 parent bbb2a5b commit 66243d0

2 files changed

Lines changed: 132 additions & 14 deletions

File tree

tests/integration/init.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ mod test_config {
293293
}
294294
}
295295

296-
297-
// SILENT PAYMENTS
296+
// SILENT PAYMENTS
298297
#[cfg(feature = "silent-payments")]
299298
mod test_silent_payments {
300299
use super::*;
@@ -305,13 +304,19 @@ mod test_silent_payments {
305304
#[test]
306305
fn test_silent_payment_code_network_hrp() {
307306
BdkCli::new("regtest", None)
308-
.cmd("silent_payment_code", &["--scan_key", SCAN, "--spend_key", SPEND])
307+
.cmd(
308+
"silent_payment_code",
309+
&["--scan_key", SCAN, "--spend_key", SPEND],
310+
)
309311
.assert()
310312
.success()
311313
.stdout(predicate::str::contains("sprt1"));
312314

313315
BdkCli::new("testnet", None)
314-
.cmd("silent_payment_code", &["--scan_key", SCAN, "--spend_key", SPEND])
316+
.cmd(
317+
"silent_payment_code",
318+
&["--scan_key", SCAN, "--spend_key", SPEND],
319+
)
315320
.assert()
316321
.success()
317322
.stdout(predicate::str::contains("tsp1"));
@@ -320,7 +325,10 @@ mod test_silent_payments {
320325
#[test]
321326
fn test_silent_payment_code_rejects_invalid_pubkey() {
322327
BdkCli::new("regtest", None)
323-
.cmd("silent_payment_code", &["--scan_key", "deadbeef", "--spend_key", SPEND])
328+
.cmd(
329+
"silent_payment_code",
330+
&["--scan_key", "deadbeef", "--spend_key", SPEND],
331+
)
324332
.assert()
325333
.failure()
326334
.stderr(predicate::str::contains("malformed public key"));

tests/integration/online.rs

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,17 +484,11 @@ mod test_online {
484484
}
485485

486486
#[cfg(feature = "rpc")]
487-
mod test_online_rpc {
488-
use crate::common::BdkCli;
489-
use bdk_testenv::{TestEnv, bitcoincore_rpc::RpcApi};
490-
use bdk_wallet::bitcoin::{Address, Amount, Network};
491-
use serde_json::Value;
492-
use std::str::FromStr;
493-
use tempfile::TempDir;
487+
mod test_rpc {
488+
use super::*;
494489

495490
static WALLET_NAME: &str = "test_rpc_wallet";
496491

497-
498492
#[test]
499493
fn test_rpc_full_scan_reflects_funding() {
500494
let env = TestEnv::new().expect("start testenv");
@@ -537,7 +531,7 @@ mod test_online {
537531
.require_network(Network::Regtest)
538532
.unwrap();
539533

540-
// fund + confirm
534+
// fund + confirm
541535
let node_addr = env
542536
.rpc_client()
543537
.get_new_address(None, None)
@@ -570,4 +564,120 @@ mod test_online {
570564
);
571565
}
572566
}
567+
568+
#[cfg(feature = "silent-payments")]
569+
mod test_sp {
570+
use super::*;
571+
use bdk_wallet::bitcoin::{Transaction, consensus::encode::deserialize_hex};
572+
573+
const SCAN: &str = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";
574+
const SPEND: &str = "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5";
575+
static WALLET: &str = "sp_wallet";
576+
577+
#[test]
578+
fn test_create_sp_tx_produces_taproot_output() {
579+
let env = TestEnv::new().unwrap();
580+
let url = env.electrsd.electrum_url.as_str();
581+
let tmp = TempDir::new().unwrap();
582+
let cli = BdkCli::new("regtest", Some(tmp.path().to_path_buf()));
583+
584+
let desc = cli.cmd("descriptor", &["--type", "tr"]).output().unwrap();
585+
let dv: Value = serde_json::from_slice(&desc.stdout).unwrap();
586+
let ext_desc = dv["private_descriptors"]["external"].as_str().unwrap();
587+
let int_desc = dv["private_descriptors"]["internal"].as_str().unwrap();
588+
cli.build_base_cmd()
589+
.args([
590+
"wallet",
591+
"--wallet",
592+
WALLET,
593+
"config",
594+
"--ext-descriptor",
595+
ext_desc,
596+
"--int-descriptor",
597+
int_desc,
598+
"--client-type",
599+
"electrum",
600+
"--database-type",
601+
"sqlite",
602+
"--url",
603+
url,
604+
])
605+
.assert()
606+
.success();
607+
608+
let new_address = cli
609+
.wallet_cmd(&["--wallet", WALLET, "new_address"])
610+
.output()
611+
.unwrap();
612+
let addr = Address::from_str(
613+
serde_json::from_slice::<Value>(&new_address.stdout).unwrap()["address"]
614+
.as_str()
615+
.unwrap(),
616+
)
617+
.unwrap()
618+
.require_network(Network::Regtest)
619+
.unwrap();
620+
let node_address = env
621+
.rpc_client()
622+
.get_new_address(None, None)
623+
.unwrap()
624+
.assume_checked();
625+
env.mine_blocks(101, Some(node_address)).unwrap();
626+
env.wait_until_electrum_sees_block(Duration::from_secs(10))
627+
.unwrap();
628+
let txid = env.send(&addr, Amount::from_btc(0.5).unwrap()).unwrap();
629+
env.wait_until_electrum_sees_txid(txid, Duration::from_secs(10))
630+
.unwrap();
631+
env.mine_blocks(3, None).unwrap();
632+
env.wait_until_electrum_sees_block(Duration::from_secs(10))
633+
.unwrap();
634+
cli.wallet_cmd(&["--wallet", WALLET, "full_scan"])
635+
.assert()
636+
.success();
637+
638+
let silent_payment_output = cli
639+
.cmd(
640+
"silent_payment_code",
641+
&["--scan_key", SCAN, "--spend_key", SPEND],
642+
)
643+
.output()
644+
.unwrap();
645+
let sp_code =
646+
serde_json::from_slice::<Value>(&silent_payment_output.stdout).unwrap()["message"]
647+
.as_str()
648+
.unwrap()
649+
.to_string();
650+
651+
let out = cli
652+
.wallet_cmd(&[
653+
"--wallet",
654+
WALLET,
655+
"create_sp_tx",
656+
"--to-sp",
657+
&format!("{sp_code}:20000"),
658+
])
659+
.output()
660+
.unwrap();
661+
assert!(
662+
out.status.success(),
663+
"create_sp_tx failed: {}",
664+
String::from_utf8_lossy(&out.stderr)
665+
);
666+
let rawtx = serde_json::from_slice::<Value>(&out.stdout).unwrap()["raw_tx"]
667+
.as_str()
668+
.unwrap()
669+
.to_string();
670+
671+
let tx: Transaction = deserialize_hex(&rawtx).expect("invalid raw tx");
672+
let sp_out = tx
673+
.output
674+
.iter()
675+
.find(|o| o.value.to_sat() == 20_000)
676+
.expect("no 20_000 output");
677+
assert!(
678+
sp_out.script_pubkey.is_p2tr(),
679+
"SP recipient output should be P2TR"
680+
);
681+
}
682+
}
573683
}

0 commit comments

Comments
 (0)