@@ -15,9 +15,9 @@ const utxoNetworks = {
15
15
dash : utxoLib . networks . dash ,
16
16
tltc : utxoLib . networks . litecoin ,
17
17
tbtc : utxoLib . networks . testnet ,
18
- tbch : utxoLib . networks . bitcoincash ,
19
- tzec : utxoLib . networks . zcash ,
20
- tdash : utxoLib . networks . dash ,
18
+ tbch : utxoLib . networks . bitcoincashTestnet ,
19
+ tzec : utxoLib . networks . zcashTest ,
20
+ tdash : utxoLib . networks . dashTest ,
21
21
} ;
22
22
23
23
const coinDecimals = {
@@ -33,7 +33,10 @@ const coinDecimals = {
33
33
teth : 18 ,
34
34
txrp : 6 ,
35
35
tltc : 8 ,
36
- txlm : 7
36
+ txlm : 7 ,
37
+ tbch : 8 ,
38
+ tzec : 8 ,
39
+ tdash : 8 ,
37
40
} ;
38
41
39
42
const TEN = new BN ( 10 ) ;
@@ -63,7 +66,7 @@ const getHDNodeAndVerify = function(xprv, expectedXpub) {
63
66
let node ;
64
67
65
68
try {
66
- node = prova . HDNode . fromBase58 ( xprv ) ;
69
+ node = utxoLib . HDNode . fromBase58 ( xprv ) ;
67
70
} catch ( e ) {
68
71
throw new Error ( 'invalid private key' ) ;
69
72
}
@@ -87,6 +90,7 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
87
90
throw new Error ( `Unsupported coin: ${ recoveryRequest . coin } ` ) ;
88
91
}
89
92
93
+ const isBCH = recoveryRequest . coin === 'bch' || recoveryRequest . coin === 'tbch' ;
90
94
const transaction = utxoLib . Transaction . fromHex ( recoveryRequest . transactionHex , network ) ;
91
95
92
96
const outputs = transaction . outs . map ( out => ( {
@@ -106,6 +110,13 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
106
110
// force override network as we use btc mainnet xpubs for all utxo coins
107
111
backupKeyNode . keyPair . network = network ;
108
112
113
+ // For BCH we need to add the input values to each input, because input values must be signed
114
+ if ( isBCH ) {
115
+ transaction . ins . forEach ( function ( input , i ) {
116
+ transaction . ins [ i ] . value = recoveryRequest . inputs [ i ] . amount ;
117
+ } )
118
+ }
119
+
109
120
const txBuilder = utxoLib . TransactionBuilder . fromTransaction ( transaction , network ) ;
110
121
111
122
_ . forEach ( recoveryRequest . inputs , function ( input , i ) {
@@ -121,19 +132,23 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
121
132
122
133
console . log ( `Signing input ${ i + 1 } of ${ recoveryRequest . inputs . length } with ${ derivedHDNode . neutered ( ) . toBase58 ( ) } (${ input . chainPath } )` ) ;
123
134
124
- if ( isBech32 ) {
125
- const witnessScript = Buffer . from ( input . witnessScript , 'hex' ) ;
126
- const witnessScriptHash = utxoLib . crypto . sha256 ( witnessScript ) ;
127
- const prevOutScript = utxoLib . script . witnessScriptHash . output . encode ( witnessScriptHash ) ;
128
- txBuilder . sign ( i , derivedHDNode . keyPair , prevOutScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript ) ;
129
- } else {
135
+ if ( isBCH ) {
130
136
const redeemScript = new Buffer ( input . redeemScript , 'hex' ) ;
131
-
132
- if ( isSegwit ) {
133
- const witnessScript = new Buffer ( input . witnessScript , 'hex' ) ;
134
- txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript )
137
+ txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_BITCOINCASHBIP143 | utxoLib . Transaction . SIGHASH_ALL , input . amount ) ;
138
+ } else {
139
+ if ( isBech32 ) {
140
+ const witnessScript = Buffer . from ( input . witnessScript , 'hex' ) ;
141
+ const witnessScriptHash = utxoLib . crypto . sha256 ( witnessScript ) ;
142
+ const prevOutScript = utxoLib . script . witnessScriptHash . output . encode ( witnessScriptHash ) ;
143
+ txBuilder . sign ( i , derivedHDNode . keyPair , prevOutScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript ) ;
135
144
} else {
136
- txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL ) ;
145
+ const redeemScript = new Buffer ( input . redeemScript , 'hex' ) ;
146
+ if ( isSegwit ) {
147
+ const witnessScript = new Buffer ( input . witnessScript , 'hex' ) ;
148
+ txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript )
149
+ } else {
150
+ txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL ) ;
151
+ }
137
152
}
138
153
}
139
154
} ) ;
@@ -308,7 +323,7 @@ const parseKey = function(rawkey, coin, path) {
308
323
309
324
}
310
325
// if it doesn't have commas, we expect it is an xprv or xlmsecret properly formatted
311
- return key ;
326
+ return rawkey ;
312
327
}
313
328
314
329
const handleSign = function ( args ) {
0 commit comments