ox 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/Keystore/package.json +6 -0
  3. package/_cjs/core/Keystore.js +127 -0
  4. package/_cjs/core/Keystore.js.map +1 -0
  5. package/_cjs/core/TransactionEnvelopeEip1559.js +2 -2
  6. package/_cjs/core/TransactionEnvelopeEip1559.js.map +1 -1
  7. package/_cjs/core/TransactionEnvelopeEip2930.js +2 -2
  8. package/_cjs/core/TransactionEnvelopeEip2930.js.map +1 -1
  9. package/_cjs/core/TransactionEnvelopeEip4844.js +2 -2
  10. package/_cjs/core/TransactionEnvelopeEip4844.js.map +1 -1
  11. package/_cjs/core/TransactionEnvelopeEip7702.js +2 -2
  12. package/_cjs/core/TransactionEnvelopeEip7702.js.map +1 -1
  13. package/_cjs/core/TransactionEnvelopeLegacy.js +2 -2
  14. package/_cjs/core/TransactionEnvelopeLegacy.js.map +1 -1
  15. package/_cjs/index.js +3 -2
  16. package/_cjs/index.js.map +1 -1
  17. package/_cjs/version.js +1 -1
  18. package/_esm/core/Keystore.js +248 -0
  19. package/_esm/core/Keystore.js.map +1 -0
  20. package/_esm/core/TransactionEnvelopeEip1559.js +2 -2
  21. package/_esm/core/TransactionEnvelopeEip1559.js.map +1 -1
  22. package/_esm/core/TransactionEnvelopeEip2930.js +2 -2
  23. package/_esm/core/TransactionEnvelopeEip2930.js.map +1 -1
  24. package/_esm/core/TransactionEnvelopeEip4844.js +2 -2
  25. package/_esm/core/TransactionEnvelopeEip4844.js.map +1 -1
  26. package/_esm/core/TransactionEnvelopeEip7702.js +2 -2
  27. package/_esm/core/TransactionEnvelopeEip7702.js.map +1 -1
  28. package/_esm/core/TransactionEnvelopeLegacy.js +2 -2
  29. package/_esm/core/TransactionEnvelopeLegacy.js.map +1 -1
  30. package/_esm/index.js +47 -0
  31. package/_esm/index.js.map +1 -1
  32. package/_esm/version.js +1 -1
  33. package/_types/core/Keystore.d.ts +267 -0
  34. package/_types/core/Keystore.d.ts.map +1 -0
  35. package/_types/core/P256.d.ts +1 -51
  36. package/_types/core/P256.d.ts.map +1 -1
  37. package/_types/core/Secp256k1.d.ts +1 -51
  38. package/_types/core/Secp256k1.d.ts.map +1 -1
  39. package/_types/core/TransactionEnvelopeEip1559.d.ts.map +1 -1
  40. package/_types/core/TransactionEnvelopeEip2930.d.ts.map +1 -1
  41. package/_types/core/TransactionEnvelopeEip4844.d.ts.map +1 -1
  42. package/_types/core/TransactionEnvelopeEip7702.d.ts.map +1 -1
  43. package/_types/core/TransactionEnvelopeLegacy.d.ts.map +1 -1
  44. package/_types/core/internal/rpcSchemas/wallet.d.ts +4 -1
  45. package/_types/core/internal/rpcSchemas/wallet.d.ts.map +1 -1
  46. package/_types/index.d.ts +47 -0
  47. package/_types/index.d.ts.map +1 -1
  48. package/_types/version.d.ts +1 -1
  49. package/core/Keystore.ts +414 -0
  50. package/core/TransactionEnvelopeEip1559.ts +2 -1
  51. package/core/TransactionEnvelopeEip2930.ts +2 -1
  52. package/core/TransactionEnvelopeEip4844.ts +2 -1
  53. package/core/TransactionEnvelopeEip7702.ts +2 -1
  54. package/core/TransactionEnvelopeLegacy.ts +2 -1
  55. package/core/internal/rpcSchemas/wallet.ts +8 -1
  56. package/index.ts +48 -0
  57. package/package.json +13 -7
  58. package/version.ts +1 -1
package/_types/index.d.ts CHANGED
@@ -1525,6 +1525,53 @@ export * as Fee from './core/Fee.js';
1525
1525
  * @category JSON
1526
1526
  */
1527
1527
  export * as Json from './core/Json.js';
1528
+ /**
1529
+ * Utilities & types for working with [Keystores](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage).
1530
+ *
1531
+ * @example
1532
+ * ### Encrypting & Decrypting Private Keys
1533
+ *
1534
+ * Private keys can be encrypted into a JSON keystore using {@link ox#Keystore.(encrypt:function)} and decrypted using {@link ox#Keystore.(decrypt:function)}:
1535
+ *
1536
+ * ```ts twoslash
1537
+ * import { Keystore, Secp256k1 } from 'ox'
1538
+ *
1539
+ * // Generate a random private key.
1540
+ * const privateKey = Secp256k1.randomPrivateKey()
1541
+ *
1542
+ * // Derive a key from a password.
1543
+ * const key = Keystore.pbkdf2({ password: 'testpassword' })
1544
+ *
1545
+ * // Encrypt the private key.
1546
+ * const encrypted = await Keystore.encrypt(privateKey, key)
1547
+ * // @log: {
1548
+ * // @log: "crypto": {
1549
+ * // @log: "cipher": "aes-128-ctr",
1550
+ * // @log: "ciphertext": "...",
1551
+ * // @log: "cipherparams": {
1552
+ * // @log: "iv": "...",
1553
+ * // @log: },
1554
+ * // @log: "kdf": "pbkdf2",
1555
+ * // @log: "kdfparams": {
1556
+ * // @log: "salt": "...",
1557
+ * // @log: "dklen": 32,
1558
+ * // @log: "prf": "hmac-sha256",
1559
+ * // @log: "c": 262144,
1560
+ * // @log: },
1561
+ * // @log: "mac": "...",
1562
+ * // @log: },
1563
+ * // @log: "id": "...",
1564
+ * // @log: "version": 3,
1565
+ * // @log: }
1566
+ *
1567
+ * // Decrypt the private key.
1568
+ * const decrypted = await Keystore.decrypt(encrypted, key)
1569
+ * // @log: "0x..."
1570
+ * ```
1571
+ *
1572
+ * @category Crypto
1573
+ */
1574
+ export * as Keystore from './core/Keystore.js';
1528
1575
  /**
1529
1576
  * Utility functions for working with KZG Commitments.
1530
1577
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAE/B,YAAY,EAAE,CAAA;AAEd;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkHG;AACH,OAAO,KAAK,GAAG,MAAM,eAAe,CAAA;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,OAAO,KAAK,YAAY,MAAM,wBAAwB,CAAA;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,OAAO,KAAK,aAAa,MAAM,yBAAyB,CAAA;AAExD;;;;GAIG;AACH,OAAO,KAAK,UAAU,MAAM,sBAAsB,CAAA"}
@@ -1,3 +1,3 @@
1
1
  /** @internal */
2
- export declare const version = "0.7.0";
2
+ export declare const version = "0.7.2";
3
3
  //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1,414 @@
1
+ import { ctr } from '@noble/ciphers/aes'
2
+ import {
3
+ pbkdf2Async as pbkdf2Async_noble,
4
+ pbkdf2 as pbkdf2_noble,
5
+ } from '@noble/hashes/pbkdf2'
6
+ import {
7
+ scryptAsync as scryptAsync_noble,
8
+ scrypt as scrypt_noble,
9
+ } from '@noble/hashes/scrypt'
10
+ import { sha256 } from '@noble/hashes/sha2'
11
+ import * as Bytes from './Bytes.js'
12
+ import type * as Errors from './Errors.js'
13
+ import * as Hash from './Hash.js'
14
+ import type * as Hex from './Hex.js'
15
+
16
+ /** Base Key. */
17
+ type BaseKey<
18
+ kdf extends string = string,
19
+ kdfparams extends Record<string, unknown> = Record<string, unknown>,
20
+ > = {
21
+ iv: Bytes.Bytes
22
+ key: () => string
23
+ kdfparams: kdfparams
24
+ kdf: kdf
25
+ }
26
+
27
+ /** Keystore. */
28
+ export type Keystore = {
29
+ crypto: {
30
+ cipher: 'aes-128-ctr'
31
+ ciphertext: string
32
+ cipherparams: {
33
+ iv: string
34
+ }
35
+ mac: string
36
+ } & Pick<Key, 'kdf' | 'kdfparams'>
37
+ id: string
38
+ version: 3
39
+ }
40
+
41
+ /** Key. */
42
+ export type Key = Pbkdf2Key | ScryptKey
43
+
44
+ /** PBKDF2 Key. */
45
+ export type Pbkdf2Key = BaseKey<
46
+ 'pbkdf2',
47
+ {
48
+ c: number
49
+ dklen: number
50
+ prf: 'hmac-sha256'
51
+ salt: string
52
+ }
53
+ >
54
+
55
+ /** Scrypt Key. */
56
+ export type ScryptKey = BaseKey<
57
+ 'scrypt',
58
+ {
59
+ dklen: number
60
+ n: number
61
+ p: number
62
+ r: number
63
+ salt: string
64
+ }
65
+ >
66
+
67
+ /**
68
+ * Decrypts a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/)
69
+ * into a private key.
70
+ *
71
+ * Supports the following key derivation functions (KDFs):
72
+ * - {@link ox#Keystore.(pbkdf2:function)}
73
+ * - {@link ox#Keystore.(scrypt:function)}
74
+ *
75
+ * @example
76
+ * ```ts twoslash
77
+ * // @noErrors
78
+ * import { Keystore, Secp256k1 } from 'ox'
79
+ *
80
+ * // JSON keystore.
81
+ * const keystore = { crypto: { ... }, id: '...', version: 3 }
82
+ *
83
+ * // Derive key from password.
84
+ * const key = Keystore.pbkdf2({ password: 'testpassword' })
85
+ *
86
+ * // Decrypt the private key.
87
+ * const privateKey = await Keystore.decrypt(keystore, key)
88
+ * // @log: "0x..."
89
+ * ```
90
+ *
91
+ * @param keystore - JSON keystore.
92
+ * @param key - Key to use for decryption.
93
+ * @param options - Decryption options.
94
+ * @returns Decrypted private key.
95
+ */
96
+ export async function decrypt<as extends 'Hex' | 'Bytes' = 'Hex'>(
97
+ keystore: Keystore,
98
+ key: Key,
99
+ options: decrypt.Options<as> = {},
100
+ ): Promise<decrypt.ReturnType<as>> {
101
+ const { as = 'Hex' } = options
102
+ const key_ = Bytes.from(`0x${key.key()}`)
103
+
104
+ const encKey = Bytes.slice(key_, 0, 16)
105
+ const macKey = Bytes.slice(key_, 16, 32)
106
+
107
+ const ciphertext = Bytes.from(`0x${keystore.crypto.ciphertext}`)
108
+ const mac = Hash.keccak256(Bytes.concat(macKey, ciphertext))
109
+
110
+ if (!Bytes.isEqual(mac, Bytes.from(`0x${keystore.crypto.mac}`)))
111
+ throw new Error('corrupt keystore')
112
+
113
+ const data = ctr(encKey, key.iv).decrypt(ciphertext)
114
+
115
+ if (as === 'Hex') return Bytes.toHex(data) as never
116
+ return data as never
117
+ }
118
+
119
+ export declare namespace decrypt {
120
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex' | 'Bytes'> = {
121
+ /** Output format. @default 'Hex' */
122
+ as?: as | 'Hex' | 'Bytes' | undefined
123
+ }
124
+
125
+ type ReturnType<as extends 'Hex' | 'Bytes' = 'Hex' | 'Bytes'> =
126
+ | (as extends 'Hex' ? Hex.Hex : never)
127
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
128
+ }
129
+
130
+ /**
131
+ * Encrypts a private key as a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/)
132
+ * using a derived key.
133
+ *
134
+ * Supports the following key derivation functions (KDFs):
135
+ * - {@link ox#Keystore.(pbkdf2:function)}
136
+ * - {@link ox#Keystore.(scrypt:function)}
137
+ *
138
+ * @example
139
+ * ```ts twoslash
140
+ * import { Keystore, Secp256k1 } from 'ox'
141
+ *
142
+ * // Generate a random private key.
143
+ * const privateKey = Secp256k1.randomPrivateKey()
144
+ *
145
+ * // Derive key from password.
146
+ * const key = Keystore.pbkdf2({ password: 'testpassword' })
147
+ *
148
+ * // Encrypt the private key.
149
+ * const encrypted = await Keystore.encrypt(privateKey, key)
150
+ * // @log: {
151
+ * // @log: "crypto": {
152
+ * // @log: "cipher": "aes-128-ctr",
153
+ * // @log: "ciphertext": "...",
154
+ * // @log: "cipherparams": {
155
+ * // @log: "iv": "...",
156
+ * // @log: },
157
+ * // @log: "kdf": "pbkdf2",
158
+ * // @log: "kdfparams": {
159
+ * // @log: "salt": "...",
160
+ * // @log: "dklen": 32,
161
+ * // @log: "prf": "hmac-sha256",
162
+ * // @log: "c": 262144,
163
+ * // @log: },
164
+ * // @log: "mac": "...",
165
+ * // @log: },
166
+ * // @log: "id": "...",
167
+ * // @log: "version": 3,
168
+ * // @log: }
169
+ * ```
170
+ *
171
+ * @param privateKey - Private key to encrypt.
172
+ * @param key - Key to use for encryption.
173
+ * @param options - Encryption options.
174
+ * @returns Encrypted keystore.
175
+ */
176
+ export async function encrypt(
177
+ privateKey: Bytes.Bytes | Hex.Hex,
178
+ key: Key,
179
+ options: encrypt.Options = {},
180
+ ): Promise<Keystore> {
181
+ const { id = crypto.randomUUID() } = options
182
+
183
+ const key_ = Bytes.from(`0x${key.key()}`)
184
+ const value_ = Bytes.from(privateKey)
185
+
186
+ const encKey = Bytes.slice(key_, 0, 16)
187
+ const macKey = Bytes.slice(key_, 16, 32)
188
+
189
+ const ciphertext = ctr(encKey, key.iv).encrypt(value_)
190
+ const mac = Hash.keccak256(Bytes.concat(macKey, ciphertext))
191
+
192
+ return {
193
+ crypto: {
194
+ cipher: 'aes-128-ctr',
195
+ ciphertext: Bytes.toHex(ciphertext).slice(2),
196
+ cipherparams: { iv: Bytes.toHex(key.iv).slice(2) },
197
+ kdf: key.kdf,
198
+ kdfparams: key.kdfparams,
199
+ mac: Bytes.toHex(mac).slice(2),
200
+ } as Keystore['crypto'],
201
+ id,
202
+ version: 3,
203
+ }
204
+ }
205
+
206
+ export declare namespace encrypt {
207
+ type Options = {
208
+ /** UUID. */
209
+ id?: string | undefined
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2).
215
+ *
216
+ * @example
217
+ * ```ts twoslash
218
+ * import { Keystore } from 'ox'
219
+ *
220
+ * const key = Keystore.pbkdf2({ password: 'testpassword' })
221
+ * ```
222
+ *
223
+ * @param options - PBKDF2 options.
224
+ * @returns PBKDF2 key.
225
+ */
226
+ export function pbkdf2(options: pbkdf2.Options) {
227
+ const { iv, iterations = 262_144, password } = options
228
+
229
+ const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32)
230
+ const key = Bytes.toHex(
231
+ pbkdf2_noble(sha256, password, salt, { c: iterations, dkLen: 32 }),
232
+ ).slice(2)
233
+
234
+ return defineKey({
235
+ iv,
236
+ key: () => key,
237
+ kdfparams: {
238
+ c: iterations,
239
+ dklen: 32,
240
+ prf: 'hmac-sha256',
241
+ salt: Bytes.toHex(salt).slice(2),
242
+ },
243
+ kdf: 'pbkdf2',
244
+ }) satisfies Pbkdf2Key
245
+ }
246
+
247
+ export declare namespace pbkdf2 {
248
+ type Options = {
249
+ /** The counter to use for the AES-CTR encryption. */
250
+ iv?: Bytes.Bytes | Hex.Hex | undefined
251
+ /** The number of iterations to use. @default 262_144 */
252
+ iterations?: number | undefined
253
+ /** Password to derive key from. */
254
+ password: string
255
+ /** Salt to use for key derivation. @default `Bytes.random(32)` */
256
+ salt?: Bytes.Bytes | Hex.Hex | undefined
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2).
262
+ *
263
+ * @example
264
+ * ```ts twoslash
265
+ * import { Keystore } from 'ox'
266
+ *
267
+ * const key = await Keystore.pbkdf2Async({ password: 'testpassword' })
268
+ * ```
269
+ *
270
+ * @param options - PBKDF2 options.
271
+ * @returns PBKDF2 key.
272
+ */
273
+ export async function pbkdf2Async(options: pbkdf2.Options) {
274
+ const { iv, iterations = 262_144, password } = options
275
+
276
+ const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32)
277
+ const key = Bytes.toHex(
278
+ await pbkdf2Async_noble(sha256, password, salt, {
279
+ c: iterations,
280
+ dkLen: 32,
281
+ }),
282
+ ).slice(2)
283
+
284
+ return defineKey({
285
+ iv,
286
+ key: () => key,
287
+ kdfparams: {
288
+ c: iterations,
289
+ dklen: 32,
290
+ prf: 'hmac-sha256',
291
+ salt: Bytes.toHex(salt).slice(2),
292
+ },
293
+ kdf: 'pbkdf2',
294
+ }) satisfies Pbkdf2Key
295
+ }
296
+
297
+ export declare namespace pbkdf2Async {
298
+ type Options = pbkdf2.Options
299
+ }
300
+
301
+ /**
302
+ * Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt).
303
+ *
304
+ * @example
305
+ * ```ts twoslash
306
+ * import { Keystore } from 'ox'
307
+ *
308
+ * const key = Keystore.scrypt({ password: 'testpassword' })
309
+ * ```
310
+ *
311
+ * @param options - Scrypt options.
312
+ * @returns Scrypt key.
313
+ */
314
+ export function scrypt(options: scrypt.Options) {
315
+ const { iv, n = 262_144, password } = options
316
+
317
+ const p = 8
318
+ const r = 1
319
+
320
+ const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32)
321
+ const key = Bytes.toHex(
322
+ scrypt_noble(password, salt, { N: n, dkLen: 32, r, p }),
323
+ ).slice(2)
324
+
325
+ return defineKey({
326
+ iv,
327
+ key: () => key,
328
+ kdfparams: {
329
+ dklen: 32,
330
+ n,
331
+ p,
332
+ r,
333
+ salt: Bytes.toHex(salt).slice(2),
334
+ },
335
+ kdf: 'scrypt',
336
+ }) satisfies ScryptKey
337
+ }
338
+
339
+ export declare namespace scrypt {
340
+ type Options = {
341
+ /** The counter to use for the AES-CTR encryption. */
342
+ iv?: Bytes.Bytes | Hex.Hex | undefined
343
+ /** Cost factor. @default 262_144 */
344
+ n?: number | undefined
345
+ /** Password to derive key from. */
346
+ password: string
347
+ /** Salt to use for key derivation. @default `Bytes.random(32)` */
348
+ salt?: Bytes.Bytes | Hex.Hex | undefined
349
+ }
350
+ }
351
+
352
+ /**
353
+ * Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt).
354
+ *
355
+ * @example
356
+ * ```ts twoslash
357
+ * import { Keystore } from 'ox'
358
+ *
359
+ * const key = await Keystore.scryptAsync({ password: 'testpassword' })
360
+ * ```
361
+ *
362
+ * @param options - Scrypt options.
363
+ * @returns Scrypt key.
364
+ */
365
+ export async function scryptAsync(options: scrypt.Options) {
366
+ const { iv, n = 262_144, password } = options
367
+
368
+ const p = 8
369
+ const r = 1
370
+
371
+ const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32)
372
+ const key = Bytes.toHex(
373
+ await scryptAsync_noble(password, salt, { N: n, dkLen: 32, r, p }),
374
+ ).slice(2)
375
+
376
+ return defineKey({
377
+ iv,
378
+ key: () => key,
379
+ kdfparams: {
380
+ dklen: 32,
381
+ n,
382
+ p,
383
+ r,
384
+ salt: Bytes.toHex(salt).slice(2),
385
+ },
386
+ kdf: 'scrypt',
387
+ }) satisfies ScryptKey
388
+ }
389
+
390
+ export declare namespace scryptAsync {
391
+ type Options = scrypt.Options
392
+ }
393
+
394
+ ///////////////////////////////////////////////////////////////////////////
395
+
396
+ /** @internal */
397
+ function defineKey<const key extends defineKey.Value>(
398
+ key: key,
399
+ ): key & { iv: Bytes.Bytes } {
400
+ const iv = key.iv ? Bytes.from(key.iv) : Bytes.random(16)
401
+ return { ...key, iv }
402
+ }
403
+
404
+ /** @internal */
405
+ declare namespace defineKey {
406
+ type Value<
407
+ kdf extends string = string,
408
+ kdfparams extends Record<string, unknown> = Record<string, unknown>,
409
+ > = Omit<BaseKey<kdf, kdfparams>, 'iv'> & {
410
+ iv?: Bytes.Bytes | Hex.Hex | undefined
411
+ }
412
+
413
+ type ErrorType = Errors.GlobalErrorType
414
+ }
@@ -167,7 +167,8 @@ export function deserialize(
167
167
  if (Hex.validate(to) && to !== '0x') transaction.to = to
168
168
  if (Hex.validate(gas) && gas !== '0x') transaction.gas = BigInt(gas)
169
169
  if (Hex.validate(data) && data !== '0x') transaction.data = data
170
- if (Hex.validate(nonce) && nonce !== '0x') transaction.nonce = BigInt(nonce)
170
+ if (Hex.validate(nonce))
171
+ transaction.nonce = nonce === '0x' ? 0n : BigInt(nonce)
171
172
  if (Hex.validate(value) && value !== '0x') transaction.value = BigInt(value)
172
173
  if (Hex.validate(maxFeePerGas) && maxFeePerGas !== '0x')
173
174
  transaction.maxFeePerGas = BigInt(maxFeePerGas)
@@ -153,7 +153,8 @@ export function deserialize(
153
153
  if (Hex.validate(to) && to !== '0x') transaction.to = to
154
154
  if (Hex.validate(gas) && gas !== '0x') transaction.gas = BigInt(gas)
155
155
  if (Hex.validate(data) && data !== '0x') transaction.data = data
156
- if (Hex.validate(nonce) && nonce !== '0x') transaction.nonce = BigInt(nonce)
156
+ if (Hex.validate(nonce))
157
+ transaction.nonce = nonce === '0x' ? 0n : BigInt(nonce)
157
158
  if (Hex.validate(value) && value !== '0x') transaction.value = BigInt(value)
158
159
  if (Hex.validate(gasPrice) && gasPrice !== '0x')
159
160
  transaction.gasPrice = BigInt(gasPrice)
@@ -194,7 +194,8 @@ export function deserialize(
194
194
  if (Hex.validate(to) && to !== '0x') transaction.to = to
195
195
  if (Hex.validate(gas) && gas !== '0x') transaction.gas = BigInt(gas)
196
196
  if (Hex.validate(data) && data !== '0x') transaction.data = data
197
- if (Hex.validate(nonce) && nonce !== '0x') transaction.nonce = BigInt(nonce)
197
+ if (Hex.validate(nonce))
198
+ transaction.nonce = nonce === '0x' ? 0n : BigInt(nonce)
198
199
  if (Hex.validate(value) && value !== '0x') transaction.value = BigInt(value)
199
200
  if (Hex.validate(maxFeePerBlobGas) && maxFeePerBlobGas !== '0x')
200
201
  transaction.maxFeePerBlobGas = BigInt(maxFeePerBlobGas)
@@ -169,7 +169,8 @@ export function deserialize(
169
169
  if (Hex.validate(to) && to !== '0x') transaction.to = to
170
170
  if (Hex.validate(gas) && gas !== '0x') transaction.gas = BigInt(gas)
171
171
  if (Hex.validate(data) && data !== '0x') transaction.data = data
172
- if (Hex.validate(nonce) && nonce !== '0x') transaction.nonce = BigInt(nonce)
172
+ if (Hex.validate(nonce))
173
+ transaction.nonce = nonce === '0x' ? 0n : BigInt(nonce)
173
174
  if (Hex.validate(value) && value !== '0x') transaction.value = BigInt(value)
174
175
  if (Hex.validate(maxFeePerGas) && maxFeePerGas !== '0x')
175
176
  transaction.maxFeePerGas = BigInt(maxFeePerGas)
@@ -135,7 +135,8 @@ export function deserialize(
135
135
  if (Hex.validate(to) && to !== '0x') transaction.to = to
136
136
  if (Hex.validate(gas) && gas !== '0x') transaction.gas = BigInt(gas)
137
137
  if (Hex.validate(data) && data !== '0x') transaction.data = data
138
- if (Hex.validate(nonce) && nonce !== '0x') transaction.nonce = BigInt(nonce)
138
+ if (Hex.validate(nonce))
139
+ transaction.nonce = nonce === '0x' ? 0n : BigInt(nonce)
139
140
  if (Hex.validate(value) && value !== '0x') transaction.value = BigInt(value)
140
141
  if (Hex.validate(gasPrice) && gasPrice !== '0x')
141
142
  transaction.gasPrice = BigInt(gasPrice)
@@ -157,7 +157,14 @@ export type Wallet = RpcSchema.From<
157
157
  | {
158
158
  Request: {
159
159
  method: 'wallet_getCapabilities'
160
- params?: [Address.Address | undefined] | undefined
160
+ params?:
161
+ | readonly []
162
+ | readonly [Address.Address | undefined]
163
+ | readonly [
164
+ Address.Address | undefined,
165
+ readonly Hex.Hex[] | undefined,
166
+ ]
167
+ | undefined
161
168
  }
162
169
  ReturnType: Compute<WalletCapabilitiesMap>
163
170
  }
package/index.ts CHANGED
@@ -1559,6 +1559,54 @@ export * as Fee from './core/Fee.js'
1559
1559
  */
1560
1560
  export * as Json from './core/Json.js'
1561
1561
 
1562
+ /**
1563
+ * Utilities & types for working with [Keystores](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage).
1564
+ *
1565
+ * @example
1566
+ * ### Encrypting & Decrypting Private Keys
1567
+ *
1568
+ * Private keys can be encrypted into a JSON keystore using {@link ox#Keystore.(encrypt:function)} and decrypted using {@link ox#Keystore.(decrypt:function)}:
1569
+ *
1570
+ * ```ts twoslash
1571
+ * import { Keystore, Secp256k1 } from 'ox'
1572
+ *
1573
+ * // Generate a random private key.
1574
+ * const privateKey = Secp256k1.randomPrivateKey()
1575
+ *
1576
+ * // Derive a key from a password.
1577
+ * const key = Keystore.pbkdf2({ password: 'testpassword' })
1578
+ *
1579
+ * // Encrypt the private key.
1580
+ * const encrypted = await Keystore.encrypt(privateKey, key)
1581
+ * // @log: {
1582
+ * // @log: "crypto": {
1583
+ * // @log: "cipher": "aes-128-ctr",
1584
+ * // @log: "ciphertext": "...",
1585
+ * // @log: "cipherparams": {
1586
+ * // @log: "iv": "...",
1587
+ * // @log: },
1588
+ * // @log: "kdf": "pbkdf2",
1589
+ * // @log: "kdfparams": {
1590
+ * // @log: "salt": "...",
1591
+ * // @log: "dklen": 32,
1592
+ * // @log: "prf": "hmac-sha256",
1593
+ * // @log: "c": 262144,
1594
+ * // @log: },
1595
+ * // @log: "mac": "...",
1596
+ * // @log: },
1597
+ * // @log: "id": "...",
1598
+ * // @log: "version": 3,
1599
+ * // @log: }
1600
+ *
1601
+ * // Decrypt the private key.
1602
+ * const decrypted = await Keystore.decrypt(encrypted, key)
1603
+ * // @log: "0x..."
1604
+ * ```
1605
+ *
1606
+ * @category Crypto
1607
+ */
1608
+ export * as Keystore from './core/Keystore.js'
1609
+
1562
1610
  /**
1563
1611
  * Utility functions for working with KZG Commitments.
1564
1612
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ox",
3
3
  "description": "Ethereum Standard Library",
4
- "version": "0.7.0",
4
+ "version": "0.7.2",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",
@@ -15,12 +15,13 @@
15
15
  "!jsr.json"
16
16
  ],
17
17
  "dependencies": {
18
- "@adraffy/ens-normalize": "^1.10.1",
19
- "@noble/curves": "^1.6.0",
20
- "@noble/hashes": "^1.5.0",
21
- "@scure/bip39": "^1.4.0",
22
- "@scure/bip32": "^1.5.0",
23
- "abitype": "^1.0.6",
18
+ "@adraffy/ens-normalize": "^1.11.0",
19
+ "@noble/ciphers": "^1.3.0",
20
+ "@noble/curves": "^1.9.1",
21
+ "@noble/hashes": "^1.8.0",
22
+ "@scure/bip32": "^1.7.0",
23
+ "@scure/bip39": "^1.6.0",
24
+ "abitype": "^1.0.8",
24
25
  "eventemitter3": "5.0.1"
25
26
  },
26
27
  "peerDependencies": {
@@ -212,6 +213,11 @@
212
213
  "import": "./_esm/core/Json.js",
213
214
  "default": "./_cjs/core/Json.js"
214
215
  },
216
+ "./Keystore": {
217
+ "types": "./_types/core/Keystore.d.ts",
218
+ "import": "./_esm/core/Keystore.js",
219
+ "default": "./_cjs/core/Keystore.js"
220
+ },
215
221
  "./Kzg": {
216
222
  "types": "./_types/core/Kzg.d.ts",
217
223
  "import": "./_esm/core/Kzg.js",
package/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** @internal */
2
- export const version = '0.7.0'
2
+ export const version = '0.7.2'