This commit is contained in:
Christopher Jeffrey 2016-06-30 06:49:08 -07:00
parent b58ca398fb
commit 91fd1dc60d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -202,6 +202,7 @@ Mnemonic.fromOptions = function fromOptions(options) {
* Inject properties from entropy.
* @private
* @param {Buffer} entropy
* @param {String?} lang
*/
Mnemonic.prototype.fromEntropy = function fromEntropy(entropy, lang) {
@ -222,6 +223,7 @@ Mnemonic.prototype.fromEntropy = function fromEntropy(entropy, lang) {
/**
* Instantiate mnemonic from entropy.
* @param {Buffer} entropy
* @param {String?} lang
* @returns {Mnemonic}
*/
@ -241,17 +243,14 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) {
assert(typeof phrase === 'string');
words = phrase.split(/[ \u3000]/);
words = phrase.split(/[ \u3000]+/);
bits = words.length * 11;
lang = Mnemonic.getLanguage(words[0]);
ent = new Buffer(Math.ceil(bits / 8));
ent.fill(0);
wordlist = Mnemonic.getWordlist(lang);
ent.fill(0);
for (i = 0; i < words.length; i++) {
word = words[i];
index = wordlist.indexOf(word);
@ -270,11 +269,8 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) {
// Checksum bits:
cbits = bits % 32;
// Checksum bytes:
cbytes = Math.ceil(cbits / 8);
bits -= bits % 32;
entropy = ent.slice(0, ent.length - cbytes);
ent = ent.slice(ent.length - cbytes);
@ -289,6 +285,8 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) {
throw new Error('Invalid checksum.');
}
bits -= cbits;
assert(bits / 8 === entropy.length);
assert(bits >= 128);
assert(bits % 32 === 0);
@ -302,9 +300,10 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) {
};
/**
* Instantiate mnemonic from a phrase.
* Instantiate mnemonic from a phrase (validates checksum).
* @param {String} phrase
* @returns {Mnemonic}
* @throws on bad checksum
*/
Mnemonic.fromPhrase = function fromPhrase(phrase) {
@ -487,6 +486,8 @@ Mnemonic.prototype.fromJSON = function fromJSON(json) {
this.phrase = json.phrase;
this.passphrase = json.passphrase;
assert(this.bits >= 128);
assert(this.bits % 32 === 0);
assert(this.bits / 8 === this.entropy.length);
return this;
@ -537,6 +538,9 @@ Mnemonic.prototype.fromRaw = function fromRaw(data) {
this.phrase = p.readVarString('utf8');
this.passphrase = p.readVarString('utf8');
assert(this.bits >= 128);
assert(this.bits % 32 === 0);
return this;
};