Allow converting mnemonic back to raw entropy value

Currently, this sequence produces an unexpected result:

1) Navigate to bip39-standalone.html

2) Paste a known-good mnemonic into the BIP39 Mnemonic field

3) Select "Show entropy details"

This will erase the BIP39 Mnemonic field and most of the derived
address/key information.  It shows an empty Entropy field, and zeroes
for Event Count, Bits Per Event, Raw Entropy Words, Total Bits, etc.
However, it will show valid Word Indexes and BIP39 Seed.

The way to fix it is to convert the mnemonic back into a raw entropy
value and populate it in DOM.entropy, so that everything stays
consistent.  This will only happen if the mnemonic is manually entered
by the user, not if phraseChanged() is triggered by hand-editing the
entropy value.
This commit is contained in:
Kevin Cernekee
2018-10-28 13:20:33 -07:00
parent 011533e4bc
commit 611f76238d
2 changed files with 44 additions and 16 deletions

View File

@@ -232,7 +232,14 @@
if (phraseChangeTimeoutEvent != null) {
clearTimeout(phraseChangeTimeoutEvent);
}
phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400);
phraseChangeTimeoutEvent = setTimeout(function() {
phraseChanged();
var entropy = mnemonic.toRawEntropyHex(DOM.phrase.val());
if (entropy !== null) {
DOM.entropyMnemonicLength.val("raw");
DOM.entropy.val(entropy);
}
}, 400);
}
function phraseChanged() {

View File

@@ -97,22 +97,10 @@ var Mnemonic = function(language) {
}
self.check = function(mnemonic) {
var mnemonic = self.splitWords(mnemonic);
if (mnemonic.length == 0 || mnemonic.length % 3 > 0) {
return false
var b = mnemonicToBinaryString(mnemonic);
if (b === null) {
return false;
}
// idx = map(lambda x: bin(self.wordlist.index(x))[2:].zfill(11), mnemonic)
var idx = [];
for (var i=0; i<mnemonic.length; i++) {
var word = mnemonic[i];
var wordIndex = wordlist.indexOf(word);
if (wordIndex == -1) {
return false;
}
var binaryIndex = zfill(wordIndex.toString(2), 11);
idx.push(binaryIndex);
}
var b = idx.join('');
var l = b.length;
//d = b[:l / 33 * 32]
//h = b[-l / 33:]
@@ -128,6 +116,20 @@ var Mnemonic = function(language) {
return h == nh;
}
self.toRawEntropyHex = function(mnemonic) {
var b = mnemonicToBinaryString(mnemonic);
if (b === null)
return null;
var d = b.substring(0, b.length / 33 * 32);
var nd = binaryStringToWordArray(d);
var h = "";
for (var i=0; i<nd.length; i++) {
h += ('0000000' + nd[i].toString(16)).slice(-8);
}
return h;
}
self.toSeed = function(mnemonic, passphrase) {
passphrase = passphrase || '';
mnemonic = self.joinWords(self.splitWords(mnemonic)); // removes duplicate blanks
@@ -200,6 +202,25 @@ var Mnemonic = function(language) {
return a;
}
function mnemonicToBinaryString(mnemonic) {
var mnemonic = self.splitWords(mnemonic);
if (mnemonic.length == 0 || mnemonic.length % 3 > 0) {
return null;
}
// idx = map(lambda x: bin(self.wordlist.index(x))[2:].zfill(11), mnemonic)
var idx = [];
for (var i=0; i<mnemonic.length; i++) {
var word = mnemonic[i];
var wordIndex = wordlist.indexOf(word);
if (wordIndex == -1) {
return null;
}
var binaryIndex = zfill(wordIndex.toString(2), 11);
idx.push(binaryIndex);
}
return idx.join('');
}
// Pad a numeric string on the left with zero digits until the given width
// is reached.
// Note this differs to the python implementation because it does not