ox 0.8.0 → 0.8.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 (64) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/Ed25519/package.json +6 -0
  3. package/X25519/package.json +6 -0
  4. package/_cjs/core/Bls.js +10 -0
  5. package/_cjs/core/Bls.js.map +1 -1
  6. package/_cjs/core/Ed25519.js +53 -0
  7. package/_cjs/core/Ed25519.js.map +1 -0
  8. package/_cjs/core/Keystore.js +65 -5
  9. package/_cjs/core/Keystore.js.map +1 -1
  10. package/_cjs/core/P256.js +23 -0
  11. package/_cjs/core/P256.js.map +1 -1
  12. package/_cjs/core/Secp256k1.js +20 -0
  13. package/_cjs/core/Secp256k1.js.map +1 -1
  14. package/_cjs/core/WebCryptoP256.js +31 -0
  15. package/_cjs/core/WebCryptoP256.js.map +1 -1
  16. package/_cjs/core/X25519.js +45 -0
  17. package/_cjs/core/X25519.js.map +1 -0
  18. package/_cjs/index.js +4 -2
  19. package/_cjs/index.js.map +1 -1
  20. package/_cjs/version.js +1 -1
  21. package/_esm/core/Bls.js +109 -0
  22. package/_esm/core/Bls.js.map +1 -1
  23. package/_esm/core/Ed25519.js +121 -0
  24. package/_esm/core/Ed25519.js.map +1 -0
  25. package/_esm/core/Keystore.js +107 -9
  26. package/_esm/core/Keystore.js.map +1 -1
  27. package/_esm/core/P256.js +54 -2
  28. package/_esm/core/P256.js.map +1 -1
  29. package/_esm/core/Secp256k1.js +50 -0
  30. package/_esm/core/Secp256k1.js.map +1 -1
  31. package/_esm/core/WebCryptoP256.js +72 -0
  32. package/_esm/core/WebCryptoP256.js.map +1 -1
  33. package/_esm/core/X25519.js +97 -0
  34. package/_esm/core/X25519.js.map +1 -0
  35. package/_esm/index.js +85 -4
  36. package/_esm/index.js.map +1 -1
  37. package/_esm/version.js +1 -1
  38. package/_types/core/Bls.d.ts +124 -0
  39. package/_types/core/Bls.d.ts.map +1 -1
  40. package/_types/core/Ed25519.d.ts +156 -0
  41. package/_types/core/Ed25519.d.ts.map +1 -0
  42. package/_types/core/Keystore.d.ts +66 -8
  43. package/_types/core/Keystore.d.ts.map +1 -1
  44. package/_types/core/P256.d.ts +68 -2
  45. package/_types/core/P256.d.ts.map +1 -1
  46. package/_types/core/Secp256k1.d.ts +67 -0
  47. package/_types/core/Secp256k1.d.ts.map +1 -1
  48. package/_types/core/WebCryptoP256.d.ts +76 -1
  49. package/_types/core/WebCryptoP256.d.ts.map +1 -1
  50. package/_types/core/X25519.d.ts +127 -0
  51. package/_types/core/X25519.d.ts.map +1 -0
  52. package/_types/index.d.ts +85 -4
  53. package/_types/index.d.ts.map +1 -1
  54. package/_types/version.d.ts +1 -1
  55. package/core/Bls.ts +150 -0
  56. package/core/Ed25519.ts +237 -0
  57. package/core/Keystore.ts +141 -12
  58. package/core/P256.ts +114 -2
  59. package/core/Secp256k1.ts +110 -0
  60. package/core/WebCryptoP256.ts +141 -1
  61. package/core/X25519.ts +202 -0
  62. package/index.ts +87 -4
  63. package/package.json +11 -1
  64. package/version.ts +1 -1
package/core/X25519.ts ADDED
@@ -0,0 +1,202 @@
1
+ import { x25519 } from '@noble/curves/ed25519'
2
+ import * as Bytes from './Bytes.js'
3
+ import type * as Errors from './Errors.js'
4
+ import * as Hex from './Hex.js'
5
+
6
+ /** Re-export of noble/curves X25519 utilities. */
7
+ export const noble = x25519
8
+
9
+ /**
10
+ * Creates a new X25519 key pair consisting of a private key and its corresponding public key.
11
+ *
12
+ * @example
13
+ * ```ts twoslash
14
+ * import { X25519 } from 'ox'
15
+ *
16
+ * const { privateKey, publicKey } = X25519.createKeyPair()
17
+ * ```
18
+ *
19
+ * @param options - The options to generate the key pair.
20
+ * @returns The generated key pair containing both private and public keys.
21
+ */
22
+ export function createKeyPair<as extends 'Hex' | 'Bytes' = 'Hex'>(
23
+ options: createKeyPair.Options<as> = {},
24
+ ): createKeyPair.ReturnType<as> {
25
+ const { as = 'Hex' } = options
26
+ const privateKey = randomPrivateKey({ as })
27
+ const publicKey = getPublicKey({ privateKey, as })
28
+
29
+ return {
30
+ privateKey: privateKey as never,
31
+ publicKey: publicKey as never,
32
+ }
33
+ }
34
+
35
+ export declare namespace createKeyPair {
36
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
37
+ /**
38
+ * Format of the returned private and public keys.
39
+ * @default 'Hex'
40
+ */
41
+ as?: as | 'Hex' | 'Bytes' | undefined
42
+ }
43
+
44
+ type ReturnType<as extends 'Hex' | 'Bytes'> = {
45
+ privateKey:
46
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
47
+ | (as extends 'Hex' ? Hex.Hex : never)
48
+ publicKey:
49
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
50
+ | (as extends 'Hex' ? Hex.Hex : never)
51
+ }
52
+
53
+ type ErrorType =
54
+ | Hex.fromBytes.ErrorType
55
+ | randomPrivateKey.ErrorType
56
+ | getPublicKey.ErrorType
57
+ | Errors.GlobalErrorType
58
+ }
59
+
60
+ /**
61
+ * Computes the X25519 public key from a provided private key.
62
+ *
63
+ * @example
64
+ * ```ts twoslash
65
+ * import { X25519 } from 'ox'
66
+ *
67
+ * const publicKey = X25519.getPublicKey({ privateKey: '0x...' })
68
+ * ```
69
+ *
70
+ * @param options - The options to compute the public key.
71
+ * @returns The computed public key.
72
+ */
73
+ export function getPublicKey<as extends 'Hex' | 'Bytes' = 'Hex'>(
74
+ options: getPublicKey.Options<as>,
75
+ ): getPublicKey.ReturnType<as> {
76
+ const { as = 'Hex', privateKey } = options
77
+ const privateKeyBytes = Bytes.from(privateKey)
78
+ const publicKeyBytes = x25519.getPublicKey(privateKeyBytes)
79
+ if (as === 'Hex') return Hex.fromBytes(publicKeyBytes) as never
80
+ return publicKeyBytes as never
81
+ }
82
+
83
+ export declare namespace getPublicKey {
84
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
85
+ /**
86
+ * Format of the returned public key.
87
+ * @default 'Hex'
88
+ */
89
+ as?: as | 'Hex' | 'Bytes' | undefined
90
+ /**
91
+ * Private key to compute the public key from.
92
+ */
93
+ privateKey: Hex.Hex | Bytes.Bytes
94
+ }
95
+
96
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
97
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
98
+ | (as extends 'Hex' ? Hex.Hex : never)
99
+
100
+ type ErrorType =
101
+ | Bytes.from.ErrorType
102
+ | Hex.fromBytes.ErrorType
103
+ | Errors.GlobalErrorType
104
+ }
105
+
106
+ /**
107
+ * Computes a shared secret using X25519 elliptic curve Diffie-Hellman between a private key and a public key.
108
+ *
109
+ * @example
110
+ * ```ts twoslash
111
+ * import { X25519 } from 'ox'
112
+ *
113
+ * const { privateKey: privateKeyA } = X25519.createKeyPair()
114
+ * const { publicKey: publicKeyB } = X25519.createKeyPair()
115
+ *
116
+ * const sharedSecret = X25519.getSharedSecret({
117
+ * privateKey: privateKeyA,
118
+ * publicKey: publicKeyB
119
+ * })
120
+ * ```
121
+ *
122
+ * @param options - The options to compute the shared secret.
123
+ * @returns The computed shared secret.
124
+ */
125
+ export function getSharedSecret<as extends 'Hex' | 'Bytes' = 'Hex'>(
126
+ options: getSharedSecret.Options<as>,
127
+ ): getSharedSecret.ReturnType<as> {
128
+ const { as = 'Hex', privateKey, publicKey } = options
129
+ const privateKeyBytes = Bytes.from(privateKey)
130
+ const publicKeyBytes = Bytes.from(publicKey)
131
+ const sharedSecretBytes = x25519.getSharedSecret(
132
+ privateKeyBytes,
133
+ publicKeyBytes,
134
+ )
135
+ if (as === 'Hex') return Hex.fromBytes(sharedSecretBytes) as never
136
+ return sharedSecretBytes as never
137
+ }
138
+
139
+ export declare namespace getSharedSecret {
140
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
141
+ /**
142
+ * Format of the returned shared secret.
143
+ * @default 'Hex'
144
+ */
145
+ as?: as | 'Hex' | 'Bytes' | undefined
146
+ /**
147
+ * Private key to use for the shared secret computation.
148
+ */
149
+ privateKey: Hex.Hex | Bytes.Bytes
150
+ /**
151
+ * Public key to use for the shared secret computation.
152
+ */
153
+ publicKey: Hex.Hex | Bytes.Bytes
154
+ }
155
+
156
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
157
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
158
+ | (as extends 'Hex' ? Hex.Hex : never)
159
+
160
+ type ErrorType =
161
+ | Bytes.from.ErrorType
162
+ | Hex.fromBytes.ErrorType
163
+ | Errors.GlobalErrorType
164
+ }
165
+
166
+ /**
167
+ * Generates a random X25519 private key.
168
+ *
169
+ * @example
170
+ * ```ts twoslash
171
+ * import { X25519 } from 'ox'
172
+ *
173
+ * const privateKey = X25519.randomPrivateKey()
174
+ * ```
175
+ *
176
+ * @param options - The options to generate the private key.
177
+ * @returns The generated private key.
178
+ */
179
+ export function randomPrivateKey<as extends 'Hex' | 'Bytes' = 'Hex'>(
180
+ options: randomPrivateKey.Options<as> = {},
181
+ ): randomPrivateKey.ReturnType<as> {
182
+ const { as = 'Hex' } = options
183
+ const bytes = x25519.utils.randomPrivateKey()
184
+ if (as === 'Hex') return Hex.fromBytes(bytes) as never
185
+ return bytes as never
186
+ }
187
+
188
+ export declare namespace randomPrivateKey {
189
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
190
+ /**
191
+ * Format of the returned private key.
192
+ * @default 'Hex'
193
+ */
194
+ as?: as | 'Hex' | 'Bytes' | undefined
195
+ }
196
+
197
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
198
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
199
+ | (as extends 'Hex' ? Hex.Hex : never)
200
+
201
+ type ErrorType = Hex.fromBytes.ErrorType | Errors.GlobalErrorType
202
+ }
package/index.ts CHANGED
@@ -1336,6 +1336,38 @@ export * as Caches from './core/Caches.js'
1336
1336
  */
1337
1337
  export * as ContractAddress from './core/ContractAddress.js'
1338
1338
 
1339
+ /**
1340
+ * Utilities for working with Ed25519 signatures and key pairs.
1341
+ *
1342
+ * Ed25519 is a modern elliptic curve signature scheme that provides strong security
1343
+ * guarantees and high performance. It is widely used in various cryptographic applications.
1344
+ *
1345
+ * @example
1346
+ * ### Creating Key Pairs
1347
+ *
1348
+ * ```ts twoslash
1349
+ * import { Ed25519 } from 'ox'
1350
+ *
1351
+ * const { privateKey, publicKey } = Ed25519.createKeyPair()
1352
+ * ```
1353
+ *
1354
+ * @example
1355
+ * ### Signing & Verifying
1356
+ *
1357
+ * ```ts twoslash
1358
+ * import { Ed25519 } from 'ox'
1359
+ *
1360
+ * const { privateKey, publicKey } = Ed25519.createKeyPair()
1361
+ * const payload = '0xdeadbeef'
1362
+ *
1363
+ * const signature = Ed25519.sign({ payload, privateKey })
1364
+ * const isValid = Ed25519.verify({ payload, publicKey, signature })
1365
+ * ```
1366
+ *
1367
+ * @category Crypto
1368
+ */
1369
+ export * as Ed25519 from './core/Ed25519.js'
1370
+
1339
1371
  /**
1340
1372
  * Utility functions for working with ENS names.
1341
1373
  *
@@ -1563,9 +1595,9 @@ export * as Json from './core/Json.js'
1563
1595
  * Utilities & types for working with [Keystores](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage).
1564
1596
  *
1565
1597
  * @example
1566
- * ### Encrypting & Decrypting Private Keys
1598
+ * ### Encrypting Private Keys
1567
1599
  *
1568
- * Private keys can be encrypted into a JSON keystore using {@link ox#Keystore.(encrypt:function)} and decrypted using {@link ox#Keystore.(decrypt:function)}:
1600
+ * Private keys can be encrypted into a JSON keystore using {@link ox#Keystore.(encrypt:function)}:
1569
1601
  *
1570
1602
  * ```ts twoslash
1571
1603
  * import { Keystore, Secp256k1 } from 'ox'
@@ -1577,7 +1609,7 @@ export * as Json from './core/Json.js'
1577
1609
  * const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
1578
1610
  *
1579
1611
  * // Encrypt the private key.
1580
- * const encrypted = await Keystore.encrypt(privateKey, key, opts)
1612
+ * const keystore = Keystore.encrypt(privateKey, key, opts)
1581
1613
  * // @log: {
1582
1614
  * // @log: "crypto": {
1583
1615
  * // @log: "cipher": "aes-128-ctr",
@@ -1597,10 +1629,26 @@ export * as Json from './core/Json.js'
1597
1629
  * // @log: "id": "...",
1598
1630
  * // @log: "version": 3,
1599
1631
  * // @log: }
1632
+ * ```
1633
+ *
1634
+ * @example
1635
+ * ### Decrypting Private Keys
1636
+ *
1637
+ * Private keys can be decrypted from a JSON keystore using {@link ox#Keystore.(decrypt:function)}:
1638
+ *
1639
+ * ```ts twoslash
1640
+ * // @noErrors
1641
+ * import { Keystore, Secp256k1 } from 'ox'
1642
+ *
1643
+ * const keystore = { crypto: { ... }, id: '...', version: 3 }
1644
+ *
1645
+ * // Derive the key.
1646
+ * const key = Keystore.toKey(keystore, { password: 'testpassword' })
1600
1647
  *
1601
1648
  * // Decrypt the private key.
1602
- * const decrypted = await Keystore.decrypt(encrypted, key)
1649
+ * const decrypted = Keystore.decrypt(keystore, key)
1603
1650
  * // @log: "0x..."
1651
+ *
1604
1652
  * ```
1605
1653
  *
1606
1654
  * @category Crypto
@@ -3499,3 +3547,38 @@ export * as WebCryptoP256 from './core/WebCryptoP256.js'
3499
3547
  * @category Execution Spec
3500
3548
  */
3501
3549
  export * as Withdrawal from './core/Withdrawal.js'
3550
+
3551
+ /**
3552
+ * Utilities for working with X25519 elliptic curve Diffie-Hellman key agreement.
3553
+ *
3554
+ * X25519 is a high-performance elliptic curve that can be used to perform
3555
+ * Diffie-Hellman key agreement to derive shared secrets between parties.
3556
+ * It is designed for use with the elliptic curve Diffie-Hellman (ECDH) key agreement scheme.
3557
+ *
3558
+ * @example
3559
+ * ### Creating Key Pairs
3560
+ *
3561
+ * ```ts twoslash
3562
+ * import { X25519 } from 'ox'
3563
+ *
3564
+ * const { privateKey, publicKey } = X25519.createKeyPair()
3565
+ * ```
3566
+ *
3567
+ * @example
3568
+ * ### Deriving Shared Secrets
3569
+ *
3570
+ * ```ts twoslash
3571
+ * import { X25519 } from 'ox'
3572
+ *
3573
+ * const { privateKey: privateKeyA } = X25519.createKeyPair()
3574
+ * const { publicKey: publicKeyB } = X25519.createKeyPair()
3575
+ *
3576
+ * const sharedSecret = X25519.getSharedSecret({
3577
+ * privateKey: privateKeyA,
3578
+ * publicKey: publicKeyB
3579
+ * })
3580
+ * ```
3581
+ *
3582
+ * @category Crypto
3583
+ */
3584
+ export * as X25519 from './core/X25519.js'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ox",
3
3
  "description": "Ethereum Standard Library",
4
- "version": "0.8.0",
4
+ "version": "0.8.2",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",
@@ -173,6 +173,11 @@
173
173
  "import": "./_esm/core/ContractAddress.js",
174
174
  "default": "./_cjs/core/ContractAddress.js"
175
175
  },
176
+ "./Ed25519": {
177
+ "types": "./_types/core/Ed25519.d.ts",
178
+ "import": "./_esm/core/Ed25519.js",
179
+ "default": "./_cjs/core/Ed25519.js"
180
+ },
176
181
  "./Ens": {
177
182
  "types": "./_types/core/Ens.d.ts",
178
183
  "import": "./_esm/core/Ens.js",
@@ -378,6 +383,11 @@
378
383
  "import": "./_esm/core/Withdrawal.js",
379
384
  "default": "./_cjs/core/Withdrawal.js"
380
385
  },
386
+ "./X25519": {
387
+ "types": "./_types/core/X25519.d.ts",
388
+ "import": "./_esm/core/X25519.js",
389
+ "default": "./_cjs/core/X25519.js"
390
+ },
381
391
  "./erc4337/EntryPoint": {
382
392
  "types": "./_types/erc4337/EntryPoint.d.ts",
383
393
  "import": "./_esm/erc4337/EntryPoint.js",
package/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** @internal */
2
- export const version = '0.8.0'
2
+ export const version = '0.8.2'