-
Notifications
You must be signed in to change notification settings - Fork 542
/
getBip32Entropy.test.ts
318 lines (288 loc) · 10.4 KB
/
getBip32Entropy.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { SIP_6_MAGIC_VALUE, SnapCaveatType } from '@metamask/snaps-utils';
import { TEST_SECRET_RECOVERY_PHRASE } from '@metamask/snaps-utils/test-utils';
import {
getBip32EntropyBuilder,
getBip32EntropyCaveatMapper,
getBip32EntropyCaveatSpecifications,
getBip32EntropyImplementation,
validateCaveatPaths,
} from './getBip32Entropy';
describe('validateCaveatPaths', () => {
it.each([[], null, undefined, 'foo'])(
'throws if the value is not an array or empty',
(value) => {
expect(() =>
validateCaveatPaths({
type: SnapCaveatType.PermittedDerivationPaths,
value,
}),
).toThrow(
/^Invalid BIP-32 entropy caveat: At path: value -- Expected an? array/u,
); // Different error messages for different types
},
);
it('throws if any of the paths is invalid', () => {
expect(() =>
validateCaveatPaths({
type: SnapCaveatType.PermittedDerivationPaths,
value: [{ path: ['foo'], curve: 'secp256k1' }],
}),
).toThrow('At path: value.0.path -- Path must start with "m".');
});
});
describe('specificationBuilder', () => {
const methodHooks = {
getMnemonic: jest.fn(),
getUnlockPromise: jest.fn(),
};
const specification = getBip32EntropyBuilder.specificationBuilder({
methodHooks,
});
describe('validator', () => {
it('throws if the caveat is not a single "permittedDerivationPaths"', () => {
expect(() =>
// @ts-expect-error Missing required permission types.
specification.validator({}),
).toThrow('Expected a single "permittedDerivationPaths" caveat.');
expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [{ type: 'foo', value: 'bar' }],
}),
).toThrow('Expected a single "permittedDerivationPaths" caveat.');
expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [
{ type: 'permittedDerivationPaths', value: [] },
{ type: 'permittedDerivationPaths', value: [] },
],
}),
).toThrow('Expected a single "permittedDerivationPaths" caveat.');
});
});
});
describe('getBip32EntropyCaveatMapper', () => {
it('returns a caveat value for an array of paths', () => {
expect(
getBip32EntropyCaveatMapper([
{ path: ['m', "44'", "60'"], curve: 'secp256k1' },
{ path: ['m', "0'", "0'"], curve: 'ed25519' },
]),
).toStrictEqual({
caveats: [
{
type: SnapCaveatType.PermittedDerivationPaths,
value: [
{ path: ['m', "44'", "60'"], curve: 'secp256k1' },
{ path: ['m', "0'", "0'"], curve: 'ed25519' },
],
},
],
});
});
});
describe('getBip32EntropyCaveatSpecifications', () => {
describe('decorator', () => {
const params = { path: ['m', "44'", "60'"], curve: 'secp256k1' };
it('returns the result of the method implementation', async () => {
const fn = jest.fn().mockImplementation(() => 'foo');
expect(
await getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].decorator(fn, {
type: SnapCaveatType.PermittedDerivationPaths,
value: [params],
// @ts-expect-error Missing other required properties.
})({ params }),
).toBe('foo');
});
it('allows deriving child nodes', async () => {
const fn = jest.fn().mockImplementation(() => 'foo');
expect(
await getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].decorator(fn, {
type: SnapCaveatType.PermittedDerivationPaths,
value: [params],
// @ts-expect-error Missing other required properties.
})({
params: {
path: ['m', "44'", "60'", "0'", '0', '1'],
curve: 'secp256k1',
},
}),
).toBe('foo');
});
it('allows deriving deep nodes', async () => {
const fn = jest.fn().mockImplementation(() => 'foo');
expect(
await getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].decorator(fn, {
type: SnapCaveatType.PermittedDerivationPaths,
value: [params],
// @ts-expect-error Missing other required properties.
})({
params: {
path: [
'm',
"44'",
"60'",
"0'",
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
],
curve: 'secp256k1',
},
}),
).toBe('foo');
});
it('ignores unknown fields', async () => {
const fn = jest.fn().mockImplementation(() => 'foo');
expect(
await getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].decorator(fn, {
type: SnapCaveatType.PermittedDerivationPaths,
value: [params],
// @ts-expect-error Missing other required properties.
})({
params: {
path: ['m', "44'", "60'", "0'", '0', '1'],
curve: 'secp256k1',
compressed: true,
},
}),
).toBe('foo');
});
it('throws if the path is invalid', async () => {
const fn = jest.fn().mockImplementation(() => 'foo');
await expect(
getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].decorator(fn, {
type: SnapCaveatType.PermittedDerivationPaths,
value: [params],
// @ts-expect-error Missing other required properties.
})({ params: { ...params, path: [] } }),
).rejects.toThrow(
'At path: path -- Path must be a non-empty BIP-32 derivation path array',
);
});
it('throws if the path is not specified in the caveats', async () => {
const fn = jest.fn().mockImplementation(() => 'foo');
await expect(
getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].decorator(fn, {
type: SnapCaveatType.PermittedDerivationPaths,
value: [params],
// @ts-expect-error Missing other required properties.
})({ params: { ...params, path: ['m', "44'", "0'"] } }),
).rejects.toThrow(
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
);
});
it('throws if the purpose is not allowed', async () => {
const fn = jest.fn().mockImplementation(() => 'foo');
await expect(
getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].decorator(fn, {
type: SnapCaveatType.PermittedDerivationPaths,
value: [params],
// @ts-expect-error Missing other required properties.
})({ params: { ...params, path: ['m', SIP_6_MAGIC_VALUE, "0'"] } }),
).rejects.toThrow(
'Invalid BIP-32 entropy path definition: At path: path -- The purpose "1399742832\'" is not allowed for entropy derivation.',
);
});
});
describe('validator', () => {
it('throws if the caveat values are invalid', () => {
expect(() =>
getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].validator?.({
type: SnapCaveatType.PermittedDerivationPaths,
value: [{ path: ['foo'], curve: 'secp256k1' }],
}),
).toThrow('At path: value.0.path -- Path must start with "m".');
});
it('throws if the caveat values contain forbidden paths', () => {
expect(() =>
getBip32EntropyCaveatSpecifications[
SnapCaveatType.PermittedDerivationPaths
].validator?.({
type: SnapCaveatType.PermittedDerivationPaths,
value: [{ path: ['m', SIP_6_MAGIC_VALUE, "0'"], curve: 'secp256k1' }],
}),
).toThrow(
'Invalid BIP-32 entropy caveat: At path: value.0.path -- The purpose "1399742832\'" is not allowed for entropy derivation.',
);
});
});
});
describe('getBip32EntropyImplementation', () => {
describe('getBip32Entropy', () => {
it('derives the entropy from the path', async () => {
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
const getMnemonic = jest
.fn()
.mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE);
expect(
// @ts-expect-error Missing other required properties.
await getBip32EntropyImplementation({ getUnlockPromise, getMnemonic })({
params: { path: ['m', "44'", "60'"], curve: 'secp256k1' },
}),
).toMatchInlineSnapshot(`
{
"chainCode": "0xc4d424c253ca0eab92de6d8c819a37889e15a11bbf1cb6a48ffca2faef1f4d4d",
"curve": "secp256k1",
"depth": 2,
"index": 2147483708,
"masterFingerprint": 1404659567,
"parentFingerprint": 1829122711,
"privateKey": "0xca8d3571710e2b08628926f0ec14983aded0fd039518c59522c004e0e7eb4f5a",
"publicKey": "0x041e31e8432aab932fe18b5f9798b7252394ff0b943920b40c50a79301062df5ece2b884a45c456241e35000137e6dbd92c9119ccd5f46cc92ba9568ca661b994b",
}
`);
});
it('derives a BIP-44 path', async () => {
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
const getMnemonic = jest
.fn()
.mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE);
expect(
// @ts-expect-error Missing other required properties.
await getBip32EntropyImplementation({ getUnlockPromise, getMnemonic })({
params: {
path: ['m', "44'", "60'", "0'", '0', '1'],
curve: 'secp256k1',
},
}),
).toMatchInlineSnapshot(`
{
"chainCode": "0x6265b647bc0e70480f29856be102fe866ea6a8ec9e2926c198c2e9c4cd268a43",
"curve": "secp256k1",
"depth": 5,
"index": 1,
"masterFingerprint": 1404659567,
"parentFingerprint": 942995271,
"privateKey": "0x4adb19cafa5fdf467215fa30b56a50facac2dee40a7015063c6a7a0f1f4e2576",
"publicKey": "0x04b21938e18aec1e2e7478988ccae5b556597d771c8e46ac2c8ea2a4a1a80619679230a109cd30e8af15856b15799e38991e45e55f406a8a24d5605ba0757da53c",
}
`);
});
});
});