3
3
//! Tests for methods found under the `== Wallet ==` section of the API docs.
4
4
5
5
#![ allow( non_snake_case) ] // Test names intentionally use double underscore.
6
+ #![ allow( unused_imports) ] // Some imports are only used in specific versions.
6
7
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 } ;
10
10
use integration_test:: { Node , NodeExt as _, Wallet } ;
11
- use node:: { mtype, AddressType , ImportMultiRequest , ImportMultiScriptPubKey , ImportMultiTimestamp } ;
11
+ use node:: { mtype, AddressType , ImportDescriptorsRequest , ImportMultiRequest , ImportMultiScriptPubKey , ImportMultiTimestamp } ;
12
12
use node:: vtype:: * ; // All the version specific types.
13
13
use std:: fs;
14
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
14
15
15
16
#[ test]
16
17
fn wallet__abandon_transaction ( ) {
@@ -313,8 +314,6 @@ fn wallet__import_address() {
313
314
#[ test]
314
315
#[ cfg( not( feature = "v20_and_below" ) ) ]
315
316
fn wallet__import_descriptors ( ) {
316
- use node:: { serde_json, ImportDescriptorsRequest } ;
317
-
318
317
let node = Node :: with_wallet ( Wallet :: None , & [ ] ) ;
319
318
let wallet_name = "desc_wallet" ;
320
319
@@ -325,15 +324,37 @@ fn wallet__import_descriptors() {
325
324
#[ cfg( not( feature = "v22_and_below" ) ) ]
326
325
node. client . create_wallet ( wallet_name) . expect ( "create wallet" ) ;
327
326
328
- let address = node. client . new_address ( ) . expect ( "failed to get new address" ) ;
329
- let descriptor = format ! ( "addr({})" , address) ;
327
+ node. fund_wallet ( ) ;
330
328
331
- let request = ImportDescriptorsRequest {
332
- desc : descriptor,
333
- timestamp : serde_json:: Value :: String ( "now" . to_string ( ) ) ,
334
- } ;
329
+ // 1. Get the current time
330
+ let start_time = SystemTime :: now ( )
331
+ . duration_since ( UNIX_EPOCH )
332
+ . expect ( "failed to get current time" )
333
+ . as_secs ( ) ;
335
334
336
- let _: ImportDescriptors = node. client . import_descriptors ( & [ request] ) . expect ( "importdescriptors" ) ;
335
+ // 2. Use a known private key, derive the address from it and send some coins to it.
336
+ let privkey =
337
+ PrivateKey :: from_wif ( "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy" ) . unwrap ( ) ;
338
+ let secp = secp256k1:: Secp256k1 :: new ( ) ;
339
+ let pubkey = privkey. public_key ( & secp) ;
340
+ let address = Address :: p2wpkh ( & CompressedPublicKey ( pubkey. inner ) , KnownHrp :: Regtest ) ;
341
+ let amount = Amount :: from_sat ( 10_000 ) ;
342
+ let _txid = node. client . send_to_address ( & address, amount) . expect ( "sendtoaddress" ) ;
343
+
344
+ // 3. Get the descriptor from the private key.
345
+ let raw_descriptor = format ! ( "wpkh({})" , privkey. to_wif( ) ) ;
346
+ let info = node. client . get_descriptor_info ( & raw_descriptor) . expect ( "get_descriptor_info" ) ;
347
+ let descriptor = format ! ( "{}#{}" , raw_descriptor, info. checksum) ;
348
+
349
+ // 4. Mine 100 blocks
350
+ let mining_address = node. client . new_address ( ) . expect ( "failed to get mining address" ) ;
351
+ let _blocks = node. client . generate_to_address ( 100 , & mining_address) . expect ( "generatetoaddress" ) ;
352
+
353
+ // 5. Scan for the descriptor using the time from (1)
354
+ let request = ImportDescriptorsRequest :: new ( descriptor, start_time) ;
355
+ let result: ImportDescriptors = node. client . import_descriptors ( & [ request] ) . expect ( "importdescriptors" ) ;
356
+ assert_eq ! ( result. 0 . len( ) , 1 , "should have exactly one import result" ) ;
357
+ assert ! ( result. 0 [ 0 ] . success) ;
337
358
}
338
359
339
360
#[ test]
0 commit comments