diff options
author | iancoleman <1281387+iancoleman@users.noreply.github.com> | 2019-11-11 09:21:07 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-11 09:21:07 +1100 |
commit | 3dbf117928bb17d58cc54fa1faa9354c014e665b (patch) | |
tree | f3735716fbd1c66c89698461060715979e37df92 | |
parent | 342ff964aa63354694d659c183e2525aa0983a56 (diff) | |
parent | 611f76238dedd0418c79a0d918d46d2c92b0149c (diff) | |
download | BIP39-3dbf117928bb17d58cc54fa1faa9354c014e665b.tar.gz BIP39-3dbf117928bb17d58cc54fa1faa9354c014e665b.tar.zst BIP39-3dbf117928bb17d58cc54fa1faa9354c014e665b.zip |
Merge pull request #271 from cernekee/local
Allow converting mnemonic back to raw entropy value
-rw-r--r-- | src/js/index.js | 9 | ||||
-rw-r--r-- | src/js/jsbip39.js | 51 |
2 files changed, 44 insertions, 16 deletions
diff --git a/src/js/index.js b/src/js/index.js index f3302a6..da901f6 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -232,7 +232,14 @@ | |||
232 | if (phraseChangeTimeoutEvent != null) { | 232 | if (phraseChangeTimeoutEvent != null) { |
233 | clearTimeout(phraseChangeTimeoutEvent); | 233 | clearTimeout(phraseChangeTimeoutEvent); |
234 | } | 234 | } |
235 | phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400); | 235 | phraseChangeTimeoutEvent = setTimeout(function() { |
236 | phraseChanged(); | ||
237 | var entropy = mnemonic.toRawEntropyHex(DOM.phrase.val()); | ||
238 | if (entropy !== null) { | ||
239 | DOM.entropyMnemonicLength.val("raw"); | ||
240 | DOM.entropy.val(entropy); | ||
241 | } | ||
242 | }, 400); | ||
236 | } | 243 | } |
237 | 244 | ||
238 | function phraseChanged() { | 245 | function phraseChanged() { |
diff --git a/src/js/jsbip39.js b/src/js/jsbip39.js index 3230e3b..9a6e7ec 100644 --- a/src/js/jsbip39.js +++ b/src/js/jsbip39.js | |||
@@ -97,22 +97,10 @@ var Mnemonic = function(language) { | |||
97 | } | 97 | } |
98 | 98 | ||
99 | self.check = function(mnemonic) { | 99 | self.check = function(mnemonic) { |
100 | var mnemonic = self.splitWords(mnemonic); | 100 | var b = mnemonicToBinaryString(mnemonic); |
101 | if (mnemonic.length == 0 || mnemonic.length % 3 > 0) { | 101 | if (b === null) { |
102 | return false | 102 | return false; |
103 | } | ||
104 | // idx = map(lambda x: bin(self.wordlist.index(x))[2:].zfill(11), mnemonic) | ||
105 | var idx = []; | ||
106 | for (var i=0; i<mnemonic.length; i++) { | ||
107 | var word = mnemonic[i]; | ||
108 | var wordIndex = wordlist.indexOf(word); | ||
109 | if (wordIndex == -1) { | ||
110 | return false; | ||
111 | } | ||
112 | var binaryIndex = zfill(wordIndex.toString(2), 11); | ||
113 | idx.push(binaryIndex); | ||
114 | } | 103 | } |
115 | var b = idx.join(''); | ||
116 | var l = b.length; | 104 | var l = b.length; |
117 | //d = b[:l / 33 * 32] | 105 | //d = b[:l / 33 * 32] |
118 | //h = b[-l / 33:] | 106 | //h = b[-l / 33:] |
@@ -128,6 +116,20 @@ var Mnemonic = function(language) { | |||
128 | return h == nh; | 116 | return h == nh; |
129 | } | 117 | } |
130 | 118 | ||
119 | self.toRawEntropyHex = function(mnemonic) { | ||
120 | var b = mnemonicToBinaryString(mnemonic); | ||
121 | if (b === null) | ||
122 | return null; | ||
123 | var d = b.substring(0, b.length / 33 * 32); | ||
124 | var nd = binaryStringToWordArray(d); | ||
125 | |||
126 | var h = ""; | ||
127 | for (var i=0; i<nd.length; i++) { | ||
128 | h += ('0000000' + nd[i].toString(16)).slice(-8); | ||
129 | } | ||
130 | return h; | ||
131 | } | ||
132 | |||
131 | self.toSeed = function(mnemonic, passphrase) { | 133 | self.toSeed = function(mnemonic, passphrase) { |
132 | passphrase = passphrase || ''; | 134 | passphrase = passphrase || ''; |
133 | mnemonic = self.joinWords(self.splitWords(mnemonic)); // removes duplicate blanks | 135 | mnemonic = self.joinWords(self.splitWords(mnemonic)); // removes duplicate blanks |
@@ -200,6 +202,25 @@ var Mnemonic = function(language) { | |||
200 | return a; | 202 | return a; |
201 | } | 203 | } |
202 | 204 | ||
205 | function mnemonicToBinaryString(mnemonic) { | ||
206 | var mnemonic = self.splitWords(mnemonic); | ||
207 | if (mnemonic.length == 0 || mnemonic.length % 3 > 0) { | ||
208 | return null; | ||
209 | } | ||
210 | // idx = map(lambda x: bin(self.wordlist.index(x))[2:].zfill(11), mnemonic) | ||
211 | var idx = []; | ||
212 | for (var i=0; i<mnemonic.length; i++) { | ||
213 | var word = mnemonic[i]; | ||
214 | var wordIndex = wordlist.indexOf(word); | ||
215 | if (wordIndex == -1) { | ||
216 | return null; | ||
217 | } | ||
218 | var binaryIndex = zfill(wordIndex.toString(2), 11); | ||
219 | idx.push(binaryIndex); | ||
220 | } | ||
221 | return idx.join(''); | ||
222 | } | ||
223 | |||
203 | // Pad a numeric string on the left with zero digits until the given width | 224 | // Pad a numeric string on the left with zero digits until the given width |
204 | // is reached. | 225 | // is reached. |
205 | // Note this differs to the python implementation because it does not | 226 | // Note this differs to the python implementation because it does not |