Skip to content

Commit b4ed3ba

Browse files
author
Danny Diekroeger
committed
cleaned up signing logic for more readability and easier editing
1 parent 4b1b27f commit b4ed3ba

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

app/sign.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,40 +114,44 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
114114
})
115115

116116
const txBuilder = utxoLib.TransactionBuilder.fromTransaction(transaction, network);
117-
const isBCH = recoveryRequest.coin === 'bch' || recoveryRequest.coin === 'tbch';
118117

119118
_.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
124121
if (input.chainPath.startsWith('/')) {
125122
input.chainPath = input.chainPath.slice(1);
126123
}
127124

125+
// Derive signing key from chain path
128126
const derivedHDNode = backupKeyNode.derivePath(input.chainPath);
129-
130127
console.log(`Signing input ${ i + 1 } of ${ recoveryRequest.inputs.length } with ${ derivedHDNode.neutered().toBase58() } (${ input.chainPath })`);
131128

132-
if (isBCH) {
129+
// Handle BCH
130+
if (recoveryRequest.coin === 'bch' || recoveryRequest.coin === 'tbch') {
133131
const redeemScript = new Buffer(input.redeemScript, 'hex');
134132
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;
150143
}
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);
151155
});
152156

153157
return txBuilder.build().toHex();

0 commit comments

Comments
 (0)