aboutsummaryrefslogtreecommitdiff
path: root/src/js/index.js
diff options
context:
space:
mode:
authorIan Coleman <ian@iancoleman.io>2017-11-22 15:28:58 +1100
committerIan Coleman <ian@iancoleman.io>2017-11-22 15:37:36 +1100
commit74ab4cbe90d037604120c93230ac9b0d74a6c7f1 (patch)
treec9c71ea4c4032f46f8adcce76a720a7115643a4f /src/js/index.js
parentcf6c2044973413e3cfcda03079c6a66756fb023c (diff)
downloadBIP39-74ab4cbe90d037604120c93230ac9b0d74a6c7f1.tar.gz
BIP39-74ab4cbe90d037604120c93230ac9b0d74a6c7f1.tar.zst
BIP39-74ab4cbe90d037604120c93230ac9b0d74a6c7f1.zip
Show entropy from PRNG and word indexes
see issue #132
Diffstat (limited to 'src/js/index.js')
-rw-r--r--src/js/index.js43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/js/index.js b/src/js/index.js
index 261a6d1..58090c3 100644
--- a/src/js/index.js
+++ b/src/js/index.js
@@ -37,6 +37,7 @@
37 DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event"); 37 DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event");
38 DOM.entropyWordCount = DOM.entropyContainer.find(".word-count"); 38 DOM.entropyWordCount = DOM.entropyContainer.find(".word-count");
39 DOM.entropyBinary = DOM.entropyContainer.find(".binary"); 39 DOM.entropyBinary = DOM.entropyContainer.find(".binary");
40 DOM.entropyWordIndexes = DOM.entropyContainer.find(".word-indexes");
40 DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length"); 41 DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length");
41 DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning"); 42 DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning");
42 DOM.phrase = $(".phrase"); 43 DOM.phrase = $(".phrase");
@@ -219,6 +220,8 @@
219 var passphrase = DOM.passphrase.val(); 220 var passphrase = DOM.passphrase.val();
220 calcBip32RootKeyFromSeed(phrase, passphrase); 221 calcBip32RootKeyFromSeed(phrase, passphrase);
221 calcForDerivationPath(); 222 calcForDerivationPath();
223 // Show the word indexes
224 showWordIndexes();
222 } 225 }
223 226
224 function tabChanged() { 227 function tabChanged() {
@@ -420,10 +423,20 @@
420 showValidationError(errorText); 423 showValidationError(errorText);
421 return; 424 return;
422 } 425 }
426 // get the amount of entropy to use
423 var numWords = parseInt(DOM.generatedStrength.val()); 427 var numWords = parseInt(DOM.generatedStrength.val());
424 var strength = numWords / 3 * 32; 428 var strength = numWords / 3 * 32;
425 var words = mnemonic.generate(strength); 429 var buffer = new Uint8Array(strength / 8);
430 // create secure entropy
431 var data = crypto.getRandomValues(buffer);
432 // show the words
433 var words = mnemonic.toMnemonic(data);
426 DOM.phrase.val(words); 434 DOM.phrase.val(words);
435 // show the entropy
436 var entropyHex = uint8ArrayToHex(data);
437 DOM.entropy.val(entropyHex);
438 // ensure entropy fields are consistent with what is being displayed
439 DOM.entropyMnemonicLength.val("raw");
427 return words; 440 return words;
428 } 441 }
429 442
@@ -1103,6 +1116,8 @@
1103 var phrase = mnemonic.toMnemonic(entropyArr); 1116 var phrase = mnemonic.toMnemonic(entropyArr);
1104 // Set the mnemonic in the UI 1117 // Set the mnemonic in the UI
1105 DOM.phrase.val(phrase); 1118 DOM.phrase.val(phrase);
1119 // Show the word indexes
1120 showWordIndexes();
1106 } 1121 }
1107 1122
1108 function clearEntropyFeedback() { 1123 function clearEntropyFeedback() {
@@ -1328,6 +1343,32 @@
1328 return parseInt(lastBitClean); 1343 return parseInt(lastBitClean);
1329 } 1344 }
1330 1345
1346 function uint8ArrayToHex(a) {
1347 var s = ""
1348 for (var i=0; i<a.length; i++) {
1349 var h = a[i].toString(16);
1350 while (h.length < 2) {
1351 h = "0" + h;
1352 }
1353 s = s + h;
1354 }
1355 return s;
1356 }
1357
1358 function showWordIndexes() {
1359 var phrase = DOM.phrase.val();
1360 var words = phraseToWordArray(phrase);
1361 var wordIndexes = [];
1362 var language = getLanguage();
1363 for (var i=0; i<words.length; i++) {
1364 var word = words[i];
1365 var wordIndex = WORDLISTS[language].indexOf(word);
1366 wordIndexes.push(wordIndex);
1367 }
1368 var wordIndexesStr = wordIndexes.join(", ");
1369 DOM.entropyWordIndexes.text(wordIndexesStr);
1370 }
1371
1331 var networks = [ 1372 var networks = [
1332 { 1373 {
1333 name: "BCH - Bitcoin Cash", 1374 name: "BCH - Bitcoin Cash",