11import assert from 'node:assert/strict' ;
22
3+ import * as utxolib from '@bitgo/utxo-lib' ;
34import { testutil } from '@bitgo/utxo-lib' ;
5+ import { fixedScriptWallet , Triple } from '@bitgo/wasm-utxo' ;
46
57import type { TransactionExplanation } from '../../../../src/transaction/fixedScript/explainTransaction' ;
6- import { explainPsbt } from '../../../../src/transaction/fixedScript' ;
8+ import { explainPsbt , explainPsbtWasm } from '../../../../src/transaction/fixedScript' ;
9+
10+ function hasWasmUtxoSupport ( network : utxolib . Network ) : boolean {
11+ return ! [
12+ utxolib . networks . bitcoincash ,
13+ utxolib . networks . bitcoingold ,
14+ utxolib . networks . ecash ,
15+ utxolib . networks . zcash ,
16+ ] . includes ( utxolib . getMainnet ( network ) ) ;
17+ }
718
819function describeTransactionWith ( acidTest : testutil . AcidTest ) {
920 describe ( `${ acidTest . name } ` , function ( ) {
21+ let psbtBytes : Buffer ;
1022 let refExplanation : TransactionExplanation ;
1123 before ( 'prepare' , function ( ) {
1224 const psbt = acidTest . createPsbt ( ) ;
1325 refExplanation = explainPsbt ( psbt , { pubs : acidTest . rootWalletKeys } , acidTest . network , {
1426 strict : true ,
1527 } ) ;
28+ psbtBytes = psbt . toBuffer ( ) ;
1629 } ) ;
1730
1831 it ( 'should match the expected values for explainPsbt' , function ( ) {
@@ -26,9 +39,41 @@ function describeTransactionWith(acidTest: testutil.AcidTest) {
2639 assert . strictEqual ( typeof change . address , 'string' ) ;
2740 } ) ;
2841 } ) ;
42+
43+ it ( 'should match explainPsbtWasm' , function ( ) {
44+ if ( ! hasWasmUtxoSupport ( acidTest . network ) ) {
45+ return this . skip ( ) ;
46+ }
47+
48+ const networkName = utxolib . getNetworkName ( acidTest . network ) ;
49+ assert ( networkName ) ;
50+ const wasmPsbt = fixedScriptWallet . BitGoPsbt . fromBytes ( psbtBytes , networkName ) ;
51+ const walletXpubs = acidTest . rootWalletKeys . triple . map ( ( k ) => k . neutered ( ) . toBase58 ( ) ) as Triple < string > ;
52+ const wasmExplanation = explainPsbtWasm ( wasmPsbt , walletXpubs , {
53+ replayProtection : {
54+ outputScripts : [ acidTest . getReplayProtectionOutputScript ( ) ] ,
55+ } ,
56+ } ) ;
57+
58+ for ( const key of Object . keys ( refExplanation ) ) {
59+ const refValue = refExplanation [ key ] ;
60+ const wasmValue = wasmExplanation [ key ] ;
61+ switch ( key ) {
62+ case 'displayOrder' :
63+ case 'inputSignatures' :
64+ case 'signatures' :
65+ // these are deprecated fields that we want to get rid of
66+ assert . deepStrictEqual ( wasmValue , undefined ) ;
67+ break ;
68+ default :
69+ assert . deepStrictEqual ( wasmValue , refValue , `mismatch for key ${ key } ` ) ;
70+ break ;
71+ }
72+ }
73+ } ) ;
2974 } ) ;
3075}
3176
32- describe ( 'explainPsbt' , function ( ) {
77+ describe ( 'explainPsbt(Wasm) ' , function ( ) {
3378 testutil . AcidTest . suite ( ) . forEach ( ( test ) => describeTransactionWith ( test ) ) ;
3479} ) ;
0 commit comments