-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprivate-key.test.ts
More file actions
62 lines (56 loc) · 1.8 KB
/
Copy pathprivate-key.test.ts
File metadata and controls
62 lines (56 loc) · 1.8 KB
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
import type { StructError } from '@metamask/superstruct';
import { assert } from '@metamask/superstruct';
import { PrivateKeyExportedAccountStruct } from './private-key';
const SENSITIVE_REDACTED = '***';
const RAW_PRIVATE_KEY =
'0xdeadbeef1234567890abcdef1234567890abcdef1234567890abcdef12345678';
describe('PrivateKeyExportedAccountStruct', () => {
it('accepts a valid exported account', () => {
expect(() =>
assert(
{
type: 'private-key',
privateKey: RAW_PRIVATE_KEY,
encoding: 'hexadecimal',
},
PrivateKeyExportedAccountStruct,
),
).not.toThrow();
});
it('redacts the private key value from the error when `privateKey` is invalid', () => {
let error: StructError | undefined;
try {
assert(
{ type: 'private-key', privateKey: 123, encoding: 'hexadecimal' },
PrivateKeyExportedAccountStruct,
);
} catch (caughtError) {
error = caughtError as StructError;
}
expect(error?.value).toBe(SENSITIVE_REDACTED);
expect(error?.message).toContain(SENSITIVE_REDACTED);
expect(error?.message).not.toContain('123');
});
it('redacts the private key from `branch` when a sibling field fails', () => {
let error: StructError | undefined;
try {
assert(
{
type: 'private-key',
privateKey: RAW_PRIVATE_KEY,
encoding: 'invalid-encoding',
},
PrivateKeyExportedAccountStruct,
);
} catch (caughtError) {
error = caughtError as StructError;
}
expect(error?.message).toContain('encoding');
const allBranchItems = (error?.failures() ?? []).flatMap(
(failure) => failure.branch,
);
expect(allBranchItems).not.toContainEqual(
expect.objectContaining({ privateKey: RAW_PRIVATE_KEY }),
);
});
});