@@ -5,7 +5,14 @@ import * as bitcoinjslib from 'bitcoinjs-lib';
55import * as utxolib from '@bitgo/utxo-lib' ;
66import { ECPairInterface } from '@bitgo/utxo-lib' ;
77import { ast , Descriptor , Miniscript } from '@bitgo/wasm-miniscript' ;
8- import { createAddressFromDescriptor } from '@bitgo/utxo-core/descriptor' ;
8+ import {
9+ createAddressFromDescriptor ,
10+ createPsbt ,
11+ getNewSignatureCount ,
12+ signWithKey ,
13+ toUtxoPsbt ,
14+ toWrappedPsbt ,
15+ } from '@bitgo/utxo-core/descriptor' ;
916import { getFixture , toPlainObject } from '@bitgo/utxo-core/testutil' ;
1017import { getBabylonParamByVersion } from '@bitgo/babylonlabs-io-btc-staking-ts' ;
1118
@@ -89,6 +96,40 @@ function getStakingTransactionTreeVendor(
8996 } ;
9097}
9198
99+ function createUnstakingTransaction (
100+ stakingTx : vendor . TransactionResult ,
101+ stakingDescriptor : Descriptor ,
102+ changeAddress : string ,
103+ { sequence } : { sequence : number }
104+ ) : utxolib . Psbt {
105+ const network = utxolib . networks . bitcoin ;
106+ const witnessUtxoNumber = stakingTx . transaction . outs [ 0 ] ;
107+ const witnessUtxo = {
108+ script : witnessUtxoNumber . script ,
109+ value : BigInt ( witnessUtxoNumber . value ) ,
110+ } ;
111+ return createPsbt (
112+ {
113+ network,
114+ } ,
115+ [
116+ {
117+ hash : stakingTx . transaction . getId ( ) ,
118+ index : 0 ,
119+ witnessUtxo,
120+ descriptor : stakingDescriptor ,
121+ sequence,
122+ } ,
123+ ] ,
124+ [
125+ {
126+ script : utxolib . address . toOutputScript ( changeAddress , network ) ,
127+ value : BigInt ( witnessUtxoNumber . value ) - 1000n ,
128+ } ,
129+ ]
130+ ) ;
131+ }
132+
92133function getTestnetStakingParamsWithCovenant (
93134 params : vendor . StakingParams ,
94135 covenantKeys : ECPairInterface [ ]
@@ -198,7 +239,7 @@ function describeWithKeys(
198239 stakingParams : vendor . StakingParams ,
199240 { signIntermediateTxs = false } = { }
200241) {
201- const stakerKey = getECKey ( 'staker' ) ;
242+ const stakerKey = getECKey ( 'staker' ) as ECPairInterface & { privateKey : Buffer } ;
202243 const covenantThreshold = stakingParams . covenantQuorum ;
203244 const stakingTimelock = stakingParams . minStakingTimeBlocks ;
204245 const unbondingTimelock = stakingParams . unbondingTime ;
@@ -256,15 +297,20 @@ function describeWithKeys(
256297 const feeRateSatB = 2 ;
257298 const utxo = mockUtxo ( mainWallet ) ;
258299
259- it ( 'has expected transactions' , async function ( ) {
260- const stakingTx = vendor . stakingTransaction (
300+ let stakingTx : vendor . TransactionResult ;
301+
302+ before ( 'setup stakingTx' , function ( ) {
303+ stakingTx = vendor . stakingTransaction (
261304 vendorBuilder . buildScripts ( ) ,
262305 amount ,
263306 changeAddress ,
264307 [ mockUtxo ( mainWallet ) ] ,
265308 bitcoinjslib . networks . bitcoin ,
266309 feeRateSatB
267310 ) ;
311+ } ) ;
312+
313+ it ( 'has expected transactions' , async function ( ) {
268314 await assertTransactionEqualsFixture ( `test/fixtures/babylon/stakingTransaction.${ tag } .json` , stakingTx ) ;
269315
270316 // simply one staking output and one change output
@@ -337,6 +383,22 @@ function describeWithKeys(
337383 ) ;
338384 }
339385 } ) ;
386+
387+ it ( 'creates unstaking transaction' , async function ( ) {
388+ const unstaking = createUnstakingTransaction (
389+ stakingTx ,
390+ descriptorBuilder . getStakingDescriptor ( ) ,
391+ changeAddress ,
392+ { sequence : stakingParams . minStakingTimeBlocks }
393+ ) ;
394+ const wrappedPsbt = toWrappedPsbt ( unstaking ) ;
395+ assert ( getNewSignatureCount ( signWithKey ( wrappedPsbt , stakerKey ) ) > 0 ) ;
396+ wrappedPsbt . finalize ( ) ;
397+ const tx = toUtxoPsbt ( wrappedPsbt , utxolib . networks . bitcoin ) . extractTransaction ( ) ;
398+ await assertTransactionEqualsFixture ( `test/fixtures/babylon/unstakingTransaction.${ tag } .json` , {
399+ transaction : tx ,
400+ } ) ;
401+ } ) ;
340402 } ) ;
341403 } ) ;
342404}
0 commit comments