ox 1.6.2 → 1.7.0

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 (80) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/core/AesGcm.d.ts +67 -2
  3. package/dist/core/AesGcm.d.ts.map +1 -1
  4. package/dist/core/AesGcm.js +79 -10
  5. package/dist/core/AesGcm.js.map +1 -1
  6. package/dist/core/Ed25519.d.ts +83 -2
  7. package/dist/core/Ed25519.d.ts.map +1 -1
  8. package/dist/core/Ed25519.js +79 -4
  9. package/dist/core/Ed25519.js.map +1 -1
  10. package/dist/core/MlDsa44.d.ts +84 -5
  11. package/dist/core/MlDsa44.d.ts.map +1 -1
  12. package/dist/core/MlDsa44.js +79 -4
  13. package/dist/core/MlDsa44.js.map +1 -1
  14. package/dist/core/P256.d.ts +82 -1
  15. package/dist/core/P256.d.ts.map +1 -1
  16. package/dist/core/P256.js +78 -0
  17. package/dist/core/P256.js.map +1 -1
  18. package/dist/core/Secp256k1.d.ts +85 -1
  19. package/dist/core/Secp256k1.d.ts.map +1 -1
  20. package/dist/core/Secp256k1.js +74 -3
  21. package/dist/core/Secp256k1.js.map +1 -1
  22. package/dist/core/internal/keyDerivation.d.ts +13 -0
  23. package/dist/core/internal/keyDerivation.d.ts.map +1 -0
  24. package/dist/core/internal/keyDerivation.js +13 -0
  25. package/dist/core/internal/keyDerivation.js.map +1 -0
  26. package/dist/tempo/KeyAuthorization.d.ts +16 -12
  27. package/dist/tempo/KeyAuthorization.d.ts.map +1 -1
  28. package/dist/tempo/KeyAuthorization.js +7 -13
  29. package/dist/tempo/KeyAuthorization.js.map +1 -1
  30. package/dist/tempo/MultisigConfig.d.ts +20 -17
  31. package/dist/tempo/MultisigConfig.d.ts.map +1 -1
  32. package/dist/tempo/MultisigConfig.js +17 -16
  33. package/dist/tempo/MultisigConfig.js.map +1 -1
  34. package/dist/tempo/SignatureEnvelope.d.ts +19 -17
  35. package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
  36. package/dist/tempo/SignatureEnvelope.js +35 -27
  37. package/dist/tempo/SignatureEnvelope.js.map +1 -1
  38. package/dist/tempo/index.d.ts +2 -2
  39. package/dist/tempo/index.js +2 -2
  40. package/dist/zod/tempo/KeyAuthorization.d.ts +231 -22
  41. package/dist/zod/tempo/KeyAuthorization.d.ts.map +1 -1
  42. package/dist/zod/tempo/KeyAuthorization.js +10 -2
  43. package/dist/zod/tempo/KeyAuthorization.js.map +1 -1
  44. package/dist/zod/tempo/RpcSchemaTempo.d.ts +82 -8
  45. package/dist/zod/tempo/RpcSchemaTempo.d.ts.map +1 -1
  46. package/dist/zod/tempo/Transaction.d.ts +246 -24
  47. package/dist/zod/tempo/Transaction.d.ts.map +1 -1
  48. package/dist/zod/tempo/TransactionRequest.d.ts +149 -14
  49. package/dist/zod/tempo/TransactionRequest.d.ts.map +1 -1
  50. package/dist/zod/tempo/TxEnvelopeTempo.d.ts +78 -6
  51. package/dist/zod/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  52. package/package.json +1 -1
  53. package/src/core/AesGcm.ts +137 -30
  54. package/src/core/Ed25519.ts +135 -4
  55. package/src/core/MlDsa44.ts +135 -4
  56. package/src/core/P256.ts +135 -1
  57. package/src/core/Secp256k1.ts +134 -3
  58. package/src/core/_test/AesGcm.test-d.ts +19 -0
  59. package/src/core/_test/AesGcm.test.ts +90 -1
  60. package/src/core/_test/Ed25519.test-d.ts +24 -0
  61. package/src/core/_test/Ed25519.test.ts +87 -1
  62. package/src/core/_test/MlDsa44.test-d.ts +24 -0
  63. package/src/core/_test/MlDsa44.test.ts +69 -1
  64. package/src/core/_test/P256.test-d.ts +26 -0
  65. package/src/core/_test/P256.test.ts +98 -1
  66. package/src/core/_test/Secp256k1.test-d.ts +27 -0
  67. package/src/core/_test/Secp256k1.test.ts +112 -1
  68. package/src/core/internal/keyDerivation.ts +34 -0
  69. package/src/tempo/KeyAuthorization.test-d.ts +41 -6
  70. package/src/tempo/KeyAuthorization.test.ts +77 -18
  71. package/src/tempo/KeyAuthorization.ts +36 -36
  72. package/src/tempo/MultisigConfig.test.ts +28 -8
  73. package/src/tempo/MultisigConfig.ts +28 -19
  74. package/src/tempo/SignatureEnvelope.test.ts +64 -19
  75. package/src/tempo/SignatureEnvelope.ts +45 -35
  76. package/src/tempo/index.ts +2 -2
  77. package/src/tempo/multisig.e2e.test.ts +402 -145
  78. package/src/version.ts +1 -1
  79. package/src/zod/tempo/KeyAuthorization.ts +12 -2
  80. package/src/zod/tempo/_test/KeyAuthorization.test.ts +19 -5
@@ -1,6 +1,7 @@
1
1
  import * as Bytes from './Bytes.js'
2
2
  import * as Errors from './Errors.js'
3
3
  import * as Hex from './Hex.js'
4
+ import * as mnemonic_ from './internal/mnemonic.js'
4
5
 
5
6
  export const ivLength = 16
6
7
 
@@ -138,8 +139,8 @@ export declare namespace encrypt {
138
139
  * Derives an AES-256-GCM key from a 32-byte WebAuthn PRF output.
139
140
  *
140
141
  * The permanent derivation contract uses the PRF output as the HMAC-SHA256
141
- * key. Its message is the UTF-8 bytes of `ox.aesGcm.fromPrf.v1` followed by
142
- * a 32-bit big-endian counter set to zero.
142
+ * key. The HMAC message uses the `ox.aesGcm.fromPrf.v1` domain followed by a
143
+ * 32-bit big-endian counter set to zero.
143
144
  *
144
145
  * @example
145
146
  * ```ts twoslash
@@ -158,43 +159,95 @@ export async function fromPrf(
158
159
  ): Promise<CryptoKey> {
159
160
  const bytes = Bytes.from(value)
160
161
  if (bytes.length !== 32) throw new InvalidPrfSizeError({ size: bytes.length })
162
+ return deriveKey(bytes, fromPrfDomain)
163
+ }
161
164
 
162
- const prfKey = await globalThis.crypto.subtle.importKey(
163
- 'raw',
164
- bytes,
165
- {
166
- name: 'HMAC',
167
- hash: 'SHA-256',
168
- },
169
- false,
170
- ['sign'],
171
- )
172
- const key = new Uint8Array(
173
- await globalThis.crypto.subtle.sign(
174
- 'HMAC',
175
- prfKey,
176
- Bytes.concat(fromPrfLabel, Bytes.fromNumber(0, { size: 4 })),
177
- ),
178
- )
165
+ export declare namespace fromPrf {
166
+ type ErrorType =
167
+ | Bytes.concat.ErrorType
168
+ | Bytes.from.ErrorType
169
+ | Bytes.fromNumber.ErrorType
170
+ | InvalidPrfSizeError
171
+ | Errors.GlobalErrorType
172
+ }
173
+
174
+ /**
175
+ * Derives an AES-256-GCM key from a BIP-39 mnemonic.
176
+ *
177
+ * This is equivalent to passing `Mnemonic.toSeed(mnemonic, { passphrase })`
178
+ * to {@link ox#AesGcm.fromSeed}.
179
+ *
180
+ * @example
181
+ * ```ts twoslash
182
+ * import { AesGcm } from 'ox'
183
+ *
184
+ * const key = await AesGcm.fromMnemonic(
185
+ * 'test test test test test test test test test test test junk'
186
+ * )
187
+ * ```
188
+ *
189
+ * @param mnemonic - BIP-39 mnemonic phrase.
190
+ * @param options - Options.
191
+ * @returns A nonextractable AES-256-GCM key for encryption and decryption.
192
+ */
193
+ export async function fromMnemonic(
194
+ mnemonic: string,
195
+ options: fromMnemonic.Options = {},
196
+ ): Promise<CryptoKey> {
197
+ const { passphrase } = options
198
+ const seed = mnemonic_.toSeed(mnemonic, passphrase)
179
199
  try {
180
- return await globalThis.crypto.subtle.importKey(
181
- 'raw',
182
- key,
183
- { name: 'AES-GCM' },
184
- false,
185
- ['encrypt', 'decrypt'],
186
- )
200
+ return await fromSeed(seed)
187
201
  } finally {
188
- key.fill(0)
202
+ seed.fill(0)
189
203
  }
190
204
  }
191
205
 
192
- export declare namespace fromPrf {
206
+ export declare namespace fromMnemonic {
207
+ type Options = {
208
+ /** Optional BIP-39 passphrase. */
209
+ passphrase?: string | undefined
210
+ }
211
+
212
+ type ErrorType = fromSeed.ErrorType
213
+ }
214
+
215
+ /**
216
+ * Derives an AES-256-GCM key from a seed.
217
+ *
218
+ * The seed must contain at least 32 bytes of cryptographically strong key
219
+ * material. Do not pass a password directly; use a password KDF first.
220
+ *
221
+ * The permanent derivation contract uses the seed as the HMAC-SHA256
222
+ * key. The HMAC message uses the `ox.aesGcm.fromSeed.v1` domain followed by a
223
+ * 32-bit big-endian counter set to zero.
224
+ *
225
+ * @example
226
+ * ```ts twoslash
227
+ * import { AesGcm } from 'ox'
228
+ *
229
+ * const key = await AesGcm.fromSeed(
230
+ * '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
231
+ * )
232
+ * ```
233
+ *
234
+ * @param seed - Seed containing at least 32 bytes of cryptographically strong key material.
235
+ * @returns A nonextractable AES-256-GCM key for encryption and decryption.
236
+ */
237
+ export async function fromSeed(
238
+ seed: Hex.Hex | Bytes.Bytes,
239
+ ): Promise<CryptoKey> {
240
+ const bytes = Bytes.from(seed)
241
+ if (bytes.length < 32) throw new InvalidSeedSizeError({ size: bytes.length })
242
+ return deriveKey(bytes, fromSeedDomain)
243
+ }
244
+
245
+ export declare namespace fromSeed {
193
246
  type ErrorType =
194
247
  | Bytes.concat.ErrorType
195
248
  | Bytes.from.ErrorType
196
249
  | Bytes.fromNumber.ErrorType
197
- | InvalidPrfSizeError
250
+ | InvalidSeedSizeError
198
251
  | Errors.GlobalErrorType
199
252
  }
200
253
 
@@ -290,4 +343,58 @@ export declare namespace InvalidPrfSizeError {
290
343
  }
291
344
  }
292
345
 
293
- const fromPrfLabel = Bytes.fromString('ox.aesGcm.fromPrf.v1')
346
+ /** Thrown when a seed contains fewer than 32 bytes. */
347
+ export class InvalidSeedSizeError extends Errors.BaseError {
348
+ override readonly name = 'AesGcm.InvalidSeedSizeError'
349
+
350
+ constructor(options: InvalidSeedSizeError.Options) {
351
+ super(
352
+ `Seed must contain at least 32 bytes. Received ${options.size} bytes.`,
353
+ )
354
+ }
355
+ }
356
+
357
+ export declare namespace InvalidSeedSizeError {
358
+ /** Options for {@link ox#AesGcm.InvalidSeedSizeError}. */
359
+ type Options = {
360
+ /** Received seed size. */
361
+ size: number
362
+ }
363
+ }
364
+
365
+ async function deriveKey(
366
+ seed: Bytes.Bytes,
367
+ domain: Bytes.Bytes,
368
+ ): Promise<CryptoKey> {
369
+ const baseKey = await globalThis.crypto.subtle.importKey(
370
+ 'raw',
371
+ seed,
372
+ {
373
+ name: 'HMAC',
374
+ hash: 'SHA-256',
375
+ },
376
+ false,
377
+ ['sign'],
378
+ )
379
+ const key = new Uint8Array(
380
+ await globalThis.crypto.subtle.sign(
381
+ 'HMAC',
382
+ baseKey,
383
+ Bytes.concat(domain, Bytes.fromNumber(0, { size: 4 })),
384
+ ),
385
+ )
386
+ try {
387
+ return await globalThis.crypto.subtle.importKey(
388
+ 'raw',
389
+ key,
390
+ { name: 'AES-GCM' },
391
+ false,
392
+ ['encrypt', 'decrypt'],
393
+ )
394
+ } finally {
395
+ key.fill(0)
396
+ }
397
+ }
398
+
399
+ const fromPrfDomain = Bytes.fromString('ox.aesGcm.fromPrf.v1')
400
+ const fromSeedDomain = Bytes.fromString('ox.aesGcm.fromSeed.v1')
@@ -4,6 +4,8 @@ import * as Errors from './Errors.js'
4
4
  import * as Hash from './Hash.js'
5
5
  import * as Hex from './Hex.js'
6
6
  import * as engine from './internal/ed25519.js'
7
+ import * as keyDerivation from './internal/keyDerivation.js'
8
+ import * as mnemonic_ from './internal/mnemonic.js'
7
9
 
8
10
  /** Re-export of noble/curves Ed25519 utilities. */
9
11
  export const noble = ed25519
@@ -63,8 +65,8 @@ export declare namespace createKeyPair {
63
65
  * Derives an Ed25519 private key from a 32-byte WebAuthn PRF output.
64
66
  *
65
67
  * The permanent derivation contract uses the PRF output as the HMAC-SHA256
66
- * key. Its message is the UTF-8 bytes of `ox.ed25519.fromPrf.v1` followed by
67
- * a 32-bit big-endian counter set to zero.
68
+ * key. The HMAC message uses the `ox.ed25519.fromPrf.v1` domain followed by a
69
+ * 32-bit big-endian counter set to zero.
68
70
  *
69
71
  * @example
70
72
  * ```ts twoslash
@@ -89,7 +91,7 @@ export function fromPrf<as extends 'Hex' | 'Bytes' = 'Hex'>(
89
91
 
90
92
  const privateKey = Hash.hmac256(
91
93
  bytes,
92
- Bytes.concat(fromPrfLabel, Bytes.fromNumber(0, { size: 4 })),
94
+ Bytes.concat(fromPrfDomain, Bytes.fromNumber(0, { size: 4 })),
93
95
  { as: 'Bytes' },
94
96
  )
95
97
  if (as === 'Hex') {
@@ -123,6 +125,115 @@ export declare namespace fromPrf {
123
125
  | Errors.GlobalErrorType
124
126
  }
125
127
 
128
+ /**
129
+ * Derives an Ed25519 private key from a BIP-39 mnemonic.
130
+ *
131
+ * This is equivalent to passing `Mnemonic.toSeed(mnemonic, { passphrase })`
132
+ * to {@link ox#Ed25519.fromSeed}.
133
+ *
134
+ * @example
135
+ * ```ts twoslash
136
+ * import { Ed25519 } from 'ox'
137
+ *
138
+ * const privateKey = Ed25519.fromMnemonic(
139
+ * 'test test test test test test test test test test test junk'
140
+ * )
141
+ * ```
142
+ *
143
+ * @param mnemonic - BIP-39 mnemonic phrase.
144
+ * @param options - Options.
145
+ * @returns An Ed25519 private key.
146
+ */
147
+ export function fromMnemonic<as extends 'Hex' | 'Bytes' = 'Hex'>(
148
+ mnemonic: string,
149
+ options: fromMnemonic.Options<as> = {},
150
+ ): fromMnemonic.ReturnType<as> {
151
+ const { passphrase } = options
152
+ const seed = mnemonic_.toSeed(mnemonic, passphrase)
153
+ try {
154
+ return fromSeed(seed, options)
155
+ } finally {
156
+ seed.fill(0)
157
+ }
158
+ }
159
+
160
+ export declare namespace fromMnemonic {
161
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
162
+ /**
163
+ * Format of the returned private key.
164
+ * @default 'Hex'
165
+ */
166
+ as?: as | 'Hex' | 'Bytes' | undefined
167
+ /** Optional BIP-39 passphrase. */
168
+ passphrase?: string | undefined
169
+ }
170
+
171
+ type ReturnType<as extends 'Hex' | 'Bytes'> = fromSeed.ReturnType<as>
172
+
173
+ type ErrorType = fromSeed.ErrorType
174
+ }
175
+
176
+ /**
177
+ * Derives an Ed25519 private key from a seed.
178
+ *
179
+ * The seed must contain at least 32 bytes of cryptographically strong key
180
+ * material. Do not pass a password directly; use a password KDF first.
181
+ *
182
+ * The permanent derivation contract uses the seed as the HMAC-SHA256
183
+ * key. The HMAC message uses the `ox.ed25519.fromSeed.v1` domain followed by a
184
+ * 32-bit big-endian counter set to zero.
185
+ *
186
+ * @example
187
+ * ```ts twoslash
188
+ * import { Ed25519 } from 'ox'
189
+ *
190
+ * const privateKey = Ed25519.fromSeed(
191
+ * '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
192
+ * )
193
+ * ```
194
+ *
195
+ * @param seed - Seed containing at least 32 bytes of cryptographically strong key material.
196
+ * @param options - Options.
197
+ * @returns An Ed25519 private key.
198
+ */
199
+ export function fromSeed<as extends 'Hex' | 'Bytes' = 'Hex'>(
200
+ seed: Hex.Hex | Bytes.Bytes,
201
+ options: fromSeed.Options<as> = {},
202
+ ): fromSeed.ReturnType<as> {
203
+ const { as = 'Hex' } = options
204
+ const bytes = Bytes.from(seed)
205
+ if (bytes.length < 32) throw new InvalidSeedSizeError({ size: bytes.length })
206
+
207
+ const privateKey = keyDerivation.derive(bytes, fromSeedDomain)
208
+ if (as === 'Hex') {
209
+ const value = Hex.fromBytes(privateKey)
210
+ privateKey.fill(0)
211
+ return value as never
212
+ }
213
+ return privateKey as never
214
+ }
215
+
216
+ export declare namespace fromSeed {
217
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
218
+ /**
219
+ * Format of the returned private key.
220
+ * @default 'Hex'
221
+ */
222
+ as?: as | 'Hex' | 'Bytes' | undefined
223
+ }
224
+
225
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
226
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
227
+ | (as extends 'Hex' ? Hex.Hex : never)
228
+
229
+ type ErrorType =
230
+ | Bytes.from.ErrorType
231
+ | Hex.fromBytes.ErrorType
232
+ | keyDerivation.derive.ErrorType
233
+ | InvalidSeedSizeError
234
+ | Errors.GlobalErrorType
235
+ }
236
+
126
237
  /**
127
238
  * Computes the Ed25519 public key from a provided private key.
128
239
  *
@@ -430,4 +541,24 @@ export declare namespace InvalidPrfSizeError {
430
541
  }
431
542
  }
432
543
 
433
- const fromPrfLabel = Bytes.fromString('ox.ed25519.fromPrf.v1')
544
+ /** Thrown when a seed contains fewer than 32 bytes. */
545
+ export class InvalidSeedSizeError extends Errors.BaseError {
546
+ override readonly name = 'Ed25519.InvalidSeedSizeError'
547
+
548
+ constructor(options: InvalidSeedSizeError.Options) {
549
+ super(
550
+ `Seed must contain at least 32 bytes. Received ${options.size} bytes.`,
551
+ )
552
+ }
553
+ }
554
+
555
+ export declare namespace InvalidSeedSizeError {
556
+ /** Options for {@link ox#Ed25519.InvalidSeedSizeError}. */
557
+ type Options = {
558
+ /** Received seed size. */
559
+ size: number
560
+ }
561
+ }
562
+
563
+ const fromPrfDomain = Bytes.fromString('ox.ed25519.fromPrf.v1')
564
+ const fromSeedDomain = Bytes.fromString('ox.ed25519.fromSeed.v1')
@@ -4,7 +4,9 @@ import * as Errors from './Errors.js'
4
4
  import * as Hash from './Hash.js'
5
5
  import * as Hex from './Hex.js'
6
6
  import * as Entropy from './internal/entropy.js'
7
+ import * as keyDerivation from './internal/keyDerivation.js'
7
8
  import * as engine from './internal/mlDsa44.js'
9
+ import * as mnemonic_ from './internal/mnemonic.js'
8
10
 
9
11
  /** Re-export of noble/post-quantum ML-DSA-44 utilities. */
10
12
  export const noble = ml_dsa44
@@ -77,8 +79,8 @@ export declare namespace createKeyPair {
77
79
  * Derives an ML-DSA-44 private key from a 32-byte WebAuthn PRF output.
78
80
  *
79
81
  * The permanent derivation contract uses the PRF output as the HMAC-SHA256
80
- * key. Its message is the UTF-8 bytes of `ox.mldsa44.fromPrf.v1` followed by
81
- * a 32-bit big-endian counter set to zero.
82
+ * key. The HMAC message uses the `ox.mldsa44.fromPrf.v1` domain followed by a
83
+ * 32-bit big-endian counter set to zero.
82
84
  *
83
85
  * @example
84
86
  * ```ts twoslash
@@ -103,7 +105,7 @@ export function fromPrf<as extends 'Hex' | 'Bytes' = 'Hex'>(
103
105
 
104
106
  const privateKey = Hash.hmac256(
105
107
  bytes,
106
- Bytes.concat(fromPrfLabel, Bytes.fromNumber(0, { size: 4 })),
108
+ Bytes.concat(fromPrfDomain, Bytes.fromNumber(0, { size: 4 })),
107
109
  { as: 'Bytes' },
108
110
  )
109
111
  if (as === 'Hex') {
@@ -137,6 +139,115 @@ export declare namespace fromPrf {
137
139
  | Errors.GlobalErrorType
138
140
  }
139
141
 
142
+ /**
143
+ * Derives an ML-DSA-44 private key from a BIP-39 mnemonic.
144
+ *
145
+ * This is equivalent to passing `Mnemonic.toSeed(mnemonic, { passphrase })`
146
+ * to {@link ox#MlDsa44.fromSeed}.
147
+ *
148
+ * @example
149
+ * ```ts twoslash
150
+ * import { MlDsa44 } from 'ox'
151
+ *
152
+ * const privateKey = MlDsa44.fromMnemonic(
153
+ * 'test test test test test test test test test test test junk'
154
+ * )
155
+ * ```
156
+ *
157
+ * @param mnemonic - BIP-39 mnemonic phrase.
158
+ * @param options - Options.
159
+ * @returns An ML-DSA-44 private key (32-byte seed).
160
+ */
161
+ export function fromMnemonic<as extends 'Hex' | 'Bytes' = 'Hex'>(
162
+ mnemonic: string,
163
+ options: fromMnemonic.Options<as> = {},
164
+ ): fromMnemonic.ReturnType<as> {
165
+ const { passphrase } = options
166
+ const seed = mnemonic_.toSeed(mnemonic, passphrase)
167
+ try {
168
+ return fromSeed(seed, options)
169
+ } finally {
170
+ seed.fill(0)
171
+ }
172
+ }
173
+
174
+ export declare namespace fromMnemonic {
175
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
176
+ /**
177
+ * Format of the returned private key.
178
+ * @default 'Hex'
179
+ */
180
+ as?: as | 'Hex' | 'Bytes' | undefined
181
+ /** Optional BIP-39 passphrase. */
182
+ passphrase?: string | undefined
183
+ }
184
+
185
+ type ReturnType<as extends 'Hex' | 'Bytes'> = fromSeed.ReturnType<as>
186
+
187
+ type ErrorType = fromSeed.ErrorType
188
+ }
189
+
190
+ /**
191
+ * Derives an ML-DSA-44 private key from a seed.
192
+ *
193
+ * The seed must contain at least 32 bytes of cryptographically strong key
194
+ * material. Do not pass a password directly; use a password KDF first.
195
+ *
196
+ * The permanent derivation contract uses the seed as the HMAC-SHA256
197
+ * key. The HMAC message uses the `ox.mldsa44.fromSeed.v1` domain followed by a
198
+ * 32-bit big-endian counter set to zero.
199
+ *
200
+ * @example
201
+ * ```ts twoslash
202
+ * import { MlDsa44 } from 'ox'
203
+ *
204
+ * const privateKey = MlDsa44.fromSeed(
205
+ * '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
206
+ * )
207
+ * ```
208
+ *
209
+ * @param seed - Seed containing at least 32 bytes of cryptographically strong key material.
210
+ * @param options - Options.
211
+ * @returns An ML-DSA-44 private key (32-byte seed).
212
+ */
213
+ export function fromSeed<as extends 'Hex' | 'Bytes' = 'Hex'>(
214
+ seed: Hex.Hex | Bytes.Bytes,
215
+ options: fromSeed.Options<as> = {},
216
+ ): fromSeed.ReturnType<as> {
217
+ const { as = 'Hex' } = options
218
+ const bytes = Bytes.from(seed)
219
+ if (bytes.length < 32) throw new InvalidSeedSizeError({ size: bytes.length })
220
+
221
+ const privateKey = keyDerivation.derive(bytes, fromSeedDomain)
222
+ if (as === 'Hex') {
223
+ const value = Hex.fromBytes(privateKey)
224
+ privateKey.fill(0)
225
+ return value as never
226
+ }
227
+ return privateKey as never
228
+ }
229
+
230
+ export declare namespace fromSeed {
231
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
232
+ /**
233
+ * Format of the returned private key.
234
+ * @default 'Hex'
235
+ */
236
+ as?: as | 'Hex' | 'Bytes' | undefined
237
+ }
238
+
239
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
240
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
241
+ | (as extends 'Hex' ? Hex.Hex : never)
242
+
243
+ type ErrorType =
244
+ | Bytes.from.ErrorType
245
+ | Hex.fromBytes.ErrorType
246
+ | keyDerivation.derive.ErrorType
247
+ | InvalidSeedSizeError
248
+ | Errors.GlobalErrorType
249
+ }
250
+
140
251
  /**
141
252
  * Computes the ML-DSA-44 public key from a provided private key.
142
253
  *
@@ -402,6 +513,25 @@ export declare namespace InvalidPrfSizeError {
402
513
  }
403
514
  }
404
515
 
516
+ /** Thrown when a seed contains fewer than 32 bytes. */
517
+ export class InvalidSeedSizeError extends Errors.BaseError {
518
+ override readonly name = 'MlDsa44.InvalidSeedSizeError'
519
+
520
+ constructor(options: InvalidSeedSizeError.Options) {
521
+ super(
522
+ `Seed must contain at least 32 bytes. Received ${options.size} bytes.`,
523
+ )
524
+ }
525
+ }
526
+
527
+ export declare namespace InvalidSeedSizeError {
528
+ /** Options for {@link ox#MlDsa44.InvalidSeedSizeError}. */
529
+ type Options = {
530
+ /** Received seed size. */
531
+ size: number
532
+ }
533
+ }
534
+
405
535
  function toContextBytes(
406
536
  context: Hex.Hex | Bytes.Bytes | undefined,
407
537
  ): Bytes.Bytes | undefined {
@@ -412,4 +542,5 @@ function toContextBytes(
412
542
  return bytes
413
543
  }
414
544
 
415
- const fromPrfLabel = Bytes.fromString('ox.mldsa44.fromPrf.v1')
545
+ const fromPrfDomain = Bytes.fromString('ox.mldsa44.fromPrf.v1')
546
+ const fromSeedDomain = Bytes.fromString('ox.mldsa44.fromSeed.v1')
package/src/core/P256.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { p256 as noble_p256 } from '@noble/curves/nist.js'
2
2
  import * as Bytes from './Bytes.js'
3
- import type * as Errors from './Errors.js'
3
+ import * as Errors from './Errors.js'
4
4
  import * as Hex from './Hex.js'
5
5
  import {
6
6
  formatPublicKey,
@@ -8,6 +8,8 @@ import {
8
8
  normalizePublicKey,
9
9
  normalizeSignature,
10
10
  } from './internal/cryptoIo.js'
11
+ import * as keyDerivation from './internal/keyDerivation.js'
12
+ import * as mnemonic_ from './internal/mnemonic.js'
11
13
  import * as engine from './internal/p256.js'
12
14
  import * as Entropy from './internal/entropy.js'
13
15
  import {
@@ -69,6 +71,117 @@ export declare namespace createKeyPair {
69
71
  | Errors.GlobalErrorType
70
72
  }
71
73
 
74
+ /**
75
+ * Derives a valid P256 private key from a BIP-39 mnemonic.
76
+ *
77
+ * This is equivalent to passing `Mnemonic.toSeed(mnemonic, { passphrase })`
78
+ * to {@link ox#P256.fromSeed}.
79
+ *
80
+ * @example
81
+ * ```ts twoslash
82
+ * import { P256 } from 'ox'
83
+ *
84
+ * const privateKey = P256.fromMnemonic(
85
+ * 'test test test test test test test test test test test junk'
86
+ * )
87
+ * ```
88
+ *
89
+ * @param mnemonic - BIP-39 mnemonic phrase.
90
+ * @param options - Options.
91
+ * @returns A valid P256 private key.
92
+ */
93
+ export function fromMnemonic<as extends 'Hex' | 'Bytes' = 'Hex'>(
94
+ mnemonic: string,
95
+ options: fromMnemonic.Options<as> = {},
96
+ ): fromMnemonic.ReturnType<as> {
97
+ const { passphrase } = options
98
+ const seed = mnemonic_.toSeed(mnemonic, passphrase)
99
+ try {
100
+ return fromSeed(seed, options)
101
+ } finally {
102
+ seed.fill(0)
103
+ }
104
+ }
105
+
106
+ export declare namespace fromMnemonic {
107
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
108
+ /**
109
+ * Format of the returned private key.
110
+ * @default 'Hex'
111
+ */
112
+ as?: as | 'Hex' | 'Bytes' | undefined
113
+ /** Optional BIP-39 passphrase. */
114
+ passphrase?: string | undefined
115
+ }
116
+
117
+ type ReturnType<as extends 'Hex' | 'Bytes'> = fromSeed.ReturnType<as>
118
+
119
+ type ErrorType = fromSeed.ErrorType
120
+ }
121
+
122
+ /**
123
+ * Derives a valid P256 private key from a seed.
124
+ *
125
+ * The seed must contain at least 32 bytes of cryptographically strong key
126
+ * material. Do not pass a password directly; use a password KDF first.
127
+ *
128
+ * The permanent derivation contract uses the seed as the HMAC-SHA256
129
+ * key. The HMAC message uses the `ox.p256.fromSeed.v1` domain followed by a
130
+ * 32-bit big-endian counter starting at zero. Invalid scalars are skipped.
131
+ *
132
+ * @example
133
+ * ```ts twoslash
134
+ * import { P256 } from 'ox'
135
+ *
136
+ * const privateKey = P256.fromSeed(
137
+ * '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
138
+ * )
139
+ * ```
140
+ *
141
+ * @param seed - Seed containing at least 32 bytes of cryptographically strong key material.
142
+ * @param options - Options.
143
+ * @returns A valid P256 private key.
144
+ */
145
+ export function fromSeed<as extends 'Hex' | 'Bytes' = 'Hex'>(
146
+ seed: Hex.Hex | Bytes.Bytes,
147
+ options: fromSeed.Options<as> = {},
148
+ ): fromSeed.ReturnType<as> {
149
+ const { as = 'Hex' } = options
150
+ const bytes = Bytes.from(seed)
151
+ if (bytes.length < 32) throw new InvalidSeedSizeError({ size: bytes.length })
152
+
153
+ const privateKey = keyDerivation.derive(bytes, fromSeedDomain, {
154
+ validate: noble.utils.isValidSecretKey,
155
+ })
156
+ if (as === 'Hex') {
157
+ const value = Hex.fromBytes(privateKey)
158
+ privateKey.fill(0)
159
+ return value as never
160
+ }
161
+ return privateKey as never
162
+ }
163
+
164
+ export declare namespace fromSeed {
165
+ type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
166
+ /**
167
+ * Format of the returned private key.
168
+ * @default 'Hex'
169
+ */
170
+ as?: as | 'Hex' | 'Bytes' | undefined
171
+ }
172
+
173
+ type ReturnType<as extends 'Hex' | 'Bytes'> =
174
+ | (as extends 'Bytes' ? Bytes.Bytes : never)
175
+ | (as extends 'Hex' ? Hex.Hex : never)
176
+
177
+ type ErrorType =
178
+ | Bytes.from.ErrorType
179
+ | Hex.fromBytes.ErrorType
180
+ | keyDerivation.derive.ErrorType
181
+ | InvalidSeedSizeError
182
+ | Errors.GlobalErrorType
183
+ }
184
+
72
185
  /**
73
186
  * Computes the P256 ECDSA public key from a provided private key.
74
187
  *
@@ -404,3 +517,24 @@ export declare namespace verify {
404
517
 
405
518
  type ErrorType = Errors.GlobalErrorType
406
519
  }
520
+
521
+ /** Thrown when a seed contains fewer than 32 bytes. */
522
+ export class InvalidSeedSizeError extends Errors.BaseError {
523
+ override readonly name = 'P256.InvalidSeedSizeError'
524
+
525
+ constructor(options: InvalidSeedSizeError.Options) {
526
+ super(
527
+ `Seed must contain at least 32 bytes. Received ${options.size} bytes.`,
528
+ )
529
+ }
530
+ }
531
+
532
+ export declare namespace InvalidSeedSizeError {
533
+ /** Options for {@link ox#P256.InvalidSeedSizeError}. */
534
+ type Options = {
535
+ /** Received seed size. */
536
+ size: number
537
+ }
538
+ }
539
+
540
+ const fromSeedDomain = Bytes.fromString('ox.p256.fromSeed.v1')