-
Notifications
You must be signed in to change notification settings - Fork 645
Expand file tree
/
Copy pathgetBip32Entropy.ts
More file actions
138 lines (124 loc) · 4.63 KB
/
getBip32Entropy.ts
File metadata and controls
138 lines (124 loc) · 4.63 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
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
import type { CryptographicFunctions } from '@metamask/key-tree';
import type {
PermissionSpecificationBuilder,
PermissionValidatorConstraint,
RestrictedMethodOptions,
ValidPermissionSpecification,
} from '@metamask/permission-controller';
import { PermissionType, SubjectType } from '@metamask/permission-controller';
import { rpcErrors } from '@metamask/rpc-errors';
import type {
GetBip32EntropyParams,
GetBip32EntropyResult,
} from '@metamask/snaps-sdk';
import { SnapCaveatType } from '@metamask/snaps-utils';
import type { NonEmptyArray } from '@metamask/utils';
import { assert } from '@metamask/utils';
import type { MethodHooksObject } from '../utils';
import { getNode } from '../utils';
const targetName = 'snap_getBip32Entropy';
export type GetBip32EntropyMethodHooks = {
/**
* @param keyringId - The ID of the keyring to get the mnemonic for.
* @returns The mnemonic of the user's keyring, if the keyringId is not provided, it will return the mnemonic of the primary keyring.
*/
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;
/**
* Waits for the extension to be unlocked.
*
* @returns A promise that resolves once the extension is unlocked.
*/
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
/**
* Get the cryptographic functions to use for the client. This may return an
* empty object or `undefined` to fall back to the default cryptographic
* functions.
*
* @returns The cryptographic functions to use for the client.
*/
getClientCryptography: () => CryptographicFunctions | undefined;
};
type GetBip32EntropySpecificationBuilderOptions = {
methodHooks: GetBip32EntropyMethodHooks;
};
type GetBip32EntropySpecification = ValidPermissionSpecification<{
permissionType: PermissionType.RestrictedMethod;
targetName: typeof targetName;
methodImplementation: ReturnType<typeof getBip32EntropyImplementation>;
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
validator: PermissionValidatorConstraint;
}>;
/**
* The specification builder for the `snap_getBip32Entropy` permission.
* `snap_getBip32Entropy` lets the Snap control private keys for a particular
* BIP-32 node.
*
* @param options - The specification builder options.
* @param options.methodHooks - The RPC method hooks needed by the method implementation.
* @returns The specification for the `snap_getBip32Entropy` permission.
*/
const specificationBuilder: PermissionSpecificationBuilder<
PermissionType.RestrictedMethod,
GetBip32EntropySpecificationBuilderOptions,
GetBip32EntropySpecification
> = ({ methodHooks }: GetBip32EntropySpecificationBuilderOptions) => {
return {
permissionType: PermissionType.RestrictedMethod,
targetName,
allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],
methodImplementation: getBip32EntropyImplementation(methodHooks),
validator: ({ caveats }) => {
if (
caveats?.length !== 1 ||
caveats[0].type !== SnapCaveatType.PermittedDerivationPaths
) {
throw rpcErrors.invalidParams({
message: `Expected a single "${SnapCaveatType.PermittedDerivationPaths}" caveat.`,
});
}
},
subjectTypes: [SubjectType.Snap],
};
};
const methodHooks: MethodHooksObject<GetBip32EntropyMethodHooks> = {
getMnemonic: true,
getUnlockPromise: true,
getClientCryptography: true,
};
export const getBip32EntropyBuilder = Object.freeze({
targetName,
specificationBuilder,
methodHooks,
} as const);
/**
* Builds the method implementation for `snap_getBip32Entropy`.
*
* @param hooks - The RPC method hooks.
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
* and prompts the user to unlock their MetaMask if it is locked.
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
* functions to use for the client.
* @returns The method implementation which returns a `JsonSLIP10Node`.
* @throws If the params are invalid.
*/
export function getBip32EntropyImplementation({
getMnemonic,
getUnlockPromise,
getClientCryptography,
}: GetBip32EntropyMethodHooks) {
return async function getBip32Entropy(
args: RestrictedMethodOptions<GetBip32EntropyParams>,
): Promise<GetBip32EntropyResult> {
await getUnlockPromise(true);
const { params } = args;
assert(params);
const node = await getNode({
curve: params.curve,
path: params.path,
secretRecoveryPhrase: await getMnemonic(params.keyringId),
cryptographicFunctions: getClientCryptography(),
});
return node.toJSON();
};
}