Skip to content

Commit 8502cfd

Browse files
author
Danny Diekroeger
committed
Merge branch 'master' of github.com:BitGo/key-recovery-service-v2 into support-key-signatures
2 parents 29ebdbf + e43314b commit 8502cfd

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

.github/pull_request_template.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Motivation
2+
3+
## Overview Of Changes

app/admin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ signCommand.addArgument(
6060
help: 'private key to sign the transaction with'
6161
}
6262
);
63+
signCommand.addArgument(
64+
['--path'],
65+
{
66+
action: 'store',
67+
required: false,
68+
help: 'optional derivation path to derive from the given key'
69+
}
70+
);
6371
signCommand.addArgument(
6472
['--confirm'],
6573
{

app/sign.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const _ = require('lodash');
55
const BN = require('bignumber.js');
66
const prompt = require('prompt-sync')();
77
const utils = require('./utils');
8+
const bip39 = require('bip39');
89

910
const utxoNetworks = {
1011
btc: utxoLib.networks.bitcoin,
@@ -276,13 +277,50 @@ const handleSignErc20 = function(recoveryRequest, key, skipConfirm) {
276277
return transaction.serialize().toString('hex');
277278
};
278279

280+
/* *
281+
Takes in either an xprv, xlmsecret, or 24 words.
282+
Returns an xprv or xlmsecret
283+
*/
284+
const parseKey = function(rawkey, coin, path) {
285+
286+
if(rawkey.includes(',') && rawkey.split(',').length === 24) {
287+
const mnemonic = rawkey.replace(/,/g,' '); // replace commas with spaces
288+
if(coin === 'xlm' || coin === 'txlm') {
289+
// stellar is special (thanks Jeb)
290+
const stellarWallet = stellarHd.fromMnemonic(mnemonic);
291+
return stellarWallet.getSecret(0);
292+
}
293+
294+
// every other coin can use xpubs
295+
if(!bip39.validateMnemonic(mnemonic)) {
296+
throw new Error("Invalid mnemonic");
297+
}
298+
const seed = bip39.mnemonicToSeed(mnemonic);
299+
let node = utxoLib.HDNode.fromSeedBuffer(seed);
300+
if(path) {
301+
node = node.derivePath(path);
302+
}
303+
const xprv = node.toBase58();
304+
return xprv;
305+
306+
}
307+
// if it doesn't have commas, we expect it is an xprv or xlmsecret properly formatted
308+
return key;
309+
}
310+
279311
const handleSign = function(args) {
280312
const file = args.file;
281-
const key = args.key;
282313

283314
const recoveryRequest = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
284315
const coin = recoveryRequest.coin;
285316

317+
if(!args.key) {
318+
console.log("\nEnter your private key for signing.\nEnter an xprv or 24 words.\nIf entering 24 words, separate each word with only a comma and no spaces.\n");
319+
args.key = prompt("Key: ");
320+
}
321+
322+
const key = parseKey(args.key, coin, args.path);
323+
286324
let txHex;
287325

288326
switch (coin) {
@@ -315,4 +353,4 @@ const handleSign = function(args) {
315353
console.log('Done');
316354
};
317355

318-
module.exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20 };
356+
module.exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20, parseKey };

test/sign.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)