Skip to content

Commit 0f96358

Browse files
authored
Merge branch 'master' into separate-offline
2 parents 8452cc9 + 3d7c5af commit 0f96358

File tree

12 files changed

+212
-34
lines changed

12 files changed

+212
-34
lines changed

app/sign.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const utxoNetworks = {
1414
zec: utxoLib.networks.zcash,
1515
dash: utxoLib.networks.dash,
1616
tltc: utxoLib.networks.litecoin,
17-
tbtc: utxoLib.networks.testnet
17+
tbtc: utxoLib.networks.testnet,
18+
tbch: utxoLib.networks.bitcoincashTestnet,
19+
tzec: utxoLib.networks.zcashTest,
20+
tdash: utxoLib.networks.dashTest,
1821
};
1922

2023
const coinDecimals = {
@@ -30,7 +33,10 @@ const coinDecimals = {
3033
teth: 18,
3134
txrp: 6,
3235
tltc: 8,
33-
txlm: 7
36+
txlm: 7,
37+
tbch: 8,
38+
tzec: 8,
39+
tdash: 8,
3440
};
3541

3642
const TEN = new BN(10);
@@ -60,7 +66,7 @@ const getHDNodeAndVerify = function(xprv, expectedXpub) {
6066
let node;
6167

6268
try {
63-
node = prova.HDNode.fromBase58(xprv);
69+
node = utxoLib.HDNode.fromBase58(xprv);
6470
} catch (e) {
6571
throw new Error('invalid private key');
6672
}
@@ -103,36 +109,49 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
103109
// force override network as we use btc mainnet xpubs for all utxo coins
104110
backupKeyNode.keyPair.network = network;
105111

112+
transaction.ins.forEach(function (input, i) {
113+
transaction.ins[i].value = recoveryRequest.inputs[i].amount;
114+
})
115+
106116
const txBuilder = utxoLib.TransactionBuilder.fromTransaction(transaction, network);
107117

108118
_.forEach(recoveryRequest.inputs, function(input, i) {
109-
const isBech32 = !input.redeemScript;
110-
const isSegwit = !!input.witnessScript;
111119

112-
// chain paths come from the SDK with a leading /, which is technically not allowed by BIP32
120+
// Set up chain path: chain paths come from the SDK with a leading /, which is technically not allowed by BIP32
113121
if (input.chainPath.startsWith('/')) {
114122
input.chainPath = input.chainPath.slice(1);
115123
}
116124

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

121-
if (isBech32) {
129+
// Handle BCH
130+
if (recoveryRequest.coin === 'bch' || recoveryRequest.coin === 'tbch') {
131+
const redeemScript = new Buffer(input.redeemScript, 'hex');
132+
txBuilder.sign(i, derivedHDNode.keyPair, redeemScript, utxoLib.Transaction.SIGHASH_BITCOINCASHBIP143 | utxoLib.Transaction.SIGHASH_ALL, input.amount);
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) {
122138
const witnessScript = Buffer.from(input.witnessScript, 'hex');
123139
const witnessScriptHash = utxoLib.crypto.sha256(witnessScript);
124140
const prevOutScript = utxoLib.script.witnessScriptHash.output.encode(witnessScriptHash);
125141
txBuilder.sign(i, derivedHDNode.keyPair, prevOutScript, utxoLib.Transaction.SIGHASH_ALL, input.amount, witnessScript);
126-
} else {
127-
const redeemScript = new Buffer(input.redeemScript, 'hex');
142+
return;
143+
}
128144

129-
if (isSegwit) {
130-
const witnessScript = new Buffer(input.witnessScript, 'hex');
131-
txBuilder.sign(i, derivedHDNode.keyPair, redeemScript, utxoLib.Transaction.SIGHASH_ALL, input.amount, witnessScript)
132-
} else {
133-
txBuilder.sign(i, derivedHDNode.keyPair, redeemScript, utxoLib.Transaction.SIGHASH_ALL);
134-
}
145+
// Handle Wrapped 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;
135151
}
152+
153+
// Handle all other requests
154+
txBuilder.sign(i, derivedHDNode.keyPair, redeemScript, utxoLib.Transaction.SIGHASH_ALL, input.amount);
136155
});
137156

138157
return txBuilder.build().toHex();
@@ -160,7 +179,7 @@ const handleSignEthereum = function(recoveryRequest, key, skipConfirm) {
160179

161180
const backupKeyNode = getHDNodeAndVerify(key, recoveryRequest.backupKey);
162181

163-
const backupSigningKey = backupKeyNode.getKey().getPrivateKeyBuffer();
182+
const backupSigningKey = backupKeyNode.keyPair.getPrivateKeyBuffer();
164183

165184
transaction.sign(backupSigningKey);
166185

app/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const nodemailer = require('nodemailer');
33
const smtpTransport = require('nodemailer-smtp-transport');
44
const jsrender = require('jsrender');
55
const prova = require('prova-lib');
6+
const utxoLib = require('bitgo-utxo-lib');
67
const stellar = require('stellar-base');
78
const stellarHd = require('stellar-hd-wallet');
89

@@ -98,7 +99,7 @@ exports.sendMailQ = function(toEmail, subject, template, templateParams, attachm
9899

99100
exports.deriveChildKey = function(master, derivationPath, type, neuter) {
100101
if (type === 'xpub' || type === 'xprv') {
101-
const masterNode = prova.HDNode.fromBase58(master);
102+
const masterNode = utxoLib.HDNode.fromBase58(master);
102103
const childKey = masterNode.derivePath(derivationPath);
103104

104105
if (neuter) {

bin/admin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const admin = require('../app/admin');
55

66
Promise.try(admin.run).catch(function(e) {
77
console.log(e.message);
8+
console.log(e.stack);
89
if (admin.db && admin.db.connection) {
910
admin.db.connection.close();
1011
}

config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module.exports = {
1414
tzec: 'xpub',
1515
xrp: 'xpub',
1616
txrp: 'xpub',
17+
dash: 'xpub',
18+
tdash: 'xpub',
1719
xlm: 'xlm',
1820
txlm: 'xlm'
1921
},

package-lock.json

Lines changed: 13 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"argparse": "^1.0.10",
1919
"bignumber.js": "^7.2.1",
2020
"bitcoinjs-message": "^2.0.0",
21-
"bitgo-utxo-lib": "^1.1.2",
21+
"bitgo-utxo-lib": "^1.2.1",
2222
"body-parser": "^1.18.3",
2323
"dotenv": "^6.1.0",
2424
"ethereumjs-tx": "^1.3.7",

test/sign.js

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)