@@ -5,6 +5,7 @@ const _ = require('lodash');
5
5
const BN = require ( 'bignumber.js' ) ;
6
6
const prompt = require ( 'prompt-sync' ) ( ) ;
7
7
const utils = require ( './utils' ) ;
8
+ const bip39 = require ( 'bip39' ) ;
8
9
9
10
const utxoNetworks = {
10
11
btc : utxoLib . networks . bitcoin ,
@@ -276,13 +277,45 @@ const handleSignErc20 = function(recoveryRequest, key, skipConfirm) {
276
277
return transaction . serialize ( ) . toString ( 'hex' ) ;
277
278
} ;
278
279
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
+
279
311
const handleSign = function ( args ) {
280
312
const file = args . file ;
281
- const key = args . key ;
282
313
283
314
const recoveryRequest = JSON . parse ( fs . readFileSync ( file , { encoding : 'utf8' } ) ) ;
284
315
const coin = recoveryRequest . coin ;
285
316
317
+ const key = parseKey ( args . key , coin , args . path ) ;
318
+
286
319
let txHex ;
287
320
288
321
switch ( coin ) {
@@ -315,4 +348,4 @@ const handleSign = function(args) {
315
348
console . log ( 'Done' ) ;
316
349
} ;
317
350
318
- module . exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20 } ;
351
+ module . exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20, parseKey } ;
0 commit comments