@@ -9,6 +9,35 @@ function scriptToAddress(script: Uint8Array): string {
99 return `scriptPubKey:${ Buffer . from ( script ) . toString ( 'hex' ) } ` ;
1010}
1111
12+ type ParsedWalletOutput = fixedScriptWallet . ParsedOutput & { scriptId : fixedScriptWallet . ScriptId } ;
13+ type ParsedExternalOutput = fixedScriptWallet . ParsedOutput & { scriptId : null } ;
14+
15+ function isParsedWalletOutput ( output : ParsedWalletOutput | ParsedExternalOutput ) : output is ParsedWalletOutput {
16+ return output . scriptId !== null ;
17+ }
18+
19+ function isParsedExternalOutput ( output : ParsedWalletOutput | ParsedExternalOutput ) : output is ParsedExternalOutput {
20+ return output . scriptId === null ;
21+ }
22+
23+ function toChangeOutput ( output : ParsedWalletOutput ) : FixedScriptWalletOutput {
24+ return {
25+ address : output . address ?? scriptToAddress ( output . script ) ,
26+ amount : output . value . toString ( ) ,
27+ chain : output . scriptId . chain ,
28+ index : output . scriptId . index ,
29+ external : false ,
30+ } ;
31+ }
32+
33+ function toExternalOutput ( output : ParsedExternalOutput ) : Output {
34+ return {
35+ address : output . address ?? scriptToAddress ( output . script ) ,
36+ amount : output . value . toString ( ) ,
37+ external : true ,
38+ } ;
39+ }
40+
1241export function explainPsbtWasm (
1342 psbt : fixedScriptWallet . BitGoPsbt ,
1443 walletXpubs : Triple < string > ,
@@ -25,24 +54,14 @@ export function explainPsbtWasm(
2554 const outputs : Output [ ] = [ ] ;
2655
2756 parsed . outputs . forEach ( ( output ) => {
28- const address = output . address ?? scriptToAddress ( output . script ) ;
29-
30- if ( output . scriptId ) {
57+ if ( isParsedWalletOutput ( output ) ) {
3158 // This is a change output
32- changeOutputs . push ( {
33- address,
34- amount : output . value . toString ( ) ,
35- chain : output . scriptId . chain ,
36- index : output . scriptId . index ,
37- external : false ,
38- } ) ;
39- } else {
59+ changeOutputs . push ( toChangeOutput ( output ) ) ;
60+ } else if ( isParsedExternalOutput ( output ) ) {
4061 // This is an external output
41- outputs . push ( {
42- address,
43- amount : output . value . toString ( ) ,
44- external : true ,
45- } ) ;
62+ outputs . push ( toExternalOutput ( output ) ) ;
63+ } else {
64+ throw new Error ( 'Invalid output' ) ;
4665 }
4766 } ) ;
4867
0 commit comments