@@ -114,40 +114,44 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
114
114
} )
115
115
116
116
const txBuilder = utxoLib . TransactionBuilder . fromTransaction ( transaction , network ) ;
117
- const isBCH = recoveryRequest . coin === 'bch' || recoveryRequest . coin === 'tbch' ;
118
117
119
118
_ . forEach ( recoveryRequest . inputs , function ( input , i ) {
120
- const isBech32 = ! input . redeemScript ;
121
- const isSegwit = ! ! input . witnessScript ;
122
-
123
- // chain paths come from the SDK with a leading /, which is technically not allowed by BIP32
119
+
120
+ // Set up chain path: chain paths come from the SDK with a leading /, which is technically not allowed by BIP32
124
121
if ( input . chainPath . startsWith ( '/' ) ) {
125
122
input . chainPath = input . chainPath . slice ( 1 ) ;
126
123
}
127
124
125
+ // Derive signing key from chain path
128
126
const derivedHDNode = backupKeyNode . derivePath ( input . chainPath ) ;
129
-
130
127
console . log ( `Signing input ${ i + 1 } of ${ recoveryRequest . inputs . length } with ${ derivedHDNode . neutered ( ) . toBase58 ( ) } (${ input . chainPath } )` ) ;
131
128
132
- if ( isBCH ) {
129
+ // Handle BCH
130
+ if ( recoveryRequest . coin === 'bch' || recoveryRequest . coin === 'tbch' ) {
133
131
const redeemScript = new Buffer ( input . redeemScript , 'hex' ) ;
134
132
txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_BITCOINCASHBIP143 | utxoLib . Transaction . SIGHASH_ALL , input . amount ) ;
135
- } else {
136
- if ( isBech32 ) {
137
- const witnessScript = Buffer . from ( input . witnessScript , 'hex' ) ;
138
- const witnessScriptHash = utxoLib . crypto . sha256 ( witnessScript ) ;
139
- const prevOutScript = utxoLib . script . witnessScriptHash . output . encode ( witnessScriptHash ) ;
140
- txBuilder . sign ( i , derivedHDNode . keyPair , prevOutScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript ) ;
141
- } else {
142
- const redeemScript = new Buffer ( input . redeemScript , 'hex' ) ;
143
- if ( isSegwit ) {
144
- const witnessScript = new Buffer ( input . witnessScript , 'hex' ) ;
145
- txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript )
146
- } else {
147
- txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL , input . amount ) ;
148
- }
149
- }
133
+ return ; // in a Lodash forEach loop, 'return' operates like 'continue' does in a regular javascript loop. It finishes this iteration and moves to the next iteration
134
+ }
135
+
136
+ // Handle Bech32
137
+ if ( ! input . redeemScript ) {
138
+ const witnessScript = Buffer . from ( input . witnessScript , 'hex' ) ;
139
+ const witnessScriptHash = utxoLib . crypto . sha256 ( witnessScript ) ;
140
+ const prevOutScript = utxoLib . script . witnessScriptHash . output . encode ( witnessScriptHash ) ;
141
+ txBuilder . sign ( i , derivedHDNode . keyPair , prevOutScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript ) ;
142
+ return ;
150
143
}
144
+
145
+ // Handle Segwit
146
+ const redeemScript = new Buffer ( input . redeemScript , 'hex' ) ;
147
+ if ( input . witnessScript ) {
148
+ const witnessScript = new Buffer ( input . witnessScript , 'hex' ) ;
149
+ txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL , input . amount , witnessScript ) ;
150
+ return ;
151
+ }
152
+
153
+ // Handle all other requests
154
+ txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxoLib . Transaction . SIGHASH_ALL , input . amount ) ;
151
155
} ) ;
152
156
153
157
return txBuilder . build ( ) . toHex ( ) ;
0 commit comments