-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKeyringInternalSnapClient.ts
More file actions
145 lines (133 loc) · 4.37 KB
/
Copy pathKeyringInternalSnapClient.ts
File metadata and controls
145 lines (133 loc) · 4.37 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
139
140
141
142
143
144
145
import { KeyringRpcMethod } from '@metamask/keyring-api';
import type {
LegacyKeyringRequest,
LegacyKeyringResponse,
} from '@metamask/keyring-internal-api';
import { SubmitRequestResponseV1Struct } from '@metamask/keyring-internal-api';
import { KeyringClient, type Sender } from '@metamask/keyring-snap-client';
import { strictMask, type JsonRpcRequest } from '@metamask/keyring-utils';
import type { Messenger } from '@metamask/messenger';
import type { HandleSnapRequest } from '@metamask/snaps-controllers';
import type { SnapId } from '@metamask/snaps-sdk';
import type { HandlerType } from '@metamask/snaps-utils';
import type { Json } from '@metamask/utils';
// We only need to dispatch Snap request to the Snaps controller for now.
type AllowedActions = HandleSnapRequest;
/**
* A restricted-`Messenger` used by `KeyringInternalSnapClient` to dispatch
* internal Snap requests.
*/
export type KeyringInternalSnapClientMessenger = Messenger<
'KeyringInternalSnapClient',
AllowedActions
>;
/**
* Implementation of the `Sender` interface that can be used to send requests
* to a Snap through a `Messenger`.
*/
class SnapControllerMessengerSender implements Sender {
readonly #snapId: SnapId;
readonly #origin: string;
readonly #messenger: KeyringInternalSnapClientMessenger;
readonly #handler: HandlerType;
/**
* Create a new instance of `SnapControllerSender`.
*
* @param messenger - The `Messenger` instance used when dispatching controllers actions.
* @param snapId - The ID of the Snap to use.
* @param origin - The sender's origin.
* @param handler - The handler type.
*/
constructor(
messenger: KeyringInternalSnapClientMessenger,
snapId: SnapId,
origin: string,
handler: HandlerType,
) {
this.#messenger = messenger;
this.#snapId = snapId;
this.#origin = origin;
this.#handler = handler;
}
/**
* Send a request to the Snap and return the response.
*
* @param request - JSON-RPC request to send to the Snap.
* @returns A promise that resolves to the response of the request.
*/
async send(request: JsonRpcRequest): Promise<Json> {
return this.#messenger.call('SnapController:handleRequest', {
snapId: this.#snapId,
origin: this.#origin,
handler: this.#handler,
request,
}) as Promise<Json>;
}
}
/**
* A `KeyringClient` that allows the communication with a Snap through a
* `Messenger`.
*/
export class KeyringInternalSnapClient extends KeyringClient {
readonly #messenger: KeyringInternalSnapClientMessenger;
/**
* Create a new instance of `KeyringInternalSnapClient`.
*
* The `handlerType` argument has a hard-coded default `string` value instead
* of a `HandlerType` value to prevent the `@metamask/snaps-utils` module
* from being required at runtime.
*
* @param args - Constructor arguments.
* @param args.messenger - The `KeyringInternalSnapClientMessenger` instance to use.
* @param args.snapId - The ID of the Snap to use (default: `'undefined'`).
* @param args.origin - The sender's origin (default: `'metamask'`).
* @param args.handler - The handler type (default: `'onKeyringRequest'`).
*/
constructor({
messenger,
snapId = 'undefined' as SnapId,
origin = 'metamask',
handler = 'onKeyringRequest' as HandlerType,
}: {
messenger: KeyringInternalSnapClientMessenger;
snapId?: SnapId;
origin?: string;
handler?: HandlerType;
}) {
super(
new SnapControllerMessengerSender(messenger, snapId, origin, handler),
);
this.#messenger = messenger;
}
/**
* Create a new instance of `KeyringInternalSnapClient` with the specified
* `snapId`.
*
* @param snapId - The ID of the Snap to use in the new instance.
* @returns A new instance of `KeyringInternalSnapClient` with the
* specified Snap ID.
*/
withSnapId(snapId: SnapId): KeyringInternalSnapClient {
return new KeyringInternalSnapClient({
messenger: this.#messenger,
snapId,
});
}
/**
* Submit a keyring request v1 (with no `origin`).
*
* @param request - Keyring request.
* @returns Keyring request's response.
*/
async submitLegacyRequest(
request: LegacyKeyringRequest,
): Promise<LegacyKeyringResponse> {
return strictMask(
await this.send({
method: KeyringRpcMethod.SubmitRequest,
params: request,
}),
SubmitRequestResponseV1Struct,
);
}
}