aboutsummaryrefslogtreecommitdiff
path: root/src/js
diff options
context:
space:
mode:
authorIan Coleman <coleman.ian@gmail.com>2016-08-15 16:03:01 +1000
committerIan Coleman <coleman.ian@gmail.com>2016-08-15 16:03:25 +1000
commit30c9e79de32e529e9b87b836a1bc808e6dbbc646 (patch)
tree15e7ba8106c1503c4c704629cce03d8a15663b33 /src/js
parent38523d36dcce2c11baaf8dd742d02b94c41c7b23 (diff)
downloadBIP39-30c9e79de32e529e9b87b836a1bc808e6dbbc646.tar.gz
BIP39-30c9e79de32e529e9b87b836a1bc808e6dbbc646.tar.zst
BIP39-30c9e79de32e529e9b87b836a1bc808e6dbbc646.zip
Derivation Path errors are detected.
Diffstat (limited to 'src/js')
-rw-r--r--src/js/index.js34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/js/index.js b/src/js/index.js
index f131e01..6f0da65 100644
--- a/src/js/index.js
+++ b/src/js/index.js
@@ -232,7 +232,39 @@
232 } 232 }
233 233
234 function findDerivationPathErrors(path) { 234 function findDerivationPathErrors(path) {
235 // TODO 235 // TODO is not perfect but is better than nothing
236 // Inspired by
237 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vectors
238 // and
239 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys
240 var maxDepth = 255; // TODO verify this!!
241 var maxIndexValue = Math.pow(2, 31); // TODO verify this!!
242 if (path[0] != "m") {
243 return "First character must be 'm'";
244 }
245 if (path.length > 1) {
246 if (path[1] != "/") {
247 return "Separator must be '/'";
248 }
249 var indexes = path.split("/");
250 if (indexes.length > maxDepth) {
251 return "Derivation depth is " + indexes.length + ", must be less than " + maxDepth;
252 }
253 for (var depth = 1; depth<indexes.length; depth++) {
254 var index = indexes[depth];
255 var invalidChars = index.replace(/^[0-9]+'?$/g, "")
256 if (invalidChars.length > 0) {
257 return "Invalid characters " + invalidChars + " found at depth " + depth;
258 }
259 var indexValue = parseInt(index.replace("'", ""));
260 if (isNaN(depth)) {
261 return "Invalid number at depth " + depth;
262 }
263 if (indexValue > maxIndexValue) {
264 return "Value of " + indexValue + " at depth " + depth + " must be less than " + maxIndexValue;
265 }
266 }
267 }
236 return false; 268 return false;
237 } 269 }
238 270