Skip to content

Commit

Permalink
Show entropy from PRNG and word indexes
Browse files Browse the repository at this point in the history
see issue iancoleman#132
  • Loading branch information
iancoleman committed Nov 22, 2017
1 parent cf6c204 commit 74ab4cb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ <h2>Mnemonic</h2>
<div class="filtered col-sm-9 form-control-static"></div>
<label class="col-sm-3 control-label">Raw Binary</label>
<div class="binary col-sm-9 form-control-static"></div>
<label class="col-sm-3 control-label">Word Indexes</label>
<div class="word-indexes col-sm-9 form-control-static">&nbsp;</div>
<label class="col-sm-3 control-label">Mnemonic Length</label>
<div class="col-sm-9">
<select class="mnemonic-length form-control">
Expand Down Expand Up @@ -223,7 +225,7 @@ <h2>Mnemonic</h2>
<div class="col-sm-10 checkbox">
<label>
<input type="checkbox" class="use-entropy">
<span>Supply my own source of entropy</span>
<span>Show entropy details</span>
</label>
</div>
</div>
Expand Down
43 changes: 42 additions & 1 deletion src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event");
DOM.entropyWordCount = DOM.entropyContainer.find(".word-count");
DOM.entropyBinary = DOM.entropyContainer.find(".binary");
DOM.entropyWordIndexes = DOM.entropyContainer.find(".word-indexes");
DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length");
DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning");
DOM.phrase = $(".phrase");
Expand Down Expand Up @@ -219,6 +220,8 @@
var passphrase = DOM.passphrase.val();
calcBip32RootKeyFromSeed(phrase, passphrase);
calcForDerivationPath();
// Show the word indexes
showWordIndexes();
}

function tabChanged() {
Expand Down Expand Up @@ -420,10 +423,20 @@
showValidationError(errorText);
return;
}
// get the amount of entropy to use
var numWords = parseInt(DOM.generatedStrength.val());
var strength = numWords / 3 * 32;
var words = mnemonic.generate(strength);
var buffer = new Uint8Array(strength / 8);
// create secure entropy
var data = crypto.getRandomValues(buffer);
// show the words
var words = mnemonic.toMnemonic(data);
DOM.phrase.val(words);
// show the entropy
var entropyHex = uint8ArrayToHex(data);
DOM.entropy.val(entropyHex);
// ensure entropy fields are consistent with what is being displayed
DOM.entropyMnemonicLength.val("raw");
return words;
}

Expand Down Expand Up @@ -1103,6 +1116,8 @@
var phrase = mnemonic.toMnemonic(entropyArr);
// Set the mnemonic in the UI
DOM.phrase.val(phrase);
// Show the word indexes
showWordIndexes();
}

function clearEntropyFeedback() {
Expand Down Expand Up @@ -1328,6 +1343,32 @@
return parseInt(lastBitClean);
}

function uint8ArrayToHex(a) {
var s = ""
for (var i=0; i<a.length; i++) {
var h = a[i].toString(16);
while (h.length < 2) {
h = "0" + h;
}
s = s + h;
}
return s;
}

function showWordIndexes() {
var phrase = DOM.phrase.val();
var words = phraseToWordArray(phrase);
var wordIndexes = [];
var language = getLanguage();
for (var i=0; i<words.length; i++) {
var word = words[i];
var wordIndex = WORDLISTS[language].indexOf(word);
wordIndexes.push(wordIndex);
}
var wordIndexesStr = wordIndexes.join(", ");
DOM.entropyWordIndexes.text(wordIndexesStr);
}

var networks = [
{
name: "BCH - Bitcoin Cash",
Expand Down
27 changes: 27 additions & 0 deletions tests/spec/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2643,4 +2643,31 @@ it('Can generate BIP141 addresses with P2WPKH semanitcs', function(done) {
});
});

it('Shows the entropy used by the PRNG when clicking generate', function(done) {
driver.findElement(By.css('.generate')).click();
driver.sleep(generateDelay).then(function() {
driver.findElement(By.css('.entropy'))
.getAttribute("value")
.then(function(entropy) {
expect(entropy).not.toBe("");
done();
});
});
});

it('Shows the index of each word in the mnemonic', function(done) {
driver.findElement(By.css('.phrase'))
.sendKeys("abandon abandon ability");
driver.sleep(generateDelay).then(function() {
driver.findElement(By.css('.use-entropy'))
.click();
driver.findElement(By.css('.word-indexes'))
.getText()
.then(function(indexes) {
expect(indexes).toBe("0, 0, 1");
done();
});
});
});

});

0 comments on commit 74ab4cb

Please sign in to comment.