aboutsummaryrefslogtreecommitdiff
path: root/src/js/entropy.js
diff options
context:
space:
mode:
authorIan Coleman <ian@iancoleman.io>2019-12-17 13:42:44 +1100
committerIan Coleman <ian@iancoleman.io>2019-12-17 14:10:45 +1100
commit516c16d721db88b4b2c39964e2d5e8f6310c7bff (patch)
treeca4d1e8742d46715892adabe5003396819d7d12b /src/js/entropy.js
parentf7e9fdf002e7355a122a86a8407b470b56bf3f59 (diff)
downloadBIP39-516c16d721db88b4b2c39964e2d5e8f6310c7bff.tar.gz
BIP39-516c16d721db88b4b2c39964e2d5e8f6310c7bff.tar.zst
BIP39-516c16d721db88b4b2c39964e2d5e8f6310c7bff.zip
Allow manual override for entropy type
Diffstat (limited to 'src/js/entropy.js')
-rw-r--r--src/js/entropy.js17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/js/entropy.js b/src/js/entropy.js
index a709c78..a4c7622 100644
--- a/src/js/entropy.js
+++ b/src/js/entropy.js
@@ -67,9 +67,9 @@ window.Entropy = new (function() {
67 return ints; 67 return ints;
68 } 68 }
69 69
70 this.fromString = function(rawEntropyStr) { 70 this.fromString = function(rawEntropyStr, baseStr) {
71 // Find type of entropy being used (binary, hex, dice etc) 71 // Find type of entropy being used (binary, hex, dice etc)
72 var base = getBase(rawEntropyStr); 72 var base = getBase(rawEntropyStr, baseStr);
73 // Convert dice to base6 entropy (ie 1-6 to 0-5) 73 // Convert dice to base6 entropy (ie 1-6 to 0-5)
74 // This is done by changing all 6s to 0s 74 // This is done by changing all 6s to 0s
75 if (base.str == "dice") { 75 if (base.str == "dice") {
@@ -166,13 +166,14 @@ window.Entropy = new (function() {
166 return s; 166 return s;
167 } 167 }
168 168
169 function getBase(str) { 169 function getBase(str, baseStr) {
170 // Need to get the lowest base for the supplied entropy. 170 // Need to get the lowest base for the supplied entropy.
171 // This prevents interpreting, say, dice rolls as hexadecimal. 171 // This prevents interpreting, say, dice rolls as hexadecimal.
172 var binaryMatches = matchers.binary(str); 172 var binaryMatches = matchers.binary(str);
173 var hexMatches = matchers.hex(str); 173 var hexMatches = matchers.hex(str);
174 var autodetect = baseStr === undefined;
174 // Find the lowest base that can be used, whilst ignoring any irrelevant chars 175 // Find the lowest base that can be used, whilst ignoring any irrelevant chars
175 if (binaryMatches.length == hexMatches.length && hexMatches.length > 0) { 176 if ((binaryMatches.length == hexMatches.length && hexMatches.length > 0 && autodetect) || baseStr === "binary") {
176 var ints = binaryMatches.map(function(i) { return parseInt(i, 2) }); 177 var ints = binaryMatches.map(function(i) { return parseInt(i, 2) });
177 return { 178 return {
178 ints: ints, 179 ints: ints,
@@ -183,7 +184,7 @@ window.Entropy = new (function() {
183 } 184 }
184 } 185 }
185 var cardMatches = matchers.card(str); 186 var cardMatches = matchers.card(str);
186 if (cardMatches.length >= hexMatches.length / 2) { 187 if ((cardMatches.length >= hexMatches.length / 2 && autodetect) || baseStr === "card") {
187 var ints = convertCardsToInts(cardMatches); 188 var ints = convertCardsToInts(cardMatches);
188 return { 189 return {
189 ints: ints, 190 ints: ints,
@@ -194,7 +195,7 @@ window.Entropy = new (function() {
194 } 195 }
195 } 196 }
196 var diceMatches = matchers.dice(str); 197 var diceMatches = matchers.dice(str);
197 if (diceMatches.length == hexMatches.length && hexMatches.length > 0) { 198 if ((diceMatches.length == hexMatches.length && hexMatches.length > 0 && autodetect) || baseStr === "dice") {
198 var ints = diceMatches.map(function(i) { return parseInt(i) }); 199 var ints = diceMatches.map(function(i) { return parseInt(i) });
199 return { 200 return {
200 ints: ints, 201 ints: ints,
@@ -205,7 +206,7 @@ window.Entropy = new (function() {
205 } 206 }
206 } 207 }
207 var base6Matches = matchers.base6(str); 208 var base6Matches = matchers.base6(str);
208 if (base6Matches.length == hexMatches.length && hexMatches.length > 0) { 209 if ((base6Matches.length == hexMatches.length && hexMatches.length > 0 && autodetect) || baseStr === "base 6") {
209 var ints = base6Matches.map(function(i) { return parseInt(i) }); 210 var ints = base6Matches.map(function(i) { return parseInt(i) });
210 return { 211 return {
211 ints: ints, 212 ints: ints,
@@ -216,7 +217,7 @@ window.Entropy = new (function() {
216 } 217 }
217 } 218 }
218 var base10Matches = matchers.base10(str); 219 var base10Matches = matchers.base10(str);
219 if (base10Matches.length == hexMatches.length && hexMatches.length > 0) { 220 if ((base10Matches.length == hexMatches.length && hexMatches.length > 0 && autodetect) || baseStr === "base 10") {
220 var ints = base10Matches.map(function(i) { return parseInt(i) }); 221 var ints = base10Matches.map(function(i) { return parseInt(i) });
221 return { 222 return {
222 ints: ints, 223 ints: ints,