-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env node | ||
|
||
const seedsplit = require('../lib/seedsplit') | ||
const program = require('commander') | ||
const prompt = require('prompt') | ||
const version = require('../package.json').version | ||
|
||
prompt.message = '' | ||
const prompts = { | ||
seed: { | ||
name: 'seed', | ||
description: 'Enter seed mnemonic', | ||
hidden: true, | ||
required: true | ||
}, | ||
mnemonic: { | ||
name: 'mnemonic', | ||
description: 'Enter shard mnemonic', | ||
required: true | ||
} | ||
} | ||
|
||
function split({ threshold, shards }) { | ||
if (!threshold || !shards) { | ||
console.log('Please specify threshold and shards') | ||
return | ||
} | ||
handlePrompt(prompts.seed, result => { | ||
let shardMnemonics = seedsplit.split(result.seed, shards, threshold) | ||
console.log(shardMnemonics.join('\n')) | ||
}) | ||
} | ||
|
||
function combine({ threshold }) { | ||
if (!threshold) { | ||
console.log('Please specify threshold') | ||
return | ||
} | ||
getMnemonics([], threshold, mnemonics => { | ||
let seedMnemonic = seedsplit.combine(mnemonics) | ||
console.log(seedMnemonic) | ||
}) | ||
} | ||
|
||
function getMnemonics(mnemonics, threshold, callback) { | ||
if (threshold === 0) { | ||
callback(mnemonics) | ||
} else { | ||
handlePrompt(prompts.mnemonic, result => { | ||
mnemonics.push(result.mnemonic) | ||
getMnemonics(mnemonics, --threshold, callback) | ||
}) | ||
} | ||
} | ||
|
||
function handlePrompt(question, callback) { | ||
try { | ||
prompt.get(question, (err, result) => { | ||
if (err) throw new Error(err) | ||
callback(result) | ||
}) | ||
} catch (err) { | ||
console.log(err.message) | ||
} | ||
} | ||
|
||
program | ||
.version(version) | ||
|
||
program | ||
.command('split') | ||
.description('Split a seed mnemonic into multiple mnemonics') | ||
.option('-t, --threshold <n>', 'number of shards needed to recovery seed', parseInt) | ||
.option('-s, --shards <m>', 'number of shards to create', parseInt) | ||
.action(opts => split(opts)) | ||
|
||
program | ||
.command('combine') | ||
.description('Combine multiple mnemonics into a seed mnemonic') | ||
.option('-t, --threshold <n>', 'number of shards needed to recovery seed', parseInt) | ||
.action(opts => combine(opts)) | ||
|
||
program.parse(process.argv) | ||
|
||
if (!process.argv.slice(2).length) { | ||
program.outputHelp() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters