33//! Tests for methods found under the `== Wallet ==` section of the API docs.
44
55#![ allow( non_snake_case) ] // Test names intentionally use double underscore.
6+ #![ allow( unused_imports) ] // Some imports are only used in specific versions.
67
7- #[ cfg( feature = "TODO" ) ]
8- use bitcoin:: address:: { Address , NetworkChecked } ;
9- use bitcoin:: { Amount , FeeRate , PrivateKey , PublicKey } ;
8+ use bitcoin:: address:: { Address , KnownHrp , NetworkChecked } ;
9+ use bitcoin:: { secp256k1, Amount , CompressedPublicKey , FeeRate , PrivateKey , PublicKey } ;
1010use integration_test:: { Node , NodeExt as _, Wallet } ;
1111use node:: { mtype, AddressType , ImportMultiRequest , ImportMultiScriptPubKey , ImportMultiTimestamp } ;
12+
13+ #[ cfg( not( feature = "v20_and_below" ) ) ]
14+ use node:: ImportDescriptorsRequest ;
15+
1216use node:: vtype:: * ; // All the version specific types.
1317use std:: fs;
18+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
1419
1520#[ test]
1621fn wallet__abandon_transaction ( ) {
@@ -326,8 +331,6 @@ fn wallet__import_address() {
326331#[ test]
327332#[ cfg( not( feature = "v20_and_below" ) ) ]
328333fn wallet__import_descriptors ( ) {
329- use node:: { serde_json, ImportDescriptorsRequest } ;
330-
331334 let node = Node :: with_wallet ( Wallet :: None , & [ ] ) ;
332335 let wallet_name = "desc_wallet" ;
333336
@@ -338,15 +341,37 @@ fn wallet__import_descriptors() {
338341 #[ cfg( not( feature = "v22_and_below" ) ) ]
339342 node. client . create_wallet ( wallet_name) . expect ( "create wallet" ) ;
340343
341- let address = node. client . new_address ( ) . expect ( "failed to get new address" ) ;
342- let descriptor = format ! ( "addr({})" , address) ;
344+ node. fund_wallet ( ) ;
343345
344- let request = ImportDescriptorsRequest {
345- descriptor,
346- timestamp : serde_json:: Value :: String ( "now" . to_string ( ) ) ,
347- } ;
346+ // 1. Get the current time
347+ let start_time = SystemTime :: now ( )
348+ . duration_since ( UNIX_EPOCH )
349+ . expect ( "failed to get current time" )
350+ . as_secs ( ) ;
348351
349- let _: ImportDescriptors = node. client . import_descriptors ( & [ request] ) . expect ( "importdescriptors" ) ;
352+ // 2. Use a known private key, derive the address from it and send some coins to it.
353+ let privkey =
354+ PrivateKey :: from_wif ( "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy" ) . unwrap ( ) ;
355+ let secp = secp256k1:: Secp256k1 :: new ( ) ;
356+ let pubkey = privkey. public_key ( & secp) ;
357+ let address = Address :: p2wpkh ( & CompressedPublicKey ( pubkey. inner ) , KnownHrp :: Regtest ) ;
358+ let amount = Amount :: from_sat ( 10_000 ) ;
359+ let _txid = node. client . send_to_address ( & address, amount) . expect ( "sendtoaddress" ) ;
360+
361+ // 3. Get the descriptor from the private key.
362+ let raw_descriptor = format ! ( "wpkh({})" , privkey. to_wif( ) ) ;
363+ let info = node. client . get_descriptor_info ( & raw_descriptor) . expect ( "get_descriptor_info" ) ;
364+ let descriptor = format ! ( "{}#{}" , raw_descriptor, info. checksum) ;
365+
366+ // 4. Mine 100 blocks
367+ let mining_address = node. client . new_address ( ) . expect ( "failed to get mining address" ) ;
368+ let _blocks = node. client . generate_to_address ( 100 , & mining_address) . expect ( "generatetoaddress" ) ;
369+
370+ // 5. Scan for the descriptor using the time from (1)
371+ let request = ImportDescriptorsRequest :: new ( descriptor, start_time) ;
372+ let result: ImportDescriptors = node. client . import_descriptors ( & [ request] ) . expect ( "importdescriptors" ) ;
373+ assert_eq ! ( result. 0 . len( ) , 1 , "should have exactly one import result" ) ;
374+ assert ! ( result. 0 [ 0 ] . success) ;
350375}
351376
352377#[ test]
0 commit comments