|
1 | 1 | import { HDPath } from '@interchainjs/types'; |
2 | 2 | import { BaseCryptoBytes } from '@interchainjs/utils'; |
3 | 3 | import { PrivateKey } from './private-key'; |
4 | | -import { createCosmosConfig, createEthereumConfig } from '../config/builders'; |
| 4 | +import { createCosmosConfig } from '../../../../networks/cosmos/src/auth/config'; |
| 5 | +import { createEthereumConfig } from '../../../../networks/ethereum/src/auth/config'; |
5 | 6 |
|
6 | 7 | describe('PrivateKey', () => { |
7 | 8 | const testMnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; |
8 | | - |
| 9 | + |
9 | 10 | describe('fromMnemonic', () => { |
10 | 11 | it('should create private keys from mnemonic for Cosmos', async () => { |
11 | 12 | const hdPaths = [ |
12 | 13 | HDPath.cosmos(0, 0, 0), |
13 | 14 | HDPath.cosmos(0, 0, 1), |
14 | 15 | ]; |
15 | | - |
| 16 | + |
16 | 17 | const config = createCosmosConfig().privateKeyConfig; |
17 | 18 | const privateKeys = await PrivateKey.fromMnemonic(testMnemonic, hdPaths, config); |
18 | | - |
| 19 | + |
19 | 20 | expect(privateKeys).toHaveLength(2); |
20 | 21 | expect(privateKeys[0].hdPath?.toString()).toBe('m/44\'/118\'/0\'/0/0'); |
21 | 22 | expect(privateKeys[1].hdPath?.toString()).toBe('m/44\'/118\'/0\'/0/1'); |
22 | 23 | }); |
23 | | - |
| 24 | + |
24 | 25 | it('should create private keys from mnemonic for Ethereum', async () => { |
25 | 26 | const hdPath = HDPath.eth(0, 0, 0); |
26 | 27 | const config = createEthereumConfig().privateKeyConfig; |
27 | 28 | const privateKeys = await PrivateKey.fromMnemonic(testMnemonic, [hdPath], config); |
28 | | - |
| 29 | + |
29 | 30 | expect(privateKeys).toHaveLength(1); |
30 | 31 | expect(privateKeys[0].hdPath?.toString()).toBe('m/44\'/60\'/0\'/0/0'); |
31 | 32 | }); |
32 | 33 | }); |
33 | | - |
| 34 | + |
34 | 35 | describe('fromHex', () => { |
35 | 36 | it('should create private key from hex string', () => { |
36 | 37 | const hexKey = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; |
37 | 38 | const config = createCosmosConfig().privateKeyConfig; |
38 | | - |
| 39 | + |
39 | 40 | const privateKey = PrivateKey.fromHex(hexKey, config); |
40 | | - |
| 41 | + |
41 | 42 | expect(privateKey.toHex()).toBe(hexKey); |
42 | 43 | expect(privateKey.config.algo).toBe('secp256k1'); |
43 | 44 | }); |
44 | 45 | }); |
45 | | - |
| 46 | + |
46 | 47 | describe('fromBytes', () => { |
47 | 48 | it('should create private key from bytes', () => { |
48 | 49 | const bytes = new Uint8Array(32).fill(42); |
49 | 50 | const cryptoBytes = BaseCryptoBytes.from(bytes); |
50 | 51 | const config = createCosmosConfig().privateKeyConfig; |
51 | | - |
| 52 | + |
52 | 53 | const privateKey = PrivateKey.fromBytes(cryptoBytes, config); |
53 | | - |
| 54 | + |
54 | 55 | expect(privateKey.value.value).toEqual(bytes); |
55 | 56 | expect(privateKey.config.algo).toBe('secp256k1'); |
56 | 57 | }); |
57 | 58 | }); |
58 | | - |
| 59 | + |
59 | 60 | describe('toPublicKey', () => { |
60 | 61 | it('should derive public key with default compression', async () => { |
61 | 62 | const hdPath = HDPath.cosmos(0, 0, 0); |
62 | 63 | const config = createCosmosConfig().privateKeyConfig; |
63 | 64 | const privateKeys = await PrivateKey.fromMnemonic(testMnemonic, [hdPath], config); |
64 | | - |
| 65 | + |
65 | 66 | const publicKey = privateKeys[0].toPublicKey(); |
66 | | - |
| 67 | + |
67 | 68 | expect(publicKey.compressed).toBe(true); |
68 | 69 | expect(publicKey.algo).toBe('secp256k1'); |
69 | 70 | }); |
70 | | - |
| 71 | + |
71 | 72 | it('should derive public key with custom compression', async () => { |
72 | 73 | const hdPath = HDPath.eth(0, 0, 0); |
73 | 74 | const config = createEthereumConfig().privateKeyConfig; |
74 | 75 | const privateKeys = await PrivateKey.fromMnemonic(testMnemonic, [hdPath], config); |
75 | | - |
| 76 | + |
76 | 77 | const publicKey = privateKeys[0].toPublicKey({ compressed: false }); |
77 | | - |
| 78 | + |
78 | 79 | expect(publicKey.compressed).toBe(false); |
79 | 80 | expect(publicKey.algo).toBe('secp256k1'); |
80 | 81 | }); |
81 | 82 | }); |
82 | | - |
| 83 | + |
83 | 84 | describe('sign', () => { |
84 | 85 | it('should sign data', async () => { |
85 | 86 | const hdPath = HDPath.cosmos(0, 0, 0); |
86 | 87 | const config = createCosmosConfig().privateKeyConfig; |
87 | 88 | const privateKeys = await PrivateKey.fromMnemonic(testMnemonic, [hdPath], config); |
88 | | - |
| 89 | + |
89 | 90 | const message = new TextEncoder().encode('Hello, InterchainJS!'); |
90 | 91 | const signature = await privateKeys[0].sign(message); |
91 | | - |
| 92 | + |
92 | 93 | expect(signature).toBeDefined(); |
93 | 94 | expect(signature.value).toBeInstanceOf(Uint8Array); |
94 | 95 | expect(signature.value.length).toBeGreaterThan(0); |
95 | 96 | }); |
96 | 97 | }); |
97 | | - |
| 98 | + |
98 | 99 | describe('serialization', () => { |
99 | 100 | it('should convert to hex', async () => { |
100 | 101 | const hdPath = HDPath.cosmos(0, 0, 0); |
101 | 102 | const config = createCosmosConfig().privateKeyConfig; |
102 | 103 | const privateKeys = await PrivateKey.fromMnemonic(testMnemonic, [hdPath], config); |
103 | | - |
| 104 | + |
104 | 105 | const hex = privateKeys[0].toHex(); |
105 | | - |
| 106 | + |
106 | 107 | expect(hex).toMatch(/^[0-9a-f]{64}$/); |
107 | 108 | }); |
108 | | - |
| 109 | + |
109 | 110 | it('should convert to base64', async () => { |
110 | 111 | const hdPath = HDPath.cosmos(0, 0, 0); |
111 | 112 | const config = createCosmosConfig().privateKeyConfig; |
112 | 113 | const privateKeys = await PrivateKey.fromMnemonic(testMnemonic, [hdPath], config); |
113 | | - |
| 114 | + |
114 | 115 | const base64 = privateKeys[0].toBase64(); |
115 | | - |
| 116 | + |
116 | 117 | expect(base64).toMatch(/^[A-Za-z0-9+/]+=*$/); |
117 | 118 | }); |
118 | 119 | }); |
|
0 commit comments