A full stack for bitcoin and blockchain-based applications
The Mnemonic
class provides an implementation of a mnemonic code or mnemonic sentence – a group of easy to remember words – for the generation of deterministic keys. The class handles code generation and its later conversion to a HDPrivateKey. See the official BIP-0039 for technical background information.
Mnemonics is implemented as a separate module and you must add it to your dependencies:
For node projects:
npm install bitcore-mnemonic --save
For client-side projects:
bower install bitcore-mnemonic --save
For creating a new random mnemonic code you just create a new instance.
var Mnemonic = require('bitcore-mnemonic');
var code = new Mnemonic();
code.toString(); // 'select scout crash enforce riot rival spring whale hollow radar rule sentence'
The Mnemonic
class can use any list of 2048 unique words to generate the mnemonic code. For convenience the class provides default word lists for the following languages: English (default), Chinese, French, Japanese and Spanish. Those word list are published under Mnemonic.Words.LANGUAGE
, take a look at the following example:
var Mnemonic = require('bitcore-mnemonic');
var code = new Mnemonic(Mnemonic.Words.SPANISH);
code.toString(); // natal hada sutil año sólido papel jamón combate aula flota ver esfera...
var myWordList = [ 'abandon', 'ability', 'able', 'about', 'above', ... ];
var customCode = new Mnemonic(myWordList);
The Mnemonic class provides a static method to check if a mnemonic string is valid. If you generated the mnemonic code using any of the default word list, the class will identify it, otherwise you must provide the word list used.
var Mnemonic = require('bitcore-mnemonic');
var code = 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
var valid = Mnemonic.isValid(code);
// using a custom word list
var validCutom = Mnemonic.isValid(code, customWordlist);
A mnemonic encodes entropy that can be used for creating a seed and later a HDPrivateKey. During the seed generation process a passphrase can be used. The code for doing so looks like this:
var Mnemonic = require('bitcore-mnemonic');
var code = new Mnemonic('select scout crash enforce riot rival spring whale hollow radar rule sentence');
var xpriv1 = code.toHDPrivateKey(); // no passphrase
var xpriv2 = code.toHDPrivateKey('my passphrase'); // using a passphrase