@@ -19,15 +19,17 @@ import {
1919 fetchTransactionStatus ,
2020 getClient ,
2121} from '../fetch' ;
22- import { getParserTxProperties } from '../ParserTx' ;
22+ import { getParserTxProperties , ParserTx } from '../ParserTx' ;
2323import { parseUnknown } from '../parseUnknown' ;
2424import { Parser } from '../Parser' ;
2525
2626import { formatString } from './formatString' ;
27+ import { getPrevOutputsFromPrevTxs } from '../prevTx' ;
2728
2829export type ArgsParseTransaction = ReadStringOptions & {
2930 network : utxolib . Network ;
3031 txid ?: string ;
32+ prevTx ?: string [ ] ;
3133 blockHeight ?: number ;
3234 txIndex ?: number ;
3335 all : boolean ;
@@ -71,6 +73,7 @@ export const cmdParseTx = {
7173 . options ( readStringOptions )
7274 . options ( getNetworkOptionsDemand ( ) )
7375 . option ( 'txid' , { type : 'string' } )
76+ . option ( 'prevTx' , { type : 'string' , description : 'previous transaction hex or base64 string' , array : true } )
7477 . option ( 'blockHeight' , { type : 'number' } )
7578 . option ( 'txIndex' , { type : 'number' } )
7679 . option ( 'fetchAll' , { type : 'boolean' , default : false } )
@@ -128,11 +131,14 @@ export const cmdParseTx = {
128131 throw new Error ( `no txdata` ) ;
129132 }
130133
131- const bytes = stringToBuffer ( string , [ 'hex' , 'base64' ] ) ;
134+ function decodeBytes ( bytes : Buffer ) : ParserTx {
135+ return utxolib . bitgo . isPsbt ( bytes )
136+ ? utxolib . bitgo . createPsbtFromBuffer ( bytes , argv . network )
137+ : utxolib . bitgo . createTransactionFromBuffer ( bytes , argv . network , { amountType : 'bigint' } ) ;
138+ }
132139
133- let tx = utxolib . bitgo . isPsbt ( bytes )
134- ? utxolib . bitgo . createPsbtFromBuffer ( bytes , argv . network )
135- : utxolib . bitgo . createTransactionFromBuffer ( bytes , argv . network , { amountType : 'bigint' } ) ;
140+ const bytes = stringToBuffer ( string , [ 'hex' , 'base64' ] ) ;
141+ let tx = decodeBytes ( bytes ) ;
136142
137143 const { id : txid } = getParserTxProperties ( tx , undefined ) ;
138144 if ( tx instanceof utxolib . bitgo . UtxoTransaction ) {
@@ -144,6 +150,11 @@ export const cmdParseTx = {
144150 tx = tx . extractTransaction ( ) ;
145151 }
146152
153+ const prevTxs : ParserTx [ ] = ( argv . prevTx ?? [ ] ) . map ( ( s ) => {
154+ const buf = stringToBuffer ( s , [ 'hex' , 'base64' ] ) ;
155+ return decodeBytes ( buf ) ;
156+ } ) ;
157+
147158 if ( argv . parseAsUnknown ) {
148159 console . log ( formatString ( parseUnknown ( new Parser ( ) , 'tx' , tx ) , argv ) ) ;
149160 return ;
@@ -157,7 +168,7 @@ export const cmdParseTx = {
157168
158169 const parsed = getTxParser ( argv ) . parse ( tx , {
159170 status : argv . fetchStatus && txid ? await fetchTransactionStatus ( httpClient , txid , argv . network ) : undefined ,
160- prevOutputs : argv . fetchInputs ? await fetchPrevOutputs ( httpClient , tx ) : undefined ,
171+ prevOutputs : argv . fetchInputs ? await fetchPrevOutputs ( httpClient , tx ) : getPrevOutputsFromPrevTxs ( tx , prevTxs ) ,
161172 prevOutputSpends : argv . fetchSpends ? await fetchPrevOutputSpends ( httpClient , tx ) : undefined ,
162173 outputSpends :
163174 argv . fetchSpends && tx instanceof utxolib . bitgo . UtxoTransaction
0 commit comments