Skip to content

Commit e30d8c3

Browse files
author
Danny Diekroeger
committed
sign with 24 words and derivation path enabled
1 parent b2d4528 commit e30d8c3

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

app/admin.js

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

app/sign.js

Lines changed: 35 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,45 @@ 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+
const key = parseKey(args.key, coin, args.path);
318+
286319
let txHex;
287320

288321
switch (coin) {
@@ -315,4 +348,4 @@ const handleSign = function(args) {
315348
console.log('Done');
316349
};
317350

318-
module.exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20 };
351+
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)