Skip to content

Commit 336dea0

Browse files
chore(app): use bip32 instead of HDNode
HDNode will be deprecated in a future utxolib version Issue: BG-34381
1 parent 6948de1 commit 336dea0

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

app/sign.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Promise = require('bluebird');
22
const co = Promise.coroutine;
3+
const bip32 = require('bip32');
34
const utxolib = require('@bitgo/utxo-lib');
45
const accountLib = require('@bitgo/account-lib');
56
const statics = require('@bitgo/statics');
@@ -90,7 +91,7 @@ const getHDNodeAndVerify = function(xprv, expectedXpub) {
9091
let node;
9192

9293
try {
93-
node = utxolib.HDNode.fromBase58(xprv);
94+
node = bip32.fromBase58(xprv);
9495
} catch (e) {
9596
throw new Error('invalid private key');
9697
}
@@ -131,12 +132,10 @@ const promptForConfirmationAndKey = function(recoveryRequest, outputs, skipConfi
131132
* Gets the backup private key that can be used to sign the transaction.
132133
* @param xprv The provided extended private key (BIP32).
133134
* @param expectedXpub The public key specified with the request.
134-
* @returns The private key to sign the transaction.
135+
* @returns Buffer - the private key buffer to sign the transaction.
135136
*/
136137
const getBackupSigningKey = function(xprv, expectedXpub) {
137-
const backupKeyNode = getHDNodeAndVerify(xprv, expectedXpub);
138-
139-
return backupKeyNode.keyPair.getPrivateKeyBuffer();
138+
return getHDNodeAndVerify(xprv, expectedXpub).privateKey;
140139
};
141140

142141
const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
@@ -157,10 +156,11 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
157156

158157
key = promptForConfirmationAndKey(recoveryRequest, outputs, skipConfirm, key);
159158

160-
const backupKeyNode = getHDNodeAndVerify(key, recoveryRequest.backupKey);
161-
162159
// force override network as we use btc mainnet xpubs for all utxo coins
163-
backupKeyNode.keyPair.network = network;
160+
const backupKeyNode = Object.assign(
161+
getHDNodeAndVerify(key, recoveryRequest.backupKey),
162+
{ network }
163+
);
164164

165165
transaction.ins.forEach(function (input, i) {
166166
transaction.ins[i].value = recoveryRequest.inputs[i].amount;
@@ -183,7 +183,7 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
183183
// Handle BCH
184184
if (BCH_COINS.includes(recoveryRequest.coin)) {
185185
const redeemScript = new Buffer(input.redeemScript, 'hex');
186-
txBuilder.sign(i, derivedHDNode.keyPair, redeemScript,
186+
txBuilder.sign(i, derivedHDNode, redeemScript,
187187
utxolib.Transaction.SIGHASH_BITCOINCASHBIP143 | utxolib.Transaction.SIGHASH_ALL, input.amount);
188188
// in a Lodash forEach loop, 'return' operates like 'continue' does
189189
// in a regular javascript loop. It finishes this iteration and moves to the next iteration
@@ -195,7 +195,7 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
195195
const witnessScript = Buffer.from(input.witnessScript, 'hex');
196196
const witnessScriptHash = utxolib.crypto.sha256(witnessScript);
197197
const prevOutScript = utxolib.script.witnessScriptHash.output.encode(witnessScriptHash);
198-
txBuilder.sign(i, derivedHDNode.keyPair, prevOutScript,
198+
txBuilder.sign(i, derivedHDNode, prevOutScript,
199199
utxolib.Transaction.SIGHASH_ALL, input.amount, witnessScript);
200200
return;
201201
}
@@ -204,13 +204,13 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
204204
const redeemScript = new Buffer(input.redeemScript, 'hex');
205205
if (input.witnessScript) {
206206
const witnessScript = new Buffer(input.witnessScript, 'hex');
207-
txBuilder.sign(i, derivedHDNode.keyPair, redeemScript,
207+
txBuilder.sign(i, derivedHDNode, redeemScript,
208208
utxolib.Transaction.SIGHASH_ALL, input.amount, witnessScript);
209209
return;
210210
}
211211

212212
// Handle all other requests
213-
txBuilder.sign(i, derivedHDNode.keyPair, redeemScript, utxolib.Transaction.SIGHASH_ALL, input.amount);
213+
txBuilder.sign(i, derivedHDNode, redeemScript, utxolib.Transaction.SIGHASH_ALL, input.amount);
214214
});
215215

216216
return txBuilder.build().toHex();
@@ -347,8 +347,8 @@ const handleSignXrp = function(recoveryRequest, key, skipConfirm) {
347347

348348
const backupKeyNode = getHDNodeAndVerify(key, recoveryRequest.backupKey);
349349

350-
const backupAddress = rippleKeypairs.deriveAddress(backupKeyNode.keyPair.getPublicKeyBuffer().toString('hex'));
351-
const privateKeyHex = backupKeyNode.keyPair.getPrivateKeyBuffer().toString('hex');
350+
const backupAddress = rippleKeypairs.deriveAddress(backupKeyNode.publicKey.toString('hex'));
351+
const privateKeyHex = backupKeyNode.privateKey.toString('hex');
352352
const cosignedTx = utils.signXrpWithPrivateKey(txHex, privateKeyHex, { signAs: backupAddress });
353353

354354
return rippleApi.combine([txHex, cosignedTx.signedTransaction]).signedTransaction;
@@ -421,7 +421,7 @@ const parseKey = co(function *(rawkey, coin, path) {
421421
throw new Error('Invalid mnemonic');
422422
}
423423
const seed = yield bip39.mnemonicToSeed(mnemonic);
424-
let node = utxolib.HDNode.fromSeedBuffer(seed);
424+
let node = bip32.fromSeed(seed);
425425
if (path) {
426426
node = node.derivePath(path);
427427
}

app/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const bip32 = require('bip32');
12
const Promise = require('bluebird');
23
const co = Promise.coroutine;
34
const Q = require('q');
@@ -110,7 +111,7 @@ function IsValidBip32Seed(input) {
110111

111112
exports.deriveChildKey = function(master, derivationPath, type, neuter) {
112113
if (type === 'xpub' || type === 'xprv') {
113-
const masterNode = utxolib.HDNode.fromBase58(master);
114+
const masterNode = bip32.fromBase58(master);
114115
const childKey = masterNode.derivePath(derivationPath);
115116

116117
if (neuter) {

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
"dependencies": {
2121
"@bitgo/account-lib": "^2.13.0",
2222
"@bitgo/statics": "^3.4.2",
23+
"@bitgo/utxo-lib": "1.9.6",
2324
"argparse": "^1.0.10",
2425
"bignumber.js": "^7.2.1",
26+
"bip32": "^2.0.6",
2527
"bitcoinjs-message": "^2.0.0",
2628
"bitgo": "^10.0.0",
27-
"@bitgo/utxo-lib": "1.9.6",
2829
"body-parser": "^1.18.3",
2930
"dotenv": "^6.1.0",
3031
"eosjs": "^16.0.9",

0 commit comments

Comments
 (0)