ox 0.8.1 → 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 (57) hide show
  1. package/CHANGELOG.md +14 -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/P256.js +23 -0
  9. package/_cjs/core/P256.js.map +1 -1
  10. package/_cjs/core/Secp256k1.js +20 -0
  11. package/_cjs/core/Secp256k1.js.map +1 -1
  12. package/_cjs/core/WebCryptoP256.js +31 -0
  13. package/_cjs/core/WebCryptoP256.js.map +1 -1
  14. package/_cjs/core/X25519.js +45 -0
  15. package/_cjs/core/X25519.js.map +1 -0
  16. package/_cjs/index.js +4 -2
  17. package/_cjs/index.js.map +1 -1
  18. package/_cjs/version.js +1 -1
  19. package/_esm/core/Bls.js +109 -0
  20. package/_esm/core/Bls.js.map +1 -1
  21. package/_esm/core/Ed25519.js +121 -0
  22. package/_esm/core/Ed25519.js.map +1 -0
  23. package/_esm/core/P256.js +54 -2
  24. package/_esm/core/P256.js.map +1 -1
  25. package/_esm/core/Secp256k1.js +50 -0
  26. package/_esm/core/Secp256k1.js.map +1 -1
  27. package/_esm/core/WebCryptoP256.js +72 -0
  28. package/_esm/core/WebCryptoP256.js.map +1 -1
  29. package/_esm/core/X25519.js +97 -0
  30. package/_esm/core/X25519.js.map +1 -0
  31. package/_esm/index.js +65 -0
  32. package/_esm/index.js.map +1 -1
  33. package/_esm/version.js +1 -1
  34. package/_types/core/Bls.d.ts +124 -0
  35. package/_types/core/Bls.d.ts.map +1 -1
  36. package/_types/core/Ed25519.d.ts +156 -0
  37. package/_types/core/Ed25519.d.ts.map +1 -0
  38. package/_types/core/P256.d.ts +68 -2
  39. package/_types/core/P256.d.ts.map +1 -1
  40. package/_types/core/Secp256k1.d.ts +67 -0
  41. package/_types/core/Secp256k1.d.ts.map +1 -1
  42. package/_types/core/WebCryptoP256.d.ts +76 -1
  43. package/_types/core/WebCryptoP256.d.ts.map +1 -1
  44. package/_types/core/X25519.d.ts +127 -0
  45. package/_types/core/X25519.d.ts.map +1 -0
  46. package/_types/index.d.ts +65 -0
  47. package/_types/index.d.ts.map +1 -1
  48. package/_types/version.d.ts +1 -1
  49. package/core/Bls.ts +150 -0
  50. package/core/Ed25519.ts +237 -0
  51. package/core/P256.ts +114 -2
  52. package/core/Secp256k1.ts +110 -0
  53. package/core/WebCryptoP256.ts +141 -1
  54. package/core/X25519.ts +202 -0
  55. package/index.ts +67 -0
  56. package/package.json +11 -1
  57. package/version.ts +1 -1
package/core/P256.ts CHANGED
@@ -9,6 +9,54 @@ import * as Entropy from './internal/entropy.js'
9
9
  /** Re-export of noble/curves P256 utilities. */
10
10
  export const noble = secp256r1
11
11
 
12
+ /**
13
+ * Creates a new P256 ECDSA key pair consisting of a private key and its corresponding public key.
14
+ *
15
+ * @example
16
+ * ```ts twoslash
17
+ * import { P256 } from 'ox'
18
+ *
19
+ * const { privateKey, publicKey } = P256.createKeyPair()
20
+ * ```
21
+ *
22
+ * @param options - The options to generate the key pair.
23
+ * @returns The generated key pair containing both private and public keys.
24
+ */
25
+ export function createKeyPair<as extends 'Hex' | 'Bytes' = 'Hex'>(
26
+ options: createKeyPair.Options<as> = {},
27
+ ): createKeyPair.ReturnType<as> {
28
+ const { as = 'Hex' } = options
29
+ const privateKey = randomPrivateKey({ as })
30
+ const publicKey = getPublicKey({ privateKey })
31
+
32
+ return {
33
+ privateKey: privateKey as never,
34
+ publicKey,
35
+ }
36
+ }
37
+
38
+ export declare namespace createKeyPair {
39
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
40
+ /**
41
+ * Format of the returned private key.
42
+ * @default 'Hex'
43
+ */
44
+ as?: as | 'Hex' | 'Bytes' | undefined
45
+ }
46
+
47
+ type ReturnType<as extends 'Hex' | 'Bytes'> = {
48
+ privateKey:
49
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
50
+ | (as extends 'Hex' ? Hex.Hex : never)
51
+ publicKey: PublicKey.PublicKey
52
+ }
53
+
54
+ type ErrorType =
55
+ | Hex.fromBytes.ErrorType
56
+ | PublicKey.from.ErrorType
57
+ | Errors.GlobalErrorType
58
+ }
59
+
12
60
  /**
13
61
  * Computes the P256 ECDSA public key from a provided private key.
14
62
  *
@@ -45,6 +93,71 @@ export declare namespace getPublicKey {
45
93
  type ErrorType = Errors.GlobalErrorType
46
94
  }
47
95
 
96
+ /**
97
+ * Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key.
98
+ *
99
+ * @example
100
+ * ```ts twoslash
101
+ * import { P256 } from 'ox'
102
+ *
103
+ * const { privateKey: privateKeyA } = P256.createKeyPair()
104
+ * const { publicKey: publicKeyB } = P256.createKeyPair()
105
+ *
106
+ * const sharedSecret = P256.getSharedSecret({
107
+ * privateKey: privateKeyA,
108
+ * publicKey: publicKeyB
109
+ * })
110
+ * ```
111
+ *
112
+ * @param options - The options to compute the shared secret.
113
+ * @returns The computed shared secret.
114
+ */
115
+ export function getSharedSecret<as extends 'Hex' | 'Bytes' = 'Hex'>(
116
+ options: getSharedSecret.Options<as>,
117
+ ): getSharedSecret.ReturnType<as> {
118
+ const { as = 'Hex', privateKey, publicKey } = options
119
+ const point = secp256r1.ProjectivePoint.fromHex(
120
+ PublicKey.toHex(publicKey).slice(2),
121
+ )
122
+ const privateKeyHex =
123
+ typeof privateKey === 'string'
124
+ ? privateKey.slice(2)
125
+ : Hex.fromBytes(privateKey).slice(2)
126
+ const sharedPoint = point.multiply(
127
+ secp256r1.utils.normPrivateKeyToScalar(privateKeyHex),
128
+ )
129
+ const sharedSecret = sharedPoint.toRawBytes(true) // compressed format
130
+ if (as === 'Hex') return Hex.fromBytes(sharedSecret) as never
131
+ return sharedSecret as never
132
+ }
133
+
134
+ export declare namespace getSharedSecret {
135
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
136
+ /**
137
+ * Format of the returned shared secret.
138
+ * @default 'Hex'
139
+ */
140
+ as?: as | 'Hex' | 'Bytes' | undefined
141
+ /**
142
+ * Private key to use for the shared secret computation.
143
+ */
144
+ privateKey: Hex.Hex | Bytes.Bytes
145
+ /**
146
+ * Public key to use for the shared secret computation.
147
+ */
148
+ publicKey: PublicKey.PublicKey<boolean>
149
+ }
150
+
151
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
152
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
153
+ | (as extends 'Hex' ? Hex.Hex : never)
154
+
155
+ type ErrorType =
156
+ | Hex.fromBytes.ErrorType
157
+ | PublicKey.toHex.ErrorType
158
+ | Errors.GlobalErrorType
159
+ }
160
+
48
161
  /**
49
162
  * Generates a random P256 ECDSA private key.
50
163
  *
@@ -204,8 +317,7 @@ export declare namespace sign {
204
317
  * ```ts twoslash
205
318
  * import { P256 } from 'ox'
206
319
  *
207
- * const privateKey = P256.randomPrivateKey()
208
- * const publicKey = P256.getPublicKey({ privateKey })
320
+ * const { privateKey, publicKey } = P256.createKeyPair()
209
321
  * const signature = P256.sign({ payload: '0xdeadbeef', privateKey })
210
322
  *
211
323
  * const verified = P256.verify({ // [!code focus]
package/core/Secp256k1.ts CHANGED
@@ -11,6 +11,54 @@ import type { OneOf } from './internal/types.js'
11
11
  /** Re-export of noble/curves secp256k1 utilities. */
12
12
  export const noble = secp256k1
13
13
 
14
+ /**
15
+ * Creates a new secp256k1 ECDSA key pair consisting of a private key and its corresponding public key.
16
+ *
17
+ * @example
18
+ * ```ts twoslash
19
+ * import { Secp256k1 } from 'ox'
20
+ *
21
+ * const { privateKey, publicKey } = Secp256k1.createKeyPair()
22
+ * ```
23
+ *
24
+ * @param options - The options to generate the key pair.
25
+ * @returns The generated key pair containing both private and public keys.
26
+ */
27
+ export function createKeyPair<as extends 'Hex' | 'Bytes' = 'Hex'>(
28
+ options: createKeyPair.Options<as> = {},
29
+ ): createKeyPair.ReturnType<as> {
30
+ const { as = 'Hex' } = options
31
+ const privateKey = randomPrivateKey({ as })
32
+ const publicKey = getPublicKey({ privateKey })
33
+
34
+ return {
35
+ privateKey: privateKey as never,
36
+ publicKey,
37
+ }
38
+ }
39
+
40
+ export declare namespace createKeyPair {
41
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
42
+ /**
43
+ * Format of the returned private key.
44
+ * @default 'Hex'
45
+ */
46
+ as?: as | 'Hex' | 'Bytes' | undefined
47
+ }
48
+
49
+ type ReturnType<as extends 'Hex' | 'Bytes'> = {
50
+ privateKey:
51
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
52
+ | (as extends 'Hex' ? Hex.Hex : never)
53
+ publicKey: PublicKey.PublicKey
54
+ }
55
+
56
+ type ErrorType =
57
+ | Hex.fromBytes.ErrorType
58
+ | PublicKey.from.ErrorType
59
+ | Errors.GlobalErrorType
60
+ }
61
+
14
62
  /**
15
63
  * Computes the secp256k1 ECDSA public key from a provided private key.
16
64
  *
@@ -48,6 +96,68 @@ export declare namespace getPublicKey {
48
96
  | Errors.GlobalErrorType
49
97
  }
50
98
 
99
+ /**
100
+ * Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key.
101
+ *
102
+ * @example
103
+ * ```ts twoslash
104
+ * import { Secp256k1 } from 'ox'
105
+ *
106
+ * const { privateKey: privateKeyA } = Secp256k1.createKeyPair()
107
+ * const { publicKey: publicKeyB } = Secp256k1.createKeyPair()
108
+ *
109
+ * const sharedSecret = Secp256k1.getSharedSecret({
110
+ * privateKey: privateKeyA,
111
+ * publicKey: publicKeyB
112
+ * })
113
+ * ```
114
+ *
115
+ * @param options - The options to compute the shared secret.
116
+ * @returns The computed shared secret.
117
+ */
118
+ export function getSharedSecret<as extends 'Hex' | 'Bytes' = 'Hex'>(
119
+ options: getSharedSecret.Options<as>,
120
+ ): getSharedSecret.ReturnType<as> {
121
+ const { as = 'Hex', privateKey, publicKey } = options
122
+ const point = secp256k1.ProjectivePoint.fromHex(
123
+ PublicKey.toHex(publicKey).slice(2),
124
+ )
125
+ const sharedPoint = point.multiply(
126
+ secp256k1.utils.normPrivateKeyToScalar(Hex.from(privateKey).slice(2)),
127
+ )
128
+ const sharedSecret = sharedPoint.toRawBytes(true) // compressed format
129
+ if (as === 'Hex') return Hex.fromBytes(sharedSecret) as never
130
+ return sharedSecret as never
131
+ }
132
+
133
+ export declare namespace getSharedSecret {
134
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
135
+ /**
136
+ * Format of the returned shared secret.
137
+ * @default 'Hex'
138
+ */
139
+ as?: as | 'Hex' | 'Bytes' | undefined
140
+ /**
141
+ * Private key to use for the shared secret computation.
142
+ */
143
+ privateKey: Hex.Hex | Bytes.Bytes
144
+ /**
145
+ * Public key to use for the shared secret computation.
146
+ */
147
+ publicKey: PublicKey.PublicKey<boolean>
148
+ }
149
+
150
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
151
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
152
+ | (as extends 'Hex' ? Hex.Hex : never)
153
+
154
+ type ErrorType =
155
+ | Hex.from.ErrorType
156
+ | PublicKey.toHex.ErrorType
157
+ | Hex.fromBytes.ErrorType
158
+ | Errors.GlobalErrorType
159
+ }
160
+
51
161
  /**
52
162
  * Generates a random ECDSA private key on the secp256k1 curve.
53
163
  *
@@ -1,7 +1,7 @@
1
1
  import { p256 } from '@noble/curves/p256'
2
2
  import * as Bytes from './Bytes.js'
3
3
  import type * as Errors from './Errors.js'
4
- import type * as Hex from './Hex.js'
4
+ import * as Hex from './Hex.js'
5
5
  import * as PublicKey from './PublicKey.js'
6
6
  import type * as Signature from './Signature.js'
7
7
  import type { Compute } from './internal/types.js'
@@ -68,6 +68,146 @@ export declare namespace createKeyPair {
68
68
  type ErrorType = PublicKey.from.ErrorType | Errors.GlobalErrorType
69
69
  }
70
70
 
71
+ /**
72
+ * Generates an ECDH P256 key pair for key agreement that includes:
73
+ *
74
+ * - a `privateKey` of type [`CryptoKey`](https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey)
75
+ * - a `publicKey` of type {@link ox#PublicKey.PublicKey}
76
+ *
77
+ * @example
78
+ * ```ts twoslash
79
+ * import { WebCryptoP256 } from 'ox'
80
+ *
81
+ * const { publicKey, privateKey } = await WebCryptoP256.createKeyPairECDH()
82
+ * // @log: {
83
+ * // @log: privateKey: CryptoKey {},
84
+ * // @log: publicKey: {
85
+ * // @log: x: 59295962801117472859457908919941473389380284132224861839820747729565200149877n,
86
+ * // @log: y: 24099691209996290925259367678540227198235484593389470330605641003500238088869n,
87
+ * // @log: prefix: 4,
88
+ * // @log: },
89
+ * // @log: }
90
+ * ```
91
+ *
92
+ * @param options - Options for creating the key pair.
93
+ * @returns The key pair.
94
+ */
95
+ export async function createKeyPairECDH(
96
+ options: createKeyPairECDH.Options = {},
97
+ ): Promise<createKeyPairECDH.ReturnType> {
98
+ const { extractable = false } = options
99
+ const keypair = await globalThis.crypto.subtle.generateKey(
100
+ {
101
+ name: 'ECDH',
102
+ namedCurve: 'P-256',
103
+ },
104
+ extractable,
105
+ ['deriveKey', 'deriveBits'],
106
+ )
107
+ const publicKey_raw = await globalThis.crypto.subtle.exportKey(
108
+ 'raw',
109
+ keypair.publicKey,
110
+ )
111
+ const publicKey = PublicKey.from(new Uint8Array(publicKey_raw))
112
+ return {
113
+ privateKey: keypair.privateKey,
114
+ publicKey,
115
+ }
116
+ }
117
+
118
+ export declare namespace createKeyPairECDH {
119
+ type Options = {
120
+ /** A boolean value indicating whether it will be possible to export the private key using `globalThis.crypto.subtle.exportKey()`. */
121
+ extractable?: boolean | undefined
122
+ }
123
+
124
+ type ReturnType = Compute<{
125
+ privateKey: CryptoKey
126
+ publicKey: PublicKey.PublicKey
127
+ }>
128
+
129
+ type ErrorType = PublicKey.from.ErrorType | Errors.GlobalErrorType
130
+ }
131
+
132
+ /**
133
+ * Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key using Web Crypto APIs.
134
+ *
135
+ * @example
136
+ * ```ts twoslash
137
+ * import { WebCryptoP256 } from 'ox'
138
+ *
139
+ * const { privateKey: privateKeyA } = await WebCryptoP256.createKeyPairECDH()
140
+ * const { publicKey: publicKeyB } = await WebCryptoP256.createKeyPairECDH()
141
+ *
142
+ * const sharedSecret = await WebCryptoP256.getSharedSecret({
143
+ * privateKey: privateKeyA,
144
+ * publicKey: publicKeyB
145
+ * })
146
+ * ```
147
+ *
148
+ * @param options - The options to compute the shared secret.
149
+ * @returns The computed shared secret.
150
+ */
151
+ export async function getSharedSecret<as extends 'Hex' | 'Bytes' = 'Hex'>(
152
+ options: getSharedSecret.Options<as>,
153
+ ): Promise<getSharedSecret.ReturnType<as>> {
154
+ const { as = 'Hex', privateKey, publicKey } = options
155
+
156
+ if (privateKey.algorithm.name === 'ECDSA') {
157
+ throw new Error(
158
+ 'privateKey is not compatible with ECDH. please use `createKeyPairECDH` to create an ECDH key.',
159
+ )
160
+ }
161
+
162
+ const publicKeyCrypto = await globalThis.crypto.subtle.importKey(
163
+ 'raw',
164
+ PublicKey.toBytes(publicKey),
165
+ { name: 'ECDH', namedCurve: 'P-256' },
166
+ false,
167
+ [],
168
+ )
169
+
170
+ const sharedSecretBuffer = await globalThis.crypto.subtle.deriveBits(
171
+ {
172
+ name: 'ECDH',
173
+ public: publicKeyCrypto,
174
+ },
175
+ privateKey,
176
+ 256, // 32 bytes * 8 bits/byte
177
+ )
178
+
179
+ const sharedSecret = new Uint8Array(sharedSecretBuffer)
180
+ if (as === 'Hex') return Hex.fromBytes(sharedSecret) as never
181
+ return sharedSecret as never
182
+ }
183
+
184
+ export declare namespace getSharedSecret {
185
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
186
+ /**
187
+ * Format of the returned shared secret.
188
+ * @default 'Hex'
189
+ */
190
+ as?: as | 'Hex' | 'Bytes' | undefined
191
+ /**
192
+ * Private key to use for the shared secret computation (must be a CryptoKey for ECDH).
193
+ */
194
+ privateKey: CryptoKey
195
+ /**
196
+ * Public key to use for the shared secret computation.
197
+ */
198
+ publicKey: PublicKey.PublicKey<boolean>
199
+ }
200
+
201
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
202
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
203
+ | (as extends 'Hex' ? Hex.Hex : never)
204
+
205
+ type ErrorType =
206
+ | PublicKey.toBytes.ErrorType
207
+ | Hex.fromBytes.ErrorType
208
+ | Errors.GlobalErrorType
209
+ }
210
+
71
211
  /**
72
212
  * Signs a payload with the provided `CryptoKey` private key and returns a P256 signature.
73
213
  *
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
  *
@@ -3515,3 +3547,38 @@ export * as WebCryptoP256 from './core/WebCryptoP256.js'
3515
3547
  * @category Execution Spec
3516
3548
  */
3517
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'