@@ -5,6 +5,7 @@ import { Descriptor, Psbt } from "../js";
55
66import { getDescriptorForScriptType } from "./descriptorUtil" ;
77import { assertEqualPsbt , toUtxoPsbt , toWrappedPsbt , updateInputWithDescriptor } from "./psbt.util" ;
8+ import { getKey } from "@bitgo/utxo-lib/dist/src/testutil" ;
89
910const rootWalletKeys = new utxolib . bitgo . RootWalletKeys ( utxolib . testutil . getKeyTriple ( "wasm" ) ) ;
1011
@@ -14,16 +15,6 @@ function assertEqualBuffer(a: Buffer | Uint8Array, b: Buffer | Uint8Array, messa
1415
1516const fixtures = getPsbtFixtures ( rootWalletKeys ) ;
1617
17- function getWasmDescriptor (
18- scriptType : utxolib . bitgo . outputScripts . ScriptType2Of3 ,
19- scope : "internal" | "external" ,
20- ) {
21- return Descriptor . fromString (
22- getDescriptorForScriptType ( rootWalletKeys , scriptType , scope ) ,
23- "derivable" ,
24- ) ;
25- }
26-
2718function describeUpdateInputWithDescriptor (
2819 psbt : utxolib . bitgo . UtxoPsbt ,
2920 scriptType : utxolib . bitgo . outputScripts . ScriptType2Of3 ,
@@ -40,12 +31,21 @@ function describeUpdateInputWithDescriptor(
4031 const index = 0 ;
4132 const descriptor = Descriptor . fromString ( descriptorStr , "derivable" ) ;
4233
34+ function getWrappedPsbt ( ) {
35+ return toWrappedPsbt ( psbt ) ;
36+ }
37+
38+ function getWrappedPsbtWithDescriptorInfo ( ) : Psbt {
39+ const wrappedPsbt = getWrappedPsbt ( ) ;
40+ const descriptorAtDerivation = descriptor . atDerivationIndex ( index ) ;
41+ wrappedPsbt . updateInputWithDescriptor ( 0 , descriptorAtDerivation ) ;
42+ wrappedPsbt . updateOutputWithDescriptor ( 0 , descriptorAtDerivation ) ;
43+ return wrappedPsbt ;
44+ }
45+
4346 describe ( "Wrapped PSBT updateInputWithDescriptor" , function ( ) {
4447 it ( "should update the input with the descriptor" , function ( ) {
45- const wrappedPsbt = toWrappedPsbt ( psbt ) ;
46- const descriptorAtDerivation = descriptor . atDerivationIndex ( index ) ;
47- wrappedPsbt . updateInputWithDescriptor ( 0 , descriptorAtDerivation ) ;
48- wrappedPsbt . updateOutputWithDescriptor ( 0 , descriptorAtDerivation ) ;
48+ const wrappedPsbt = getWrappedPsbtWithDescriptorInfo ( ) ;
4949 const updatedPsbt = toUtxoPsbt ( wrappedPsbt ) ;
5050 assertEqualPsbt ( updatedPsbt , getFixtureAtStage ( "unsigned" ) . psbt ) ;
5151 updatedPsbt . signAllInputsHD ( rootWalletKeys . triple [ 0 ] ) ;
@@ -85,6 +85,44 @@ function describeUpdateInputWithDescriptor(
8585 ) ;
8686 } ) ;
8787 } ) ;
88+
89+ describe ( "psbt signWithXprv" , function ( ) {
90+ type KeyName = utxolib . bitgo . KeyName | "unrelated" ;
91+ function signWithKey ( keys : KeyName [ ] , { checkFinalized = false } = { } ) {
92+ it ( `signs the input with keys ${ keys } ` , function ( ) {
93+ const psbt = getWrappedPsbtWithDescriptorInfo ( ) ;
94+ keys . forEach ( ( keyName ) => {
95+ const key = keyName === "unrelated" ? getKey ( keyName ) : rootWalletKeys [ keyName ] ;
96+ const derivationPaths = toUtxoPsbt ( psbt ) . data . inputs [ 0 ] . bip32Derivation . map (
97+ ( d ) => d . path ,
98+ ) ;
99+ assert . ok ( derivationPaths . every ( ( p ) => p === derivationPaths [ 0 ] ) ) ;
100+ const derived = key . derivePath ( derivationPaths [ 0 ] ) ;
101+ assert . deepStrictEqual ( psbt . signWithXprv ( key . toBase58 ( ) ) , {
102+ // map: input index -> pubkey array
103+ 0 : { Ecdsa : keyName === "unrelated" ? [ ] : [ derived . publicKey . toString ( "hex" ) ] } ,
104+ } ) ;
105+ } ) ;
106+
107+ if ( checkFinalized ) {
108+ psbt . finalize ( ) ;
109+ assertEqualBuffer (
110+ toUtxoPsbt ( psbt ) . extractTransaction ( ) . toBuffer ( ) ,
111+ getFixtureAtStage ( "fullsigned" )
112+ . psbt . finalizeAllInputs ( )
113+ . extractTransaction ( )
114+ . toBuffer ( ) ,
115+ ) ;
116+ }
117+ } ) ;
118+ }
119+
120+ signWithKey ( [ "user" ] ) ;
121+ signWithKey ( [ "backup" ] ) ;
122+ signWithKey ( [ "bitgo" ] ) ;
123+ signWithKey ( [ "unrelated" ] ) ;
124+ signWithKey ( [ "user" , "bitgo" ] , { checkFinalized : true } ) ;
125+ } ) ;
88126}
89127
90128fixtures . forEach ( ( { psbt, scriptType, stage } ) => {
0 commit comments