@duality-solutions/bitcore-mnemonic-dynamic
v1.0.1
Published
BIP39 Mnemonics for Dynamic
Downloads
10
Maintainers
Readme
bitcore-mnemonic-dynamic
Note: This package has been deprecated
This module is superceded the functionality in @duality-solutions/dynamic-lib, please use that package moving forward.
BIP39 Mnemonics for Dynamic
A module for bitcore-lib-dynamic that implements Mnemonic code for generating deterministic keys.
Install
npm install @duality-solutions/dynamic-mnemonic
Usage
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.
There are many examples of how to use it on the developer guide section for mnemonic. For example, the following code would generate a new random mnemonic code and convert it to a HDPrivateKey
.
var Mnemonic = require('@duality-solutions/bitcore-mnemonic-dynamic');
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 xpriv = code.toHDPrivateKey();
Mnemonic generation
For creating a new random mnemonic code you just create a new instance.
var Mnemonic = require('@duality-solutions/bitcore-mnemonic-dynamic');
var code = new Mnemonic();
code.toString(); // 'select scout crash enforce riot rival spring whale hollow radar rule sentence'
Multi-language support
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('@duality-solutions/bitcore-mnemonic-dynamic');
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);
Validating a mnemonic
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('@duality-solutions/bitcore-mnemonic-dynamic');
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);
Generating a private key
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('@duality-solutions/bitcore-mnemonic-dynamic');
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
Browser Usage
A webpack config is included in order to build a browser bundle:
npm run build
This will generate a file dist/dynamic-mnemonic.js
, which can be included into your HTML a-like so:
<html lang="en">
<head>
<title>Tester - Bitcore Mnemonic Dynamic</title>
<script language="javascript" src="dist/bitcore-mnemonic-dynamic.js"></script>
</head>
<body>
<script>
document.addEventListener("DOMContentLoaded", function() {
var mnemonic = new dynamicMnemonic();
console.log("MNEMONIC: " + mnemonic.toString());
var xpriv1 = mnemonic.toHDPrivateKey(); // no passphrase
var xpriv2 = mnemonic.toHDPrivateKey('my passphrase'); // using a passphrase
console.log("xpriv1: " + xpriv1);
console.log("xpriv2: " + xpriv2);
});
</script>
</body>
</html>
This test snippet is included as a file in this repo called "example.html", and you can test this in a browser. We'll use Docker to simplify this:
docker run --rm --name nginx -p 127.0.0.1:80:80 -v $PWD:/usr/share/nginx/html nginx:alpine
Now navigate to http://localhost/example.html and open a console window. You should see the expected console output from the console.log
lines in the HTML snippet above.
Contributing
Feel free to dive in! Open an issue or submit PRs.
License
Code released under the MIT license.
Copyright 2013-2019 BitPay, Inc. Bitcore is a trademark maintained by BitPay, Inc.
Copyright 2019 Duality Blockchain Solutions LLC.