Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const bip39 = require('bip39')
const bip39 = require('bip39');

import {
DEFAULT_COUNT,
parseCount,
convertCountToStrength,
} from './util.js';

export function cli(args) {
const m = bip39.generateMnemonic();
const countStr = args && args[2];
const count = countStr ? parseCount(countStr) : DEFAULT_COUNT;
const strength = convertCountToStrength(count);
const m = bip39.generateMnemonic(strength);
console.log(m);
}
36 changes: 36 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const DEFAULT_COUNT = 12;

function parseCount(
countStr,
silent = false,
defaultCount = DEFAULT_COUNT,
) {
let count;
count = parseInt(countStr);
if (isNaN(count)) {
if (!silent) {
console.error(
'failed to parse count input:', countStr);
}
} else if (count < 12 ||
count > 24 ||
count % 3 !== 0) {
count = undefined;
if (!silent) {
console.error(
'invalid count input, may only be 12, 15, 18, 21, or 24:', countStr);
}
}
return count || defaultCount;
}

function convertCountToStrength(count) {
return count * 32 / 3;
}

export {
DEFAULT_COUNT,
parseCount,
convertCountToStrength,
};