@@ -28,8 +28,8 @@ describe('BN254 Keystore', () => {
2828 } ) ;
2929
3030 describe ( 'createBn254Keystore' , ( ) => {
31- it ( 'creates a valid BN254 keystore structure' , ( ) => {
32- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
31+ it ( 'creates a valid BN254 keystore structure' , async ( ) => {
32+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
3333
3434 expect ( keystore . version ) . toBe ( 4 ) ;
3535 expect ( keystore . path ) . toBe ( testPath ) ;
@@ -50,8 +50,8 @@ describe('BN254 Keystore', () => {
5050 expect ( keystore . crypto . checksum . message ) . toMatch ( / ^ [ 0 - 9 a - f ] { 64 } $ / ) ;
5151 } ) ;
5252
53- it ( 'encrypts the private key so it can be decrypted' , ( ) => {
54- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
53+ it ( 'encrypts the private key so it can be decrypted' , async ( ) => {
54+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
5555
5656 // Derive the decryption key using the same KDF
5757 const salt = Buffer . from ( keystore . crypto . kdf . params . salt , 'hex' ) ;
@@ -74,87 +74,87 @@ describe('BN254 Keystore', () => {
7474 expect ( '0x' + decrypted . toString ( 'hex' ) ) . toBe ( testPrivateKey ) ;
7575 } ) ;
7676
77- it ( 'produces different ciphertexts for the same key with different passwords' , ( ) => {
78- const keystore1 = createBn254Keystore ( 'password1' , testPrivateKey , testPublicKey , testPath ) ;
79- const keystore2 = createBn254Keystore ( 'password2' , testPrivateKey , testPublicKey , testPath ) ;
77+ it ( 'produces different ciphertexts for the same key with different passwords' , async ( ) => {
78+ const keystore1 = await createBn254Keystore ( 'password1' , testPrivateKey , testPublicKey , testPath ) ;
79+ const keystore2 = await createBn254Keystore ( 'password2' , testPrivateKey , testPublicKey , testPath ) ;
8080
8181 expect ( keystore1 . crypto . cipher . message ) . not . toBe ( keystore2 . crypto . cipher . message ) ;
8282 expect ( keystore1 . crypto . checksum . message ) . not . toBe ( keystore2 . crypto . checksum . message ) ;
8383 } ) ;
8484
85- it ( 'produces different ciphertexts on each call due to random salt and IV' , ( ) => {
86- const keystore1 = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
87- const keystore2 = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
85+ it ( 'produces different ciphertexts on each call due to random salt and IV' , async ( ) => {
86+ const keystore1 = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
87+ const keystore2 = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
8888
8989 expect ( keystore1 . crypto . kdf . params . salt ) . not . toBe ( keystore2 . crypto . kdf . params . salt ) ;
9090 expect ( keystore1 . crypto . cipher . params . iv ) . not . toBe ( keystore2 . crypto . cipher . params . iv ) ;
9191 expect ( keystore1 . crypto . cipher . message ) . not . toBe ( keystore2 . crypto . cipher . message ) ;
9292 } ) ;
9393
94- it ( 'accepts private keys with or without 0x prefix' , ( ) => {
95- const withPrefix = createBn254Keystore ( testPassword , '0x' + '42' . repeat ( 32 ) , testPublicKey , testPath ) ;
96- const withoutPrefix = createBn254Keystore ( testPassword , '42' . repeat ( 32 ) , testPublicKey , testPath ) ;
94+ it ( 'accepts private keys with or without 0x prefix' , async ( ) => {
95+ const withPrefix = await createBn254Keystore ( testPassword , '0x' + '42' . repeat ( 32 ) , testPublicKey , testPath ) ;
96+ const withoutPrefix = await createBn254Keystore ( testPassword , '42' . repeat ( 32 ) , testPublicKey , testPath ) ;
9797
9898 // Both should produce valid keystores with same length ciphertext
9999 expect ( withPrefix . crypto . cipher . message . length ) . toBe ( 64 ) ;
100100 expect ( withoutPrefix . crypto . cipher . message . length ) . toBe ( 64 ) ;
101101 } ) ;
102102
103- it ( 'stores public key without 0x prefix in description field' , ( ) => {
103+ it ( 'stores public key without 0x prefix in description field' , async ( ) => {
104104 const pubkeyWithPrefix = '0x' + 'ab' . repeat ( 33 ) ;
105- const keystore = createBn254Keystore ( testPassword , testPrivateKey , pubkeyWithPrefix , testPath ) ;
105+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , pubkeyWithPrefix , testPath ) ;
106106
107107 expect ( keystore . description ) . toBe ( 'ab' . repeat ( 33 ) ) ;
108108 expect ( keystore . pubkey ) . toBe ( pubkeyWithPrefix ) ;
109109 } ) ;
110110
111- it ( 'throws error for invalid private key length' , ( ) => {
111+ it ( 'throws error for invalid private key length' , async ( ) => {
112112 const shortKey = '0x1234' ;
113- expect ( ( ) => createBn254Keystore ( testPassword , shortKey , testPublicKey , testPath ) ) . toThrow (
113+ await expect ( createBn254Keystore ( testPassword , shortKey , testPublicKey , testPath ) ) . rejects . toThrow (
114114 'BLS private key must be 32-byte hex' ,
115115 ) ;
116116
117117 const longKey = '0x' + '42' . repeat ( 33 ) ;
118- expect ( ( ) => createBn254Keystore ( testPassword , longKey , testPublicKey , testPath ) ) . toThrow (
118+ await expect ( createBn254Keystore ( testPassword , longKey , testPublicKey , testPath ) ) . rejects . toThrow (
119119 'BLS private key must be 32-byte hex' ,
120120 ) ;
121121 } ) ;
122122
123- it ( 'throws error for non-hex private key' , ( ) => {
123+ it ( 'throws error for non-hex private key' , async ( ) => {
124124 const invalidKey = '0xGGGG' + '42' . repeat ( 30 ) ;
125- expect ( ( ) => createBn254Keystore ( testPassword , invalidKey , testPublicKey , testPath ) ) . toThrow (
125+ await expect ( createBn254Keystore ( testPassword , invalidKey , testPublicKey , testPath ) ) . rejects . toThrow (
126126 'BLS private key must be 32-byte hex' ,
127127 ) ;
128128 } ) ;
129129
130- it ( 'normalizes password using NFKD' , ( ) => {
130+ it ( 'normalizes password using NFKD' , async ( ) => {
131131 // Password with combining characters
132132 const password = 'café' ; // é can be represented as single char or e + combining accent
133- const keystore = createBn254Keystore ( password , testPrivateKey , testPublicKey , testPath ) ;
133+ const keystore = await createBn254Keystore ( password , testPrivateKey , testPublicKey , testPath ) ;
134134
135135 // Should successfully create keystore (normalization happens internally)
136136 expect ( keystore ) . toBeDefined ( ) ;
137137 expect ( keystore . version ) . toBe ( 4 ) ;
138138 } ) ;
139139
140- it ( 'handles empty password' , ( ) => {
141- const keystore = createBn254Keystore ( '' , testPrivateKey , testPublicKey , testPath ) ;
140+ it ( 'handles empty password' , async ( ) => {
141+ const keystore = await createBn254Keystore ( '' , testPrivateKey , testPublicKey , testPath ) ;
142142
143143 expect ( keystore ) . toBeDefined ( ) ;
144144 expect ( keystore . crypto . cipher . message ) . toMatch ( / ^ [ 0 - 9 a - f ] { 64 } $ / ) ;
145145 } ) ;
146146
147- it ( 'preserves derivation path exactly as provided' , ( ) => {
147+ it ( 'preserves derivation path exactly as provided' , async ( ) => {
148148 const customPath = 'm/12381/3600/99/88/77' ;
149- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , customPath ) ;
149+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , customPath ) ;
150150
151151 expect ( keystore . path ) . toBe ( customPath ) ;
152152 } ) ;
153153 } ) ;
154154
155155 describe ( 'loadBn254Keystore' , ( ) => {
156- it ( 'loads and validates a valid BN254 keystore file' , ( ) => {
157- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
156+ it ( 'loads and validates a valid BN254 keystore file' , async ( ) => {
157+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
158158 const filePath = join ( tempDir , 'valid-keystore.json' ) ;
159159 writeFileSync ( filePath , JSON . stringify ( keystore ) ) ;
160160
@@ -191,8 +191,8 @@ describe('BN254 Keystore', () => {
191191 } ) ;
192192
193193 describe ( 'decryptBn254Keystore' , ( ) => {
194- it ( 'successfully decrypts a keystore with correct password' , ( ) => {
195- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
194+ it ( 'successfully decrypts a keystore with correct password' , async ( ) => {
195+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
196196 const filePath = join ( tempDir , 'decrypt-test.json' ) ;
197197 writeFileSync ( filePath , JSON . stringify ( keystore ) ) ;
198198
@@ -201,18 +201,18 @@ describe('BN254 Keystore', () => {
201201 expect ( decrypted ) . toBe ( testPrivateKey ) ;
202202 } ) ;
203203
204- it ( 'throws on incorrect password' , ( ) => {
205- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
204+ it ( 'throws on incorrect password' , async ( ) => {
205+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
206206 const filePath = join ( tempDir , 'wrong-password-test.json' ) ;
207207 writeFileSync ( filePath , JSON . stringify ( keystore ) ) ;
208208
209209 expect ( ( ) => decryptBn254Keystore ( filePath , 'wrong-password' ) ) . toThrow ( Bn254KeystoreError ) ;
210210 expect ( ( ) => decryptBn254Keystore ( filePath , 'wrong-password' ) ) . toThrow ( / C h e c k s u m v e r i f i c a t i o n f a i l e d / ) ;
211211 } ) ;
212212
213- it ( 'works with empty password if keystore was created with empty password' , ( ) => {
213+ it ( 'works with empty password if keystore was created with empty password' , async ( ) => {
214214 const emptyPassword = '' ;
215- const keystore = createBn254Keystore ( emptyPassword , testPrivateKey , testPublicKey , testPath ) ;
215+ const keystore = await createBn254Keystore ( emptyPassword , testPrivateKey , testPublicKey , testPath ) ;
216216 const filePath = join ( tempDir , 'empty-password-test.json' ) ;
217217 writeFileSync ( filePath , JSON . stringify ( keystore ) ) ;
218218
@@ -223,25 +223,25 @@ describe('BN254 Keystore', () => {
223223 } ) ;
224224
225225 describe ( 'decryptBn254KeystoreFromObject' , ( ) => {
226- it ( 'decrypts from an in-memory keystore object' , ( ) => {
227- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
226+ it ( 'decrypts from an in-memory keystore object' , async ( ) => {
227+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
228228
229229 const decrypted = decryptBn254KeystoreFromObject ( keystore , testPassword ) ;
230230
231231 expect ( decrypted ) . toBe ( testPrivateKey ) ;
232232 } ) ;
233233
234- it ( 'throws on unsupported KDF function' , ( ) => {
235- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
234+ it ( 'throws on unsupported KDF function' , async ( ) => {
235+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
236236 // Manually modify to unsupported KDF
237237 ( keystore . crypto . kdf as any ) . function = 'scrypt' ;
238238
239239 expect ( ( ) => decryptBn254KeystoreFromObject ( keystore , testPassword ) ) . toThrow ( Bn254KeystoreError ) ;
240240 expect ( ( ) => decryptBn254KeystoreFromObject ( keystore , testPassword ) ) . toThrow ( / U n s u p p o r t e d K D F f u n c t i o n / ) ;
241241 } ) ;
242242
243- it ( 'throws on unsupported cipher function' , ( ) => {
244- const keystore = createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
243+ it ( 'throws on unsupported cipher function' , async ( ) => {
244+ const keystore = await createBn254Keystore ( testPassword , testPrivateKey , testPublicKey , testPath ) ;
245245 // Manually modify to unsupported cipher
246246 ( keystore . crypto . cipher as any ) . function = 'aes-256-gcm' ;
247247
@@ -251,30 +251,30 @@ describe('BN254 Keystore', () => {
251251 } ) ;
252252
253253 describe ( 'round-trip encryption and decryption' , ( ) => {
254- it ( 'encrypts and then decrypts to get original key' , ( ) => {
254+ it ( 'encrypts and then decrypts to get original key' , async ( ) => {
255255 const originalKey = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' ;
256256 const password = 'super-secret' ;
257257 const pubkey = '0x' + 'ff' . repeat ( 33 ) ;
258258 const path = 'm/12381/3600/5/0/0' ;
259259
260260 // Create encrypted keystore
261- const encrypted = createBn254Keystore ( password , originalKey , pubkey , path ) ;
261+ const encrypted = await createBn254Keystore ( password , originalKey , pubkey , path ) ;
262262
263263 // Decrypt it
264264 const decrypted = decryptBn254KeystoreFromObject ( encrypted , password ) ;
265265
266266 expect ( decrypted ) . toBe ( originalKey ) ;
267267 } ) ;
268268
269- it ( 'round-trips multiple different keys' , ( ) => {
269+ it ( 'round-trips multiple different keys' , async ( ) => {
270270 const testCases = [
271271 { key : '0x' + '11' . repeat ( 32 ) , password : 'pass1' } ,
272272 { key : '0x' + '22' . repeat ( 32 ) , password : 'pass2' } ,
273273 { key : '0x' + 'ab' . repeat ( 32 ) , password : 'pass3' } ,
274274 ] ;
275275
276276 for ( const { key, password } of testCases ) {
277- const encrypted = createBn254Keystore ( password , key , testPublicKey , testPath ) ;
277+ const encrypted = await createBn254Keystore ( password , key , testPublicKey , testPath ) ;
278278 const decrypted = decryptBn254KeystoreFromObject ( encrypted , password ) ;
279279 expect ( decrypted ) . toBe ( key ) ;
280280 }
0 commit comments