-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.js
More file actions
812 lines (708 loc) · 24.3 KB
/
Copy pathtest.js
File metadata and controls
812 lines (708 loc) · 24.3 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
import test from 'brittle'
import { createMnemonic, sign, getPublicKey, importMnemonic, readMnemonic, deleteMnemonic, importPrivateKey, readPrivateKey, deletePrivateKey, Signer, parseTonHdPath } from './src/index.js'
import { secp256k1 } from '@noble/curves/secp256k1.js'
import { ed25519 } from '@noble/curves/ed25519.js'
import { mnemonicNew, mnemonicValidate, mnemonicToPrivateKey, mnemonicToHDSeed, deriveEd25519Path } from 'ton-crypto'
const TEST_MNEMONIC = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'
const tonWords = await mnemonicNew(24)
const TON_MNEMONIC = tonWords.join(' ')
test('ton-crypto smoke test - mnemonicNew works', async (t) => {
const words = await mnemonicNew(24)
t.is(words.length, 24)
const valid = await mnemonicValidate(words)
t.ok(valid)
const keyPair = await mnemonicToPrivateKey(words)
t.is(keyPair.secretKey.length, 64)
t.is(keyPair.publicKey.length, 32)
const hdSeed = await mnemonicToHDSeed(words)
t.is(hdSeed.length, 64)
const derived = await deriveEd25519Path(hdSeed, [0, 1, 2])
t.is(derived.length, 32)
})
// Detect if secure storage (Keychain/KeyStore) is accessible in this environment
let storageAvailable = false
try {
const probeKey = new Uint8Array([1, 2, 3])
await importPrivateKey({ privateKey: probeKey, name: '_test_probe' })
await deletePrivateKey({ name: '_test_probe' })
storageAvailable = true
} catch {}
const storageTest = storageAvailable ? test : test.skip
test('createMnemonic - creates 12 word mnemonic', async (t) => {
const mnemonic = await createMnemonic({
wordCount: 12,
returnMnemonic: true,
storeMnemonic: false
})
const words = Buffer.from(mnemonic).toString('utf8').split(' ')
t.is(words.length, 12)
})
test('createMnemonic - creates 24 word mnemonic', async (t) => {
const mnemonic = await createMnemonic({
wordCount: 24,
returnMnemonic: true,
storeMnemonic: false
})
const words = Buffer.from(mnemonic).toString('utf8').split(' ')
t.is(words.length, 24)
})
test('sign with secp256k1', async (t) => {
const data = Buffer.from('test message to sign')
const sig = await sign({
mnemonic: TEST_MNEMONIC,
path: "m/44'/60'/0'/0/0",
curve: 'secp256k1',
data
})
t.ok(sig instanceof Uint8Array)
t.is(sig.length, 65)
})
test('getPublicKey with secp256k1', async (t) => {
const pubKey = await getPublicKey({
mnemonic: TEST_MNEMONIC,
path: "m/44'/60'/0'/0/0",
curve: 'secp256k1'
})
t.is(pubKey.length, 33)
})
test('sign with ed25519', async (t) => {
const data = Buffer.from('test message to sign')
const sig = await sign({
mnemonic: TEST_MNEMONIC,
path: "m/44'/60'/0'/0'/0'",
curve: 'ed25519',
data
})
t.is(sig.length, 64)
})
test('getPublicKey with ed25519', async (t) => {
const pubKey = await getPublicKey({
mnemonic: TEST_MNEMONIC,
path: "m/44'/60'/0'/0'/0'",
curve: 'ed25519'
})
t.is(pubKey.length, 32)
})
test('different paths produce different keys', async (t) => {
const pubKey1 = await getPublicKey({
mnemonic: TEST_MNEMONIC,
path: "m/44'/60'/0'/0/0",
curve: 'secp256k1'
})
const pubKey2 = await getPublicKey({
mnemonic: TEST_MNEMONIC,
path: "m/44'/60'/0'/0/1",
curve: 'secp256k1'
})
t.not(Buffer.from(pubKey1).toString('hex'), Buffer.from(pubKey2).toString('hex'))
})
test('password changes derived keys', async (t) => {
const pubKey1 = await getPublicKey({
mnemonic: TEST_MNEMONIC,
password: '',
path: "m/44'/60'/0'/0/0",
curve: 'secp256k1'
})
const pubKey2 = await getPublicKey({
mnemonic: TEST_MNEMONIC,
password: 'secret',
path: "m/44'/60'/0'/0/0",
curve: 'secp256k1'
})
t.not(Buffer.from(pubKey1).toString('hex'), Buffer.from(pubKey2).toString('hex'))
})
test('importPrivateKey - rejects non-Uint8Array', async (t) => {
await t.exception(async () => {
await importPrivateKey({ privateKey: 'string key' })
}, /must be a Uint8Array/)
await t.exception(async () => {
await importPrivateKey({ privateKey: null })
}, /must be a Uint8Array/)
})
test('importPrivateKey - rejects when no privateKey provided', async (t) => {
await t.exception(async () => {
await importPrivateKey({})
}, /must be a Uint8Array/)
})
test('importMnemonic - rejects non-Uint8Array', async (t) => {
await t.exception(async () => {
await importMnemonic({ mnemonic: 'string mnemonic' })
}, /must be a Uint8Array/)
await t.exception(async () => {
await importMnemonic({ mnemonic: null })
}, /must be a Uint8Array/)
})
test('importMnemonic - rejects invalid mnemonic when validate=true', async (t) => {
const invalidMnemonic = Buffer.from('invalid mnemonic words here', 'utf8')
await t.exception(async () => {
await importMnemonic({ mnemonic: invalidMnemonic, validate: true })
}, /invalid mnemonic/)
})
test('Signer - isUnlocked returns false initially', (t) => {
const s = new Signer()
t.is(s.isUnlocked(), false)
})
test('Signer - lock clears state', (t) => {
const s = new Signer()
s._secret = Buffer.from(TEST_MNEMONIC, 'utf8')
t.is(s.isUnlocked(), true)
s.lock()
t.is(s.isUnlocked(), false)
t.is(s._secret, null)
})
test('Signer - lock zeroes buffer before clearing', (t) => {
const s = new Signer()
const original = Buffer.from(TEST_MNEMONIC, 'utf8')
s._secret = original
s.lock()
const allZeros = original.every(byte => byte === 0)
t.is(allZeros, true)
})
test('Signer - auto-lock timer clears mnemonic', async (t) => {
const s = new Signer({ autoLockMs: 50 })
s._secret = Buffer.from(TEST_MNEMONIC, 'utf8')
s._resetAutoLockTimer()
t.is(s.isUnlocked(), true)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(s.isUnlocked(), false)
})
test('Signer - manual lock cancels auto-lock timer', (t) => {
const s = new Signer({ autoLockMs: 1000 })
s._secret = Buffer.from(TEST_MNEMONIC, 'utf8')
s._resetAutoLockTimer()
t.ok(s._autoLockTimer !== null)
s.lock()
t.is(s._autoLockTimer, null)
})
test('Signer - autoLockMs 0 disables auto-lock', (t) => {
const s = new Signer({ autoLockMs: 0 })
s._secret = Buffer.from(TEST_MNEMONIC, 'utf8')
s._resetAutoLockTimer()
t.is(s._autoLockTimer, null)
})
test('sign with seedDerivation ton', async (t) => {
const data = Buffer.from('test message to sign')
const sig = await sign({
mnemonic: TON_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton',
data
})
t.is(sig.length, 64)
})
test('getPublicKey with seedDerivation ton', async (t) => {
const pubKey = await getPublicKey({
mnemonic: TON_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton'
})
t.is(pubKey.length, 32)
})
test('seedDerivation ton sign/verify round-trip', async (t) => {
const data = Buffer.from('ton verify test')
const sig = await sign({
mnemonic: TON_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton',
data
})
const pubKey = await getPublicKey({
mnemonic: TON_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton'
})
const valid = ed25519.verify(sig, data, pubKey)
t.ok(valid)
})
test('seedDerivation ton matches ton-crypto mnemonicToPrivateKey', async (t) => {
const words = TON_MNEMONIC.split(/\s+/)
const keyPair = await mnemonicToPrivateKey(words)
const expectedPubKey = keyPair.publicKey
const pubKey = await getPublicKey({
mnemonic: TON_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton'
})
t.alike(pubKey, new Uint8Array(expectedPubKey))
})
test('seedDerivation ton produces different keys than bip39', async (t) => {
const tonKey = await getPublicKey({
mnemonic: TON_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton'
})
const bip39Key = await getPublicKey({
mnemonic: TON_MNEMONIC,
path: "m/44'/607'/0'",
curve: 'ed25519',
seedDerivation: 'bip39'
})
t.not(Buffer.from(tonKey).toString('hex'), Buffer.from(bip39Key).toString('hex'))
})
test('seedDerivation ton rejects secp256k1', async (t) => {
await t.exception(async () => {
await sign({
mnemonic: TEST_MNEMONIC,
curve: 'secp256k1',
seedDerivation: 'ton',
data: Buffer.from('test')
})
}, /TON derivation only supports ed25519/)
})
test('seedDerivation ton throws when path is provided', async (t) => {
await t.exception(async () => {
await sign({
mnemonic: TEST_MNEMONIC,
path: "m/44'/607'/0'",
curve: 'ed25519',
seedDerivation: 'ton',
data: Buffer.from('test')
})
}, /path is not supported for TON standard derivation/)
await t.exception(async () => {
await getPublicKey({
mnemonic: TEST_MNEMONIC,
path: "m/44'/607'/0'",
curve: 'ed25519',
seedDerivation: 'ton'
})
}, /path is not supported for TON standard derivation/)
})
// --- parseTonHdPath tests ---
test('parseTonHdPath - valid path', (t) => {
t.alike(parseTonHdPath("m/0'/1'/2'"), [0, 1, 2])
t.alike(parseTonHdPath("m/44'"), [44])
t.alike(parseTonHdPath("m/0'/0'/0'"), [0, 0, 0])
})
test('parseTonHdPath - missing m/ prefix', async (t) => {
await t.exception(() => parseTonHdPath("0'/1'/2'"), /must start with 'm\//)
await t.exception(() => parseTonHdPath(''), /must start with 'm\//)
await t.exception(() => parseTonHdPath(undefined), /must start with 'm\//)
})
test('parseTonHdPath - non-hardened index', async (t) => {
await t.exception(() => parseTonHdPath('m/0/1/2'), /must be hardened/)
await t.exception(() => parseTonHdPath("m/0'/1/2'"), /must be hardened/)
})
test('parseTonHdPath - empty segments', async (t) => {
await t.exception(() => parseTonHdPath('m/'), /at least one segment/)
})
test('parseTonHdPath - negative numbers', async (t) => {
await t.exception(() => parseTonHdPath("m/-1'"), /Invalid TON HD path index/)
})
test('parseTonHdPath - index at hardened offset boundary', async (t) => {
t.alike(parseTonHdPath("m/2147483647'"), [0x7FFFFFFF])
await t.exception(() => parseTonHdPath("m/2147483648'"), /Invalid TON HD path index/)
})
// --- ton-hd signing tests ---
test('sign with seedDerivation ton-hd', async (t) => {
const data = Buffer.from('test message to sign')
const sig = await sign({
mnemonic: TON_MNEMONIC,
path: "m/0'/1'/2'",
curve: 'ed25519',
seedDerivation: 'ton-hd',
data
})
t.is(sig.length, 64)
})
test('getPublicKey with seedDerivation ton-hd', async (t) => {
const pubKey = await getPublicKey({
mnemonic: TON_MNEMONIC,
path: "m/0'/1'/2'",
curve: 'ed25519',
seedDerivation: 'ton-hd'
})
t.is(pubKey.length, 32)
})
test('seedDerivation ton-hd sign/verify round-trip', async (t) => {
const data = Buffer.from('ton-hd verify test')
const sig = await sign({
mnemonic: TON_MNEMONIC,
path: "m/0'/1'/2'",
curve: 'ed25519',
seedDerivation: 'ton-hd',
data
})
const pubKey = await getPublicKey({
mnemonic: TON_MNEMONIC,
path: "m/0'/1'/2'",
curve: 'ed25519',
seedDerivation: 'ton-hd'
})
const valid = ed25519.verify(sig, data, pubKey)
t.ok(valid)
})
test('seedDerivation ton-hd different paths produce different keys', async (t) => {
const pubKey1 = await getPublicKey({
mnemonic: TON_MNEMONIC,
path: "m/0'/1'/2'",
curve: 'ed25519',
seedDerivation: 'ton-hd'
})
const pubKey2 = await getPublicKey({
mnemonic: TON_MNEMONIC,
path: "m/0'/1'/3'",
curve: 'ed25519',
seedDerivation: 'ton-hd'
})
t.not(Buffer.from(pubKey1).toString('hex'), Buffer.from(pubKey2).toString('hex'))
})
test('seedDerivation ton-hd throws when path is missing', async (t) => {
await t.exception(async () => {
await sign({
mnemonic: TEST_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton-hd',
data: Buffer.from('test')
})
}, /path is required for TON HD derivation/)
await t.exception(async () => {
await getPublicKey({
mnemonic: TEST_MNEMONIC,
curve: 'ed25519',
seedDerivation: 'ton-hd'
})
}, /path is required for TON HD derivation/)
})
test('seedDerivation ton-hd rejects non-hardened path segments', async (t) => {
await t.exception(async () => {
await sign({
mnemonic: TEST_MNEMONIC,
path: 'm/0/1/2',
curve: 'ed25519',
seedDerivation: 'ton-hd',
data: Buffer.from('test')
})
}, /must be hardened/)
})
test('seedDerivation ton-hd rejects secp256k1 curve', async (t) => {
await t.exception(async () => {
await sign({
mnemonic: TEST_MNEMONIC,
path: "m/0'/1'/2'",
curve: 'secp256k1',
seedDerivation: 'ton-hd',
data: Buffer.from('test')
})
}, /TON HD derivation only supports ed25519/)
await t.exception(async () => {
await getPublicKey({
mnemonic: TEST_MNEMONIC,
path: "m/0'/1'/2'",
curve: 'secp256k1',
seedDerivation: 'ton-hd'
})
}, /TON HD derivation only supports ed25519/)
})
// --- createMnemonic TON tests ---
test('createMnemonic with seedDerivation ton generates valid TON mnemonic', async (t) => {
const result = await createMnemonic({
wordCount: 24,
returnMnemonic: true,
storeMnemonic: false,
seedDerivation: 'ton'
})
const words = Buffer.from(result).toString('utf8').split(' ')
t.is(words.length, 24)
const valid = await mnemonicValidate(words)
t.ok(valid)
})
test('createMnemonic with password generates password-type TON mnemonic', async (t) => {
const result = await createMnemonic({
wordCount: 24,
returnMnemonic: true,
storeMnemonic: false,
seedDerivation: 'ton',
password: 'my-secret'
})
const words = Buffer.from(result).toString('utf8').split(' ')
t.is(words.length, 24)
const isBasic = await mnemonicValidate(words)
t.is(isBasic, false, 'password-protected mnemonic should not validate as basic')
const isValidWithPassword = await mnemonicValidate(words, 'my-secret')
t.ok(isValidWithPassword, 'validates with the same password used during creation')
})
test('importMnemonic with seedDerivation ton validates TON mnemonic', async (t) => {
const words = await mnemonicNew(24)
const valid = await mnemonicValidate(words)
t.ok(valid, 'generated mnemonic is valid per ton-crypto')
const mnemonicBytes = Buffer.from(words.join(' '), 'utf8')
try {
await importMnemonic({
mnemonic: mnemonicBytes,
seedDerivation: 'ton',
validate: true
})
} catch (e) {
// Validation errors must propagate; storage errors are OK (keychain may be unavailable)
if (/invalid mnemonic|must be/.test(e.message)) throw e
}
t.pass('importMnemonic did not reject valid TON mnemonic')
})
test('importMnemonic rejects invalid TON mnemonic', async (t) => {
const invalidBytes = Buffer.from('invalid words that are not a ton mnemonic at all here now', 'utf8')
await t.exception(async () => {
await importMnemonic({
mnemonic: invalidBytes,
seedDerivation: 'ton',
validate: true
})
}, /invalid mnemonic/)
})
test('importMnemonic with password validates password-type TON mnemonic', async (t) => {
const words = await mnemonicNew(24, 'test-pass')
const isBasic = await mnemonicValidate(words)
t.is(isBasic, false, 'password-type mnemonic should not validate as basic')
const mnemonicBytes = Buffer.from(words.join(' '), 'utf8')
try {
await importMnemonic({
mnemonic: mnemonicBytes,
seedDerivation: 'ton',
password: 'test-pass',
validate: true
})
} catch (e) {
if (/invalid mnemonic|must be/.test(e.message)) throw e
}
t.pass('importMnemonic did not reject password-type mnemonic')
})
test('importMnemonic rejects password-type TON mnemonic when no password provided', async (t) => {
const words = await mnemonicNew(24, 'x')
const mnemonicBytes = Buffer.from(words.join(' '), 'utf8')
await t.exception(async () => {
await importMnemonic({
mnemonic: mnemonicBytes,
seedDerivation: 'ton',
validate: true
})
}, /invalid mnemonic/)
})
test('importMnemonic rejects basic TON mnemonic when password provided', async (t) => {
const words = await mnemonicNew(24)
const mnemonicBytes = Buffer.from(words.join(' '), 'utf8')
await t.exception(async () => {
await importMnemonic({
mnemonic: mnemonicBytes,
seedDerivation: 'ton',
password: 'anything',
validate: true
})
}, /invalid mnemonic/)
})
test('createMnemonic rejects password with bip39 seedDerivation', async (t) => {
await t.exception(async () => {
await createMnemonic({
seedDerivation: 'bip39',
password: 'secret',
returnMnemonic: true,
storeMnemonic: false
})
}, /password is only supported with TON seed derivation/)
})
test('importMnemonic rejects password with bip39 seedDerivation', async (t) => {
const mnemonicBytes = Buffer.from(TEST_MNEMONIC, 'utf8')
await t.exception(async () => {
await importMnemonic({
mnemonic: mnemonicBytes,
seedDerivation: 'bip39',
password: 'secret',
validate: true
})
}, /password is only supported with TON seed derivation/)
})
test('sign with secretType privateKey - secp256k1', async (t) => {
const privateKey = secp256k1.utils.randomSecretKey()
const data = Buffer.from('test message to sign')
const sig = await sign({
mnemonic: privateKey,
curve: 'secp256k1',
secretType: 'privateKey',
data
})
t.ok(sig instanceof Uint8Array)
t.is(sig.length, 65)
})
test('sign with secretType privateKey - ed25519', async (t) => {
const privateKey = ed25519.utils.randomSecretKey()
const data = Buffer.from('test message to sign')
const sig = await sign({
mnemonic: privateKey,
curve: 'ed25519',
secretType: 'privateKey',
data
})
t.is(sig.length, 64)
})
test('getPublicKey with secretType privateKey - secp256k1', async (t) => {
const privateKey = secp256k1.utils.randomSecretKey()
const pubKey = await getPublicKey({
mnemonic: privateKey,
curve: 'secp256k1',
secretType: 'privateKey'
})
const expected = secp256k1.getPublicKey(privateKey, true)
t.alike(pubKey, expected)
})
test('getPublicKey with secretType privateKey - ed25519', async (t) => {
const privateKey = ed25519.utils.randomSecretKey()
const pubKey = await getPublicKey({
mnemonic: privateKey,
curve: 'ed25519',
secretType: 'privateKey'
})
const expected = ed25519.getPublicKey(privateKey)
t.alike(pubKey, expected)
})
test('sign with secretType privateKey verifies correctly - secp256k1', async (t) => {
const privateKey = secp256k1.utils.randomSecretKey()
const data = Buffer.from('verify this message')
const sig = await sign({
mnemonic: privateKey,
curve: 'secp256k1',
secretType: 'privateKey',
data
})
const pubKey = await getPublicKey({
mnemonic: privateKey,
curve: 'secp256k1',
secretType: 'privateKey'
})
// Recovery byte is first; compact r,s is bytes 1-64
// prehash: false must match what sign() uses
const valid = secp256k1.verify(sig.subarray(1, 65), data, pubKey, { prehash: false })
t.ok(valid)
})
test('sign with secretType privateKey verifies correctly - ed25519', async (t) => {
const privateKey = ed25519.utils.randomSecretKey()
const data = Buffer.from('verify this message')
const sig = await sign({
mnemonic: privateKey,
curve: 'ed25519',
secretType: 'privateKey',
data
})
const pubKey = await getPublicKey({
mnemonic: privateKey,
curve: 'ed25519',
secretType: 'privateKey'
})
const valid = ed25519.verify(sig, data, pubKey)
t.ok(valid)
})
test('Signer with secretType privateKey - sign and getPublicKey', async (t) => {
const privateKey = secp256k1.utils.randomSecretKey()
const s = new Signer({ secretType: 'privateKey' })
s._secret = Buffer.from(privateKey)
const data = Buffer.from('signer test message')
const sig = await s.sign({ curve: 'secp256k1', data })
t.ok(sig instanceof Uint8Array)
t.is(sig.length, 65)
const pubKey = await s.getPublicKey({ curve: 'secp256k1' })
const expected = secp256k1.getPublicKey(privateKey, true)
t.alike(pubKey, expected)
s.lock()
})
// --- Storage tests (skipped when Keychain/KeyStore is not accessible) ---
storageTest('importPrivateKey stores raw bytes, readPrivateKey retrieves them unchanged', async (t) => {
const name = 'test_pk_roundtrip'
const privateKey = secp256k1.utils.randomSecretKey()
await importPrivateKey({ privateKey, name })
const retrieved = await readPrivateKey({ name })
t.alike(retrieved, privateKey)
await deletePrivateKey({ name })
})
storageTest('binary round-trip: 32 bytes with embedded 0x00 survive storage', async (t) => {
const name = 'test_pk_nullbytes'
const privateKey = new Uint8Array(32)
privateKey[0] = 0x00
privateKey[1] = 0xff
privateKey[2] = 0x00
privateKey[3] = 0x00
privateKey[15] = 0xab
privateKey[16] = 0x00
privateKey[31] = 0x01
await importPrivateKey({ privateKey, name })
const retrieved = await readPrivateKey({ name })
t.is(retrieved.length, 32, 'retrieved length must match original')
t.alike(retrieved, privateKey)
await deletePrivateKey({ name })
})
storageTest('store two mnemonics under different names, read both back correctly', async (t) => {
const name1 = 'test_mn_one'
const name2 = 'test_mn_two'
const mn1 = Buffer.from(TEST_MNEMONIC, 'utf8')
const mn2Str = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
const mn2 = Buffer.from(mn2Str, 'utf8')
await importMnemonic({ mnemonic: mn1, name: name1, validate: true })
await importMnemonic({ mnemonic: mn2, name: name2, validate: true })
const read1 = await readMnemonic({ name: name1 })
const read2 = await readMnemonic({ name: name2 })
t.is(Buffer.from(read1).toString('utf8'), TEST_MNEMONIC)
t.is(Buffer.from(read2).toString('utf8'), mn2Str)
await deleteMnemonic({ name: name1 })
await deleteMnemonic({ name: name2 })
})
storageTest('deleteMnemonic({ name: x }) removes only that entry', async (t) => {
const nameA = 'test_del_a'
const nameB = 'test_del_b'
const mnA = Buffer.from(TEST_MNEMONIC, 'utf8')
const mnB = Buffer.from('zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong', 'utf8')
await importMnemonic({ mnemonic: mnA, name: nameA, validate: true })
await importMnemonic({ mnemonic: mnB, name: nameB, validate: true })
await deleteMnemonic({ name: nameA })
const readB = await readMnemonic({ name: nameB })
t.is(Buffer.from(readB).toString('utf8'), 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong')
await t.exception(async () => {
await readMnemonic({ name: nameA })
})
await deleteMnemonic({ name: nameB })
})
storageTest('createMnemonic with explicit name stores under that name', async (t) => {
const name = 'test_create_named'
await createMnemonic({ wordCount: 12, storeMnemonic: true, name })
const read = await readMnemonic({ name })
const words = Buffer.from(read).toString('utf8').split(' ')
t.is(words.length, 12)
await deleteMnemonic({ name })
})
storageTest('default names - createMnemonic uses mnemonic, importPrivateKey uses private_key', async (t) => {
await createMnemonic({ wordCount: 12, storeMnemonic: true })
const mn = await readMnemonic()
const words = Buffer.from(mn).toString('utf8').split(' ')
t.is(words.length, 12)
await deleteMnemonic()
const pk = secp256k1.utils.randomSecretKey()
await importPrivateKey({ privateKey: pk })
const readPk = await readPrivateKey()
t.alike(readPk, pk)
await deletePrivateKey()
})
storageTest('sign/getPublicKey with secretType privateKey reading from storage - secp256k1', async (t) => {
const name = 'test_stored_pk_secp'
const privateKey = secp256k1.utils.randomSecretKey()
await importPrivateKey({ privateKey, name })
const data = Buffer.from('test stored sign secp256k1')
const sig = await sign({ curve: 'secp256k1', secretType: 'privateKey', name, data })
t.ok(sig instanceof Uint8Array)
t.is(sig.length, 65)
const pubKey = await getPublicKey({ curve: 'secp256k1', secretType: 'privateKey', name })
const expected = secp256k1.getPublicKey(privateKey, true)
t.alike(pubKey, expected)
const valid = secp256k1.verify(sig.subarray(1, 65), data, pubKey, { prehash: false })
t.ok(valid)
await deletePrivateKey({ name })
})
storageTest('sign/getPublicKey with secretType privateKey reading from storage - ed25519', async (t) => {
const name = 'test_stored_pk_ed'
const privateKey = ed25519.utils.randomSecretKey()
await importPrivateKey({ privateKey, name })
const data = Buffer.from('test stored sign ed25519')
const sig = await sign({ curve: 'ed25519', secretType: 'privateKey', name, data })
t.is(sig.length, 64)
const pubKey = await getPublicKey({ curve: 'ed25519', secretType: 'privateKey', name })
const expected = ed25519.getPublicKey(privateKey)
t.alike(pubKey, expected)
const valid = ed25519.verify(sig, data, pubKey)
t.ok(valid)
await deletePrivateKey({ name })
})