Skip to content

Commit

Permalink
Merge pull request #1 from bitpay/master
Browse files Browse the repository at this point in the history
rebase master
  • Loading branch information
dabura667 committed Sep 21, 2015
2 parents 07df984 + b6fbea3 commit 35db781
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var xpriv = code.toHDPrivateKey();

## Contributing

See [CONTRIBUTING.md](https://github.com/bitpay/bitcore) on the main bitcore repo for information about how to contribute.
See [CONTRIBUTING.md](https://github.com/bitpay/bitcore/blob/master/CONTRIBUTING.md) on the main bitcore repo for information about how to contribute.

## License

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bitcore-mnemonic",
"main": "./bitcore-mnemonic.min.js",
"version": "0.11.0",
"version": "0.13.2",
"homepage": "https://github.com/bitpay/bitcore-mnemonic",
"authors": [
"BitPay"
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ code.toString(); // 'select scout crash enforce riot rival spring whale hollow r

## 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, Japanese and Spanish. Those word list are published under `Mnemonic.Words.LANGUAGE`, take a look at the following example:
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:

```javascript
var Mnemonic = require('bitcore-mnemonic');
Expand Down
29 changes: 20 additions & 9 deletions lib/mnemonic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var bitcore = require('bitcore');
var BN = bitcore.crypto.BN;
var unorm = require('unorm');
var _ = bitcore.deps._;

Expand Down Expand Up @@ -107,7 +108,7 @@ Mnemonic.Words = require('./words');
* @returns {boolean}
*/
Mnemonic.isValid = function(mnemonic, wordlist) {
mnemonic = unorm.nfkd(mnemonic)
mnemonic = unorm.nfkd(mnemonic);
wordlist = wordlist || Mnemonic._getDictionary(mnemonic);

if (!wordlist) {
Expand Down Expand Up @@ -141,8 +142,12 @@ Mnemonic.isValid = function(mnemonic, wordlist) {
* @returns {boolean}
*/
Mnemonic._belongsToWordlist = function(mnemonic, wordlist) {
var word = unorm.nfkd(mnemonic).split(' ')[0];
return wordlist.indexOf(word) !== -1; // only checks for a word
var words = unorm.nfkd(mnemonic).split(' ');
for (var i = 0; i < words.length; i++) {
var ind = wordlist.indexOf(words[i]);
if (ind < 0) return false;
}
return true;
};

/**
Expand Down Expand Up @@ -254,10 +259,11 @@ Mnemonic._entropy2mnemonic = function(entropy, wordlist) {
var wi = parseInt(bin.slice(i * 11, (i + 1) * 11), 2);
mnemonic.push(wordlist[wi]);
}
if (wordlist == Mnemonic.Words.JAPANESE) {
var ret = mnemonic.join('\u3000');
var ret;
if (wordlist === Mnemonic.Words.JAPANESE) {
ret = mnemonic.join('\u3000');
} else {
var ret = mnemonic.join(' ');
ret = mnemonic.join(' ');
}
return ret;
};
Expand All @@ -274,11 +280,16 @@ Mnemonic._entropyChecksum = function(entropy) {
var bits = entropy.length * 8;
var cs = bits / 32;

var hashbits = parseInt(hash.toString('hex'), 16).toString(2);
var hashbits = new BN(hash.toString('hex'), 16).toString(2);

// zero pad the hash bits
hashbits = (new Array(256).join('0') + hashbits).slice(-256).slice(0, cs);
while (hashbits.length % 256 !== 0) {
hashbits = '0' + hashbits;
}

var checksum = hashbits.slice(0, cs);

return hashbits;
return checksum;
};

module.exports = Mnemonic;
5 changes: 5 additions & 0 deletions lib/words/french.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/words/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
'CHINESE': require('./chinese'),
'ENGLISH': require('./english'),
'FRENCH': require('./french'),
'JAPANESE': require('./japanese'),
'SPANISH': require('./spanish')
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bitcore-mnemonic",
"version": "0.11.0",
"version": "0.13.2",
"description": "BIP39 Mnemonics implemented for Bitcore.",
"author": "BitPay <[email protected]>",
"main": "index.js",
Expand Down Expand Up @@ -37,7 +37,7 @@
"gulp": "^3.8.10"
},
"dependencies": {
"bitcore": "^0.12.0",
"bitcore": "^0.13.0",
"unorm": "^1.3.3"
}
}
10 changes: 9 additions & 1 deletion test/mnemonic.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Mnemonic', function() {

it('should fail with unknown word list', function() {
(function() {
return new Mnemonic('pilote foster august tomorrow kit daughter unknown awesome model town village master');
return new Mnemonic('pilots foster august tomorrow kit daughter unknown awesome model town village master');
}).should.throw(errors.Mnemonic.UnknownWordlist);
});

Expand Down Expand Up @@ -90,6 +90,11 @@ describe('Mnemonic', function() {
Mnemonic.Words.CHINESE[0].should.equal('的');
});

it('french wordlist is complete', function() {
Mnemonic.Words.FRENCH.length.should.equal(2048);
Mnemonic.Words.FRENCH[0].should.equal('abaisser');
});

it('allows use different phrase lengths', function() {
var mnemonic;

Expand Down Expand Up @@ -121,6 +126,9 @@ describe('Mnemonic', function() {

var invalid3 = Mnemonic.isValid('totally invalid phrase');
invalid3.should.equal(false);

var valid2 = Mnemonic.isValid('caution opprimer époque belote devenir ficeler filleul caneton apologie nectar frapper fouiller');
valid2.should.equal(true);
});

it('has a toString method', function() {
Expand Down

0 comments on commit 35db781

Please sign in to comment.