ox 0.7.2 → 0.8.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 +37 -0
- package/_cjs/core/Keystore.js +20 -24
- package/_cjs/core/Keystore.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/core/Keystore.js +28 -32
- package/_esm/core/Keystore.js.map +1 -1
- package/_esm/index.js +2 -2
- package/_esm/version.js +1 -1
- package/_types/core/Keystore.d.ts +28 -31
- package/_types/core/Keystore.d.ts.map +1 -1
- package/_types/index.d.ts +2 -2
- package/_types/version.d.ts +1 -1
- package/core/Keystore.ts +52 -50
- package/index.ts +2 -2
- package/package.json +1 -1
- package/version.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
# ox
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`7fc1da0`](https://github.com/wevm/ox/commit/7fc1da0717a17dbac0e4effed2ea3911c7ca3236) Thanks [@jxom](https://github.com/jxom)! - **Breaking(`Keystore`):** Keystore derivation functions (e.g. `Keystore.pbkdf2`) now return a tuple of the key and derivation options,
|
|
8
|
+
instead of an object with the key and options.
|
|
9
|
+
|
|
10
|
+
```diff
|
|
11
|
+
import { Keystore } from 'ox'
|
|
12
|
+
|
|
13
|
+
- const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
14
|
+
+ const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
- [`7fc1da0`](https://github.com/wevm/ox/commit/7fc1da0717a17dbac0e4effed2ea3911c7ca3236) Thanks [@jxom](https://github.com/jxom)! - **Breaking(`Keystore`):** `Keystore.decrypt` function interface no longer requires an object as the second parameter, now it only requires the key itself.
|
|
18
|
+
|
|
19
|
+
```diff
|
|
20
|
+
import { Keystore } from 'ox'
|
|
21
|
+
|
|
22
|
+
const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
23
|
+
|
|
24
|
+
const encrypted = await Keystore.encrypt(secret, key, opts)
|
|
25
|
+
|
|
26
|
+
+ const decrypted = await Keystore.decrypt(encrypted, key)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- [`7fc1da0`](https://github.com/wevm/ox/commit/7fc1da0717a17dbac0e4effed2ea3911c7ca3236) Thanks [@jxom](https://github.com/jxom)! - **Breaking(`Keystore`):** `Keystore.encrypt` function interface has changed to require derivation options (`opts`).
|
|
30
|
+
|
|
31
|
+
```diff
|
|
32
|
+
import { Keystore } from 'ox'
|
|
33
|
+
|
|
34
|
+
const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
35
|
+
|
|
36
|
+
- const encrypted = await Keystore.encrypt(secret, key)
|
|
37
|
+
+ const encrypted = await Keystore.encrypt(secret, key, opts)
|
|
38
|
+
```
|
|
39
|
+
|
|
3
40
|
## 0.7.2
|
|
4
41
|
|
|
5
42
|
### Patch Changes
|
package/_cjs/core/Keystore.js
CHANGED
|
@@ -14,33 +14,33 @@ const Bytes = require("./Bytes.js");
|
|
|
14
14
|
const Hash = require("./Hash.js");
|
|
15
15
|
async function decrypt(keystore, key, options = {}) {
|
|
16
16
|
const { as = 'Hex' } = options;
|
|
17
|
-
const key_ = Bytes.from(
|
|
17
|
+
const key_ = Bytes.from(typeof key === 'function' ? key() : key);
|
|
18
18
|
const encKey = Bytes.slice(key_, 0, 16);
|
|
19
19
|
const macKey = Bytes.slice(key_, 16, 32);
|
|
20
20
|
const ciphertext = Bytes.from(`0x${keystore.crypto.ciphertext}`);
|
|
21
21
|
const mac = Hash.keccak256(Bytes.concat(macKey, ciphertext));
|
|
22
22
|
if (!Bytes.isEqual(mac, Bytes.from(`0x${keystore.crypto.mac}`)))
|
|
23
23
|
throw new Error('corrupt keystore');
|
|
24
|
-
const data = (0, aes_1.ctr)(encKey,
|
|
24
|
+
const data = (0, aes_1.ctr)(encKey, Bytes.from(`0x${keystore.crypto.cipherparams.iv}`)).decrypt(ciphertext);
|
|
25
25
|
if (as === 'Hex')
|
|
26
26
|
return Bytes.toHex(data);
|
|
27
27
|
return data;
|
|
28
28
|
}
|
|
29
|
-
async function encrypt(privateKey, key, options
|
|
30
|
-
const { id = crypto.randomUUID() } = options;
|
|
31
|
-
const key_ = Bytes.from(
|
|
29
|
+
async function encrypt(privateKey, key, options) {
|
|
30
|
+
const { id = crypto.randomUUID(), kdf, kdfparams, iv } = options;
|
|
31
|
+
const key_ = Bytes.from(typeof key === 'function' ? key() : key);
|
|
32
32
|
const value_ = Bytes.from(privateKey);
|
|
33
33
|
const encKey = Bytes.slice(key_, 0, 16);
|
|
34
34
|
const macKey = Bytes.slice(key_, 16, 32);
|
|
35
|
-
const ciphertext = (0, aes_1.ctr)(encKey,
|
|
35
|
+
const ciphertext = (0, aes_1.ctr)(encKey, iv).encrypt(value_);
|
|
36
36
|
const mac = Hash.keccak256(Bytes.concat(macKey, ciphertext));
|
|
37
37
|
return {
|
|
38
38
|
crypto: {
|
|
39
39
|
cipher: 'aes-128-ctr',
|
|
40
40
|
ciphertext: Bytes.toHex(ciphertext).slice(2),
|
|
41
|
-
cipherparams: { iv: Bytes.toHex(
|
|
42
|
-
kdf
|
|
43
|
-
kdfparams
|
|
41
|
+
cipherparams: { iv: Bytes.toHex(iv).slice(2) },
|
|
42
|
+
kdf,
|
|
43
|
+
kdfparams,
|
|
44
44
|
mac: Bytes.toHex(mac).slice(2),
|
|
45
45
|
},
|
|
46
46
|
id,
|
|
@@ -50,10 +50,9 @@ async function encrypt(privateKey, key, options = {}) {
|
|
|
50
50
|
function pbkdf2(options) {
|
|
51
51
|
const { iv, iterations = 262_144, password } = options;
|
|
52
52
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32);
|
|
53
|
-
const key = Bytes.toHex((0, pbkdf2_1.pbkdf2)(sha2_1.sha256, password, salt, { c: iterations, dkLen: 32 }))
|
|
54
|
-
return defineKey({
|
|
53
|
+
const key = Bytes.toHex((0, pbkdf2_1.pbkdf2)(sha2_1.sha256, password, salt, { c: iterations, dkLen: 32 }));
|
|
54
|
+
return defineKey(() => key, {
|
|
55
55
|
iv,
|
|
56
|
-
key: () => key,
|
|
57
56
|
kdfparams: {
|
|
58
57
|
c: iterations,
|
|
59
58
|
dklen: 32,
|
|
@@ -69,10 +68,9 @@ async function pbkdf2Async(options) {
|
|
|
69
68
|
const key = Bytes.toHex(await (0, pbkdf2_1.pbkdf2Async)(sha2_1.sha256, password, salt, {
|
|
70
69
|
c: iterations,
|
|
71
70
|
dkLen: 32,
|
|
72
|
-
}))
|
|
73
|
-
return defineKey({
|
|
71
|
+
}));
|
|
72
|
+
return defineKey(() => key, {
|
|
74
73
|
iv,
|
|
75
|
-
key: () => key,
|
|
76
74
|
kdfparams: {
|
|
77
75
|
c: iterations,
|
|
78
76
|
dklen: 32,
|
|
@@ -87,10 +85,9 @@ function scrypt(options) {
|
|
|
87
85
|
const p = 8;
|
|
88
86
|
const r = 1;
|
|
89
87
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32);
|
|
90
|
-
const key = Bytes.toHex((0, scrypt_1.scrypt)(password, salt, { N: n, dkLen: 32, r, p }))
|
|
91
|
-
return defineKey({
|
|
88
|
+
const key = Bytes.toHex((0, scrypt_1.scrypt)(password, salt, { N: n, dkLen: 32, r, p }));
|
|
89
|
+
return defineKey(() => key, {
|
|
92
90
|
iv,
|
|
93
|
-
key: () => key,
|
|
94
91
|
kdfparams: {
|
|
95
92
|
dklen: 32,
|
|
96
93
|
n,
|
|
@@ -106,10 +103,9 @@ async function scryptAsync(options) {
|
|
|
106
103
|
const p = 8;
|
|
107
104
|
const r = 1;
|
|
108
105
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32);
|
|
109
|
-
const key = Bytes.toHex(await (0, scrypt_1.scryptAsync)(password, salt, { N: n, dkLen: 32, r, p }))
|
|
110
|
-
return defineKey({
|
|
106
|
+
const key = Bytes.toHex(await (0, scrypt_1.scryptAsync)(password, salt, { N: n, dkLen: 32, r, p }));
|
|
107
|
+
return defineKey(() => key, {
|
|
111
108
|
iv,
|
|
112
|
-
key: () => key,
|
|
113
109
|
kdfparams: {
|
|
114
110
|
dklen: 32,
|
|
115
111
|
n,
|
|
@@ -120,8 +116,8 @@ async function scryptAsync(options) {
|
|
|
120
116
|
kdf: 'scrypt',
|
|
121
117
|
});
|
|
122
118
|
}
|
|
123
|
-
function defineKey(key) {
|
|
124
|
-
const iv =
|
|
125
|
-
return { ...
|
|
119
|
+
function defineKey(key, options) {
|
|
120
|
+
const iv = options.iv ? Bytes.from(options.iv) : Bytes.random(16);
|
|
121
|
+
return [key, { ...options, iv }];
|
|
126
122
|
}
|
|
127
123
|
//# sourceMappingURL=Keystore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Keystore.js","sourceRoot":"","sources":["../../core/Keystore.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"Keystore.js","sourceRoot":"","sources":["../../core/Keystore.ts"],"names":[],"mappings":";;AAiGA,0BAwBC;AA2DD,0BA4BC;AAsBD,wBAkBC;AA4BD,kCAqBC;AAmBD,wBAsBC;AA4BD,kCAsBC;AApYD,4CAAwC;AACxC,iDAG6B;AAC7B,iDAG6B;AAC7B,6CAA2C;AAC3C,oCAAmC;AAEnC,kCAAiC;AAqF1B,KAAK,UAAU,OAAO,CAC3B,QAAkB,EAClB,GAAQ,EACR,UAA+B,EAAE;IAEjC,MAAM,EAAE,EAAE,GAAG,KAAK,EAAE,GAAG,OAAO,CAAA;IAC9B,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,GAAG,KAAK,UAAU,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;IAEhE,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE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AAS,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE;QAC1B,EAAE;QACF,SAAS,EAAE;YACT,KAAK,EAAE,EAAE;YACT,CAAC;YACD,CAAC;YACD,CAAC;YACD,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;SACjC;QACD,GAAG,EAAE,QAAQ;KACd,CAAmC,CAAA;AACtC,CAAC;AASD,SAAS,SAAS,CAGhB,GAAQ,EAAE,OAAgB;IAC1B,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;IACjE,OAAO,CAAC,GAAG,EAAE,EAAE,GAAG,OAAO,EAAE,EAAE,EAAE,CAAU,CAAA;AAC3C,CAAC"}
|
package/_cjs/version.js
CHANGED
package/_esm/core/Keystore.js
CHANGED
|
@@ -20,8 +20,8 @@ import * as Hash from './Hash.js';
|
|
|
20
20
|
* // JSON keystore.
|
|
21
21
|
* const keystore = { crypto: { ... }, id: '...', version: 3 }
|
|
22
22
|
*
|
|
23
|
-
* //
|
|
24
|
-
* const key =
|
|
23
|
+
* // Key that was previously derived from `Keystore.scrypt` or `Keystore.pbkdf2`.
|
|
24
|
+
* const key = "0x..."
|
|
25
25
|
*
|
|
26
26
|
* // Decrypt the private key.
|
|
27
27
|
* const privateKey = await Keystore.decrypt(keystore, key)
|
|
@@ -35,14 +35,14 @@ import * as Hash from './Hash.js';
|
|
|
35
35
|
*/
|
|
36
36
|
export async function decrypt(keystore, key, options = {}) {
|
|
37
37
|
const { as = 'Hex' } = options;
|
|
38
|
-
const key_ = Bytes.from(
|
|
38
|
+
const key_ = Bytes.from(typeof key === 'function' ? key() : key);
|
|
39
39
|
const encKey = Bytes.slice(key_, 0, 16);
|
|
40
40
|
const macKey = Bytes.slice(key_, 16, 32);
|
|
41
41
|
const ciphertext = Bytes.from(`0x${keystore.crypto.ciphertext}`);
|
|
42
42
|
const mac = Hash.keccak256(Bytes.concat(macKey, ciphertext));
|
|
43
43
|
if (!Bytes.isEqual(mac, Bytes.from(`0x${keystore.crypto.mac}`)))
|
|
44
44
|
throw new Error('corrupt keystore');
|
|
45
|
-
const data = ctr(encKey,
|
|
45
|
+
const data = ctr(encKey, Bytes.from(`0x${keystore.crypto.cipherparams.iv}`)).decrypt(ciphertext);
|
|
46
46
|
if (as === 'Hex')
|
|
47
47
|
return Bytes.toHex(data);
|
|
48
48
|
return data;
|
|
@@ -63,10 +63,10 @@ export async function decrypt(keystore, key, options = {}) {
|
|
|
63
63
|
* const privateKey = Secp256k1.randomPrivateKey()
|
|
64
64
|
*
|
|
65
65
|
* // Derive key from password.
|
|
66
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
66
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
67
67
|
*
|
|
68
68
|
* // Encrypt the private key.
|
|
69
|
-
* const encrypted = await Keystore.encrypt(privateKey, key)
|
|
69
|
+
* const encrypted = await Keystore.encrypt(privateKey, key, opts)
|
|
70
70
|
* // @log: {
|
|
71
71
|
* // @log: "crypto": {
|
|
72
72
|
* // @log: "cipher": "aes-128-ctr",
|
|
@@ -93,21 +93,21 @@ export async function decrypt(keystore, key, options = {}) {
|
|
|
93
93
|
* @param options - Encryption options.
|
|
94
94
|
* @returns Encrypted keystore.
|
|
95
95
|
*/
|
|
96
|
-
export async function encrypt(privateKey, key, options
|
|
97
|
-
const { id = crypto.randomUUID() } = options;
|
|
98
|
-
const key_ = Bytes.from(
|
|
96
|
+
export async function encrypt(privateKey, key, options) {
|
|
97
|
+
const { id = crypto.randomUUID(), kdf, kdfparams, iv } = options;
|
|
98
|
+
const key_ = Bytes.from(typeof key === 'function' ? key() : key);
|
|
99
99
|
const value_ = Bytes.from(privateKey);
|
|
100
100
|
const encKey = Bytes.slice(key_, 0, 16);
|
|
101
101
|
const macKey = Bytes.slice(key_, 16, 32);
|
|
102
|
-
const ciphertext = ctr(encKey,
|
|
102
|
+
const ciphertext = ctr(encKey, iv).encrypt(value_);
|
|
103
103
|
const mac = Hash.keccak256(Bytes.concat(macKey, ciphertext));
|
|
104
104
|
return {
|
|
105
105
|
crypto: {
|
|
106
106
|
cipher: 'aes-128-ctr',
|
|
107
107
|
ciphertext: Bytes.toHex(ciphertext).slice(2),
|
|
108
|
-
cipherparams: { iv: Bytes.toHex(
|
|
109
|
-
kdf
|
|
110
|
-
kdfparams
|
|
108
|
+
cipherparams: { iv: Bytes.toHex(iv).slice(2) },
|
|
109
|
+
kdf,
|
|
110
|
+
kdfparams,
|
|
111
111
|
mac: Bytes.toHex(mac).slice(2),
|
|
112
112
|
},
|
|
113
113
|
id,
|
|
@@ -121,7 +121,7 @@ export async function encrypt(privateKey, key, options = {}) {
|
|
|
121
121
|
* ```ts twoslash
|
|
122
122
|
* import { Keystore } from 'ox'
|
|
123
123
|
*
|
|
124
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
124
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
125
125
|
* ```
|
|
126
126
|
*
|
|
127
127
|
* @param options - PBKDF2 options.
|
|
@@ -130,10 +130,9 @@ export async function encrypt(privateKey, key, options = {}) {
|
|
|
130
130
|
export function pbkdf2(options) {
|
|
131
131
|
const { iv, iterations = 262_144, password } = options;
|
|
132
132
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32);
|
|
133
|
-
const key = Bytes.toHex(pbkdf2_noble(sha256, password, salt, { c: iterations, dkLen: 32 }))
|
|
134
|
-
return defineKey({
|
|
133
|
+
const key = Bytes.toHex(pbkdf2_noble(sha256, password, salt, { c: iterations, dkLen: 32 }));
|
|
134
|
+
return defineKey(() => key, {
|
|
135
135
|
iv,
|
|
136
|
-
key: () => key,
|
|
137
136
|
kdfparams: {
|
|
138
137
|
c: iterations,
|
|
139
138
|
dklen: 32,
|
|
@@ -150,7 +149,7 @@ export function pbkdf2(options) {
|
|
|
150
149
|
* ```ts twoslash
|
|
151
150
|
* import { Keystore } from 'ox'
|
|
152
151
|
*
|
|
153
|
-
* const key = await Keystore.pbkdf2Async({ password: 'testpassword' })
|
|
152
|
+
* const [key, opts] = await Keystore.pbkdf2Async({ password: 'testpassword' })
|
|
154
153
|
* ```
|
|
155
154
|
*
|
|
156
155
|
* @param options - PBKDF2 options.
|
|
@@ -162,10 +161,9 @@ export async function pbkdf2Async(options) {
|
|
|
162
161
|
const key = Bytes.toHex(await pbkdf2Async_noble(sha256, password, salt, {
|
|
163
162
|
c: iterations,
|
|
164
163
|
dkLen: 32,
|
|
165
|
-
}))
|
|
166
|
-
return defineKey({
|
|
164
|
+
}));
|
|
165
|
+
return defineKey(() => key, {
|
|
167
166
|
iv,
|
|
168
|
-
key: () => key,
|
|
169
167
|
kdfparams: {
|
|
170
168
|
c: iterations,
|
|
171
169
|
dklen: 32,
|
|
@@ -182,7 +180,7 @@ export async function pbkdf2Async(options) {
|
|
|
182
180
|
* ```ts twoslash
|
|
183
181
|
* import { Keystore } from 'ox'
|
|
184
182
|
*
|
|
185
|
-
* const key = Keystore.scrypt({ password: 'testpassword' })
|
|
183
|
+
* const [key, opts] = Keystore.scrypt({ password: 'testpassword' })
|
|
186
184
|
* ```
|
|
187
185
|
*
|
|
188
186
|
* @param options - Scrypt options.
|
|
@@ -193,10 +191,9 @@ export function scrypt(options) {
|
|
|
193
191
|
const p = 8;
|
|
194
192
|
const r = 1;
|
|
195
193
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32);
|
|
196
|
-
const key = Bytes.toHex(scrypt_noble(password, salt, { N: n, dkLen: 32, r, p }))
|
|
197
|
-
return defineKey({
|
|
194
|
+
const key = Bytes.toHex(scrypt_noble(password, salt, { N: n, dkLen: 32, r, p }));
|
|
195
|
+
return defineKey(() => key, {
|
|
198
196
|
iv,
|
|
199
|
-
key: () => key,
|
|
200
197
|
kdfparams: {
|
|
201
198
|
dklen: 32,
|
|
202
199
|
n,
|
|
@@ -214,7 +211,7 @@ export function scrypt(options) {
|
|
|
214
211
|
* ```ts twoslash
|
|
215
212
|
* import { Keystore } from 'ox'
|
|
216
213
|
*
|
|
217
|
-
* const key = await Keystore.scryptAsync({ password: 'testpassword' })
|
|
214
|
+
* const [key, opts] = await Keystore.scryptAsync({ password: 'testpassword' })
|
|
218
215
|
* ```
|
|
219
216
|
*
|
|
220
217
|
* @param options - Scrypt options.
|
|
@@ -225,10 +222,9 @@ export async function scryptAsync(options) {
|
|
|
225
222
|
const p = 8;
|
|
226
223
|
const r = 1;
|
|
227
224
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32);
|
|
228
|
-
const key = Bytes.toHex(await scryptAsync_noble(password, salt, { N: n, dkLen: 32, r, p }))
|
|
229
|
-
return defineKey({
|
|
225
|
+
const key = Bytes.toHex(await scryptAsync_noble(password, salt, { N: n, dkLen: 32, r, p }));
|
|
226
|
+
return defineKey(() => key, {
|
|
230
227
|
iv,
|
|
231
|
-
key: () => key,
|
|
232
228
|
kdfparams: {
|
|
233
229
|
dklen: 32,
|
|
234
230
|
n,
|
|
@@ -241,8 +237,8 @@ export async function scryptAsync(options) {
|
|
|
241
237
|
}
|
|
242
238
|
///////////////////////////////////////////////////////////////////////////
|
|
243
239
|
/** @internal */
|
|
244
|
-
function defineKey(key) {
|
|
245
|
-
const iv =
|
|
246
|
-
return { ...
|
|
240
|
+
function defineKey(key, options) {
|
|
241
|
+
const iv = options.iv ? Bytes.from(options.iv) : Bytes.random(16);
|
|
242
|
+
return [key, { ...options, iv }];
|
|
247
243
|
}
|
|
248
244
|
//# sourceMappingURL=Keystore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Keystore.js","sourceRoot":"","sources":["../../core/Keystore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,oBAAoB,CAAA;AACxC,OAAO,E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,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CACnE,CAAA;IAED,OAAO,SAAS,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE;QAC1B,EAAE;QACF,SAAS,EAAE;YACT,KAAK,EAAE,EAAE;YACT,CAAC;YACD,CAAC;YACD,CAAC;YACD,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;SACjC;QACD,GAAG,EAAE,QAAQ;KACd,CAAmC,CAAA;AACtC,CAAC;AAMD,2EAA2E;AAE3E,gBAAgB;AAChB,SAAS,SAAS,CAGhB,GAAQ,EAAE,OAAgB;IAC1B,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;IACjE,OAAO,CAAC,GAAG,EAAE,EAAE,GAAG,OAAO,EAAE,EAAE,EAAE,CAAU,CAAA;AAC3C,CAAC"}
|
package/_esm/index.js
CHANGED
|
@@ -1538,10 +1538,10 @@ export * as Json from './core/Json.js';
|
|
|
1538
1538
|
* const privateKey = Secp256k1.randomPrivateKey()
|
|
1539
1539
|
*
|
|
1540
1540
|
* // Derive a key from a password.
|
|
1541
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
1541
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
1542
1542
|
*
|
|
1543
1543
|
* // Encrypt the private key.
|
|
1544
|
-
* const encrypted = await Keystore.encrypt(privateKey, key)
|
|
1544
|
+
* const encrypted = await Keystore.encrypt(privateKey, key, opts)
|
|
1545
1545
|
* // @log: {
|
|
1546
1546
|
* // @log: "crypto": {
|
|
1547
1547
|
* // @log: "cipher": "aes-128-ctr",
|
package/_esm/version.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import * as Bytes from './Bytes.js';
|
|
2
2
|
import type * as Hex from './Hex.js';
|
|
3
|
-
/** Base
|
|
4
|
-
type
|
|
3
|
+
/** Base Derivation Options. */
|
|
4
|
+
type BaseDeriveOpts<kdf extends string = string, kdfparams extends Record<string, unknown> = Record<string, unknown>> = {
|
|
5
5
|
iv: Bytes.Bytes;
|
|
6
|
-
key: () => string;
|
|
7
6
|
kdfparams: kdfparams;
|
|
8
7
|
kdf: kdf;
|
|
9
8
|
};
|
|
@@ -16,21 +15,23 @@ export type Keystore = {
|
|
|
16
15
|
iv: string;
|
|
17
16
|
};
|
|
18
17
|
mac: string;
|
|
19
|
-
} & Pick<
|
|
18
|
+
} & Pick<DeriveOpts, 'kdf' | 'kdfparams'>;
|
|
20
19
|
id: string;
|
|
21
20
|
version: 3;
|
|
22
21
|
};
|
|
23
22
|
/** Key. */
|
|
24
|
-
export type Key =
|
|
25
|
-
/**
|
|
26
|
-
export type
|
|
23
|
+
export type Key = (() => Hex.Hex) | Hex.Hex;
|
|
24
|
+
/** Derivation Options. */
|
|
25
|
+
export type DeriveOpts = Pbkdf2DeriveOpts | ScryptDeriveOpts;
|
|
26
|
+
/** PBKDF2 Derivation Options. */
|
|
27
|
+
export type Pbkdf2DeriveOpts = BaseDeriveOpts<'pbkdf2', {
|
|
27
28
|
c: number;
|
|
28
29
|
dklen: number;
|
|
29
30
|
prf: 'hmac-sha256';
|
|
30
31
|
salt: string;
|
|
31
32
|
}>;
|
|
32
|
-
/** Scrypt
|
|
33
|
-
export type
|
|
33
|
+
/** Scrypt Derivation Options. */
|
|
34
|
+
export type ScryptDeriveOpts = BaseDeriveOpts<'scrypt', {
|
|
34
35
|
dklen: number;
|
|
35
36
|
n: number;
|
|
36
37
|
p: number;
|
|
@@ -53,8 +54,8 @@ export type ScryptKey = BaseKey<'scrypt', {
|
|
|
53
54
|
* // JSON keystore.
|
|
54
55
|
* const keystore = { crypto: { ... }, id: '...', version: 3 }
|
|
55
56
|
*
|
|
56
|
-
* //
|
|
57
|
-
* const key =
|
|
57
|
+
* // Key that was previously derived from `Keystore.scrypt` or `Keystore.pbkdf2`.
|
|
58
|
+
* const key = "0x..."
|
|
58
59
|
*
|
|
59
60
|
* // Decrypt the private key.
|
|
60
61
|
* const privateKey = await Keystore.decrypt(keystore, key)
|
|
@@ -90,10 +91,10 @@ export declare namespace decrypt {
|
|
|
90
91
|
* const privateKey = Secp256k1.randomPrivateKey()
|
|
91
92
|
*
|
|
92
93
|
* // Derive key from password.
|
|
93
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
94
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
94
95
|
*
|
|
95
96
|
* // Encrypt the private key.
|
|
96
|
-
* const encrypted = await Keystore.encrypt(privateKey, key)
|
|
97
|
+
* const encrypted = await Keystore.encrypt(privateKey, key, opts)
|
|
97
98
|
* // @log: {
|
|
98
99
|
* // @log: "crypto": {
|
|
99
100
|
* // @log: "cipher": "aes-128-ctr",
|
|
@@ -120,9 +121,9 @@ export declare namespace decrypt {
|
|
|
120
121
|
* @param options - Encryption options.
|
|
121
122
|
* @returns Encrypted keystore.
|
|
122
123
|
*/
|
|
123
|
-
export declare function encrypt(privateKey: Bytes.Bytes | Hex.Hex, key: Key, options
|
|
124
|
+
export declare function encrypt(privateKey: Bytes.Bytes | Hex.Hex, key: Key, options: encrypt.Options): Promise<Keystore>;
|
|
124
125
|
export declare namespace encrypt {
|
|
125
|
-
type Options = {
|
|
126
|
+
type Options = DeriveOpts & {
|
|
126
127
|
/** UUID. */
|
|
127
128
|
id?: string | undefined;
|
|
128
129
|
};
|
|
@@ -134,15 +135,14 @@ export declare namespace encrypt {
|
|
|
134
135
|
* ```ts twoslash
|
|
135
136
|
* import { Keystore } from 'ox'
|
|
136
137
|
*
|
|
137
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
138
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
138
139
|
* ```
|
|
139
140
|
*
|
|
140
141
|
* @param options - PBKDF2 options.
|
|
141
142
|
* @returns PBKDF2 key.
|
|
142
143
|
*/
|
|
143
|
-
export declare function pbkdf2(options: pbkdf2.Options): {
|
|
144
|
+
export declare function pbkdf2(options: pbkdf2.Options): [() => `0x${string}`, {
|
|
144
145
|
readonly iv: Uint8Array | `0x${string}` | undefined;
|
|
145
|
-
readonly key: () => string;
|
|
146
146
|
readonly kdfparams: {
|
|
147
147
|
readonly c: number;
|
|
148
148
|
readonly dklen: 32;
|
|
@@ -152,7 +152,7 @@ export declare function pbkdf2(options: pbkdf2.Options): {
|
|
|
152
152
|
readonly kdf: "pbkdf2";
|
|
153
153
|
} & {
|
|
154
154
|
iv: Bytes.Bytes;
|
|
155
|
-
};
|
|
155
|
+
}];
|
|
156
156
|
export declare namespace pbkdf2 {
|
|
157
157
|
type Options = {
|
|
158
158
|
/** The counter to use for the AES-CTR encryption. */
|
|
@@ -172,15 +172,14 @@ export declare namespace pbkdf2 {
|
|
|
172
172
|
* ```ts twoslash
|
|
173
173
|
* import { Keystore } from 'ox'
|
|
174
174
|
*
|
|
175
|
-
* const key = await Keystore.pbkdf2Async({ password: 'testpassword' })
|
|
175
|
+
* const [key, opts] = await Keystore.pbkdf2Async({ password: 'testpassword' })
|
|
176
176
|
* ```
|
|
177
177
|
*
|
|
178
178
|
* @param options - PBKDF2 options.
|
|
179
179
|
* @returns PBKDF2 key.
|
|
180
180
|
*/
|
|
181
|
-
export declare function pbkdf2Async(options: pbkdf2.Options): Promise<{
|
|
181
|
+
export declare function pbkdf2Async(options: pbkdf2.Options): Promise<[() => `0x${string}`, {
|
|
182
182
|
readonly iv: Uint8Array | `0x${string}` | undefined;
|
|
183
|
-
readonly key: () => string;
|
|
184
183
|
readonly kdfparams: {
|
|
185
184
|
readonly c: number;
|
|
186
185
|
readonly dklen: 32;
|
|
@@ -190,7 +189,7 @@ export declare function pbkdf2Async(options: pbkdf2.Options): Promise<{
|
|
|
190
189
|
readonly kdf: "pbkdf2";
|
|
191
190
|
} & {
|
|
192
191
|
iv: Bytes.Bytes;
|
|
193
|
-
}>;
|
|
192
|
+
}]>;
|
|
194
193
|
export declare namespace pbkdf2Async {
|
|
195
194
|
type Options = pbkdf2.Options;
|
|
196
195
|
}
|
|
@@ -201,15 +200,14 @@ export declare namespace pbkdf2Async {
|
|
|
201
200
|
* ```ts twoslash
|
|
202
201
|
* import { Keystore } from 'ox'
|
|
203
202
|
*
|
|
204
|
-
* const key = Keystore.scrypt({ password: 'testpassword' })
|
|
203
|
+
* const [key, opts] = Keystore.scrypt({ password: 'testpassword' })
|
|
205
204
|
* ```
|
|
206
205
|
*
|
|
207
206
|
* @param options - Scrypt options.
|
|
208
207
|
* @returns Scrypt key.
|
|
209
208
|
*/
|
|
210
|
-
export declare function scrypt(options: scrypt.Options): {
|
|
209
|
+
export declare function scrypt(options: scrypt.Options): [() => `0x${string}`, {
|
|
211
210
|
readonly iv: Uint8Array | `0x${string}` | undefined;
|
|
212
|
-
readonly key: () => string;
|
|
213
211
|
readonly kdfparams: {
|
|
214
212
|
readonly dklen: 32;
|
|
215
213
|
readonly n: number;
|
|
@@ -220,7 +218,7 @@ export declare function scrypt(options: scrypt.Options): {
|
|
|
220
218
|
readonly kdf: "scrypt";
|
|
221
219
|
} & {
|
|
222
220
|
iv: Bytes.Bytes;
|
|
223
|
-
};
|
|
221
|
+
}];
|
|
224
222
|
export declare namespace scrypt {
|
|
225
223
|
type Options = {
|
|
226
224
|
/** The counter to use for the AES-CTR encryption. */
|
|
@@ -240,15 +238,14 @@ export declare namespace scrypt {
|
|
|
240
238
|
* ```ts twoslash
|
|
241
239
|
* import { Keystore } from 'ox'
|
|
242
240
|
*
|
|
243
|
-
* const key = await Keystore.scryptAsync({ password: 'testpassword' })
|
|
241
|
+
* const [key, opts] = await Keystore.scryptAsync({ password: 'testpassword' })
|
|
244
242
|
* ```
|
|
245
243
|
*
|
|
246
244
|
* @param options - Scrypt options.
|
|
247
245
|
* @returns Scrypt key.
|
|
248
246
|
*/
|
|
249
|
-
export declare function scryptAsync(options: scrypt.Options): Promise<{
|
|
247
|
+
export declare function scryptAsync(options: scrypt.Options): Promise<[() => `0x${string}`, {
|
|
250
248
|
readonly iv: Uint8Array | `0x${string}` | undefined;
|
|
251
|
-
readonly key: () => string;
|
|
252
249
|
readonly kdfparams: {
|
|
253
250
|
readonly dklen: 32;
|
|
254
251
|
readonly n: number;
|
|
@@ -259,7 +256,7 @@ export declare function scryptAsync(options: scrypt.Options): Promise<{
|
|
|
259
256
|
readonly kdf: "scrypt";
|
|
260
257
|
} & {
|
|
261
258
|
iv: Bytes.Bytes;
|
|
262
|
-
}>;
|
|
259
|
+
}]>;
|
|
263
260
|
export declare namespace scryptAsync {
|
|
264
261
|
type Options = scrypt.Options;
|
|
265
262
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Keystore.d.ts","sourceRoot":"","sources":["../../core/Keystore.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AAGnC,OAAO,KAAK,KAAK,GAAG,MAAM,UAAU,CAAA;AAEpC
|
|
1
|
+
{"version":3,"file":"Keystore.d.ts","sourceRoot":"","sources":["../../core/Keystore.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AAGnC,OAAO,KAAK,KAAK,GAAG,MAAM,UAAU,CAAA;AAEpC,+BAA+B;AAC/B,KAAK,cAAc,CACjB,GAAG,SAAS,MAAM,GAAG,MAAM,EAC3B,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IACjE;IACF,EAAE,EAAE,KAAK,CAAC,KAAK,CAAA;IACf,SAAS,EAAE,SAAS,CAAA;IACpB,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,gBAAgB;AAChB,MAAM,MAAM,QAAQ,GAAG;IACrB,MAAM,EAAE;QAC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IAAI,CAAC,EAAE,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,GAAG,SAAS,CAAA;KACzC,CAAA;CACF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO;;;;;;;;;;QA4HJ,KAAK,CAAC,KAAK;IAvG/D;AAED,MAAM,CAAC,OAAO,WAAW,WAAW,CAAC;IACnC,KAAK,OAAO,GAAG,MAAM,CAAC,OAAO,CAAA;CAC9B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO;;;;;;;;;;;QAoFO,KAAK,CAAC,KAAK;GA9D/D;AAED,MAAM,CAAC,OAAO,WAAW,MAAM,CAAC;IAC9B,KAAK,OAAO,GAAG;QACb,qDAAqD;QACrD,EAAE,CAAC,EAAE,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,GAAG,SAAS,CAAA;QACtC,oCAAoC;QACpC,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QACtB,mCAAmC;QACnC,QAAQ,EAAE,MAAM,CAAA;QAChB,kEAAkE;QAClE,IAAI,CAAC,EAAE,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,GAAG,SAAS,CAAA;KACzC,CAAA;CACF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO;;;;;;;;;;;QAkCJ,KAAK,CAAC,KAAK;IAZ/D;AAED,MAAM,CAAC,OAAO,WAAW,WAAW,CAAC;IACnC,KAAK,OAAO,GAAG,MAAM,CAAC,OAAO,CAAA;CAC9B"}
|
package/_types/index.d.ts
CHANGED
|
@@ -1540,10 +1540,10 @@ export * as Json from './core/Json.js';
|
|
|
1540
1540
|
* const privateKey = Secp256k1.randomPrivateKey()
|
|
1541
1541
|
*
|
|
1542
1542
|
* // Derive a key from a password.
|
|
1543
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
1543
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
1544
1544
|
*
|
|
1545
1545
|
* // Encrypt the private key.
|
|
1546
|
-
* const encrypted = await Keystore.encrypt(privateKey, key)
|
|
1546
|
+
* const encrypted = await Keystore.encrypt(privateKey, key, opts)
|
|
1547
1547
|
* // @log: {
|
|
1548
1548
|
* // @log: "crypto": {
|
|
1549
1549
|
* // @log: "cipher": "aes-128-ctr",
|
package/_types/version.d.ts
CHANGED
package/core/Keystore.ts
CHANGED
|
@@ -13,13 +13,12 @@ import type * as Errors from './Errors.js'
|
|
|
13
13
|
import * as Hash from './Hash.js'
|
|
14
14
|
import type * as Hex from './Hex.js'
|
|
15
15
|
|
|
16
|
-
/** Base
|
|
17
|
-
type
|
|
16
|
+
/** Base Derivation Options. */
|
|
17
|
+
type BaseDeriveOpts<
|
|
18
18
|
kdf extends string = string,
|
|
19
19
|
kdfparams extends Record<string, unknown> = Record<string, unknown>,
|
|
20
20
|
> = {
|
|
21
21
|
iv: Bytes.Bytes
|
|
22
|
-
key: () => string
|
|
23
22
|
kdfparams: kdfparams
|
|
24
23
|
kdf: kdf
|
|
25
24
|
}
|
|
@@ -33,16 +32,19 @@ export type Keystore = {
|
|
|
33
32
|
iv: string
|
|
34
33
|
}
|
|
35
34
|
mac: string
|
|
36
|
-
} & Pick<
|
|
35
|
+
} & Pick<DeriveOpts, 'kdf' | 'kdfparams'>
|
|
37
36
|
id: string
|
|
38
37
|
version: 3
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
/** Key. */
|
|
42
|
-
export type Key =
|
|
41
|
+
export type Key = (() => Hex.Hex) | Hex.Hex
|
|
43
42
|
|
|
44
|
-
/**
|
|
45
|
-
export type
|
|
43
|
+
/** Derivation Options. */
|
|
44
|
+
export type DeriveOpts = Pbkdf2DeriveOpts | ScryptDeriveOpts
|
|
45
|
+
|
|
46
|
+
/** PBKDF2 Derivation Options. */
|
|
47
|
+
export type Pbkdf2DeriveOpts = BaseDeriveOpts<
|
|
46
48
|
'pbkdf2',
|
|
47
49
|
{
|
|
48
50
|
c: number
|
|
@@ -52,8 +54,8 @@ export type Pbkdf2Key = BaseKey<
|
|
|
52
54
|
}
|
|
53
55
|
>
|
|
54
56
|
|
|
55
|
-
/** Scrypt
|
|
56
|
-
export type
|
|
57
|
+
/** Scrypt Derivation Options. */
|
|
58
|
+
export type ScryptDeriveOpts = BaseDeriveOpts<
|
|
57
59
|
'scrypt',
|
|
58
60
|
{
|
|
59
61
|
dklen: number
|
|
@@ -80,8 +82,8 @@ export type ScryptKey = BaseKey<
|
|
|
80
82
|
* // JSON keystore.
|
|
81
83
|
* const keystore = { crypto: { ... }, id: '...', version: 3 }
|
|
82
84
|
*
|
|
83
|
-
* //
|
|
84
|
-
* const key =
|
|
85
|
+
* // Key that was previously derived from `Keystore.scrypt` or `Keystore.pbkdf2`.
|
|
86
|
+
* const key = "0x..."
|
|
85
87
|
*
|
|
86
88
|
* // Decrypt the private key.
|
|
87
89
|
* const privateKey = await Keystore.decrypt(keystore, key)
|
|
@@ -99,7 +101,7 @@ export async function decrypt<as extends 'Hex' | 'Bytes' = 'Hex'>(
|
|
|
99
101
|
options: decrypt.Options<as> = {},
|
|
100
102
|
): Promise<decrypt.ReturnType<as>> {
|
|
101
103
|
const { as = 'Hex' } = options
|
|
102
|
-
const key_ = Bytes.from(
|
|
104
|
+
const key_ = Bytes.from(typeof key === 'function' ? key() : key)
|
|
103
105
|
|
|
104
106
|
const encKey = Bytes.slice(key_, 0, 16)
|
|
105
107
|
const macKey = Bytes.slice(key_, 16, 32)
|
|
@@ -110,7 +112,10 @@ export async function decrypt<as extends 'Hex' | 'Bytes' = 'Hex'>(
|
|
|
110
112
|
if (!Bytes.isEqual(mac, Bytes.from(`0x${keystore.crypto.mac}`)))
|
|
111
113
|
throw new Error('corrupt keystore')
|
|
112
114
|
|
|
113
|
-
const data = ctr(
|
|
115
|
+
const data = ctr(
|
|
116
|
+
encKey,
|
|
117
|
+
Bytes.from(`0x${keystore.crypto.cipherparams.iv}`),
|
|
118
|
+
).decrypt(ciphertext)
|
|
114
119
|
|
|
115
120
|
if (as === 'Hex') return Bytes.toHex(data) as never
|
|
116
121
|
return data as never
|
|
@@ -143,10 +148,10 @@ export declare namespace decrypt {
|
|
|
143
148
|
* const privateKey = Secp256k1.randomPrivateKey()
|
|
144
149
|
*
|
|
145
150
|
* // Derive key from password.
|
|
146
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
151
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
147
152
|
*
|
|
148
153
|
* // Encrypt the private key.
|
|
149
|
-
* const encrypted = await Keystore.encrypt(privateKey, key)
|
|
154
|
+
* const encrypted = await Keystore.encrypt(privateKey, key, opts)
|
|
150
155
|
* // @log: {
|
|
151
156
|
* // @log: "crypto": {
|
|
152
157
|
* // @log: "cipher": "aes-128-ctr",
|
|
@@ -176,26 +181,26 @@ export declare namespace decrypt {
|
|
|
176
181
|
export async function encrypt(
|
|
177
182
|
privateKey: Bytes.Bytes | Hex.Hex,
|
|
178
183
|
key: Key,
|
|
179
|
-
options: encrypt.Options
|
|
184
|
+
options: encrypt.Options,
|
|
180
185
|
): Promise<Keystore> {
|
|
181
|
-
const { id = crypto.randomUUID() } = options
|
|
186
|
+
const { id = crypto.randomUUID(), kdf, kdfparams, iv } = options
|
|
182
187
|
|
|
183
|
-
const key_ = Bytes.from(
|
|
188
|
+
const key_ = Bytes.from(typeof key === 'function' ? key() : key)
|
|
184
189
|
const value_ = Bytes.from(privateKey)
|
|
185
190
|
|
|
186
191
|
const encKey = Bytes.slice(key_, 0, 16)
|
|
187
192
|
const macKey = Bytes.slice(key_, 16, 32)
|
|
188
193
|
|
|
189
|
-
const ciphertext = ctr(encKey,
|
|
194
|
+
const ciphertext = ctr(encKey, iv).encrypt(value_)
|
|
190
195
|
const mac = Hash.keccak256(Bytes.concat(macKey, ciphertext))
|
|
191
196
|
|
|
192
197
|
return {
|
|
193
198
|
crypto: {
|
|
194
199
|
cipher: 'aes-128-ctr',
|
|
195
200
|
ciphertext: Bytes.toHex(ciphertext).slice(2),
|
|
196
|
-
cipherparams: { iv: Bytes.toHex(
|
|
197
|
-
kdf
|
|
198
|
-
kdfparams
|
|
201
|
+
cipherparams: { iv: Bytes.toHex(iv).slice(2) },
|
|
202
|
+
kdf,
|
|
203
|
+
kdfparams,
|
|
199
204
|
mac: Bytes.toHex(mac).slice(2),
|
|
200
205
|
} as Keystore['crypto'],
|
|
201
206
|
id,
|
|
@@ -204,7 +209,7 @@ export async function encrypt(
|
|
|
204
209
|
}
|
|
205
210
|
|
|
206
211
|
export declare namespace encrypt {
|
|
207
|
-
type Options = {
|
|
212
|
+
type Options = DeriveOpts & {
|
|
208
213
|
/** UUID. */
|
|
209
214
|
id?: string | undefined
|
|
210
215
|
}
|
|
@@ -217,7 +222,7 @@ export declare namespace encrypt {
|
|
|
217
222
|
* ```ts twoslash
|
|
218
223
|
* import { Keystore } from 'ox'
|
|
219
224
|
*
|
|
220
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
225
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
221
226
|
* ```
|
|
222
227
|
*
|
|
223
228
|
* @param options - PBKDF2 options.
|
|
@@ -229,11 +234,10 @@ export function pbkdf2(options: pbkdf2.Options) {
|
|
|
229
234
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32)
|
|
230
235
|
const key = Bytes.toHex(
|
|
231
236
|
pbkdf2_noble(sha256, password, salt, { c: iterations, dkLen: 32 }),
|
|
232
|
-
)
|
|
237
|
+
)
|
|
233
238
|
|
|
234
|
-
return defineKey({
|
|
239
|
+
return defineKey(() => key, {
|
|
235
240
|
iv,
|
|
236
|
-
key: () => key,
|
|
237
241
|
kdfparams: {
|
|
238
242
|
c: iterations,
|
|
239
243
|
dklen: 32,
|
|
@@ -241,7 +245,7 @@ export function pbkdf2(options: pbkdf2.Options) {
|
|
|
241
245
|
salt: Bytes.toHex(salt).slice(2),
|
|
242
246
|
},
|
|
243
247
|
kdf: 'pbkdf2',
|
|
244
|
-
}) satisfies
|
|
248
|
+
}) satisfies [Key, Pbkdf2DeriveOpts]
|
|
245
249
|
}
|
|
246
250
|
|
|
247
251
|
export declare namespace pbkdf2 {
|
|
@@ -264,7 +268,7 @@ export declare namespace pbkdf2 {
|
|
|
264
268
|
* ```ts twoslash
|
|
265
269
|
* import { Keystore } from 'ox'
|
|
266
270
|
*
|
|
267
|
-
* const key = await Keystore.pbkdf2Async({ password: 'testpassword' })
|
|
271
|
+
* const [key, opts] = await Keystore.pbkdf2Async({ password: 'testpassword' })
|
|
268
272
|
* ```
|
|
269
273
|
*
|
|
270
274
|
* @param options - PBKDF2 options.
|
|
@@ -279,11 +283,10 @@ export async function pbkdf2Async(options: pbkdf2.Options) {
|
|
|
279
283
|
c: iterations,
|
|
280
284
|
dkLen: 32,
|
|
281
285
|
}),
|
|
282
|
-
)
|
|
286
|
+
)
|
|
283
287
|
|
|
284
|
-
return defineKey({
|
|
288
|
+
return defineKey(() => key, {
|
|
285
289
|
iv,
|
|
286
|
-
key: () => key,
|
|
287
290
|
kdfparams: {
|
|
288
291
|
c: iterations,
|
|
289
292
|
dklen: 32,
|
|
@@ -291,7 +294,7 @@ export async function pbkdf2Async(options: pbkdf2.Options) {
|
|
|
291
294
|
salt: Bytes.toHex(salt).slice(2),
|
|
292
295
|
},
|
|
293
296
|
kdf: 'pbkdf2',
|
|
294
|
-
}) satisfies
|
|
297
|
+
}) satisfies [Key, Pbkdf2DeriveOpts]
|
|
295
298
|
}
|
|
296
299
|
|
|
297
300
|
export declare namespace pbkdf2Async {
|
|
@@ -305,7 +308,7 @@ export declare namespace pbkdf2Async {
|
|
|
305
308
|
* ```ts twoslash
|
|
306
309
|
* import { Keystore } from 'ox'
|
|
307
310
|
*
|
|
308
|
-
* const key = Keystore.scrypt({ password: 'testpassword' })
|
|
311
|
+
* const [key, opts] = Keystore.scrypt({ password: 'testpassword' })
|
|
309
312
|
* ```
|
|
310
313
|
*
|
|
311
314
|
* @param options - Scrypt options.
|
|
@@ -320,11 +323,10 @@ export function scrypt(options: scrypt.Options) {
|
|
|
320
323
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32)
|
|
321
324
|
const key = Bytes.toHex(
|
|
322
325
|
scrypt_noble(password, salt, { N: n, dkLen: 32, r, p }),
|
|
323
|
-
)
|
|
326
|
+
)
|
|
324
327
|
|
|
325
|
-
return defineKey({
|
|
328
|
+
return defineKey(() => key, {
|
|
326
329
|
iv,
|
|
327
|
-
key: () => key,
|
|
328
330
|
kdfparams: {
|
|
329
331
|
dklen: 32,
|
|
330
332
|
n,
|
|
@@ -333,7 +335,7 @@ export function scrypt(options: scrypt.Options) {
|
|
|
333
335
|
salt: Bytes.toHex(salt).slice(2),
|
|
334
336
|
},
|
|
335
337
|
kdf: 'scrypt',
|
|
336
|
-
}) satisfies
|
|
338
|
+
}) satisfies [Key, ScryptDeriveOpts]
|
|
337
339
|
}
|
|
338
340
|
|
|
339
341
|
export declare namespace scrypt {
|
|
@@ -356,7 +358,7 @@ export declare namespace scrypt {
|
|
|
356
358
|
* ```ts twoslash
|
|
357
359
|
* import { Keystore } from 'ox'
|
|
358
360
|
*
|
|
359
|
-
* const key = await Keystore.scryptAsync({ password: 'testpassword' })
|
|
361
|
+
* const [key, opts] = await Keystore.scryptAsync({ password: 'testpassword' })
|
|
360
362
|
* ```
|
|
361
363
|
*
|
|
362
364
|
* @param options - Scrypt options.
|
|
@@ -371,11 +373,10 @@ export async function scryptAsync(options: scrypt.Options) {
|
|
|
371
373
|
const salt = options.salt ? Bytes.from(options.salt) : Bytes.random(32)
|
|
372
374
|
const key = Bytes.toHex(
|
|
373
375
|
await scryptAsync_noble(password, salt, { N: n, dkLen: 32, r, p }),
|
|
374
|
-
)
|
|
376
|
+
)
|
|
375
377
|
|
|
376
|
-
return defineKey({
|
|
378
|
+
return defineKey(() => key, {
|
|
377
379
|
iv,
|
|
378
|
-
key: () => key,
|
|
379
380
|
kdfparams: {
|
|
380
381
|
dklen: 32,
|
|
381
382
|
n,
|
|
@@ -384,7 +385,7 @@ export async function scryptAsync(options: scrypt.Options) {
|
|
|
384
385
|
salt: Bytes.toHex(salt).slice(2),
|
|
385
386
|
},
|
|
386
387
|
kdf: 'scrypt',
|
|
387
|
-
}) satisfies
|
|
388
|
+
}) satisfies [Key, ScryptDeriveOpts]
|
|
388
389
|
}
|
|
389
390
|
|
|
390
391
|
export declare namespace scryptAsync {
|
|
@@ -394,19 +395,20 @@ export declare namespace scryptAsync {
|
|
|
394
395
|
///////////////////////////////////////////////////////////////////////////
|
|
395
396
|
|
|
396
397
|
/** @internal */
|
|
397
|
-
function defineKey<
|
|
398
|
-
key
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
398
|
+
function defineKey<
|
|
399
|
+
const key extends Key,
|
|
400
|
+
const options extends defineKey.Options,
|
|
401
|
+
>(key: key, options: options): [key, options & { iv: Bytes.Bytes }] {
|
|
402
|
+
const iv = options.iv ? Bytes.from(options.iv) : Bytes.random(16)
|
|
403
|
+
return [key, { ...options, iv }] as never
|
|
402
404
|
}
|
|
403
405
|
|
|
404
406
|
/** @internal */
|
|
405
407
|
declare namespace defineKey {
|
|
406
|
-
type
|
|
408
|
+
type Options<
|
|
407
409
|
kdf extends string = string,
|
|
408
410
|
kdfparams extends Record<string, unknown> = Record<string, unknown>,
|
|
409
|
-
> = Omit<
|
|
411
|
+
> = Omit<BaseDeriveOpts<kdf, kdfparams>, 'iv'> & {
|
|
410
412
|
iv?: Bytes.Bytes | Hex.Hex | undefined
|
|
411
413
|
}
|
|
412
414
|
|
package/index.ts
CHANGED
|
@@ -1574,10 +1574,10 @@ export * as Json from './core/Json.js'
|
|
|
1574
1574
|
* const privateKey = Secp256k1.randomPrivateKey()
|
|
1575
1575
|
*
|
|
1576
1576
|
* // Derive a key from a password.
|
|
1577
|
-
* const key = Keystore.pbkdf2({ password: 'testpassword' })
|
|
1577
|
+
* const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' })
|
|
1578
1578
|
*
|
|
1579
1579
|
* // Encrypt the private key.
|
|
1580
|
-
* const encrypted = await Keystore.encrypt(privateKey, key)
|
|
1580
|
+
* const encrypted = await Keystore.encrypt(privateKey, key, opts)
|
|
1581
1581
|
* // @log: {
|
|
1582
1582
|
* // @log: "crypto": {
|
|
1583
1583
|
* // @log: "cipher": "aes-128-ctr",
|
package/package.json
CHANGED
package/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @internal */
|
|
2
|
-
export const version = '0.
|
|
2
|
+
export const version = '0.8.0'
|