@@ -18,8 +18,15 @@ Tx.parseUnknown = function (txHex) {
18
18
//@ts -ignore - trust me bro, it's an error
19
19
let err = e ;
20
20
let msg = err . message ;
21
+ let out = "" ;
22
+ try {
23
+ out = Tx . _debugPrint ( txInfo ) ;
24
+ } catch ( e ) {
25
+ //@ts -ignore
26
+ out = e . message ;
27
+ }
21
28
Object . assign ( err , {
22
- message : Tx . _debugPrint ( txInfo ) ,
29
+ message : out ,
23
30
code : "E_TX_PARSE" ,
24
31
transaction : txInfo ,
25
32
} ) ;
@@ -47,37 +54,31 @@ Tx._parse = function (tx, hex) {
47
54
tx . offset += 8 ;
48
55
tx . version = version ;
49
56
tx . versionHex = versionHex ;
50
- tx . inputs = [ ] ;
51
- tx . outputs = [ ] ;
52
57
53
58
let [ numInputs , numInputsSize ] = parseVarIntHex ( hex , tx . offset ) ;
54
59
tx . offset += numInputsSize ;
55
60
tx . numInputsHex = numInputs . toString ( 16 ) ;
56
61
tx . numInputsHex = tx . numInputsHex . padStart ( 2 , "0" ) ;
57
62
tx . numInputs = numInputs ;
58
63
64
+ tx . inputs = [ ] ;
59
65
for ( let i = 0 ; i < numInputs ; i += 1 ) {
60
66
let input = { } ;
61
67
tx . inputs . push ( input ) ;
62
68
63
69
input . txidHex = hex . substr ( tx . offset , 64 ) ;
64
- input . txid = Tx . utils . reverseHex ( tx . txidHex ) ;
70
+ input . txid = Tx . utils . reverseHex ( input . txidHex ) ;
71
+ input . txId = input . txid ; // TODO
65
72
tx . offset += 64 ;
66
73
67
74
input . outputIndexHex = hex . substr ( tx . offset , 8 ) ;
68
75
let outputIndexHexLe = Tx . utils . reverseHex ( input . outputIndexHex ) ;
69
76
input . outputIndex = parseInt ( outputIndexHexLe , 16 ) ;
70
- // console.info(
71
- // ` ${outputIndexHex} # Previous Output index (${outputIndex})`,
72
- // );
73
77
tx . offset += 8 ;
74
78
75
79
// TODO VarInt
76
80
input . scriptSizeHex = hex . substr ( tx . offset , 2 ) ;
77
81
input . scriptSize = parseInt ( input . scriptSizeHex , 16 ) ;
78
- // console.info(
79
- // ` ${input.scriptSizeHex} # Script Size (${input.scriptSize} bytes)`,
80
- // );
81
82
tx . offset += 2 ;
82
83
83
84
input . script = "" ;
@@ -204,13 +205,14 @@ Tx._parse = function (tx, hex) {
204
205
tx . numOutputsHex = tx . numOutputsHex . padStart ( 2 , "0" ) ;
205
206
tx . numOutputs = numOutputs ;
206
207
208
+ tx . outputs = [ ] ;
207
209
for ( let i = 0 ; i < tx . numOutputs ; i += 1 ) {
208
210
let output = { } ;
209
211
tx . outputs . push ( output ) ;
210
212
211
213
output . satoshisHex = hex . substr ( tx . offset , 16 ) ;
212
214
tx . offset += 16 ;
213
- let satsHex = Tx . utils . reverseHex ( tx . satoshisHex ) ;
215
+ let satsHex = Tx . utils . reverseHex ( output . satoshisHex ) ;
214
216
output . satoshis = parseInt ( satsHex , 16 ) ;
215
217
tx . totalSatoshis += output . satoshis ;
216
218
// console.info(
@@ -230,6 +232,7 @@ Tx._parse = function (tx, hex) {
230
232
231
233
output . scriptTypeHex = output . script . slice ( 0 , 2 ) ;
232
234
output . scriptType = parseInt ( output . scriptTypeHex , 16 ) ;
235
+ output . pubKeyHash = "" ;
233
236
output . memo = "" ;
234
237
output . message = "" ;
235
238
if ( output . scriptType === OP_RETURN ) {
@@ -242,6 +245,9 @@ Tx._parse = function (tx, hex) {
242
245
} catch ( e ) {
243
246
output . message = "<non-UTF-8 bytes>" ;
244
247
}
248
+ } else {
249
+ // TODO check the script type
250
+ output . pubKeyHash = output . script . slice ( 6 , - 4 ) ;
245
251
}
246
252
}
247
253
@@ -328,7 +334,7 @@ Tx._debugPrint = function (tx) {
328
334
lines . push (
329
335
`${ tx . numInputsHex } # Inputs (${ tx . numInputs } )` ,
330
336
) ;
331
- for ( let i = 0 ; i < tx . inputs . length ; i += 1 ) {
337
+ for ( let i = 0 ; i < tx . inputs ? .length ; i += 1 ) {
332
338
let count = i + 1 ;
333
339
let input = tx . inputs [ i ] ;
334
340
lines . push ( "" ) ;
@@ -338,18 +344,26 @@ Tx._debugPrint = function (tx) {
338
344
let txid2 = input . txidHex . slice ( 16 , 32 ) ;
339
345
let txid3 = input . txidHex . slice ( 32 , 48 ) ;
340
346
let txid4 = input . txidHex . slice ( 48 , 64 ) ;
341
- lines . push ( ` ${ txid1 } # Previous Output TX ID" ` ) ;
347
+ lines . push ( ` ${ txid1 } # Previous Output TX ID` ) ;
342
348
lines . push ( ` ${ txid2 } ` ) ;
343
349
lines . push ( ` ${ txid3 } ` ) ;
344
350
lines . push ( ` ${ txid4 } ` ) ;
351
+
352
+ lines . push (
353
+ ` ${ input . outputIndexHex } # Previous Output index (${ input . outputIndex } )` ,
354
+ ) ;
355
+
356
+ lines . push (
357
+ ` ${ input . scriptSizeHex } # Script Size (${ input . scriptSize } bytes)` ,
358
+ ) ;
345
359
}
346
360
347
361
// outputs
348
362
lines . push ( "" ) ;
349
363
lines . push (
350
364
`${ tx . numOutputsHex } # Outputs (${ tx . numOutputs } )` ,
351
365
) ;
352
- for ( let i = 0 ; i < tx . outputs . length ; i += 1 ) {
366
+ for ( let i = 0 ; i < tx . outputs ? .length ; i += 1 ) {
353
367
let count = i + 1 ;
354
368
let output = tx . outputs [ i ] ;
355
369
0 commit comments