Skip to content

Commit 6e86402

Browse files
committed
Improve import_descriptors test
The current test of the RPC always returns true due to scanning for the timestamp `now`. Improve the test to be more thorough by creating a new address and then scanning for the descriptor at a later time.
1 parent 5f55e62 commit 6e86402

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

integration_test/tests/wallet.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,53 @@ fn wallet__import_address() {
314314
#[cfg(not(feature = "v20_and_below"))]
315315
fn wallet__import_descriptors() {
316316
use node::{serde_json, ImportDescriptorsRequest};
317+
use std::time::{SystemTime, UNIX_EPOCH};
317318

318319
let node = Node::with_wallet(Wallet::None, &[]);
319320
let wallet_name = "desc_wallet";
321+
322+
#[cfg(feature = "v22_and_below")]
320323
node.client.create_wallet_with_descriptors(wallet_name).expect("create descriptor wallet");
321324

322-
let address = node.client.new_address().expect("failed to get new address");
323-
let descriptor = format!("addr({})", address);
325+
// v23 onwards uses descriptor wallets by default.
326+
#[cfg(not(feature = "v22_and_below"))]
327+
node.client.create_wallet(wallet_name).expect("create wallet");
328+
329+
node.fund_wallet();
330+
331+
// 1. Get the current time
332+
let start_time = SystemTime::now()
333+
.duration_since(UNIX_EPOCH)
334+
.expect("failed to get current time")
335+
.as_secs();
336+
337+
// 2. Use a known private key, derive the address from it and send some coins to it.
338+
let privkey =
339+
PrivateKey::from_wif("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap();
340+
let secp = bitcoin::secp256k1::Secp256k1::new();
341+
let pubkey = privkey.public_key(&secp);
342+
let address = bitcoin::Address::p2pkh(pubkey, privkey.network);
343+
let amount = bitcoin::Amount::from_sat(10_000);
344+
let _txid = node.client.send_to_address(&address, amount).expect("sendtoaddress");
345+
346+
// 3. Get the descriptor from the private key.
347+
let raw_descriptor = format!("wpkh({})", privkey.to_wif());
348+
let info = node.client.get_descriptor_info(&raw_descriptor).expect("get_descriptor_info");
349+
let descriptor = format!("{}#{}", raw_descriptor, info.checksum);
350+
351+
// 4. Mine 100 blocks
352+
let mining_address = node.client.new_address().expect("failed to get mining address");
353+
let _blocks = node.client.generate_to_address(100, &mining_address).expect("generatetoaddress");
324354

355+
// 5. Scan for the descriptor using the time from (1)
325356
let request = ImportDescriptorsRequest {
326357
desc: descriptor,
327-
timestamp: serde_json::Value::String("now".to_string()),
358+
timestamp: serde_json::Value::Number(serde_json::Number::from(start_time)),
328359
};
329360

330-
let _: ImportDescriptors = node.client.import_descriptors(&[request]).expect("importdescriptors");
361+
let result: ImportDescriptors = node.client.import_descriptors(&[request]).expect("importdescriptors");
362+
assert_eq!(result.0.len(), 1, "should have exactly one import result");
363+
assert!(result.0[0].success);
331364
}
332365

333366
#[test]

0 commit comments

Comments
 (0)