11import * as utxolib from "@bitgo/utxo-lib" ;
22import * as assert from "node:assert" ;
3- import { getPsbtFixtures , toPsbtWithPrevOutOnly } from "./psbtFixtures" ;
3+ import { getPsbtFixtures , PsbtStage } from "./psbtFixtures" ;
44import { Descriptor , Psbt } from "../js" ;
55
66import { getDescriptorForScriptType } from "./descriptorUtil" ;
7+ import { assertEqualPsbt , toUtxoPsbt , toWrappedPsbt , updateInputWithDescriptor } from "./psbt.util" ;
78
89const rootWalletKeys = new utxolib . bitgo . RootWalletKeys ( utxolib . testutil . getKeyTriple ( "wasm" ) ) ;
910
10- function toWrappedPsbt ( psbt : utxolib . bitgo . UtxoPsbt | Buffer | Uint8Array ) {
11- if ( psbt instanceof utxolib . bitgo . UtxoPsbt ) {
12- psbt = psbt . toBuffer ( ) ;
13- }
14- if ( psbt instanceof Buffer || psbt instanceof Uint8Array ) {
15- return Psbt . deserialize ( psbt ) ;
16- }
17- throw new Error ( "Invalid input" ) ;
18- }
19-
20- function toUtxoPsbt ( psbt : Psbt | Buffer | Uint8Array ) {
21- if ( psbt instanceof Psbt ) {
22- psbt = psbt . serialize ( ) ;
23- }
24- if ( psbt instanceof Buffer || psbt instanceof Uint8Array ) {
25- return utxolib . bitgo . UtxoPsbt . fromBuffer ( Buffer . from ( psbt ) , {
26- network : utxolib . networks . bitcoin ,
27- } ) ;
28- }
29- throw new Error ( "Invalid input" ) ;
30- }
31-
3211function assertEqualBuffer ( a : Buffer | Uint8Array , b : Buffer | Uint8Array , message ?: string ) {
3312 assert . strictEqual ( Buffer . from ( a ) . toString ( "hex" ) , Buffer . from ( b ) . toString ( "hex" ) , message ) ;
3413}
3514
3615const fixtures = getPsbtFixtures ( rootWalletKeys ) ;
3716
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+
3827function describeUpdateInputWithDescriptor (
3928 psbt : utxolib . bitgo . UtxoPsbt ,
4029 scriptType : utxolib . bitgo . outputScripts . ScriptType2Of3 ,
4130) {
42- const fullSignedFixture = fixtures . find (
43- ( f ) => f . scriptType === scriptType && f . stage === "fullsigned" ,
44- ) ;
45- if ( ! fullSignedFixture ) {
46- throw new Error ( "Could not find fullsigned fixture" ) ;
31+ function getFixtureAtStage ( stage : PsbtStage ) {
32+ const f = fixtures . find ( ( f ) => f . scriptType === scriptType && f . stage === stage ) ;
33+ if ( ! f ) {
34+ throw new Error ( `Could not find fixture for scriptType ${ scriptType } and stage ${ stage } ` ) ;
35+ }
36+ return f ;
4737 }
4838
49- describe ( "updateInputWithDescriptor" , function ( ) {
39+ const descriptorStr = getDescriptorForScriptType ( rootWalletKeys , scriptType , "internal" ) ;
40+ const index = 0 ;
41+ const descriptor = Descriptor . fromString ( descriptorStr , "derivable" ) ;
42+
43+ describe ( "Wrapped PSBT updateInputWithDescriptor" , function ( ) {
5044 it ( "should update the input with the descriptor" , function ( ) {
51- const descriptorStr = getDescriptorForScriptType ( rootWalletKeys , scriptType , "internal" ) ;
52- const index = 0 ;
53- const descriptor = Descriptor . fromString ( descriptorStr , "derivable" ) ;
5445 const wrappedPsbt = toWrappedPsbt ( psbt ) ;
5546 wrappedPsbt . updateInputWithDescriptor ( 0 , descriptor . atDerivationIndex ( index ) ) ;
47+ wrappedPsbt . updateOutputWithDescriptor ( 0 , descriptor . atDerivationIndex ( index ) ) ;
5648 const updatedPsbt = toUtxoPsbt ( wrappedPsbt ) ;
49+ assertEqualPsbt ( updatedPsbt , getFixtureAtStage ( "unsigned" ) . psbt ) ;
5750 updatedPsbt . signAllInputsHD ( rootWalletKeys . triple [ 0 ] ) ;
5851 updatedPsbt . signAllInputsHD ( rootWalletKeys . triple [ 2 ] ) ;
5952 const wrappedSignedPsbt = toWrappedPsbt ( updatedPsbt ) ;
@@ -63,11 +56,34 @@ function describeUpdateInputWithDescriptor(
6356 assertEqualBuffer ( updatedPsbt . toBuffer ( ) , wrappedSignedPsbt . serialize ( ) ) ;
6457
6558 assertEqualBuffer (
66- fullSignedFixture . psbt . clone ( ) . finalizeAllInputs ( ) . extractTransaction ( ) . toBuffer ( ) ,
59+ getFixtureAtStage ( "fullsigned" )
60+ . psbt . clone ( )
61+ . finalizeAllInputs ( )
62+ . extractTransaction ( )
63+ . toBuffer ( ) ,
6764 updatedPsbt . extractTransaction ( ) . toBuffer ( ) ,
6865 ) ;
6966 } ) ;
7067 } ) ;
68+
69+ describe ( "updateInputWithDescriptor util" , function ( ) {
70+ it ( "should update the input with the descriptor" , function ( ) {
71+ const cloned = psbt . clone ( ) ;
72+ updateInputWithDescriptor ( cloned , 0 , descriptor . atDerivationIndex ( index ) ) ;
73+ cloned . signAllInputsHD ( rootWalletKeys . triple [ 0 ] ) ;
74+ cloned . signAllInputsHD ( rootWalletKeys . triple [ 2 ] ) ;
75+ cloned . finalizeAllInputs ( ) ;
76+
77+ assertEqualBuffer (
78+ getFixtureAtStage ( "fullsigned" )
79+ . psbt . clone ( )
80+ . finalizeAllInputs ( )
81+ . extractTransaction ( )
82+ . toBuffer ( ) ,
83+ cloned . extractTransaction ( ) . toBuffer ( ) ,
84+ ) ;
85+ } ) ;
86+ } ) ;
7187}
7288
7389fixtures . forEach ( ( { psbt, scriptType, stage } ) => {
0 commit comments