@@ -5,41 +5,22 @@ 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 , toWrappedPsbt } from '@bitgo/utxo-core/descriptor' ;
9- import { getFixture , getKey , toPlainObject } from '@bitgo/utxo-core/testutil' ;
8+ import { createAddressFromDescriptor } from '@bitgo/utxo-core/descriptor' ;
9+ import { getFixture , toPlainObject } from '@bitgo/utxo-core/testutil' ;
1010
1111import { BabylonDescriptorBuilder , finalityBabylonProvider0 , testnetStakingParams } from '../../../src/babylon' ;
1212import { normalize } from '../fixtures.utils' ;
1313
14- bitcoinjslib . initEccLib ( utxolib . ecc ) ;
15-
16- function getECKey ( seed : string ) : ECPairInterface {
17- const { privateKey } = getKey ( seed ) ;
18- assert ( privateKey ) ;
19- return utxolib . ECPair . fromPrivateKey ( privateKey ) ;
20- }
14+ import { getSignedExtract } from './transaction.utils' ;
15+ import { fromXOnlyPublicKey , getECKey , getECKeys , getXOnlyPubkey } from './key.utils' ;
16+ import { getVendorMsgCreateBtcDelegation } from './vendor.utils' ;
2117
22- function getECKeys ( key : string , count : number ) : ECPairInterface [ ] {
23- return Array . from ( { length : count } , ( _ , i ) => getECKey ( `${ key } ${ i } ` ) ) ;
24- }
25-
26- function getXOnlyPubkey ( key : ECPairInterface ) : Buffer {
27- return key . publicKey . subarray ( 1 ) ;
28- }
29-
30- function fromXOnlyPublicKey ( key : Buffer ) : ECPairInterface {
31- for ( const prefix of [ 0x02 , 0x03 ] ) {
32- try {
33- return utxolib . ECPair . fromPublicKey ( Buffer . concat ( [ Buffer . from ( [ prefix ] ) , key ] ) ) ;
34- } catch {
35- continue ;
36- }
37- }
38- throw new Error ( 'Invalid x-only public key' ) ;
39- }
18+ bitcoinjslib . initEccLib ( utxolib . ecc ) ;
4019
41- function getTestnetStakingParams ( ) : vendor . StakingParams {
20+ function getTestnetStakingParams ( ) : vendor . VersionedStakingParams {
4221 return {
22+ version : testnetStakingParams . version ,
23+ btcActivationHeight : testnetStakingParams . btc_activation_height ,
4324 covenantNoCoordPks : testnetStakingParams . covenant_pks ,
4425 covenantQuorum : testnetStakingParams . covenant_quorum ,
4526 unbondingTime : testnetStakingParams . unbonding_time_blocks ,
@@ -73,29 +54,6 @@ type TransactionTree = {
7354 slashingWithdraw : PsbtWithFee | undefined ;
7455} ;
7556
76- function getSigned (
77- psbt : bitcoinjslib . Psbt ,
78- descriptor : Descriptor ,
79- signers : ECPairInterface [ ]
80- ) : bitcoinjslib . Transaction {
81- const wrappedPsbt = toWrappedPsbt ( psbt . toBuffer ( ) ) ;
82- const signedInputs = psbt . data . inputs . flatMap ( ( input , i ) => {
83- assert ( input . witnessUtxo ) ;
84- if ( Buffer . from ( descriptor . scriptPubkey ( ) ) . equals ( input . witnessUtxo . script ) ) {
85- wrappedPsbt . updateInputWithDescriptor ( i , descriptor ) ;
86- const signResults = signers . map ( ( signer ) => {
87- assert ( signer . privateKey ) ;
88- return wrappedPsbt . signWithPrv ( signer . privateKey ) ;
89- } ) ;
90- return [ [ i , signResults ] ] ;
91- }
92- return [ ] ;
93- } ) ;
94- assert ( signedInputs . length > 0 ) ;
95- wrappedPsbt . finalize ( ) ;
96- return bitcoinjslib . Psbt . fromBuffer ( Buffer . from ( wrappedPsbt . serialize ( ) ) ) . extractTransaction ( ) ;
97- }
98-
9957function getStakingTransactionTreeVendor (
10058 builder : vendor . Staking ,
10159 amount : number ,
@@ -119,14 +77,14 @@ function getStakingTransactionTreeVendor(
11977 const signSequence = signers ? [ signers . staker , signers . finalityProvider , ...signers . covenant ] : undefined ;
12078 const unbondingSlashingWithdraw = signSequence
12179 ? builder . createWithdrawSlashingPsbt (
122- getSigned ( unbondingSlashing . psbt , descriptorBuilder . getUnbondingDescriptor ( ) , signSequence ) ,
80+ getSignedExtract ( unbondingSlashing . psbt , descriptorBuilder . getUnbondingDescriptor ( ) , signSequence ) ,
12381 feeRateSatB
12482 )
12583 : undefined ;
12684 const slashing = builder . createStakingOutputSlashingPsbt ( staking . transaction ) ;
12785 const slashingWithdraw = signSequence
12886 ? builder . createWithdrawSlashingPsbt (
129- getSigned ( slashing . psbt , descriptorBuilder . getStakingDescriptor ( ) , signSequence ) ,
87+ getSignedExtract ( slashing . psbt , descriptorBuilder . getStakingDescriptor ( ) , signSequence ) ,
13088 feeRateSatB
13189 )
13290 : undefined ;
@@ -143,7 +101,7 @@ function getStakingTransactionTreeVendor(
143101 } ;
144102}
145103
146- function getTestnetStakingParamsWithCovenant ( covenantKeys : ECPairInterface [ ] ) : vendor . StakingParams {
104+ function getTestnetStakingParamsWithCovenant ( covenantKeys : ECPairInterface [ ] ) : vendor . VersionedStakingParams {
147105 return {
148106 ...getTestnetStakingParams ( ) ,
149107 covenantNoCoordPks : covenantKeys . map ( ( pk ) => getXOnlyPubkey ( pk ) . toString ( 'hex' ) ) ,
@@ -189,6 +147,7 @@ function parseScripts(scripts: unknown) {
189147}
190148
191149async function assertEqualsFixture ( fixtureName : string , value : unknown ) : Promise < void > {
150+ value = normalize ( value ) ;
192151 assert . deepStrictEqual ( await getFixture ( fixtureName , value ) , value ) ;
193152}
194153
@@ -245,7 +204,7 @@ function describeWithKeys(
245204 tag : string ,
246205 finalityProviderKeys : ECPairInterface [ ] ,
247206 covenantKeys : ECPairInterface [ ] ,
248- stakingParams : vendor . StakingParams ,
207+ stakingParams : vendor . VersionedStakingParams ,
249208 { signIntermediateTxs = false } = { }
250209) {
251210 const stakerKey = getECKey ( 'staker' ) ;
@@ -331,13 +290,13 @@ function describeWithKeys(
331290 ] ) ;
332291 } ) ;
333292
334- it ( 'has expected transactions (vendorStaking.Staking)' , async function ( this : Mocha . Context ) {
335- if ( finalityProviderKeys . length !== 1 ) {
336- this . skip ( ) ;
337- }
293+ if ( finalityProviderKeys . length !== 1 ) {
294+ return ;
295+ }
338296
339- const finalityProvider = finalityProviderKeys [ 0 ] ;
297+ const finalityProvider = finalityProviderKeys [ 0 ] ;
340298
299+ it ( 'has expected transactions (vendorStaking.Staking)' , async function ( this : Mocha . Context ) {
341300 const vendorStakingTxBuilder = new vendor . Staking (
342301 bitcoinjslib . networks . bitcoin ,
343302 {
@@ -366,14 +325,30 @@ function describeWithKeys(
366325 ) ;
367326 await assertTransactionEqualsFixture ( `test/fixtures/babylon/txTree.${ tag } .json` , txTree ) ;
368327 } ) ;
328+
329+ it ( 'creates MsgCreateBTCDelegation' , async function ( ) {
330+ await assertEqualsFixture (
331+ `test/fixtures/babylon/msgCreateBTCDelegation.${ tag } .json` ,
332+ await getVendorMsgCreateBtcDelegation (
333+ stakerKey ,
334+ finalityProvider ,
335+ descriptorBuilder ,
336+ stakingParams ,
337+ changeAddress ,
338+ amount ,
339+ utxo ,
340+ feeRateSatB
341+ )
342+ ) ;
343+ } ) ;
369344 } ) ;
370345 } ) ;
371346}
372347
373348function describeWithKeysFromStakingParams (
374349 tag : string ,
375350 finalityProviderKeys : ECPairInterface [ ] ,
376- stakingParams : vendor . StakingParams
351+ stakingParams : vendor . VersionedStakingParams
377352) {
378353 describeWithKeys (
379354 tag ,
0 commit comments