ox 1.6.3 → 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.
- package/CHANGELOG.md +6 -0
- package/dist/core/AesGcm.d.ts +67 -2
- package/dist/core/AesGcm.d.ts.map +1 -1
- package/dist/core/AesGcm.js +79 -10
- package/dist/core/AesGcm.js.map +1 -1
- package/dist/core/Ed25519.d.ts +83 -2
- package/dist/core/Ed25519.d.ts.map +1 -1
- package/dist/core/Ed25519.js +79 -4
- package/dist/core/Ed25519.js.map +1 -1
- package/dist/core/MlDsa44.d.ts +84 -5
- package/dist/core/MlDsa44.d.ts.map +1 -1
- package/dist/core/MlDsa44.js +79 -4
- package/dist/core/MlDsa44.js.map +1 -1
- package/dist/core/P256.d.ts +82 -1
- package/dist/core/P256.d.ts.map +1 -1
- package/dist/core/P256.js +78 -0
- package/dist/core/P256.js.map +1 -1
- package/dist/core/Secp256k1.d.ts +85 -1
- package/dist/core/Secp256k1.d.ts.map +1 -1
- package/dist/core/Secp256k1.js +74 -3
- package/dist/core/Secp256k1.js.map +1 -1
- package/dist/core/internal/keyDerivation.d.ts +13 -0
- package/dist/core/internal/keyDerivation.d.ts.map +1 -0
- package/dist/core/internal/keyDerivation.js +13 -0
- package/dist/core/internal/keyDerivation.js.map +1 -0
- package/package.json +1 -1
- package/src/core/AesGcm.ts +137 -30
- package/src/core/Ed25519.ts +135 -4
- package/src/core/MlDsa44.ts +135 -4
- package/src/core/P256.ts +135 -1
- package/src/core/Secp256k1.ts +134 -3
- package/src/core/_test/AesGcm.test-d.ts +19 -0
- package/src/core/_test/AesGcm.test.ts +90 -1
- package/src/core/_test/Ed25519.test-d.ts +24 -0
- package/src/core/_test/Ed25519.test.ts +87 -1
- package/src/core/_test/MlDsa44.test-d.ts +24 -0
- package/src/core/_test/MlDsa44.test.ts +69 -1
- package/src/core/_test/P256.test-d.ts +26 -0
- package/src/core/_test/P256.test.ts +98 -1
- package/src/core/_test/Secp256k1.test-d.ts +27 -0
- package/src/core/_test/Secp256k1.test.ts +112 -1
- package/src/core/internal/keyDerivation.ts +34 -0
- package/src/version.ts +1 -1
package/src/core/MlDsa44.ts
CHANGED
|
@@ -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.
|
|
81
|
-
*
|
|
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(
|
|
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
|
|
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
|
|
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')
|
package/src/core/Secp256k1.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
normalizePublicKey,
|
|
11
11
|
normalizeSignature,
|
|
12
12
|
} from './internal/cryptoIo.js'
|
|
13
|
+
import * as keyDerivation from './internal/keyDerivation.js'
|
|
13
14
|
import * as engine from './internal/secp256k1.js'
|
|
14
15
|
import * as Entropy from './internal/entropy.js'
|
|
15
16
|
import {
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
toRecoveredBytes,
|
|
19
20
|
} from './internal/signature.js'
|
|
20
21
|
import type { OneOf } from './internal/types.js'
|
|
22
|
+
import * as Mnemonic from './Mnemonic.js'
|
|
21
23
|
import * as PublicKey from './PublicKey.js'
|
|
22
24
|
import type * as Signature from './Signature.js'
|
|
23
25
|
|
|
@@ -76,7 +78,7 @@ export declare namespace createKeyPair {
|
|
|
76
78
|
* Derives a valid secp256k1 private key from a 32-byte WebAuthn PRF output.
|
|
77
79
|
*
|
|
78
80
|
* The permanent derivation contract uses the PRF output as the HMAC-SHA256
|
|
79
|
-
* key.
|
|
81
|
+
* key. The HMAC message uses the `ox.secp256k1.fromPrf.v1` domain followed by
|
|
80
82
|
* a 32-bit big-endian counter starting at zero. Invalid scalars are skipped.
|
|
81
83
|
*
|
|
82
84
|
* @example
|
|
@@ -103,7 +105,7 @@ export function fromPrf<as extends 'Hex' | 'Bytes' = 'Hex'>(
|
|
|
103
105
|
for (let counter = 0; ; counter++) {
|
|
104
106
|
const candidate = Hash.hmac256(
|
|
105
107
|
bytes,
|
|
106
|
-
Bytes.concat(
|
|
108
|
+
Bytes.concat(fromPrfDomain, Bytes.fromNumber(counter, { size: 4 })),
|
|
107
109
|
{ as: 'Bytes' },
|
|
108
110
|
)
|
|
109
111
|
if (noble.utils.isValidSecretKey(candidate)) {
|
|
@@ -141,6 +143,115 @@ export declare namespace fromPrf {
|
|
|
141
143
|
| Errors.GlobalErrorType
|
|
142
144
|
}
|
|
143
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Derives a valid secp256k1 private key from a BIP-39 mnemonic.
|
|
148
|
+
*
|
|
149
|
+
* This is equivalent to {@link ox#Mnemonic.toPrivateKey}, and derives the
|
|
150
|
+
* private key at `m/44'/60'/0'/0/0` by default.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts twoslash
|
|
154
|
+
* import { Secp256k1 } from 'ox'
|
|
155
|
+
*
|
|
156
|
+
* const privateKey = Secp256k1.fromMnemonic(
|
|
157
|
+
* 'test test test test test test test test test test test junk'
|
|
158
|
+
* )
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @param mnemonic - BIP-39 mnemonic phrase.
|
|
162
|
+
* @param options - Options.
|
|
163
|
+
* @returns A valid secp256k1 private key.
|
|
164
|
+
*/
|
|
165
|
+
export function fromMnemonic<as extends 'Hex' | 'Bytes' = 'Hex'>(
|
|
166
|
+
mnemonic: string,
|
|
167
|
+
options: fromMnemonic.Options<as> = {},
|
|
168
|
+
): fromMnemonic.ReturnType<as> {
|
|
169
|
+
const { as = 'Hex', passphrase, path } = options
|
|
170
|
+
return Mnemonic.toPrivateKey(mnemonic, { as, passphrase, path }) as never
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export declare namespace fromMnemonic {
|
|
174
|
+
type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
|
|
175
|
+
/**
|
|
176
|
+
* Format of the returned private key.
|
|
177
|
+
* @default 'Hex'
|
|
178
|
+
*/
|
|
179
|
+
as?: as | 'Hex' | 'Bytes' | undefined
|
|
180
|
+
/** Derivation path. @default `m/44'/60'/0'/0/0` */
|
|
181
|
+
path?: string | undefined
|
|
182
|
+
/** Optional BIP-39 passphrase. */
|
|
183
|
+
passphrase?: string | undefined
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
type ReturnType<as extends 'Hex' | 'Bytes'> =
|
|
187
|
+
Mnemonic.toPrivateKey.ReturnType<as>
|
|
188
|
+
|
|
189
|
+
type ErrorType = Mnemonic.toPrivateKey.ErrorType
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Derives a valid secp256k1 private key from a seed.
|
|
194
|
+
*
|
|
195
|
+
* The seed must contain at least 32 bytes of cryptographically strong key
|
|
196
|
+
* material. Do not pass a password directly; use a password KDF first.
|
|
197
|
+
*
|
|
198
|
+
* The permanent derivation contract uses the seed as the HMAC-SHA256
|
|
199
|
+
* key. The HMAC message uses the `ox.secp256k1.fromSeed.v1` domain followed by
|
|
200
|
+
* a 32-bit big-endian counter starting at zero. Invalid scalars are skipped.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts twoslash
|
|
204
|
+
* import { Secp256k1 } from 'ox'
|
|
205
|
+
*
|
|
206
|
+
* const privateKey = Secp256k1.fromSeed(
|
|
207
|
+
* '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
|
|
208
|
+
* )
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* @param seed - Seed containing at least 32 bytes of cryptographically strong key material.
|
|
212
|
+
* @param options - Options.
|
|
213
|
+
* @returns A valid secp256k1 private key.
|
|
214
|
+
*/
|
|
215
|
+
export function fromSeed<as extends 'Hex' | 'Bytes' = 'Hex'>(
|
|
216
|
+
seed: Hex.Hex | Bytes.Bytes,
|
|
217
|
+
options: fromSeed.Options<as> = {},
|
|
218
|
+
): fromSeed.ReturnType<as> {
|
|
219
|
+
const { as = 'Hex' } = options
|
|
220
|
+
const bytes = Bytes.from(seed)
|
|
221
|
+
if (bytes.length < 32) throw new InvalidSeedSizeError({ size: bytes.length })
|
|
222
|
+
|
|
223
|
+
const privateKey = keyDerivation.derive(bytes, fromSeedDomain, {
|
|
224
|
+
validate: noble.utils.isValidSecretKey,
|
|
225
|
+
})
|
|
226
|
+
if (as === 'Hex') {
|
|
227
|
+
const value = Hex.fromBytes(privateKey)
|
|
228
|
+
privateKey.fill(0)
|
|
229
|
+
return value as never
|
|
230
|
+
}
|
|
231
|
+
return privateKey as never
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export declare namespace fromSeed {
|
|
235
|
+
type Options<as extends 'Hex' | 'Bytes' = 'Hex'> = {
|
|
236
|
+
/**
|
|
237
|
+
* Format of the returned private key.
|
|
238
|
+
* @default 'Hex'
|
|
239
|
+
*/
|
|
240
|
+
as?: as | 'Hex' | 'Bytes' | undefined
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
type ReturnType<as extends 'Hex' | 'Bytes'> =
|
|
244
|
+
| (as extends 'Bytes' ? Bytes.Bytes : never)
|
|
245
|
+
| (as extends 'Hex' ? Hex.Hex : never)
|
|
246
|
+
|
|
247
|
+
type ErrorType =
|
|
248
|
+
| Bytes.from.ErrorType
|
|
249
|
+
| Hex.fromBytes.ErrorType
|
|
250
|
+
| keyDerivation.derive.ErrorType
|
|
251
|
+
| InvalidSeedSizeError
|
|
252
|
+
| Errors.GlobalErrorType
|
|
253
|
+
}
|
|
254
|
+
|
|
144
255
|
/**
|
|
145
256
|
* Computes the secp256k1 ECDSA public key from a provided private key.
|
|
146
257
|
*
|
|
@@ -593,4 +704,24 @@ export declare namespace InvalidPrfSizeError {
|
|
|
593
704
|
}
|
|
594
705
|
}
|
|
595
706
|
|
|
596
|
-
|
|
707
|
+
/** Thrown when a seed contains fewer than 32 bytes. */
|
|
708
|
+
export class InvalidSeedSizeError extends Errors.BaseError {
|
|
709
|
+
override readonly name = 'Secp256k1.InvalidSeedSizeError'
|
|
710
|
+
|
|
711
|
+
constructor(options: InvalidSeedSizeError.Options) {
|
|
712
|
+
super(
|
|
713
|
+
`Seed must contain at least 32 bytes. Received ${options.size} bytes.`,
|
|
714
|
+
)
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
export declare namespace InvalidSeedSizeError {
|
|
719
|
+
/** Options for {@link ox#Secp256k1.InvalidSeedSizeError}. */
|
|
720
|
+
type Options = {
|
|
721
|
+
/** Received seed size. */
|
|
722
|
+
size: number
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const fromPrfDomain = Bytes.fromString('ox.secp256k1.fromPrf.v1')
|
|
727
|
+
const fromSeedDomain = Bytes.fromString('ox.secp256k1.fromSeed.v1')
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AesGcm } from 'ox'
|
|
2
|
+
import { expectTypeOf, test } from 'vp/test'
|
|
3
|
+
|
|
4
|
+
test('fromMnemonic', () => {
|
|
5
|
+
const mnemonic = 'test test test test test test test test test test test junk'
|
|
6
|
+
|
|
7
|
+
expectTypeOf(AesGcm.fromMnemonic(mnemonic)).toEqualTypeOf<
|
|
8
|
+
Promise<CryptoKey>
|
|
9
|
+
>()
|
|
10
|
+
expectTypeOf(
|
|
11
|
+
AesGcm.fromMnemonic(mnemonic, { passphrase: 'qwerty' }),
|
|
12
|
+
).toEqualTypeOf<Promise<CryptoKey>>()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('fromSeed', () => {
|
|
16
|
+
expectTypeOf(AesGcm.fromSeed(new Uint8Array(32))).toEqualTypeOf<
|
|
17
|
+
Promise<CryptoKey>
|
|
18
|
+
>()
|
|
19
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AesGcm, Bytes, Hex } from 'ox'
|
|
1
|
+
import { AesGcm, Bytes, Hex, Mnemonic } from 'ox'
|
|
2
2
|
import { describe, expect, test } from 'vp/test'
|
|
3
3
|
|
|
4
4
|
describe('decrypt', () => {
|
|
@@ -159,6 +159,92 @@ describe('fromPrf', () => {
|
|
|
159
159
|
})
|
|
160
160
|
})
|
|
161
161
|
|
|
162
|
+
describe('fromMnemonic', () => {
|
|
163
|
+
const mnemonic = 'test test test test test test test test test test test junk'
|
|
164
|
+
|
|
165
|
+
test('default', async () => {
|
|
166
|
+
const key = await AesGcm.fromMnemonic(mnemonic)
|
|
167
|
+
const expectedKey = await AesGcm.fromSeed(Mnemonic.toSeed(mnemonic))
|
|
168
|
+
const value = Bytes.fromString('i am a secret message')
|
|
169
|
+
const encrypted = await AesGcm.encrypt(value, key)
|
|
170
|
+
|
|
171
|
+
await expect(AesGcm.decrypt(encrypted, expectedKey)).resolves.toEqual(value)
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
test('options: passphrase', async () => {
|
|
175
|
+
const key = await AesGcm.fromMnemonic(mnemonic, {
|
|
176
|
+
passphrase: 'qwerty',
|
|
177
|
+
})
|
|
178
|
+
const defaultKey = await AesGcm.fromMnemonic(mnemonic)
|
|
179
|
+
const encrypted = await AesGcm.encrypt('0xdeadbeef', key)
|
|
180
|
+
|
|
181
|
+
await expect(AesGcm.decrypt(encrypted, defaultKey)).rejects.toThrowError()
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('fromSeed', () => {
|
|
186
|
+
const seed =
|
|
187
|
+
'0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
|
|
188
|
+
|
|
189
|
+
test('vector', async () => {
|
|
190
|
+
const key = await AesGcm.fromSeed(seed)
|
|
191
|
+
const expectedKey = await globalThis.crypto.subtle.importKey(
|
|
192
|
+
'raw',
|
|
193
|
+
Bytes.fromHex(
|
|
194
|
+
'0xd42ffe5cb894ce61e9f448e8083f53b2ab90d2acad0929e80b0abb85e915cb3a',
|
|
195
|
+
),
|
|
196
|
+
{ name: 'AES-GCM' },
|
|
197
|
+
false,
|
|
198
|
+
['decrypt'],
|
|
199
|
+
)
|
|
200
|
+
const value = Bytes.fromString('i am a secret message')
|
|
201
|
+
const encrypted = await AesGcm.encrypt(value, key)
|
|
202
|
+
|
|
203
|
+
await expect(AesGcm.decrypt(encrypted, expectedKey)).resolves.toEqual(value)
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
test('value: Bytes', async () => {
|
|
207
|
+
const key = await AesGcm.fromSeed(Bytes.fromHex(seed))
|
|
208
|
+
const value = Bytes.fromString('i am a secret message')
|
|
209
|
+
|
|
210
|
+
await expect(
|
|
211
|
+
AesGcm.decrypt(await AesGcm.encrypt(value, key), key),
|
|
212
|
+
).resolves.toEqual(value)
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
test('behavior: accepts seeds longer than 32 bytes', async () => {
|
|
216
|
+
const key = await AesGcm.fromSeed(new Uint8Array(64))
|
|
217
|
+
|
|
218
|
+
expect({
|
|
219
|
+
algorithm: key.algorithm,
|
|
220
|
+
extractable: key.extractable,
|
|
221
|
+
type: key.type,
|
|
222
|
+
usages: key.usages,
|
|
223
|
+
}).toMatchInlineSnapshot(`
|
|
224
|
+
{
|
|
225
|
+
"algorithm": {
|
|
226
|
+
"length": 256,
|
|
227
|
+
"name": "AES-GCM",
|
|
228
|
+
},
|
|
229
|
+
"extractable": false,
|
|
230
|
+
"type": "secret",
|
|
231
|
+
"usages": [
|
|
232
|
+
"encrypt",
|
|
233
|
+
"decrypt",
|
|
234
|
+
],
|
|
235
|
+
}
|
|
236
|
+
`)
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
test('error: seed is too short', async () => {
|
|
240
|
+
await expect(
|
|
241
|
+
AesGcm.fromSeed(new Uint8Array(31)),
|
|
242
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
243
|
+
`[AesGcm.InvalidSeedSizeError: Seed must contain at least 32 bytes. Received 31 bytes.]`,
|
|
244
|
+
)
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
|
|
162
248
|
describe('getKey', () => {
|
|
163
249
|
test('default', async () => {
|
|
164
250
|
const key = await AesGcm.getKey({ password: 'qwerty' })
|
|
@@ -180,9 +266,12 @@ test('exports', () => {
|
|
|
180
266
|
"decrypt",
|
|
181
267
|
"encrypt",
|
|
182
268
|
"fromPrf",
|
|
269
|
+
"fromMnemonic",
|
|
270
|
+
"fromSeed",
|
|
183
271
|
"getKey",
|
|
184
272
|
"randomSalt",
|
|
185
273
|
"InvalidPrfSizeError",
|
|
274
|
+
"InvalidSeedSizeError",
|
|
186
275
|
]
|
|
187
276
|
`)
|
|
188
277
|
})
|
|
@@ -40,6 +40,30 @@ test('fromPrf', () => {
|
|
|
40
40
|
).toEqualTypeOf<Bytes.Bytes>()
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
+
test('fromMnemonic', () => {
|
|
44
|
+
const mnemonic = 'test test test test test test test test test test test junk'
|
|
45
|
+
|
|
46
|
+
expectTypeOf(Ed25519.fromMnemonic(mnemonic)).toEqualTypeOf<Hex.Hex>()
|
|
47
|
+
expectTypeOf(
|
|
48
|
+
Ed25519.fromMnemonic(mnemonic, { passphrase: 'qwerty' }),
|
|
49
|
+
).toEqualTypeOf<Hex.Hex>()
|
|
50
|
+
expectTypeOf(
|
|
51
|
+
Ed25519.fromMnemonic(mnemonic, { as: 'Bytes' }),
|
|
52
|
+
).toEqualTypeOf<Bytes.Bytes>()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('fromSeed', () => {
|
|
56
|
+
const seed =
|
|
57
|
+
'0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
|
|
58
|
+
|
|
59
|
+
expectTypeOf(Ed25519.fromSeed(seed)).toEqualTypeOf<Hex.Hex>()
|
|
60
|
+
expectTypeOf(Ed25519.fromSeed(new Uint8Array(32))).toEqualTypeOf<Hex.Hex>()
|
|
61
|
+
expectTypeOf(Ed25519.fromSeed(seed, { as: 'Hex' })).toEqualTypeOf<Hex.Hex>()
|
|
62
|
+
expectTypeOf(
|
|
63
|
+
Ed25519.fromSeed(seed, { as: 'Bytes' }),
|
|
64
|
+
).toEqualTypeOf<Bytes.Bytes>()
|
|
65
|
+
})
|
|
66
|
+
|
|
43
67
|
test('getPublicKey', () => {
|
|
44
68
|
// Default behavior (Hex)
|
|
45
69
|
{
|