1- import * as utxolib from '@bitgo/utxo-lib' ;
21import { ITransactionRecipient } from '@bitgo/sdk-core' ;
3- import * as coreDescriptors from '@bitgo/utxo-core/descriptor ' ;
2+ import { Psbt , descriptorWallet } from '@bitgo/wasm-utxo ' ;
43
54import { AbstractUtxoCoin , ParseTransactionOptions } from '../../abstractUtxoCoin' ;
65import { BaseOutput , BaseParsedTransaction , BaseParsedTransactionOutputs } from '../types' ;
@@ -10,8 +9,9 @@ import { IDescriptorWallet } from '../../descriptor/descriptorWallet';
109import { fromExtendedAddressFormatToScript , toExtendedAddressFormat } from '../recipient' ;
1110import { outputDifferencesWithExpected , OutputDifferenceWithExpected } from '../outputDifference' ;
1211import { UtxoCoinName } from '../../names' ;
12+ import { sumValues , toWasmPsbt , UtxoLibPsbt } from '../../wasmUtil' ;
1313
14- type ParsedOutput = coreDescriptors . ParsedOutput ;
14+ type ParsedOutput = Omit < descriptorWallet . ParsedOutput , 'script' > & { script : Buffer } ;
1515
1616export type RecipientOutput = Omit < ParsedOutput , 'value' > & {
1717 value : bigint | 'max' ;
@@ -22,6 +22,7 @@ function toRecipientOutput(recipient: ITransactionRecipient, coinName: UtxoCoinN
2222 address : recipient . address ,
2323 value : recipient . amount === 'max' ? 'max' : BigInt ( recipient . amount ) ,
2424 script : fromExtendedAddressFormatToScript ( recipient . address , coinName ) ,
25+ scriptId : undefined , // Recipients are external outputs
2526 } ;
2627}
2728
@@ -32,24 +33,25 @@ type ParsedOutputs = OutputDifferenceWithExpected<ParsedOutput, RecipientOutput>
3233} ;
3334
3435function parseOutputsWithPsbt (
35- psbt : utxolib . bitgo . UtxoPsbt ,
36- descriptorMap : coreDescriptors . DescriptorMap ,
37- recipientOutputs : RecipientOutput [ ]
36+ psbt : Psbt ,
37+ descriptorMap : descriptorWallet . DescriptorMap ,
38+ recipientOutputs : RecipientOutput [ ] ,
39+ coinName : UtxoCoinName
3840) : ParsedOutputs {
39- const parsed = coreDescriptors . parse ( psbt , descriptorMap , psbt . network ) ;
40- const changeOutputs = parsed . outputs . filter ( ( o ) => o . scriptId !== undefined ) ;
41- const outputDiffs = outputDifferencesWithExpected ( parsed . outputs , recipientOutputs ) ;
41+ const parsed = descriptorWallet . parse ( psbt , descriptorMap , coinName ) ;
42+ const outputs : ParsedOutput [ ] = parsed . outputs . map ( ( output ) => ( {
43+ ...output ,
44+ script : Buffer . from ( output . script ) ,
45+ } ) ) ;
46+ const changeOutputs = outputs . filter ( ( o ) => o . scriptId !== undefined ) ;
47+ const outputDiffs = outputDifferencesWithExpected ( outputs , recipientOutputs ) ;
4248 return {
43- outputs : parsed . outputs ,
49+ outputs,
4450 changeOutputs,
4551 ...outputDiffs ,
4652 } ;
4753}
4854
49- function sumValues ( arr : { value : bigint } [ ] ) : bigint {
50- return arr . reduce ( ( sum , e ) => sum + e . value , BigInt ( 0 ) ) ;
51- }
52-
5355function toBaseOutputs ( outputs : ParsedOutput [ ] , coinName : UtxoCoinName ) : BaseOutput < bigint > [ ] ;
5456function toBaseOutputs ( outputs : RecipientOutput [ ] , coinName : UtxoCoinName ) : BaseOutput < bigint | 'max' > [ ] ;
5557function toBaseOutputs (
@@ -85,16 +87,18 @@ function toBaseParsedTransactionOutputs(
8587}
8688
8789export function toBaseParsedTransactionOutputsFromPsbt (
88- psbt : utxolib . bitgo . UtxoPsbt ,
89- descriptorMap : coreDescriptors . DescriptorMap ,
90+ psbt : Psbt | UtxoLibPsbt | Uint8Array ,
91+ descriptorMap : descriptorWallet . DescriptorMap ,
9092 recipients : ITransactionRecipient [ ] ,
9193 coinName : UtxoCoinName
9294) : ParsedOutputsBigInt {
95+ const wasmPsbt = toWasmPsbt ( psbt ) ;
9396 return toBaseParsedTransactionOutputs (
9497 parseOutputsWithPsbt (
95- psbt ,
98+ wasmPsbt ,
9699 descriptorMap ,
97- recipients . map ( ( r ) => toRecipientOutput ( r , coinName ) )
100+ recipients . map ( ( r ) => toRecipientOutput ( r , coinName ) ) ,
101+ coinName
98102 ) ,
99103 coinName
100104 ) ;
@@ -125,13 +129,16 @@ export function parse(
125129 throw new Error ( 'recipients is required' ) ;
126130 }
127131 const psbt = coin . decodeTransactionFromPrebuild ( params . txPrebuild ) ;
128- if ( ! ( psbt instanceof utxolib . bitgo . UtxoPsbt ) ) {
129- throw new Error ( 'expected psbt to be an instance of UtxoPsbt' ) ;
132+ let wasmPsbt : Psbt ;
133+ try {
134+ wasmPsbt = toWasmPsbt ( psbt as Psbt | UtxoLibPsbt | Uint8Array ) ;
135+ } catch ( e ) {
136+ throw new Error ( `expected psbt to be a wasm-utxo or utxo-lib PSBT: ${ e instanceof Error ? e . message : e } ` ) ;
130137 }
131138 const walletKeys = toBip32Triple ( keychains ) ;
132139 const descriptorMap = getDescriptorMapFromWallet ( wallet , walletKeys , getPolicyForEnv ( params . wallet . bitgo . env ) ) ;
133140 return {
134- ...toBaseParsedTransactionOutputsFromPsbt ( psbt , descriptorMap , recipients , coin . name ) ,
141+ ...toBaseParsedTransactionOutputsFromPsbt ( wasmPsbt , descriptorMap , recipients , coin . name ) ,
135142 keychains,
136143 keySignatures : getKeySignatures ( wallet ) ?? { } ,
137144 customChange : undefined ,
0 commit comments