Skip to content

Commit

Permalink
Added a cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
oed committed Mar 28, 2017
1 parent c82ae3e commit 0b19355
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# seedsplit
Seedsplit lets you split a mnemonic seed into a selected number of shards which are also encoded as mnemonics. n-of-m shards can then be combined in order to get the initial mnemonic seed. This is accomplished by using Shamirs Secret Sharing.

# Tests
## Example usage
To split the mnemonic seed:
```
$ seedsplit split -t 2 -s 3
```
You will now be prompted for a seed mnemonic.

To combine mnemonics to get a seed:
```
$ seedsplit combine -t 2
```
You will be prompted for t number of mnemonics.

## Tests
To run tests:
```
$ npm test
Expand Down
88 changes: 88 additions & 0 deletions bin/cli.js
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()
}

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
},
"author": "oed",
"license": "Unlicense",
"bin": {
"seedsplit": "./bin/cli.js"
},
"dependencies": {
"bip39": "^2.3.0",
"prompt": "^1.0.0",
"secrets.js": "^0.1.8"
},
"devDependencies": {
Expand Down

0 comments on commit 0b19355

Please sign in to comment.