@@ -26,6 +26,7 @@ import {
26
26
standByUntilBurnBlock ,
27
27
testEnv ,
28
28
} from '../test-utils/test-helpers' ;
29
+ import { RPCClient } from 'rpc-bitcoin' ;
29
30
30
31
const BTC_PRIVATE_KEY = '0000000000000000000000000000000000000000000000000000000000000002' ;
31
32
@@ -44,35 +45,49 @@ describe.each([P2SH_P2WPKH, P2WPKH, P2WSH, P2TR])(
44
45
45
46
const { btcAddr, btcAddrDecoded, btcAddrRegtest, btcDescriptor } = addressSetup ( ) ;
46
47
48
+ let bitcoinRpcClient : RPCClient ;
49
+
50
+ test ( 'setup BTC wallet client' , async ( ) => {
51
+ const { BTC_RPC_PORT , BTC_RPC_HOST , BTC_RPC_PW , BTC_RPC_USER } = process . env ;
52
+ bitcoinRpcClient = new RPCClient ( {
53
+ url : BTC_RPC_HOST ,
54
+ port : Number ( BTC_RPC_PORT ) ,
55
+ user : BTC_RPC_USER ,
56
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
57
+ pass : BTC_RPC_PW ! ,
58
+ timeout : 120000 ,
59
+ wallet : btcAddrRegtest ,
60
+ } ) ;
61
+ const createWalletResult = await bitcoinRpcClient . createwallet ( {
62
+ wallet_name : btcAddrRegtest ,
63
+ blank : true ,
64
+ disable_private_keys : true ,
65
+ descriptors : true ,
66
+ load_on_startup : false ,
67
+ } as any ) ;
68
+ expect ( createWalletResult . name ) . toBe ( btcAddrRegtest ) ;
69
+ expect ( createWalletResult . warning ) . toBeFalsy ( ) ;
70
+
71
+ // descriptor wallets, if legacy wallet import fails
72
+ const info = await bitcoinRpcClient . getdescriptorinfo ( {
73
+ descriptor : btcDescriptor ,
74
+ } ) ;
75
+ const request = { label : btcAddrRegtest , desc : info . descriptor , timestamp : 'now' } ;
76
+ const importDescriptorRes : { success : boolean } [ ] = await bitcoinRpcClient . rpc (
77
+ 'importdescriptors' ,
78
+ { requests : [ request ] } ,
79
+ btcAddrRegtest
80
+ ) ;
81
+ expect ( importDescriptorRes [ 0 ] . success ) . toBe ( true ) ;
82
+ const btcWalletAddrs = await bitcoinRpcClient . getaddressesbylabel ( {
83
+ label : btcAddrRegtest ,
84
+ } ) ;
85
+ expect ( Object . keys ( btcWalletAddrs ) ) . toEqual ( [ btcAddrRegtest ] ) ;
86
+ } ) ;
87
+
47
88
test ( 'prepare' , async ( ) => {
48
89
await standByForPoxCycle ( ) ;
49
90
50
- try {
51
- // legacy wallets
52
- await testEnv . bitcoinRpcClient . importaddress ( {
53
- address : btcAddrRegtest ,
54
- label : btcAddrRegtest ,
55
- } ) ;
56
- const btcWalletAddrs = await testEnv . bitcoinRpcClient . getaddressesbylabel ( {
57
- label : btcAddrRegtest ,
58
- } ) ;
59
- expect ( Object . keys ( btcWalletAddrs ) ) . toContain ( btcAddrRegtest ) ;
60
- } catch ( e ) {
61
- // descriptor wallets, if legacy wallet import fails
62
- await withDescriptorWallet ( async walletName => {
63
- const info = await testEnv . bitcoinRpcClient . getdescriptorinfo ( {
64
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
65
- descriptor : btcDescriptor ! ,
66
- } ) ;
67
- const request = { label : btcAddrRegtest , desc : info . descriptor , timestamp : 'now' } ;
68
- await testEnv . bitcoinRpcClient . rpc (
69
- 'importdescriptors' ,
70
- { requests : [ request ] } ,
71
- walletName
72
- ) ;
73
- } ) ;
74
- }
75
-
76
91
poxInfo = await testEnv . client . getPox ( ) ;
77
92
78
93
burnBlockHeight = poxInfo . current_burnchain_block_height as number ;
@@ -172,7 +187,7 @@ describe.each([P2SH_P2WPKH, P2WPKH, P2WSH, P2TR])(
172
187
const rewards = await fetchGet < BurnchainRewardListResponse > (
173
188
`/extended/v1/burnchain/rewards/${ btcAddr } `
174
189
) ;
175
- expect ( rewards . results ) . toHaveLength ( 1 ) ;
190
+ expect ( rewards . results . length ) . toBeGreaterThan ( 0 ) ;
176
191
177
192
const firstReward = rewards . results . sort (
178
193
( a , b ) => a . burn_block_height - b . burn_block_height
@@ -207,7 +222,7 @@ describe.each([P2SH_P2WPKH, P2WPKH, P2WSH, P2TR])(
207
222
208
223
const blockResult : {
209
224
tx : { vout ?: { scriptPubKey : { address ?: string } ; value ?: number } [ ] } [ ] ;
210
- } = await testEnv . bitcoinRpcClient . getblock ( {
225
+ } = await bitcoinRpcClient . getblock ( {
211
226
blockhash : hexToBuffer ( firstReward . burn_block_hash ) . toString ( 'hex' ) ,
212
227
verbosity : 2 ,
213
228
} ) ;
@@ -231,43 +246,45 @@ describe.each([P2SH_P2WPKH, P2WPKH, P2WSH, P2TR])(
231
246
( a , b ) => a . burn_block_height - b . burn_block_height
232
247
) [ 0 ] ;
233
248
234
- let txs = await withDescriptorWallet (
235
- async walletName =>
236
- ( await testEnv . bitcoinRpcClient . listtransactions (
237
- {
238
- label : btcAddrRegtest ,
239
- include_watchonly : true ,
240
- } ,
241
- btcDescriptor ? walletName : undefined
242
- ) ) as {
243
- address : string ;
244
- category : string ;
245
- amount : number ;
246
- blockhash : string ;
247
- blockheight : number ;
248
- } [ ]
249
+ let txs : {
250
+ address : string ;
251
+ category : string ;
252
+ amount : number ;
253
+ blockhash : string ;
254
+ blockheight : number ;
255
+ } [ ] = await bitcoinRpcClient . listtransactions (
256
+ {
257
+ label : btcAddrRegtest ,
258
+ include_watchonly : true ,
259
+ } ,
260
+ btcAddrRegtest
249
261
) ;
250
262
txs = txs . filter ( r => r . address === btcAddrRegtest ) ;
251
- expect ( txs . length ) . toBe ( 1 ) ;
252
- expect ( txs [ 0 ] . category ) . toBe ( 'receive' ) ;
253
- expect ( txs [ 0 ] . blockhash ) . toBe ( hexToBuffer ( firstReward . burn_block_hash ) . toString ( 'hex' ) ) ;
254
- const sats = new bignumber ( txs [ 0 ] . amount ) . shiftedBy ( 8 ) . toString ( ) ;
263
+
264
+ expect ( txs . length ) . toBeGreaterThan ( 0 ) ;
265
+
266
+ const firstTx = txs . sort ( ( a , b ) => a . blockheight - b . blockheight ) [ 0 ] ;
267
+ expect ( firstTx . category ) . toBe ( 'receive' ) ;
268
+ expect ( firstTx . blockhash ) . toBe ( hexToBuffer ( firstReward . burn_block_hash ) . toString ( 'hex' ) ) ;
269
+ const sats = new bignumber ( firstTx . amount ) . shiftedBy ( 8 ) . toString ( ) ;
255
270
expect ( sats ) . toBe ( firstReward . reward_amount ) ;
256
271
} ) ;
257
272
258
273
test ( 'BTC stacking reward received' , async ( ) => {
259
- const received = await withDescriptorWallet (
260
- async walletName =>
261
- ( await testEnv . bitcoinRpcClient . getreceivedbyaddress (
262
- {
263
- address : btcAddrRegtest ,
264
- minconf : 0 ,
265
- } ,
266
- btcDescriptor ? walletName : undefined
267
- ) ) as number
274
+ const received : number = await bitcoinRpcClient . getreceivedbyaddress (
275
+ {
276
+ address : btcAddrRegtest ,
277
+ minconf : 0 ,
278
+ } ,
279
+ btcAddrRegtest
268
280
) ;
269
281
expect ( received ) . toBeGreaterThan ( 0 ) ;
270
282
} ) ;
283
+
284
+ afterAll ( async ( ) => {
285
+ // after: unload descriptor wallet
286
+ await bitcoinRpcClient . unloadwallet ( { wallet_name : btcAddrRegtest } ) ;
287
+ } ) ;
271
288
}
272
289
) ;
273
290
@@ -300,7 +317,7 @@ function P2SH_P2WPKH() {
300
317
btcAddrDecoded,
301
318
btcAddrRegtest,
302
319
btcPubKey,
303
- btcDescriptor : undefined ,
320
+ btcDescriptor : `sh(wpkh( ${ btcPubKey } ))` ,
304
321
} ;
305
322
}
306
323
@@ -336,7 +353,7 @@ function P2WSH() {
336
353
btcAddrDecoded,
337
354
btcAddrRegtest,
338
355
btcPubKey,
339
- btcDescriptor : undefined ,
356
+ btcDescriptor : `wsh(multi(1, ${ btcPubKey } ))` ,
340
357
} ;
341
358
}
342
359
@@ -369,7 +386,7 @@ function P2WPKH() {
369
386
btcAddrDecoded,
370
387
btcAddrRegtest,
371
388
btcPubKey,
372
- btcDescriptor : undefined ,
389
+ btcDescriptor : `wpkh( ${ btcPubKey } )` ,
373
390
} ;
374
391
}
375
392
@@ -408,25 +425,3 @@ function P2TR() {
408
425
btcDescriptor : `tr(${ btcPubKey } )` ,
409
426
} ;
410
427
}
411
-
412
- // helper
413
- async function withDescriptorWallet < R > ( fn : ( walletName : string ) => Promise < R > | R ) : Promise < R > {
414
- // before: load or create descriptor wallet
415
- try {
416
- await testEnv . bitcoinRpcClient . loadwallet ( { filename : 'descriptor-wallet' } ) ;
417
- } catch ( e ) {
418
- await testEnv . bitcoinRpcClient . createwallet ( {
419
- wallet_name : 'descriptor-wallet' ,
420
- disable_private_keys : true ,
421
- descriptors : true ,
422
- load_on_startup : false ,
423
- } as any ) ;
424
- }
425
-
426
- const res = await fn ( 'descriptor-wallet' ) ;
427
-
428
- // after: unload descriptor walletl
429
- await testEnv . bitcoinRpcClient . unloadwallet ( { wallet_name : 'descriptor-wallet' } ) ;
430
-
431
- return res ;
432
- }
0 commit comments