diff options
author | Ian Coleman <coleman.ian@gmail.com> | 2016-11-10 18:58:18 +1100 |
---|---|---|
committer | Ian Coleman <coleman.ian@gmail.com> | 2016-11-10 18:58:18 +1100 |
commit | 3599674db4855614d9123f2f80dcf0a37c34eb79 (patch) | |
tree | 6bcfd76aa73c71cb4b79c21455da88c51d882468 | |
parent | 1cf1bbaff575e647ef38b091a00edfa2e1ce024d (diff) | |
download | BIP39-3599674db4855614d9123f2f80dcf0a37c34eb79.tar.gz BIP39-3599674db4855614d9123f2f80dcf0a37c34eb79.tar.zst BIP39-3599674db4855614d9123f2f80dcf0a37c34eb79.zip |
Mnemonic length can be set when using raw entropy
-rw-r--r-- | src/index.html | 2 | ||||
-rw-r--r-- | src/js/index.js | 21 | ||||
-rw-r--r-- | tests.js | 25 |
3 files changed, 45 insertions, 3 deletions
diff --git a/src/index.html b/src/index.html index feefbd8..beda577 100644 --- a/src/index.html +++ b/src/index.html | |||
@@ -137,7 +137,7 @@ | |||
137 | <label class="col-sm-2 control-label">Mnemonic Length</label> | 137 | <label class="col-sm-2 control-label">Mnemonic Length</label> |
138 | <div class="col-sm-10"> | 138 | <div class="col-sm-10"> |
139 | <select class="mnemonic-length form-control"> | 139 | <select class="mnemonic-length form-control"> |
140 | <option value="raw">From entropy length</option> | 140 | <option value="raw">From entropy length (3 words per 32 bits)</option> |
141 | <option value="12">12 Words</option> | 141 | <option value="12">12 Words</option> |
142 | <option value="15">15 Words</option> | 142 | <option value="15">15 Words</option> |
143 | <option value="18">18 Words</option> | 143 | <option value="18">18 Words</option> |
diff --git a/src/js/index.js b/src/js/index.js index 45f378d..66585a9 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -68,6 +68,7 @@ | |||
68 | DOM.network.on("change", networkChanged); | 68 | DOM.network.on("change", networkChanged); |
69 | DOM.useEntropy.on("change", setEntropyVisibility); | 69 | DOM.useEntropy.on("change", setEntropyVisibility); |
70 | DOM.entropy.on("input", delayedEntropyChanged); | 70 | DOM.entropy.on("input", delayedEntropyChanged); |
71 | DOM.entropyMnemonicLength.on("change", entropyChanged); | ||
71 | DOM.phrase.on("input", delayedPhraseChanged); | 72 | DOM.phrase.on("input", delayedPhraseChanged); |
72 | DOM.passphrase.on("input", delayedPhraseChanged); | 73 | DOM.passphrase.on("input", delayedPhraseChanged); |
73 | DOM.generate.on("click", generateClicked); | 74 | DOM.generate.on("click", generateClicked); |
@@ -744,9 +745,25 @@ | |||
744 | } | 745 | } |
745 | // Show entropy details | 746 | // Show entropy details |
746 | showEntropyFeedback(entropy); | 747 | showEntropyFeedback(entropy); |
748 | // Use entropy hash if not using raw entropy | ||
749 | var bits = entropy.binaryStr; | ||
750 | var mnemonicLength = DOM.entropyMnemonicLength.val(); | ||
751 | if (mnemonicLength != "raw") { | ||
752 | // Get bits by hashing entropy with SHA256 | ||
753 | var hash = sjcl.hash.sha256.hash(entropy.cleanStr); | ||
754 | var hex = sjcl.codec.hex.fromBits(hash); | ||
755 | bits = BigInteger.parse(hex, 16).toString(2); | ||
756 | for (var i=0; i<256-bits.length; i++) { | ||
757 | bits = "0" + bits; | ||
758 | } | ||
759 | // Truncate hash to suit number of words | ||
760 | mnemonicLength = parseInt(mnemonicLength); | ||
761 | var numberOfBits = 32 * mnemonicLength / 3; | ||
762 | bits = bits.substring(0, numberOfBits); | ||
763 | } | ||
747 | // Discard trailing entropy | 764 | // Discard trailing entropy |
748 | var bitsToUse = Math.floor(entropy.binaryStr.length / 32) * 32; | 765 | var bitsToUse = Math.floor(bits.length / 32) * 32; |
749 | var binaryStr = entropy.binaryStr.substring(0, bitsToUse); | 766 | var binaryStr = bits.substring(0, bitsToUse); |
750 | // Convert entropy string to numeric array | 767 | // Convert entropy string to numeric array |
751 | var entropyArr = []; | 768 | var entropyArr = []; |
752 | for (var i=0; i<binaryStr.length / 8; i++) { | 769 | for (var i=0; i<binaryStr.length / 8; i++) { |
@@ -2774,6 +2774,31 @@ page.open(url, function(status) { | |||
2774 | }); | 2774 | }); |
2775 | }, | 2775 | }, |
2776 | 2776 | ||
2777 | // Mnemonic length can be selected even for weak entropy | ||
2778 | function() { | ||
2779 | page.open(url, function(status) { | ||
2780 | // use entropy | ||
2781 | page.evaluate(function() { | ||
2782 | $(".use-entropy").prop("checked", true).trigger("change"); | ||
2783 | $(".entropy").val("012345"); | ||
2784 | $(".mnemonic-length").val("18").trigger("change"); | ||
2785 | }); | ||
2786 | // check the mnemonic is the correct length | ||
2787 | waitForGenerate(function() { | ||
2788 | var phrase = page.evaluate(function() { | ||
2789 | return $(".phrase").val(); | ||
2790 | }); | ||
2791 | var numberOfWords = phrase.split(/\s/g).length; | ||
2792 | if (numberOfWords != 18) { | ||
2793 | console.log("Weak entropy cannot be overridden to give 18 word mnemonic"); | ||
2794 | console.log(phrase); | ||
2795 | fail(); | ||
2796 | } | ||
2797 | next(); | ||
2798 | }); | ||
2799 | }); | ||
2800 | }, | ||
2801 | |||
2777 | // If you wish to add more tests, do so here... | 2802 | // If you wish to add more tests, do so here... |
2778 | 2803 | ||
2779 | // Here is a blank test template | 2804 | // Here is a blank test template |