]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blame - tests.js
Test BIP44 coin field
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / tests.js
CommitLineData
88e2cdaa
IC
1// Usage:
2// $ phantomjs tests.js
3
4
5var page = require('webpage').create();
6var url = 'src/index.html';
3eef9d0d 7var testMaxTime = 5000;
88e2cdaa
IC
8
9page.onResourceError = function(e) {
10 console.log("Error loading " + e.url);
11 phantom.exit();
12}
13
14function fail() {
15 console.log("Failed");
16 phantom.exit();
17}
18
3eef9d0d
IC
19function waitForGenerate(fn, maxTime) {
20 if (!maxTime) {
21 maxTime = testMaxTime;
22 }
23 var start = new Date().getTime();
24 var prevAddressCount = -1;
25 var wait = function keepWaiting() {
26 var now = new Date().getTime();
27 var hasTimedOut = now - start > maxTime;
28 var addressCount = page.evaluate(function() {
29 return $(".address").length;
30 });
31 var hasFinished = addressCount > 0 && addressCount == prevAddressCount;
32 prevAddressCount = addressCount;
33 if (hasFinished) {
34 fn();
35 }
36 else if (hasTimedOut) {
37 console.log("Test timed out");
38 fn();
39 }
40 else {
41 setTimeout(keepWaiting, 100);
42 }
43 }
44 wait();
45}
46
88e2cdaa
IC
47function next() {
48 if (tests.length > 0) {
49 var testsStr = tests.length == 1 ? "test" : "tests";
50 console.log(tests.length + " " + testsStr + " remaining");
51 tests.shift()();
52 }
53 else {
54 console.log("Finished with 0 failures");
55 phantom.exit();
56 }
57}
58
fb372687
IC
59/**
60 * Randomize array element order in-place.
61 * Using Durstenfeld shuffle algorithm.
62 * See http://stackoverflow.com/a/12646864
63 */
64function shuffle(array) {
65 for (var i = array.length - 1; i > 0; i--) {
66 var j = Math.floor(Math.random() * (i + 1));
67 var temp = array[i];
68 array[i] = array[j];
69 array[j] = temp;
70 }
71 return array;
72}
73
88e2cdaa
IC
74tests = [
75
76// Page loads with status of 'success'
77function() {
78page.open(url, function(status) {
79 if (status != "success") {
80 console.log("Page did not load with status 'success'");
81 fail();
82 }
83 next();
84});
85},
86
87// Page has text
88function() {
89page.open(url, function(status) {
90 var content = page.evaluate(function() {
91 return document.body.textContent.trim();
92 });
93 if (!content) {
94 console.log("Page does not have text");
95 fail();
96 }
97 next();
98});
99},
100
101// Entering mnemonic generates addresses
102function() {
103page.open(url, function(status) {
104 var expected = "1Di3Vp7tBWtyQaDABLAjfWtF6V7hYKJtug";
105 // set the phrase
106 page.evaluate(function() {
107 $(".phrase").val("abandon abandon ability").trigger("input");
108 });
109 // get the address
3eef9d0d 110 waitForGenerate(function() {
88e2cdaa
IC
111 var actual = page.evaluate(function() {
112 return $(".address:first").text();
113 });
114 if (actual != expected) {
115 console.log("Mnemonic did not generate address");
116 console.log("Expected: " + expected);
117 console.log("Got: " + actual);
118 fail();
119 }
120 next();
3eef9d0d 121 });
88e2cdaa
IC
122});
123},
124
125// Random button generates random mnemonic
126function() {
127page.open(url, function(status) {
128 // check initial phrase is empty
129 var phrase = page.evaluate(function() {
130 return $(".phrase").text();
131 });
132 if (phrase != "") {
133 console.log("Initial phrase is not blank");
134 fail();
135 }
136 // press the 'generate' button
137 page.evaluate(function() {
138 $(".generate").click();
139 });
140 // get the new phrase
3eef9d0d 141 waitForGenerate(function() {
88e2cdaa
IC
142 var phrase = page.evaluate(function() {
143 return $(".phrase").val();
144 });
145 if (phrase.length <= 0) {
146 console.log("Phrase not generated by pressing button");
147 fail();
148 }
149 next();
3eef9d0d 150 });
88e2cdaa
IC
151});
152},
153
154// Mnemonic length can be customized
155function() {
156page.open(url, function(status) {
157 // set the length to 6
54563907 158 var expectedLength = "6";
88e2cdaa 159 page.evaluate(function() {
54563907
IC
160 $(".strength option[selected]").removeAttr("selected");
161 $(".strength option[value=6]").prop("selected", true);
88e2cdaa
IC
162 });
163 // press the 'generate' button
164 page.evaluate(function() {
165 $(".generate").click();
166 });
167 // check the new phrase is six words long
3eef9d0d 168 waitForGenerate(function() {
88e2cdaa
IC
169 var actualLength = page.evaluate(function() {
170 var words = $(".phrase").val().split(" ");
171 return words.length;
172 });
173 if (actualLength != expectedLength) {
174 console.log("Phrase not generated with correct length");
175 console.log("Expected: " + expectedLength);
176 console.log("Actual: " + actualLength);
177 fail();
178 }
54563907 179 next();
3eef9d0d 180 });
88e2cdaa
IC
181});
182},
183
88e2cdaa 184// Passphrase can be set
54563907
IC
185function() {
186page.open(url, function(status) {
187 // set the phrase and passphrase
188 var expected = "15pJzUWPGzR7avffV9nY5by4PSgSKG9rba";
189 page.evaluate(function() {
190 $(".phrase").val("abandon abandon ability");
191 $(".passphrase").val("secure_passphrase").trigger("input");
192 });
193 // check the address is generated correctly
3eef9d0d 194 waitForGenerate(function() {
54563907
IC
195 var actual = page.evaluate(function() {
196 return $(".address:first").text();
197 });
198 if (actual != expected) {
199 console.log("Passphrase results in wrong address");
200 console.log("Expected: " + expected);
201 console.log("Actual: " + actual);
202 fail();
203 }
204 next();
3eef9d0d 205 });
54563907
IC
206});
207},
208
88e2cdaa 209// Network can be set to bitcoin testnet
54563907
IC
210function() {
211page.open(url, function(status) {
59193779 212 // set the phrase and coin
54563907
IC
213 var expected = "mucaU5iiDaJDb69BHLeDv8JFfGiyg2nJKi";
214 page.evaluate(function() {
215 $(".phrase").val("abandon abandon ability");
216 $(".phrase").trigger("input");
217 $(".network option[selected]").removeAttr("selected");
218 $(".network option[value=1]").prop("selected", true);
219 $(".network").trigger("change");
220 });
221 // check the address is generated correctly
3eef9d0d 222 waitForGenerate(function() {
54563907
IC
223 var actual = page.evaluate(function() {
224 return $(".address:first").text();
225 });
226 if (actual != expected) {
227 console.log("Bitcoin testnet address is incorrect");
228 console.log("Expected: " + expected);
229 console.log("Actual: " + actual);
230 fail();
231 }
232 next();
3eef9d0d 233 });
54563907
IC
234});
235},
236
88e2cdaa 237// Network can be set to litecoin
59193779
IC
238function() {
239page.open(url, function(status) {
240 // set the phrase and coin
241 var expected = "LQ4XU8RX2ULPmPq9FcUHdVmPVchP9nwXdn";
242 page.evaluate(function() {
243 $(".phrase").val("abandon abandon ability");
244 $(".phrase").trigger("input");
245 $(".network option[selected]").removeAttr("selected");
246 $(".network option[value=2]").prop("selected", true);
247 $(".network").trigger("change");
248 });
249 // check the address is generated correctly
3eef9d0d 250 waitForGenerate(function() {
59193779
IC
251 var actual = page.evaluate(function() {
252 return $(".address:first").text();
253 });
254 if (actual != expected) {
255 console.log("Litecoin address is incorrect");
256 console.log("Expected: " + expected);
257 console.log("Actual: " + actual);
258 fail();
259 }
260 next();
3eef9d0d 261 });
59193779
IC
262});
263},
264
88e2cdaa 265// Network can be set to dogecoin
59193779
IC
266function() {
267page.open(url, function(status) {
268 // set the phrase and coin
269 var expected = "DPQH2AtuzkVSG6ovjKk4jbUmZ6iXLpgbJA";
270 page.evaluate(function() {
271 $(".phrase").val("abandon abandon ability");
272 $(".phrase").trigger("input");
273 $(".network option[selected]").removeAttr("selected");
274 $(".network option[value=3]").prop("selected", true);
275 $(".network").trigger("change");
276 });
277 // check the address is generated correctly
3eef9d0d 278 waitForGenerate(function() {
59193779
IC
279 var actual = page.evaluate(function() {
280 return $(".address:first").text();
281 });
282 if (actual != expected) {
283 console.log("Dogecoin address is incorrect");
284 console.log("Expected: " + expected);
285 console.log("Actual: " + actual);
286 fail();
287 }
288 next();
3eef9d0d 289 });
59193779
IC
290});
291},
292
88e2cdaa 293// Network can be set to shadowcash
59193779
IC
294function() {
295page.open(url, function(status) {
296 // set the phrase and coin
297 var expected = "SiSZtfYAXEFvMm3XM8hmtkGDyViRwErtCG";
298 page.evaluate(function() {
299 $(".phrase").val("abandon abandon ability");
300 $(".phrase").trigger("input");
301 $(".network option[selected]").removeAttr("selected");
302 $(".network option[value=4]").prop("selected", true);
303 $(".network").trigger("change");
304 });
305 // check the address is generated correctly
3eef9d0d 306 waitForGenerate(function() {
59193779
IC
307 var actual = page.evaluate(function() {
308 return $(".address:first").text();
309 });
310 if (actual != expected) {
311 console.log("Shadowcash address is incorrect");
312 console.log("Expected: " + expected);
313 console.log("Actual: " + actual);
314 fail();
315 }
316 next();
3eef9d0d 317 });
59193779
IC
318});
319},
320
88e2cdaa 321// Network can be set to shadowcash testnet
59193779
IC
322function() {
323page.open(url, function(status) {
324 // set the phrase and coin
325 var expected = "tM2EDpVKaTiEg2NZg3yKg8eqjLr55BErHe";
326 page.evaluate(function() {
327 $(".phrase").val("abandon abandon ability");
328 $(".phrase").trigger("input");
329 $(".network option[selected]").removeAttr("selected");
330 $(".network option[value=5]").prop("selected", true);
331 $(".network").trigger("change");
332 });
333 // check the address is generated correctly
3eef9d0d 334 waitForGenerate(function() {
59193779
IC
335 var actual = page.evaluate(function() {
336 return $(".address:first").text();
337 });
338 if (actual != expected) {
339 console.log("Shadowcash testnet address is incorrect");
340 console.log("Expected: " + expected);
341 console.log("Actual: " + actual);
342 fail();
343 }
344 next();
3eef9d0d 345 });
59193779
IC
346});
347},
348
88e2cdaa 349// Network can be set to viacoin
59193779
IC
350function() {
351page.open(url, function(status) {
352 // set the phrase and coin
353 var expected = "Vq9Eq4N5SQnjqZvxtxzo7hZPW5XnyJsmXT";
354 page.evaluate(function() {
355 $(".phrase").val("abandon abandon ability");
356 $(".phrase").trigger("input");
357 $(".network option[selected]").removeAttr("selected");
358 $(".network option[value=6]").prop("selected", true);
359 $(".network").trigger("change");
360 });
361 // check the address is generated correctly
3eef9d0d 362 waitForGenerate(function() {
59193779
IC
363 var actual = page.evaluate(function() {
364 return $(".address:first").text();
365 });
366 if (actual != expected) {
367 console.log("Viacoin address is incorrect");
368 console.log("Expected: " + expected);
369 console.log("Actual: " + actual);
370 fail();
371 }
372 next();
3eef9d0d 373 });
59193779
IC
374});
375},
376
88e2cdaa 377// Network can be set to viacoin testnet
59193779
IC
378function() {
379page.open(url, function(status) {
380 // set the phrase and coin
381 var expected = "tM2EDpVKaTiEg2NZg3yKg8eqjLr55BErHe";
382 page.evaluate(function() {
383 $(".phrase").val("abandon abandon ability");
384 $(".phrase").trigger("input");
385 $(".network option[selected]").removeAttr("selected");
386 $(".network option[value=7]").prop("selected", true);
387 $(".network").trigger("change");
388 });
389 // check the address is generated correctly
3eef9d0d 390 waitForGenerate(function() {
59193779
IC
391 var actual = page.evaluate(function() {
392 return $(".address:first").text();
393 });
394 if (actual != expected) {
395 console.log("Viacoin testnet address is incorrect");
396 console.log("Expected: " + expected);
397 console.log("Actual: " + actual);
398 fail();
399 }
400 next();
3eef9d0d 401 });
59193779
IC
402});
403},
404
88e2cdaa 405// Network can be set to jumbucks
59193779
IC
406function() {
407page.open(url, function(status) {
408 // set the phrase and coin
409 var expected = "JLEXccwDXADK4RxBPkRez7mqsHVoJBEUew";
410 page.evaluate(function() {
411 $(".phrase").val("abandon abandon ability");
412 $(".phrase").trigger("input");
413 $(".network option[selected]").removeAttr("selected");
414 $(".network option[value=8]").prop("selected", true);
415 $(".network").trigger("change");
416 });
417 // check the address is generated correctly
3eef9d0d 418 waitForGenerate(function() {
59193779
IC
419 var actual = page.evaluate(function() {
420 return $(".address:first").text();
421 });
422 if (actual != expected) {
423 console.log("Jumbucks address is incorrect");
424 console.log("Expected: " + expected);
425 console.log("Actual: " + actual);
426 fail();
427 }
428 next();
3eef9d0d 429 });
59193779
IC
430});
431},
432
88e2cdaa 433// Network can be set to clam
59193779
IC
434function() {
435page.open(url, function(status) {
436 // set the phrase and coin
437 var expected = "xCp4sakjVx4pUAZ6cBCtuin8Ddb6U1sk9y";
438 page.evaluate(function() {
439 $(".phrase").val("abandon abandon ability");
440 $(".phrase").trigger("input");
441 $(".network option[selected]").removeAttr("selected");
442 $(".network option[value=9]").prop("selected", true);
443 $(".network").trigger("change");
444 });
445 // check the address is generated correctly
3eef9d0d 446 waitForGenerate(function() {
59193779
IC
447 var actual = page.evaluate(function() {
448 return $(".address:first").text();
449 });
450 if (actual != expected) {
451 console.log("CLAM address is incorrect");
452 console.log("Expected: " + expected);
453 console.log("Actual: " + actual);
454 fail();
455 }
456 next();
3eef9d0d 457 });
59193779
IC
458});
459},
460
88e2cdaa 461// BIP39 seed is set from phrase
c196ad55
IC
462function() {
463page.open(url, function(status) {
464 // set the phrase
465 var expected = "20da140d3dd1df8713cefcc4d54ce0e445b4151027a1ab567b832f6da5fcc5afc1c3a3f199ab78b8e0ab4652efd7f414ac2c9a3b81bceb879a70f377aa0a58f3";
466 page.evaluate(function() {
467 $(".phrase").val("abandon abandon ability");
468 $(".phrase").trigger("input");
469 });
470 // check the address is generated correctly
3eef9d0d 471 waitForGenerate(function() {
c196ad55
IC
472 var actual = page.evaluate(function() {
473 return $(".seed").val();
474 });
475 if (actual != expected) {
476 console.log("BIP39 seed is incorrectly generated from mnemonic");
477 console.log("Expected: " + expected);
478 console.log("Actual: " + actual);
479 fail();
480 }
481 next();
3eef9d0d 482 });
c196ad55
IC
483});
484},
485
88e2cdaa 486// BIP32 root key is set from phrase
ec60b662
IC
487function() {
488page.open(url, function(status) {
489 // set the phrase
490 var expected = "xprv9s21ZrQH143K2jkGDCeTLgRewT9F2pH5JZs2zDmmjXes34geVnFiuNa8KTvY5WoYvdn4Ag6oYRoB6cXtc43NgJAEqDXf51xPm6fhiMCKwpi";
491 page.evaluate(function() {
492 $(".phrase").val("abandon abandon ability");
493 $(".phrase").trigger("input");
494 });
495 // check the address is generated correctly
3eef9d0d 496 waitForGenerate(function() {
ec60b662
IC
497 var actual = page.evaluate(function() {
498 return $(".root-key").val();
499 });
500 if (actual != expected) {
501 console.log("Root key is incorrectly generated from mnemonic");
502 console.log("Expected: " + expected);
503 console.log("Actual: " + actual);
504 fail();
505 }
506 next();
3eef9d0d 507 });
ec60b662
IC
508});
509},
510
88e2cdaa 511// Tabs show correct addresses when changed
cf7258fd
IC
512function() {
513page.open(url, function(status) {
514 // set the phrase
515 var expected = "17uQ7s2izWPwBmEVFikTmZUjbBKWYdJchz";
516 page.evaluate(function() {
517 $(".phrase").val("abandon abandon ability");
518 $(".phrase").trigger("input");
519 });
520 // change tabs
521 waitForGenerate(function() {
522 page.evaluate(function() {
523 $("#bip32-tab a").click();
524 });
525 // check the address is generated correctly
526 waitForGenerate(function() {
527 var actual = page.evaluate(function() {
528 return $(".address:first").text();
529 });
530 if (actual != expected) {
531 console.log("Clicking tab generates incorrect address");
532 console.log("Expected: " + expected);
533 console.log("Actual: " + actual);
534 fail();
535 }
536 next();
537 });
538 });
539});
540},
88e2cdaa
IC
541
542// BIP44 derivation path is shown
d077e1e7
IC
543function() {
544page.open(url, function(status) {
545 // set the phrase
546 var expected = "m/44'/0'/0'/0";
547 page.evaluate(function() {
548 $(".phrase").val("abandon abandon ability");
549 $(".phrase").trigger("input");
550 });
551 // check the derivation path of the first address
552 waitForGenerate(function() {
553 var actual = page.evaluate(function() {
554 return $("#bip44 .path").val();
555 });
556 if (actual != expected) {
557 console.log("BIP44 derivation path is incorrect");
558 console.log("Expected: " + expected);
559 console.log("Actual: " + actual);
560 fail();
561 }
562 next();
563 });
564});
565},
566
88e2cdaa 567// BIP44 extended private key is shown
4fd2925d
IC
568function() {
569page.open(url, function(status) {
570 // set the phrase
571 var expected = "xprvA2DxxvPZcyRvYgZMGS53nadR32mVDeCyqQYyFhrCVbJNjPoxMeVf7QT5g7mQASbTf9Kp4cryvcXnu2qurjWKcrdsr91jXymdCDNxKgLFKJG";
572 page.evaluate(function() {
573 $(".phrase").val("abandon abandon ability");
574 $(".phrase").trigger("input");
575 });
06286adb 576 // check the BIP44 extended private key
4fd2925d
IC
577 waitForGenerate(function() {
578 var actual = page.evaluate(function() {
579 return $(".extended-priv-key").val();
580 });
581 if (actual != expected) {
582 console.log("BIP44 extended private key is incorrect");
583 console.log("Expected: " + expected);
584 console.log("Actual: " + actual);
585 fail();
586 }
587 next();
588 });
589});
590},
591
88e2cdaa 592// BIP44 extended public key is shown
39fd45bb
IC
593function() {
594page.open(url, function(status) {
595 // set the phrase
596 var expected = "xpub6FDKNRvTTLzDmAdpNTc49ia9b4byd6vqCdUa46Fp3vqMcC96uBoufCmZXQLiN5AK3iSCJMhf9gT2sxkpyaPepRuA7W3MujV5tGmF5VfbueM";
597 page.evaluate(function() {
598 $(".phrase").val("abandon abandon ability");
599 $(".phrase").trigger("input");
600 });
06286adb 601 // check the BIP44 extended public key
39fd45bb
IC
602 waitForGenerate(function() {
603 var actual = page.evaluate(function() {
604 return $(".extended-pub-key").val();
605 });
606 if (actual != expected) {
607 console.log("BIP44 extended public key is incorrect");
608 console.log("Expected: " + expected);
609 console.log("Actual: " + actual);
610 fail();
611 }
612 next();
613 });
614});
615},
616
06286adb
IC
617// BIP44 purpose field changes address list
618function() {
619page.open(url, function(status) {
620 // set the phrase
621 var expected = "1JbDzRJ2cDT8aat2xwKd6Pb2zzavow5MhF";
622 page.evaluate(function() {
623 $(".phrase").val("abandon abandon ability");
624 $(".phrase").trigger("input");
625 });
626 waitForGenerate(function() {
627 // change the bip44 purpose field to 45
628 page.evaluate(function() {
629 $("#bip44 .purpose").val("45");
630 $("#bip44 .purpose").trigger("input");
631 });
632 waitForGenerate(function() {
633 // check the address for the new derivation path
634 var actual = page.evaluate(function() {
635 return $(".address:first").text();
636 });
637 if (actual != expected) {
638 console.log("BIP44 purpose field generates incorrect address");
639 console.log("Expected: " + expected);
640 console.log("Actual: " + actual);
641 fail();
642 }
643 next();
644 });
645 });
646});
647},
648
88e2cdaa 649// BIP44 coin field changes address list
9eb72cdf
IC
650function() {
651page.open(url, function(status) {
652 // set the phrase
653 var expected = "1F6dB2djQYrxoyfZZmfr6D5voH8GkJTghk";
654 page.evaluate(function() {
655 $(".phrase").val("abandon abandon ability");
656 $(".phrase").trigger("input");
657 });
658 waitForGenerate(function() {
659 // change the bip44 purpose field to 45
660 page.evaluate(function() {
661 $("#bip44 .coin").val("1");
662 $("#bip44 .coin").trigger("input");
663 });
664 waitForGenerate(function() {
665 // check the address for the new derivation path
666 var actual = page.evaluate(function() {
667 return $(".address:first").text();
668 });
669 if (actual != expected) {
670 console.log("BIP44 coin field generates incorrect address");
671 console.log("Expected: " + expected);
672 console.log("Actual: " + actual);
673 fail();
674 }
675 next();
676 });
677 });
678});
679},
680
88e2cdaa
IC
681// BIP44 account field changes address list
682// BIP44 external/internal field changes address list
683
684// BIP32 derivation path can be set
685// BIP32 can use hardened derivation paths
686// BIP32 extended private key is shown
687// BIP32 extended public key is shown
688
689// Derivation path is shown in table
690// Derivation path for address can be hardened
691// Derivation path visibility can be toggled
692// Address is shown
693// Addresses are shown in order of derivation path
694// Address visibility can be toggled
695// Private key is shown
696// Private key visibility can be toggled
697
698// More addresses can be generated
699// A custom number of additional addresses can be generated
700// Additional addresses are shown in order of derivation path
701
702// BIP32 root key can be set by the user
54563907
IC
703// Setting BIP32 root key clears the existing phrase, passphrase and seed
704// Clearing of phrase, passphrase and seed can be cancelled by user
88e2cdaa
IC
705// Custom BIP32 root key is used when changing the derivation path
706
707// Incorrect mnemonic shows error
708// Incorrect word shows suggested replacement
709// Incorrect BIP32 root key shows error
710// Derivation path not starting with m shows error
711// Derivation path containing invalid characters shows useful error
712
713// Github Issue 11: Default word length is 15
714// https://github.com/dcpos/bip39/issues/11
715
716// Github Issue 12: Generate more rows with private keys hidden
717// https://github.com/dcpos/bip39/issues/12
718
719// Github Issue 19: Mnemonic is not sensitive to whitespace
720// https://github.com/dcpos/bip39/issues/19
721
722// Github Issue 23: Use correct derivation path when changing tabs
723// https://github.com/dcpos/bip39/issues/23
724
725];
726
727console.log("Running tests...");
fb372687 728tests = shuffle(tests);
88e2cdaa 729next();