-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathunit_tests_bip39_utils.html
More file actions
1597 lines (1402 loc) · 74.1 KB
/
unit_tests_bip39_utils.html
File metadata and controls
1597 lines (1402 loc) · 74.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BIP39 Utils - Interactive Test Suite</title>
<style>
* { box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: #1a1a2e;
color: #eee;
line-height: 1.6;
}
h1 { color: #00d4ff; margin-bottom: 5px; }
h2 { color: #00d4ff; border-bottom: 1px solid #333; padding-bottom: 10px; margin-top: 30px; }
h3 { color: #888; margin: 0 0 20px 0; font-weight: normal; }
.test-section {
background: #16213e;
border-radius: 8px;
padding: 20px;
margin: 15px 0;
}
.test-section h4 {
margin: 0 0 15px 0;
color: #fff;
font-size: 16px;
}
.test-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 10px;
align-items: center;
}
input, textarea, select {
background: #0f0f23;
border: 1px solid #333;
color: #fff;
padding: 10px;
border-radius: 4px;
font-family: monospace;
font-size: 14px;
}
input { flex: 1; min-width: 200px; }
textarea { width: 100%; min-height: 80px; resize: vertical; }
button {
background: #00d4ff;
color: #000;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
white-space: nowrap;
}
button:hover { background: #00a8cc; }
button.secondary {
background: #4a5568;
color: #fff;
}
button.secondary:hover { background: #5a6578; }
.output {
background: #0f0f23;
border: 1px solid #333;
border-radius: 4px;
padding: 15px;
margin-top: 10px;
font-family: monospace;
font-size: 13px;
word-break: break-all;
white-space: pre-wrap;
}
.success { color: #00ff88; }
.error { color: #ff4444; }
.info { color: #00d4ff; }
.warn { color: #ffaa00; }
#test-results {
background: #0f0f23;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
font-family: monospace;
font-size: 13px;
max-height: 400px;
overflow-y: auto;
}
.test-pass { color: #00ff88; }
.test-fail { color: #ff4444; }
.test-count {
font-size: 18px;
margin-bottom: 15px;
padding-bottom: 15px;
border-bottom: 1px solid #333;
}
a { color: #00d4ff; }
.note {
background: #1e3a5f;
border-left: 3px solid #00d4ff;
padding: 10px 15px;
margin: 10px 0;
font-size: 13px;
}
.warning {
background: #3f2e1e;
border-left: 3px solid #ffaa00;
padding: 10px 15px;
margin: 10px 0;
font-size: 13px;
}
.phrase-display {
background: #0f0f23;
border: 2px solid #00d4ff;
border-radius: 8px;
padding: 15px;
font-size: 16px;
letter-spacing: 1px;
text-align: center;
margin: 10px 0;
}
.word-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
margin: 10px 0;
}
.word-item {
background: #16213e;
padding: 8px;
border-radius: 4px;
text-align: center;
font-family: monospace;
}
.word-num {
color: #666;
font-size: 10px;
}
.derivation-path {
font-family: monospace;
background: #0f0f23;
padding: 4px 8px;
border-radius: 4px;
display: inline-block;
}
</style>
</head>
<body>
<h1>🔑 BIP39 Utils</h1>
<h3>Standalone BIP39/BIP32 HD Wallet Library v<span id="version"></span></h3>
<div class="note">
<strong>Dependencies:</strong>
<a href="https://github.com/bitrequest/bitrequest.github.io/blob/master/assets_js_lib_sjcl.js">sjcl.js</a> →
<a href="https://github.com/bitrequest/bitrequest.github.io/blob/master/assets_js_lib_crypto_utils.js">crypto_utils.js</a> →
<a href="https://github.com/bitrequest/bitrequest.github.io/blob/master/assets_js_lib_bip39_utils.js">bip39_utils.js</a><br>
<strong>Test phrase:</strong> <a href="https://github.com/bitcoinbook/bitcoinbook/blob/f8b883dcd4e3d1b9adf40fed59b7e898fbd9241f/ch05.asciidoc">army van defense carry jealous true garbage claim echo media make crunch</a><br>
<strong>Repository:</strong> <a href="https://github.com/bitrequest/bip39-utils-js">github.com/bitrequest/bip39-utils-js</a>
</div>
<div class="warning">
⚠️ <strong>Security Warning:</strong> Never enter real seed phrases on websites. This tool is for testing and educational purposes only.
</div>
<!-- Automated Tests -->
<h2>🧪 Automated Unit Tests</h2>
<button onclick="runAllTests()">Run All Tests</button>
<div id="test-results"></div>
<!-- Interactive Tools -->
<h2>🛠️ Interactive Tools</h2>
<!-- Generate Mnemonic -->
<div class="test-section">
<h4>Generate New Mnemonic Phrase</h4>
<div class="test-row">
<select id="word-count">
<option value="12" selected>12 words (128-bit)</option>
<option value="15">15 words (160-bit)</option>
<option value="18">18 words (192-bit)</option>
<option value="21">21 words (224-bit)</option>
<option value="24">24 words (256-bit)</option>
</select>
<button onclick="generateMnemonic()">Generate Mnemonic</button>
</div>
<div class="output" id="generate-output"></div>
</div>
<!-- Validate Mnemonic -->
<div class="test-section">
<h4>Validate Mnemonic Phrase</h4>
<div class="test-row">
<textarea id="validate-input" placeholder="Enter 12/15/18/21/24 word mnemonic phrase"></textarea>
</div>
<div class="test-row">
<button onclick="validateMnemonic()">Validate</button>
<button class="secondary" onclick="useTestPhrase()">Use Test Phrase</button>
</div>
<div class="output" id="validate-output"></div>
</div>
<!-- Mnemonic to Seed -->
<div class="test-section">
<h4>Mnemonic → Seed (PBKDF2)</h4>
<div class="test-row">
<textarea id="seed-mnemonic" placeholder="Enter mnemonic phrase"></textarea>
</div>
<div class="test-row">
<input type="text" id="seed-passphrase" placeholder="Optional BIP39 passphrase">
</div>
<div class="test-row">
<button onclick="mnemonicToSeed()">Generate Seed</button>
<button class="secondary" onclick="useTestPhraseForSeed()">Use Test Phrase</button>
</div>
<div class="output" id="seed-output"></div>
</div>
<!-- Seed to Root Key -->
<div class="test-section">
<h4>Seed → Master Root Key (HMAC-SHA512)</h4>
<div class="test-row">
<textarea id="rootkey-seed" placeholder="Enter 128-character hex seed"></textarea>
</div>
<div class="test-row">
<button onclick="seedToRootKey()">Get Root Key</button>
<button class="secondary" onclick="useTestSeed()">Use Test Seed</button>
</div>
<div class="output" id="rootkey-output"></div>
</div>
<!-- BIP32 Derivation -->
<div class="test-section">
<h4>BIP32 Key Derivation</h4>
<div class="test-row">
<input type="text" id="derive-key" placeholder="64-char hex private key (master key)">
</div>
<div class="test-row">
<input type="text" id="derive-chaincode" placeholder="64-char hex chain code">
</div>
<div class="test-row">
<input type="text" id="derive-path-base" value="m/84'/0'/0'/0/" readonly style="width: 200px; background: #1a1a2e; color: #888;">
<input type="number" id="derive-path-index" value="0" min="0" max="999" style="width: 70px;" onchange="deriveKeyIfReady()">
<select id="derive-coin" onchange="updateDerivePath()">
<option value="bitcoin-segwit" data-path="m/84'/0'/0'/0/">Bitcoin SegWit</option>
<option value="bitcoin" data-path="m/44'/0'/0'/0/">Bitcoin Legacy</option>
<option value="litecoin-segwit" data-path="m/84'/2'/0'/0/">Litecoin SegWit</option>
<option value="litecoin" data-path="m/44'/2'/0'/0/">Litecoin Legacy</option>
<option value="dogecoin" data-path="m/44'/3'/0'/0/">Dogecoin</option>
<option value="dash" data-path="m/44'/5'/0'/0/">Dash</option>
<option value="ethereum" data-path="m/44'/60'/0'/0/">Ethereum</option>
<option value="bitcoin-cash" data-path="m/44'/145'/0'/0/">Bitcoin Cash</option>
<option value="kaspa" data-path="m/44'/111111'/0'/0/">Kaspa</option>
</select>
</div>
<div class="test-row">
<button onclick="deriveKey()">Derive Key</button>
<button class="secondary" onclick="useTestRootKey()">Use Test Root Key</button>
</div>
<div class="output" id="derive-output"></div>
</div>
<!-- Batch Derivation -->
<div class="test-section">
<h4>Batch Address Derivation</h4>
<div class="test-row">
<textarea id="batch-mnemonic" placeholder="Enter mnemonic phrase"></textarea>
</div>
<div class="test-row">
<select id="batch-coin">
<option value="bitcoin-segwit">Bitcoin SegWit</option>
<option value="bitcoin">Bitcoin Legacy</option>
<option value="litecoin-segwit">Litecoin SegWit</option>
<option value="litecoin">Litecoin Legacy</option>
<option value="dogecoin">Dogecoin</option>
<option value="dash">Dash</option>
<option value="ethereum">Ethereum</option>
<option value="bitcoin-cash">Bitcoin Cash</option>
<option value="kaspa">Kaspa</option>
</select>
<input type="number" id="batch-count" value="5" min="1" max="20" style="width: 80px;">
<button onclick="batchDerive()">Derive Addresses</button>
</div>
<div class="test-row">
<button class="secondary" onclick="useTestPhraseForBatch()">Use Test Phrase</button>
</div>
<div class="output" id="batch-output"></div>
</div>
<!-- Generate xPubs -->
<div class="test-section">
<h4>Generate Extended Public Keys (xpub/zpub)</h4>
<div class="test-row">
<textarea id="xpub-gen-mnemonic" placeholder="Enter mnemonic phrase"></textarea>
</div>
<div class="test-row">
<button onclick="generateAllXpubs()">Generate xPubs</button>
<button class="secondary" onclick="useTestPhraseForXpubs()">Use Test Phrase</button>
</div>
<div class="output" id="xpub-gen-output"></div>
</div>
<!-- xPub Parsing -->
<div class="test-section">
<h4>Parse Extended Public Key (xpub/zpub)</h4>
<div class="test-row">
<textarea id="xpub-input" placeholder="Enter xpub, ypub, or zpub"></textarea>
</div>
<div class="test-row">
<button onclick="parseXpub()">Parse xPub</button>
<label style="margin-left: 10px; color: #888;">Index:</label>
<input type="number" id="xpub-index" value="0" min="0" max="999" style="width: 80px;">
</div>
<div class="output" id="xpub-output"></div>
</div>
<!-- Spark Keys Derivation -->
<div class="test-section">
<h4>⚡ Spark Keys Derivation (seed → derive_spark_keys)</h4>
<div class="note" style="margin-bottom: 12px;">
Derives 5 key pairs for the <a href="https://spark.info" target="_blank">Spark</a> protocol via BIP32 paths rooted at <span class="derivation-path">m/1073373'/{account}'</span>. Outputs identity, signing, deposit, static_deposit, and htlc keys plus the Spark address.
</div>
<div class="test-row">
<textarea id="spark-mnemonic" placeholder="Enter mnemonic phrase"></textarea>
</div>
<div class="test-row">
<label style="color: #888; white-space: nowrap;">Account #</label>
<input type="number" id="spark-account" value="1" min="0" max="999" style="width: 80px;">
<button onclick="deriveSparkKeys()">Derive Spark Keys</button>
<button class="secondary" onclick="useTestPhraseForSpark()">Use Test Phrase</button>
</div>
<div class="output" id="spark-output"></div>
</div>
<!-- Scripts -->
<script src="assets_js_lib_sjcl.js"></script>
<script src="assets_js_lib_crypto_utils.js"></script>
<script src="assets_js_lib_bip39_utils.js"></script>
<script>
// Display version
document.getElementById("version").textContent = Bip39Utils.version || "1.1.0";
// Shorthand reference to test constants from library
const TestVector = Bip39Utils.bip39_utils_test_vectors;
// Kaspa BIP32 configuration (for interactive tools)
const KASPA_CONFIG = {
"root_path": "m/44'/111111'/0'/0/",
"prefix": {
"pub": 0,
"pubx": 59716398, // 0x038f332e → kpub
"privx": 59715316 // 0x038f2ef4 → kprv
},
"pk_vbytes": {
"wif": 128
}
};
// Test framework
const tests = [];
let passed = 0, failed = 0;
function test(name, fn) {
tests.push({ name, fn });
}
function assertEqual(actual, expected, msg) {
if (actual !== expected) {
throw new Error(msg + " Expected: " + expected + " Got: " + actual);
}
}
function assertTrue(condition, msg) {
if (!condition) {
throw new Error(msg);
}
}
// Helper to log errors to console with full details
function logError(context, e) {
console.group("❌ " + context + " Error");
console.error("Message:", e.message || e);
console.error("Full error:", e);
if (e.stack) console.error("Stack:", e.stack);
console.groupEnd();
}
// ============================================================
// BUILT-IN LIBRARY TESTS
// ============================================================
test("Bip39Utils.test_seed: verify mnemonic → seed derivation", () => {
assertTrue(Bip39Utils.test_seed() === true, "seed derivation should work");
});
test("Bip39Utils.test_derivation: verify BIP44 address derivation", () => {
assertTrue(Bip39Utils.test_derivation() === true, "address derivation should work");
});
test("Bip39Utils.test_xpub_support: verify xpub derivation", () => {
assertTrue(Bip39Utils.test_xpub_support() === true, "xpub derivation should work");
});
test("Bip39Utils.test_bip39_compatibility: full compatibility check", () => {
const results = Bip39Utils.test_bip39_compatibility();
assertTrue(results.compatible === true, "BIP39 should be compatible");
assertTrue(results.crypto_api === true, "crypto API should be available");
assertTrue(results.bigint === true, "BigInt should be functional");
assertTrue(results.secp256k1 === true, "secp256k1 should work");
assertTrue(results.seed === true, "seed derivation should work");
assertTrue(results.derivation === true, "address derivation should work");
assertTrue(results.xpub === true, "xpub derivation should work");
});
// ============================================================
// TEST CONSTANTS VALIDATION
// ============================================================
test("bip39_utils_const: test_phrase produces expected_seed", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
assertEqual(seed, TestVector.expected_seed, "Should produce expected seed");
});
test("bip39_utils_const: expected_seed produces expected_address", () => {
const root_key = Bip39Utils.get_rootkey(TestVector.expected_seed);
const derive_params = {
"dpath": "m/44'/0'/0'/0/0",
"key": root_key.slice(0, 64),
"cc": root_key.slice(64)
};
const derived_keys = Bip39Utils.derive_x(derive_params);
const bip32_config = Bip39Utils.get_bip32_config("bitcoin");
const derived_address = Bip39Utils.format_keys(TestVector.expected_seed, derived_keys, bip32_config, 0, "bitcoin");
assertEqual(derived_address.address, TestVector.expected_address, "Should produce expected address");
});
test("bip39_utils_const: test_xpub produces expected_address", () => {
const xpub_data = Bip39Utils.key_cc_xpub(TestVector.test_xpub);
const derive_params = {
"dpath": "M/0/0",
"key": xpub_data.key,
"cc": xpub_data.cc,
"vb": xpub_data.version
};
const derived_keys = Bip39Utils.derive_x(derive_params);
const bip32_config = Bip39Utils.get_bip32_config("bitcoin");
const derived_address = Bip39Utils.format_keys(null, derived_keys, bip32_config, 0, "bitcoin");
assertEqual(derived_address.address, TestVector.expected_address, "Xpub should produce expected address");
});
// ============================================================
// UNIT TESTS
// ============================================================
// === Mnemonic Validation Tests ===
test("validate_mnemonic: valid test phrase", () => {
const result = Bip39Utils.validate_mnemonic(TestVector.test_phrase);
assertEqual(result, true, "Test phrase should be valid");
});
test("validate_mnemonic: invalid phrase (wrong word)", () => {
const invalid = "army van defense carry jealous true garbage claim echo media make xyz";
const result = Bip39Utils.validate_mnemonic(invalid);
assertEqual(result, false, "Invalid word should fail");
});
test("validate_mnemonic: invalid checksum", () => {
// Use a phrase with wrong checksum word (abandon x11 + abandon = invalid checksum)
const badChecksum = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon";
const result = Bip39Utils.validate_mnemonic(badChecksum);
assertEqual(result, false, "Bad checksum should fail");
});
test("validate_mnemonic: valid all-abandon phrase", () => {
// "abandon" x 11 + "about" is actually a valid BIP39 phrase (correct checksum)
const allAbandon = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const result = Bip39Utils.validate_mnemonic(allAbandon);
assertEqual(result, true, "This phrase has correct checksum");
});
test("validate_mnemonic: 24 word phrase", () => {
const phrase24 = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art";
const result = Bip39Utils.validate_mnemonic(phrase24);
assertTrue(typeof result === "boolean", "Should handle 24 words");
});
test("validate_mnemonic: word 'true' works correctly", () => {
// Test that the word "true" (which is in BIP39 wordlist) validates correctly
const result = Bip39Utils.validate_mnemonic(TestVector.test_phrase);
assertEqual(result, true, "Phrase with word 'true' should validate");
});
// === Mnemonic to Seed Tests ===
test("mnemonic_to_seed: test phrase", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
assertEqual(seed, TestVector.expected_seed, "Seed should match expected");
});
test("mnemonic_to_seed: output length (128 hex chars)", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
assertEqual(seed.length, 128, "Seed should be 64 bytes = 128 hex");
});
test("mnemonic_to_seed: deterministic", () => {
const seed1 = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const seed2 = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
assertEqual(seed1, seed2, "Same phrase should give same seed");
});
test("mnemonic_to_seed: with passphrase", () => {
const seedNoPass = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const seedWithPass = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase, "password");
assertTrue(seedNoPass !== seedWithPass, "Passphrase should change seed");
});
// === Root Key Tests ===
test("get_rootkey: output length (128 hex chars)", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const rootkey = Bip39Utils.get_rootkey(seed);
assertEqual(rootkey.length, 128, "Root key should be 128 hex chars");
});
test("get_rootkey: deterministic", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root1 = Bip39Utils.get_rootkey(seed);
const root2 = Bip39Utils.get_rootkey(seed);
assertEqual(root1, root2, "Should be deterministic");
});
test("get_rootkey: different seeds give different roots", () => {
const seed1 = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const seed2 = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase, "password");
const root1 = Bip39Utils.get_rootkey(seed1);
const root2 = Bip39Utils.get_rootkey(seed2);
assertTrue(root1 !== root2, "Different seeds give different roots");
});
// === Key Derivation Tests ===
test("derive_x: BIP44 path", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/0'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
assertTrue(derived.hasOwnProperty("key"), "Should have key");
assertEqual(derived.key.length, 64, "Key should be 32 bytes");
});
test("derive_x: BIP84 path", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/84'/0'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
assertTrue(derived.hasOwnProperty("key"), "Should have key");
assertEqual(derived.purpose, "84'", "Should track purpose");
});
test("derive_x: different indices give different keys", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const key0 = Bip39Utils.derive_x({
dpath: "m/84'/0'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const key1 = Bip39Utils.derive_x({
dpath: "m/84'/0'/0'/0/1",
key: root.slice(0, 64),
cc: root.slice(64)
});
assertTrue(key0.key !== key1.key, "Different indices give different keys");
});
test("derive_child_key: single derivation step", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const child = Bip39Utils.derive_child_key(
root.slice(0, 64),
root.slice(64),
"80000000", // hardened 0
false,
true
);
assertTrue(child.hasOwnProperty("key"), "Should have key");
assertTrue(child.hasOwnProperty("chaincode"), "Should have chain code");
});
// === Address Generation Tests ===
test("format_keys: Bitcoin legacy address", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/0'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const formatted = Bip39Utils.format_keys(seed, derived, Bip39Utils.get_bip32_config("bitcoin"), 0, "bitcoin");
assertTrue(formatted.address.startsWith("1"), "Legacy address starts with 1");
});
test("format_keys: Bitcoin SegWit address (BIP84)", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/84'/0'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const formatted = Bip39Utils.format_keys(seed, derived, Bip39Utils.get_bip32_config("bitcoin"), 0, "bitcoin");
assertTrue(formatted.address.startsWith("bc1q"), "SegWit address starts with bc1q");
});
test("format_keys: Ethereum address", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/60'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const formatted = Bip39Utils.format_keys(seed, derived, Bip39Utils.get_bip32_config("ethereum"), 0, "ethereum");
assertTrue(formatted.address.startsWith("0x"), "Ethereum address starts with 0x");
assertEqual(formatted.address.length, 42, "Ethereum address is 42 chars");
});
test("format_keys: Litecoin SegWit address", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/84'/2'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const formatted = Bip39Utils.format_keys(seed, derived, Bip39Utils.get_bip32_config("litecoin"), 0, "litecoin");
assertTrue(formatted.address.startsWith("ltc1q"), "Litecoin SegWit starts with ltc1q");
});
test("format_keys: Dogecoin address", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/3'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const formatted = Bip39Utils.format_keys(seed, derived, Bip39Utils.get_bip32_config("dogecoin"), 0, "dogecoin");
assertTrue(formatted.address.startsWith("D"), "Dogecoin address starts with D");
});
// === Kaspa Address Derivation Tests ===
test("Kaspa address derivation: valid kaspa: address format", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/111111'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const pubkey = CryptoUtils.get_publickey(derived.key);
const address = CryptoUtils.pub_to_kaspa_address(pubkey);
assertTrue(address.startsWith("kaspa:q"), "Kaspa address starts with kaspa:q");
assertEqual(address.length, 67, "Kaspa address is 67 chars (kaspa: + 61 bech32)");
});
test("Kaspa address derivation: multiple indices produce different addresses", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived0 = Bip39Utils.derive_x({
dpath: "m/44'/111111'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const derived1 = Bip39Utils.derive_x({
dpath: "m/44'/111111'/0'/0/1",
key: root.slice(0, 64),
cc: root.slice(64)
});
const addr0 = CryptoUtils.pub_to_kaspa_address(CryptoUtils.get_publickey(derived0.key));
const addr1 = CryptoUtils.pub_to_kaspa_address(CryptoUtils.get_publickey(derived1.key));
assertTrue(addr0 !== addr1, "Different indices produce different addresses");
});
test("Kaspa kpub generation: produces valid kpub", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/111111'/0'",
key: root.slice(0, 64),
cc: root.slice(64)
});
const ext_keys_result = Bip39Utils.ext_keys(derived, KASPA_CONFIG);
assertTrue(ext_keys_result.xpub.startsWith("kpub"), "Kaspa xpub starts with kpub");
});
test("Kaspa kpub parsing: can parse kpub and derive addresses", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/111111'/0'",
key: root.slice(0, 64),
cc: root.slice(64)
});
const ext_keys_result = Bip39Utils.ext_keys(derived, KASPA_CONFIG);
const kpub = ext_keys_result.xpub;
// Parse kpub and derive address
const parsed = Bip39Utils.key_cc_xpub(kpub);
const child_derived = Bip39Utils.derive_x({
dpath: "M/0/0",
key: parsed.key,
cc: parsed.cc
});
const address = CryptoUtils.pub_to_kaspa_address(child_derived.key);
assertTrue(address.startsWith("kaspa:q"), "Derived address from kpub is valid");
});
test("format_keys: expected Bitcoin address from test phrase", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/44'/0'/0'/0/0",
key: root.slice(0, 64),
cc: root.slice(64)
});
const formatted = Bip39Utils.format_keys(seed, derived, Bip39Utils.get_bip32_config("bitcoin"), 0, "bitcoin");
assertEqual(formatted.address, TestVector.expected_address, "Should match expected address");
});
// === xPub Tests ===
test("key_cc_xpub: parse xpub structure", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/84'/0'/0'",
key: root.slice(0, 64),
cc: root.slice(64)
});
const extended = Bip39Utils.ext_keys(derived, Bip39Utils.get_bip32_config("bitcoin"));
// ext_keys returns ext_pub for the public extended key
const parsed = Bip39Utils.key_cc_xpub(extended.ext_pub);
assertTrue(parsed.hasOwnProperty("key"), "Should have key");
assertTrue(parsed.hasOwnProperty("cc"), "Should have chain code");
});
test("ext_keys: generate extended keys", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/84'/0'/0'",
key: root.slice(0, 64),
cc: root.slice(64)
});
const config = Bip39Utils.get_bip32_config("bitcoin");
const extended = Bip39Utils.ext_keys(derived, config);
// ext_keys returns object with ext_key (always) and ext_pub (when xpub is false)
assertTrue(typeof extended === "object", "Should return object");
assertTrue(Object.keys(extended).length > 0, "Should have at least one key");
});
test("xpub_obj: create xpub object", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
// xpub_obj signature: (coin, root_path, chain_code, key)
const xpubObj = Bip39Utils.xpub_obj("bitcoin", "m/84'/0'/0'/0", root.slice(64), root.slice(0, 64));
assertTrue(typeof xpubObj === "object", "Should return object");
assertTrue(xpubObj.hasOwnProperty("xpub"), "Should have xpub property");
});
// === Mnemonic Generation Tests ===
test("generate_mnemonic: 12 words", () => {
const mnemonic = Bip39Utils.generate_mnemonic(12);
const words = mnemonic.split(" ");
assertEqual(words.length, 12, "Should be 12 words");
});
test("generate_mnemonic: valid checksum", () => {
const mnemonic = Bip39Utils.generate_mnemonic(12);
const isValid = Bip39Utils.validate_mnemonic(mnemonic);
assertEqual(isValid, true, "Generated mnemonic should be valid");
});
test("generate_mnemonic: 24 words valid", () => {
const mnemonic = Bip39Utils.generate_mnemonic(24);
const words = mnemonic.split(" ");
assertEqual(words.length, 24, "Should be 24 words");
const isValid = Bip39Utils.validate_mnemonic(mnemonic);
assertEqual(isValid, true, "Should be valid");
});
test("generate_mnemonic: random each time", () => {
const m1 = Bip39Utils.generate_mnemonic(12);
const m2 = Bip39Utils.generate_mnemonic(12);
assertTrue(m1 !== m2, "Should be random");
});
// === find_invalid_word Tests ===
test("find_invalid_word: valid phrase returns undefined", () => {
// find_invalid_word expects an array of words
const words = TestVector.test_phrase.split(" ");
const result = Bip39Utils.find_invalid_word(words);
assertEqual(result, undefined, "Valid phrase should return undefined");
});
test("find_invalid_word: detects invalid word", () => {
const invalid = "army van defense carry jealous true garbage claim echo media make xyz123".split(" ");
const result = Bip39Utils.find_invalid_word(invalid);
assertEqual(result, "xyz123", "Should return the invalid word");
});
// === BIP32 Configuration Tests ===
test("get_bip32_config: Bitcoin config", () => {
const config = Bip39Utils.get_bip32_config("bitcoin");
assertTrue(config.hasOwnProperty("root_path"), "Should have root_path");
assertTrue(config.hasOwnProperty("prefix"), "Should have prefix");
});
test("get_bip32_config: Ethereum config", () => {
const config = Bip39Utils.get_bip32_config("ethereum");
assertTrue(config !== false, "Should return config for Ethereum");
});
test("get_bip32_config: unknown coin returns false", () => {
const config = Bip39Utils.get_bip32_config("unknowncoin");
assertEqual(config, false, "Unknown coin should return false");
});
test("bip32_configs: has expected coins", () => {
const configs = Bip39Utils.bip32_configs;
assertTrue(configs.hasOwnProperty("bitcoin"), "Has bitcoin");
assertTrue(configs.hasOwnProperty("litecoin"), "Has litecoin");
assertTrue(configs.hasOwnProperty("ethereum"), "Has ethereum");
});
// === Utility Tests ===
test("shuffle_array: returns same length", () => {
const original = [1, 2, 3, 4, 5];
const shuffled = Bip39Utils.shuffle_array([...original]);
assertEqual(shuffled.length, original.length, "Same length");
});
test("shuffle_array: contains same elements", () => {
const original = [1, 2, 3, 4, 5];
const shuffled = Bip39Utils.shuffle_array([...original]);
const sortedOriginal = [...original].sort();
const sortedShuffled = [...shuffled].sort();
assertEqual(JSON.stringify(sortedOriginal), JSON.stringify(sortedShuffled), "Same elements");
});
test("parse_seed: prepares mnemonic and passphrase for PBKDF2", () => {
// parse_seed takes mnemonic and optional passphrase, returns bit arrays for PBKDF2
const parsed = Bip39Utils.parse_seed(TestVector.test_phrase, "");
assertTrue(parsed.hasOwnProperty("mnemonic"), "Should have mnemonic bits");
assertTrue(parsed.hasOwnProperty("passphrase"), "Should have passphrase bits (salted)");
});
test("objectify_extended: parse extended key", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const derived = Bip39Utils.derive_x({
dpath: "m/84'/0'/0'",
key: root.slice(0, 64),
cc: root.slice(64)
});
const extended = Bip39Utils.ext_keys(derived, Bip39Utils.get_bip32_config("bitcoin"));
// objectify_extended takes a Base58Check decoded hex string, not an encoded xpub
const decoded = CryptoUtils.b58check_decode(extended.ext_key);
const obj = Bip39Utils.objectify_extended(decoded);
assertTrue(typeof obj === "object", "Should return object");
});
// === keypair_array Tests ===
test("keypair_array: derive multiple addresses", () => {
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
const root = Bip39Utils.get_rootkey(seed);
const config = Bip39Utils.get_bip32_config("bitcoin");
const pairs = Bip39Utils.keypair_array(
seed,
new Array(5),
0,
"m/84'/0'/0'/0/",
config,
root.slice(0, 64),
root.slice(64),
"bitcoin",
null
);
assertEqual(pairs.length, 5, "Should return 5 addresses");
assertTrue(pairs[0].address.startsWith("bc1q"), "First address is SegWit");
});
// === Test Constants ===
test("test_phrase: is defined", () => {
assertTrue(typeof TestVector.test_phrase === "string", "test_phrase should be string");
assertTrue(TestVector.test_phrase.length > 0, "test_phrase should not be empty");
});
test("expected_seed: is defined", () => {
assertTrue(typeof TestVector.expected_seed === "string", "expected_seed should be string");
assertEqual(TestVector.expected_seed.length, 128, "expected_seed should be 128 chars");
});
test("expected_address: is defined", () => {
assertTrue(typeof TestVector.expected_address === "string", "expected_address should be string");
assertTrue(TestVector.expected_address.startsWith("1"), "expected_address should start with 1");
});
// === Integration Test ===
test("full derivation flow: phrase → seed → address", () => {
// This test verifies the complete derivation chain
const seed = Bip39Utils.mnemonic_to_seed(TestVector.test_phrase);
assertEqual(seed, TestVector.expected_seed, "Seed should match");
const root_key = Bip39Utils.get_rootkey(seed);
const derive_params = {
"dpath": "m/44'/0'/0'/0/0",
"key": root_key.slice(0, 64),
"cc": root_key.slice(64)
};
const derived_keys = Bip39Utils.derive_x(derive_params);
const bip32_config = Bip39Utils.get_bip32_config("bitcoin");
const result = Bip39Utils.format_keys(seed, derived_keys, bip32_config, 0, "bitcoin");
assertEqual(result.address, TestVector.expected_address, "Address should match expected");
});
// === Spark Derivation Tests ===
test("test_spark_derivation: identity pubkey and address match", () => {
assertTrue(Bip39Utils.test_spark_derivation() === true, "Spark derivation should work");
});
test("derive_spark_keys: returns all 5 key types", () => {
const keys = Bip39Utils.derive_spark_keys(TestVector.expected_seed, 1);
const expected_types = ["identity", "signing", "deposit", "static_deposit", "htlc"];
for (const type of expected_types) {
assertTrue(keys.hasOwnProperty(type), "Should have " + type + " key");
assertTrue(keys[type].hasOwnProperty("privkey"), type + " should have privkey");
assertTrue(keys[type].hasOwnProperty("pubkey"), type + " should have pubkey");
}
});
test("derive_spark_keys: identity pubkey matches expected", () => {
const keys = Bip39Utils.derive_spark_keys(TestVector.expected_seed, 1);
const expected_pubkey = "02a8d8b3ffb8096c83c33ab79dc9efc6c351691f9b793f5af8b5fd5c13a9c3495a";
assertEqual(keys.identity.pubkey, expected_pubkey, "Identity pubkey should match");
});
test("derive_spark_keys: all privkeys are 64-char hex", () => {
const keys = Bip39Utils.derive_spark_keys(TestVector.expected_seed, 1);
const key_types = ["identity", "signing", "deposit", "static_deposit", "htlc"];
for (const type of key_types) {
assertEqual(keys[type].privkey.length, 64, type + " privkey should be 64 chars");
assertTrue(/^[0-9a-f]+$/.test(keys[type].privkey), type + " privkey should be hex");
}
});
test("derive_spark_keys: all pubkeys are compressed (02/03 prefix, 66 chars)", () => {
const keys = Bip39Utils.derive_spark_keys(TestVector.expected_seed, 1);
const key_types = ["identity", "signing", "deposit", "static_deposit", "htlc"];
for (const type of key_types) {
assertEqual(keys[type].pubkey.length, 66, type + " pubkey should be 66 chars");
assertTrue(keys[type].pubkey.startsWith("02") || keys[type].pubkey.startsWith("03"), type + " pubkey should be compressed");
}
});
test("encode_spark_address: produces correct mainnet address", () => {
const keys = Bip39Utils.derive_spark_keys(TestVector.expected_seed, 1);
const address = Bip39Utils.encode_spark_address(keys.identity.pubkey, "mainnet");
const expected = "spark1pgss92xck0lmsztvs0pn4duae8huds63dy0ek7flttuttl2uzw5uxj26ep0pvs";
assertEqual(address, expected, "Spark address should match expected");
});
test("encode_spark_address: mainnet address starts with 'spark1'", () => {
const keys = Bip39Utils.derive_spark_keys(TestVector.expected_seed, 1);
const address = Bip39Utils.encode_spark_address(keys.identity.pubkey, "mainnet");
assertTrue(address.startsWith("spark1"), "Mainnet Spark address should start with spark1");
});
test("seed_to_spark_address: convenience function matches manual derivation", () => {
const direct = Bip39Utils.seed_to_spark_address(TestVector.expected_seed, 1);
const manual_keys = Bip39Utils.derive_spark_keys(TestVector.expected_seed, 1);