@metamask-previews/keyring-api 8.1.0-38c4bd5
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 +428 -0
- package/README.md +401 -0
- package/dist/JsonRpcRequest.d.ts +16 -0
- package/dist/JsonRpcRequest.js +13 -0
- package/dist/JsonRpcRequest.js.map +1 -0
- package/dist/KeyringClient.d.ts +28 -0
- package/dist/KeyringClient.js +124 -0
- package/dist/KeyringClient.js.map +1 -0
- package/dist/KeyringSnapRpcClient.d.ts +39 -0
- package/dist/KeyringSnapRpcClient.js +68 -0
- package/dist/KeyringSnapRpcClient.js.map +1 -0
- package/dist/api/account.d.ts +62 -0
- package/dist/api/account.js +56 -0
- package/dist/api/account.js.map +1 -0
- package/dist/api/balance.d.ts +9 -0
- package/dist/api/balance.js +11 -0
- package/dist/api/balance.js.map +1 -0
- package/dist/api/caip.d.ts +37 -0
- package/dist/api/caip.js +48 -0
- package/dist/api/caip.js.map +1 -0
- package/dist/api/export.d.ts +8 -0
- package/dist/api/export.js +7 -0
- package/dist/api/export.js.map +1 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.js +24 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/keyring.d.ts +159 -0
- package/dist/api/keyring.js +3 -0
- package/dist/api/keyring.js.map +1 -0
- package/dist/api/request.d.ts +39 -0
- package/dist/api/request.js +29 -0
- package/dist/api/request.js.map +1 -0
- package/dist/api/response.d.ts +24 -0
- package/dist/api/response.js +44 -0
- package/dist/api/response.js.map +1 -0
- package/dist/btc/index.d.ts +1 -0
- package/dist/btc/index.js +18 -0
- package/dist/btc/index.js.map +1 -0
- package/dist/btc/types.d.ts +33 -0
- package/dist/btc/types.js +40 -0
- package/dist/btc/types.js.map +1 -0
- package/dist/contexts.d.ts +7 -0
- package/dist/contexts.js +3 -0
- package/dist/contexts.js.map +1 -0
- package/dist/eth/erc4337/index.d.ts +1 -0
- package/dist/eth/erc4337/index.js +18 -0
- package/dist/eth/erc4337/index.js.map +1 -0
- package/dist/eth/erc4337/types.d.ts +96 -0
- package/dist/eth/erc4337/types.js +61 -0
- package/dist/eth/erc4337/types.js.map +1 -0
- package/dist/eth/index.d.ts +3 -0
- package/dist/eth/index.js +20 -0
- package/dist/eth/index.js.map +1 -0
- package/dist/eth/types.d.ts +80 -0
- package/dist/eth/types.js +73 -0
- package/dist/eth/types.js.map +1 -0
- package/dist/eth/utils.d.ts +8 -0
- package/dist/eth/utils.js +15 -0
- package/dist/eth/utils.js.map +1 -0
- package/dist/events.d.ts +10 -0
- package/dist/events.js +17 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/api.d.ts +415 -0
- package/dist/internal/api.js +139 -0
- package/dist/internal/api.js.map +1 -0
- package/dist/internal/eth/EthKeyring.d.ts +33 -0
- package/dist/internal/eth/EthKeyring.js +3 -0
- package/dist/internal/eth/EthKeyring.js.map +1 -0
- package/dist/internal/eth/index.d.ts +1 -0
- package/dist/internal/eth/index.js +18 -0
- package/dist/internal/eth/index.js.map +1 -0
- package/dist/internal/events.d.ts +159 -0
- package/dist/internal/events.js +72 -0
- package/dist/internal/events.js.map +1 -0
- package/dist/internal/index.d.ts +5 -0
- package/dist/internal/index.js +22 -0
- package/dist/internal/index.js.map +1 -0
- package/dist/internal/rpc.d.ts +25 -0
- package/dist/internal/rpc.js +33 -0
- package/dist/internal/rpc.js.map +1 -0
- package/dist/internal/types.d.ts +330 -0
- package/dist/internal/types.js +49 -0
- package/dist/internal/types.js.map +1 -0
- package/dist/rpc-handler.d.ts +29 -0
- package/dist/rpc-handler.js +136 -0
- package/dist/rpc-handler.js.map +1 -0
- package/dist/snap-utils.d.ts +11 -0
- package/dist/snap-utils.js +21 -0
- package/dist/snap-utils.js.map +1 -0
- package/dist/superstruct.d.ts +83 -0
- package/dist/superstruct.js +81 -0
- package/dist/superstruct.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +19 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/types.d.ts +17 -0
- package/dist/utils/types.js +29 -0
- package/dist/utils/types.js.map +1 -0
- package/dist/utils/typing.d.ts +32 -0
- package/dist/utils/typing.js +21 -0
- package/dist/utils/typing.js.map +1 -0
- package/package.json +84 -0
package/README.md
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
# MetaMask Keyring API
|
|
2
|
+
|
|
3
|
+
This TypeScript module simplifies the integration of Snaps with MetaMask using
|
|
4
|
+
the Keyring API.
|
|
5
|
+
|
|
6
|
+
Features:
|
|
7
|
+
|
|
8
|
+
- **Keyring API Interface**: The module exposes an interface representing the
|
|
9
|
+
Keyring API. Snaps can implement this interface to seamlessly interact with
|
|
10
|
+
MetaMask and leverage its functionality.
|
|
11
|
+
|
|
12
|
+
- **Dapp Client**: The module includes a client that enables dapps to
|
|
13
|
+
communicate with the account Snap. This client allows dapps to send requests
|
|
14
|
+
to the Snap, such as retrieving account information or submitting requests.
|
|
15
|
+
|
|
16
|
+
- **MetaMask Client**: The module provides a client specifically designed for
|
|
17
|
+
MetaMask integration. This client enables MetaMask to send requests directly
|
|
18
|
+
to the account Snap, facilitating smooth interoperability between the two
|
|
19
|
+
applications.
|
|
20
|
+
|
|
21
|
+
- **Request Handler Helper Functions**: The module offers a set of helper
|
|
22
|
+
functions to simplify the implementation of the request handler in the
|
|
23
|
+
account Snap. These functions assist in processing incoming requests,
|
|
24
|
+
validating data, and handling various request types from dapps and MetaMask.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
yarn add @metamask/keyring-api
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
or
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @metamask/keyring-api
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Account Snaps
|
|
39
|
+
|
|
40
|
+
> :point_up: **Important**: Before implementing your Snap, please make sure to
|
|
41
|
+
> read the [security recommendations](./docs/security.md) and the [architecture
|
|
42
|
+
> document](./docs/architecture.md).
|
|
43
|
+
|
|
44
|
+
Starting with MetaMask 11.4, Snaps can implement the Keyring API. This allows
|
|
45
|
+
users to manage their accounts in a more flexible way, and enables developers
|
|
46
|
+
to build new types of accounts.
|
|
47
|
+
|
|
48
|
+
> :pencil2: **Note:** You can also build MetaMask from [source][extension-repo]
|
|
49
|
+
> from the `develop` branch.
|
|
50
|
+
|
|
51
|
+
Follow these steps to implement the Keyring API in your Snap. Please note that
|
|
52
|
+
these instruction assume that you are already familiar with the process of
|
|
53
|
+
[developing a Snap](https://docs.metamask.io/snaps/).
|
|
54
|
+
|
|
55
|
+
1. **Implement the Keyring API:**
|
|
56
|
+
|
|
57
|
+
Inside your Snap, implement the `Keyring` API:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
class MySnapKeyring implements Keyring {
|
|
61
|
+
// Implement the required methods here...
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
> :point_up: **Important**: Ensure that your keyring implements the [methods
|
|
66
|
+
> called by MetaMask][exposed-methods], otherwise some features may not
|
|
67
|
+
> work.
|
|
68
|
+
|
|
69
|
+
2. **Handle requests submitted by MetaMask:**
|
|
70
|
+
|
|
71
|
+
MetaMask will submit requests through the `submitRequest` method of your the
|
|
72
|
+
Keyring API (check the supported [EVM methods](./docs/evm-methods.md)). Here
|
|
73
|
+
is an example of request:
|
|
74
|
+
|
|
75
|
+
```json
|
|
76
|
+
{
|
|
77
|
+
"id": "d6e23af6-4bea-48dd-aeb0-7d3c30ea67f9",
|
|
78
|
+
"scope": "",
|
|
79
|
+
"account": "69438371-bef3-4957-9f91-c3f22c1d75f3",
|
|
80
|
+
"request": {
|
|
81
|
+
"method": "personal_sign",
|
|
82
|
+
"params": [
|
|
83
|
+
"0x4578616d706c652060706572736f6e616c5f7369676e60206d657373616765",
|
|
84
|
+
"0x5874174dcf1ab6F7Efd8496f4f09404CD1c5bA84"
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Where:
|
|
91
|
+
|
|
92
|
+
- `id` is unique identifier for the request.
|
|
93
|
+
|
|
94
|
+
- `scope` is the CAIP-2 chain ID of the selected chain. Currently, this
|
|
95
|
+
property is always an empty string. Your Snap should use the chain ID
|
|
96
|
+
present in the request object instead.
|
|
97
|
+
|
|
98
|
+
- `account` is the ID of the account that should handle the request.
|
|
99
|
+
|
|
100
|
+
- `request` is the request object.
|
|
101
|
+
|
|
102
|
+
Your Snap must respond with either a synchronous result:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
return { pending: false, result };
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Or an asynchronous result:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
return { pending: true, redirect: { message, url } };
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The redirect message and URL will be displayed to the user to inform them
|
|
115
|
+
about how to continue the transaction flow.
|
|
116
|
+
|
|
117
|
+
3. **Notify MetaMask about events:**
|
|
118
|
+
|
|
119
|
+
The following actions must be notified to MetaMask:
|
|
120
|
+
|
|
121
|
+
1. When an account is created:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
try {
|
|
125
|
+
emitSnapKeyringEvent(snap, KeyringEvent.AccountCreated, { account });
|
|
126
|
+
// Update your snap's state...
|
|
127
|
+
} catch (error) {
|
|
128
|
+
// Handle the error...
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
MetaMask will return an error if the account already exists or if the
|
|
133
|
+
account object is invalid.
|
|
134
|
+
|
|
135
|
+
2. When an account is updated:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
try {
|
|
139
|
+
emitSnapKeyringEvent(snap, KeyringEvent.AccountUpdated, { account });
|
|
140
|
+
// Update your snap's state...
|
|
141
|
+
} catch (error) {
|
|
142
|
+
// Handle the error...
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
MetaMask will return an error if the account does not exist, if the
|
|
147
|
+
account object is invalid, or if the account address changed.
|
|
148
|
+
|
|
149
|
+
3. When an account is deleted:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
try {
|
|
153
|
+
emitSnapKeyringEvent(snap, KeyringEvent.AccountDeleted, {
|
|
154
|
+
id: account.id,
|
|
155
|
+
});
|
|
156
|
+
// Update your snap's state...
|
|
157
|
+
} catch (error) {
|
|
158
|
+
// Handle the error...
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The delete event is idempotent, so it is safe to emit it even if the
|
|
163
|
+
account does not exist.
|
|
164
|
+
|
|
165
|
+
4. When a request is approved:
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
try {
|
|
169
|
+
emitSnapKeyringEvent(snap, KeyringEvent.RequestApproved, {
|
|
170
|
+
id: request.id,
|
|
171
|
+
result,
|
|
172
|
+
});
|
|
173
|
+
// Update your snap's state...
|
|
174
|
+
} catch (error) {
|
|
175
|
+
// Handle the error...
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
MetaMask will return an error if the request does not exist.
|
|
180
|
+
|
|
181
|
+
> :pencil2: **Note:** This only applies to Snaps that implement the
|
|
182
|
+
> [asynchronous transaction flow][async-flow].
|
|
183
|
+
|
|
184
|
+
5. When a request is rejected:
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
try {
|
|
188
|
+
emitSnapKeyringEvent(snap, KeyringEvent.RequestRejected, {
|
|
189
|
+
id: request.id,
|
|
190
|
+
});
|
|
191
|
+
// Update your snap's state...
|
|
192
|
+
} catch (error) {
|
|
193
|
+
// Handle the error...
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
MetaMask will return an error if the request does not exist.
|
|
198
|
+
|
|
199
|
+
> :pencil2: **Note:** This only applies to Snaps that implement the
|
|
200
|
+
> [asynchronous transaction flow][async-flow].
|
|
201
|
+
|
|
202
|
+
4. **Expose the Keyring API:**
|
|
203
|
+
|
|
204
|
+
Then create a handler to expose the keyring methods to MetaMask and your dapp:
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
export const onKeyringRequest: OnKeyringRequestHandler = async ({
|
|
208
|
+
origin,
|
|
209
|
+
request,
|
|
210
|
+
}) => {
|
|
211
|
+
// Your custom logic here...
|
|
212
|
+
return handleKeyringRequest(keyring, request);
|
|
213
|
+
};
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
5. **Call the keyring methods from your dapp:**
|
|
217
|
+
|
|
218
|
+
Now you should be able to call your account Snap from your dapp, for
|
|
219
|
+
example:
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
const client = new KeyringSnapRpcClient(snapId, window.ethereum);
|
|
223
|
+
const accounts = await client.listAccounts();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Migrating from 0.1.x to 0.2.x
|
|
227
|
+
|
|
228
|
+
The following changes were made to the API, which may require changes to your
|
|
229
|
+
implementation:
|
|
230
|
+
|
|
231
|
+
- In the `KeyringAccount` type, the `supportedMethods` property was renamed to
|
|
232
|
+
`methods`.
|
|
233
|
+
|
|
234
|
+
```diff
|
|
235
|
+
- supportedMethods: string[];
|
|
236
|
+
+ methods: string[];
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
- In the `KeyringAccount` type, the `name` property was removed.
|
|
240
|
+
|
|
241
|
+
```diff
|
|
242
|
+
- name: string;
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
- In the `KeyringAccount` type, add the `options` property can no longer be
|
|
246
|
+
null.
|
|
247
|
+
|
|
248
|
+
```diff
|
|
249
|
+
- options: Record<string, unknown> | null;
|
|
250
|
+
+ options: Record<string, unknown>;
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
- In the `KeyringAccount` type, the `eth_signTypedData` method was removed from
|
|
254
|
+
the list of available methods.
|
|
255
|
+
|
|
256
|
+
```diff
|
|
257
|
+
- 'eth_signTypedData',
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
It was an alias for the `eth_signTypedData_v1` method, which is still
|
|
261
|
+
present.
|
|
262
|
+
|
|
263
|
+
- Snaps should now use the `emitSnapKeyringEvent()` helper function to notify
|
|
264
|
+
MetaMask about events:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
// Emit an event to indicate that an account was created.
|
|
268
|
+
emitSnapKeyringEvent(snap, KeyringEvent.AccountCreated, { account });
|
|
269
|
+
|
|
270
|
+
// Emit an event to indicate that an account was updated.
|
|
271
|
+
emitSnapKeyringEvent(snap, KeyringEvent.AccountUpdated, { account });
|
|
272
|
+
|
|
273
|
+
// Emit an event to indicate that an account was deleted.
|
|
274
|
+
emitSnapKeyringEvent(snap, KeyringEvent.AccountDeleted, { id: account.id });
|
|
275
|
+
|
|
276
|
+
// Emit an event to indicate that a request was approved.
|
|
277
|
+
emitSnapKeyringEvent(snap, KeyringEvent.RequestApproved, {
|
|
278
|
+
id: request.id,
|
|
279
|
+
result,
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// Emit an event to indicate that a request was rejected.
|
|
283
|
+
emitSnapKeyringEvent(snap, KeyringEvent.RequestRejected, { id: request.id });
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
> :point_up: **Important**: For all events above, MetaMask may return an error
|
|
287
|
+
> indicating that the event was not handled, possibly because it contains
|
|
288
|
+
> invalid arguments.
|
|
289
|
+
|
|
290
|
+
- Keyrings that implement the [asynchronous transaction flow][async-flow] can
|
|
291
|
+
now return an optional `redirect` property that contains an URL and a message
|
|
292
|
+
to be displayed to the user. This will, in a future release of MetaMask, be
|
|
293
|
+
used to inform the user on how to continue the transaction flow.
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
return {
|
|
297
|
+
pending: true,
|
|
298
|
+
redirect: {
|
|
299
|
+
message: 'Please go to the Snap Dapp to finish sining the transaction.',
|
|
300
|
+
url: 'https://example.com/sign?tx=1234',
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
- The `buildHandlersChain` helper function was removed from the API. Instead,
|
|
306
|
+
you must implement your own handler. For example:
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
export const onRpcRequest: OnRpcRequestHandler = async ({
|
|
310
|
+
request,
|
|
311
|
+
origin,
|
|
312
|
+
}) => {
|
|
313
|
+
// Check if origin is allowed to call the method.
|
|
314
|
+
if (!hasPermission(origin, request.method)) {
|
|
315
|
+
throw new Error(
|
|
316
|
+
`Origin '${origin}' is not allowed to call '${request.method}'`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Dispatch the request to the keyring.
|
|
321
|
+
return handleKeyringRequest(keyring, request);
|
|
322
|
+
};
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Migrating from 0.2.x to 1.x.x
|
|
326
|
+
|
|
327
|
+
The following changes were made to the API, which may require changes to your
|
|
328
|
+
implementation:
|
|
329
|
+
|
|
330
|
+
- Your Snap must expose the Keyring methods through the `onKeyringRequest`
|
|
331
|
+
export instead of the `onRpcRequest` export.
|
|
332
|
+
|
|
333
|
+
- Your Snap must request the new `endowment:keyring` endowment, and list any
|
|
334
|
+
dapp that should be allowed to call the Keyring methods.
|
|
335
|
+
|
|
336
|
+
For more details about the changes, please refer to the [security
|
|
337
|
+
guidelines](./docs/security.md).
|
|
338
|
+
|
|
339
|
+
## API
|
|
340
|
+
|
|
341
|
+
See our documentation:
|
|
342
|
+
|
|
343
|
+
- [Latest published API documentation](https://metamask.github.io/keyring-api/latest/)
|
|
344
|
+
- [Latest development API documentation](https://metamask.github.io/keyring-api/staging/)
|
|
345
|
+
|
|
346
|
+
## Contributing
|
|
347
|
+
|
|
348
|
+
### Setup
|
|
349
|
+
|
|
350
|
+
- Install [Node.js](https://nodejs.org) version 16
|
|
351
|
+
- If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
|
|
352
|
+
- Install [Yarn v3](https://yarnpkg.com/getting-started/install)
|
|
353
|
+
- Run `yarn install` to install dependencies and run any required post-install scripts
|
|
354
|
+
|
|
355
|
+
### Testing and Linting
|
|
356
|
+
|
|
357
|
+
Run `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.
|
|
358
|
+
|
|
359
|
+
Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.
|
|
360
|
+
|
|
361
|
+
### Release & Publishing
|
|
362
|
+
|
|
363
|
+
The project follows the same release process as the other libraries in the MetaMask organization. The GitHub Actions [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) are used to automate the release process; see those repositories for more information about how they work.
|
|
364
|
+
|
|
365
|
+
1. Choose a release version.
|
|
366
|
+
|
|
367
|
+
- The release version should be chosen according to SemVer. Analyze the changes to see whether they include any breaking changes, new features, or deprecations, then choose the appropriate SemVer version. See [the SemVer specification](https://semver.org/) for more information.
|
|
368
|
+
|
|
369
|
+
2. If this release is backporting changes onto a previous release, then ensure there is a major version branch for that version (e.g. `1.x` for a `v1` backport release).
|
|
370
|
+
|
|
371
|
+
- The major version branch should be set to the most recent release with that major version. For example, when backporting a `v1.0.2` release, you'd want to ensure there was a `1.x` branch that was set to the `v1.0.1` tag.
|
|
372
|
+
|
|
373
|
+
3. Trigger the [`workflow_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event [manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) for the `Create Release Pull Request` action to create the release PR.
|
|
374
|
+
|
|
375
|
+
- For a backport release, the base branch should be the major version branch that you ensured existed in step 2. For a normal release, the base branch should be the main branch for that repository (which should be the default value).
|
|
376
|
+
- This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.
|
|
377
|
+
|
|
378
|
+
4. Update the changelog to move each change entry into the appropriate change category ([See here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories, and the correct ordering), and edit them to be more easily understood by users of the package.
|
|
379
|
+
|
|
380
|
+
- Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
|
|
381
|
+
- Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
|
|
382
|
+
- Consolidate related changes into one change entry if it makes it easier to explain.
|
|
383
|
+
- Run `yarn auto-changelog validate --prettier --rc` to check that the changelog is correctly formatted.
|
|
384
|
+
|
|
385
|
+
5. Review and QA the release.
|
|
386
|
+
|
|
387
|
+
- If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.
|
|
388
|
+
|
|
389
|
+
6. Squash & Merge the release.
|
|
390
|
+
|
|
391
|
+
- This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.
|
|
392
|
+
|
|
393
|
+
7. Publish the release on npm.
|
|
394
|
+
|
|
395
|
+
- Wait for the `publish-release` GitHub Action workflow to finish. This should trigger a second job (`publish-npm`), which will wait for a run approval by the [`npm publishers`](https://github.com/orgs/MetaMask/teams/npm-publishers) team.
|
|
396
|
+
- Approve the `publish-npm` job (or ask somebody on the npm publishers team to approve it for you).
|
|
397
|
+
- Once the `publish-npm` job has finished, check npm to verify that it has been published.
|
|
398
|
+
|
|
399
|
+
[extension-repo]: https://github.com/MetaMask/metamask-extension
|
|
400
|
+
[exposed-methods]: ./docs/security.md#limit-the-methods-exposed-to-dapps
|
|
401
|
+
[async-flow]: ./docs/architecture.md#asynchronous-transaction-flow
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Infer } from '@metamask/superstruct';
|
|
2
|
+
export declare const JsonRpcRequestStruct: import("@metamask/superstruct").Struct<{
|
|
3
|
+
jsonrpc: "2.0";
|
|
4
|
+
id: string | number | null;
|
|
5
|
+
method: string;
|
|
6
|
+
params?: import("@metamask/utils").Json[] | Record<string, import("@metamask/utils").Json>;
|
|
7
|
+
}, {
|
|
8
|
+
jsonrpc: import("@metamask/superstruct").Struct<"2.0", "2.0">;
|
|
9
|
+
id: import("@metamask/superstruct").Struct<string | number | null, null>;
|
|
10
|
+
method: import("@metamask/superstruct").Struct<string, null>;
|
|
11
|
+
params: import("@metamask/superstruct").Struct<import("./superstruct").ExactOptionalTag | import("@metamask/utils").Json[] | Record<string, import("@metamask/utils").Json>, null>;
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* JSON-RPC request type.
|
|
15
|
+
*/
|
|
16
|
+
export declare type JsonRpcRequest = Infer<typeof JsonRpcRequestStruct>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonRpcRequestStruct = void 0;
|
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
6
|
+
const superstruct_2 = require("./superstruct");
|
|
7
|
+
exports.JsonRpcRequestStruct = (0, superstruct_2.object)({
|
|
8
|
+
jsonrpc: (0, superstruct_1.literal)('2.0'),
|
|
9
|
+
id: (0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)(), (0, superstruct_1.literal)(null)]),
|
|
10
|
+
method: (0, superstruct_1.string)(),
|
|
11
|
+
params: (0, superstruct_2.exactOptional)((0, superstruct_1.union)([(0, superstruct_1.array)(utils_1.JsonStruct), (0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct)])),
|
|
12
|
+
});
|
|
13
|
+
//# sourceMappingURL=JsonRpcRequest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonRpcRequest.js","sourceRoot":"","sources":["../src/JsonRpcRequest.ts"],"names":[],"mappings":";;;AAAA,uDAO+B;AAE/B,2CAA6C;AAE7C,+CAAsD;AAEzC,QAAA,oBAAoB,GAAG,IAAA,oBAAM,EAAC;IACzC,OAAO,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;IACvB,EAAE,EAAE,IAAA,mBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,oBAAM,GAAE,EAAE,IAAA,qBAAO,EAAC,IAAI,CAAC,CAAC,CAAC;IAC9C,MAAM,EAAE,IAAA,oBAAM,GAAE;IAChB,MAAM,EAAE,IAAA,2BAAa,EACnB,IAAA,mBAAK,EAAC,CAAC,IAAA,mBAAK,EAAC,kBAAU,CAAC,EAAE,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC,CACzD;CACF,CAAC,CAAC","sourcesContent":["import {\n array,\n literal,\n number,\n record,\n string,\n union,\n} from '@metamask/superstruct';\nimport type { Infer } from '@metamask/superstruct';\nimport { JsonStruct } from '@metamask/utils';\n\nimport { exactOptional, object } from './superstruct';\n\nexport const JsonRpcRequestStruct = object({\n jsonrpc: literal('2.0'),\n id: union([string(), number(), literal(null)]),\n method: string(),\n params: exactOptional(\n union([array(JsonStruct), record(string(), JsonStruct)]),\n ),\n});\n\n/**\n * JSON-RPC request type.\n */\nexport type JsonRpcRequest = Infer<typeof JsonRpcRequestStruct>;\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Json } from '@metamask/utils';
|
|
2
|
+
import type { Keyring, KeyringAccount, KeyringRequest, KeyringAccountData, KeyringResponse, CaipAssetType, Balance } from './api';
|
|
3
|
+
import type { JsonRpcRequest } from './JsonRpcRequest';
|
|
4
|
+
export declare type Sender = {
|
|
5
|
+
send(request: JsonRpcRequest): Promise<Json>;
|
|
6
|
+
};
|
|
7
|
+
export declare class KeyringClient implements Keyring {
|
|
8
|
+
#private;
|
|
9
|
+
/**
|
|
10
|
+
* Create a new instance of `KeyringClient`.
|
|
11
|
+
*
|
|
12
|
+
* @param sender - The `Sender` instance to use to send requests to the snap.
|
|
13
|
+
*/
|
|
14
|
+
constructor(sender: Sender);
|
|
15
|
+
listAccounts(): Promise<KeyringAccount[]>;
|
|
16
|
+
getAccount(id: string): Promise<KeyringAccount>;
|
|
17
|
+
getAccountBalances(id: string, assets: CaipAssetType[]): Promise<Record<CaipAssetType, Balance>>;
|
|
18
|
+
createAccount(options?: Record<string, Json>): Promise<KeyringAccount>;
|
|
19
|
+
filterAccountChains(id: string, chains: string[]): Promise<string[]>;
|
|
20
|
+
updateAccount(account: KeyringAccount): Promise<void>;
|
|
21
|
+
deleteAccount(id: string): Promise<void>;
|
|
22
|
+
exportAccount(id: string): Promise<KeyringAccountData>;
|
|
23
|
+
listRequests(): Promise<KeyringRequest[]>;
|
|
24
|
+
getRequest(id: string): Promise<KeyringRequest>;
|
|
25
|
+
submitRequest(request: KeyringRequest): Promise<KeyringResponse>;
|
|
26
|
+
approveRequest(id: string, data?: Record<string, Json>): Promise<void>;
|
|
27
|
+
rejectRequest(id: string): Promise<void>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _KeyringClient_instances, _KeyringClient_sender, _KeyringClient_send;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.KeyringClient = void 0;
|
|
16
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
17
|
+
const uuid_1 = require("uuid");
|
|
18
|
+
const api_1 = require("./internal/api");
|
|
19
|
+
const rpc_1 = require("./internal/rpc");
|
|
20
|
+
const superstruct_2 = require("./superstruct");
|
|
21
|
+
class KeyringClient {
|
|
22
|
+
/**
|
|
23
|
+
* Create a new instance of `KeyringClient`.
|
|
24
|
+
*
|
|
25
|
+
* @param sender - The `Sender` instance to use to send requests to the snap.
|
|
26
|
+
*/
|
|
27
|
+
constructor(sender) {
|
|
28
|
+
_KeyringClient_instances.add(this);
|
|
29
|
+
_KeyringClient_sender.set(this, void 0);
|
|
30
|
+
__classPrivateFieldSet(this, _KeyringClient_sender, sender, "f");
|
|
31
|
+
}
|
|
32
|
+
async listAccounts() {
|
|
33
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
34
|
+
method: rpc_1.KeyringRpcMethod.ListAccounts,
|
|
35
|
+
}), api_1.ListAccountsResponseStruct);
|
|
36
|
+
}
|
|
37
|
+
async getAccount(id) {
|
|
38
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
39
|
+
method: rpc_1.KeyringRpcMethod.GetAccount,
|
|
40
|
+
params: { id },
|
|
41
|
+
}), api_1.GetAccountResponseStruct);
|
|
42
|
+
}
|
|
43
|
+
async getAccountBalances(id, assets) {
|
|
44
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
45
|
+
method: rpc_1.KeyringRpcMethod.GetAccountBalances,
|
|
46
|
+
params: { id, assets },
|
|
47
|
+
}), api_1.GetAccountBalancesResponseStruct);
|
|
48
|
+
}
|
|
49
|
+
async createAccount(options = {}) {
|
|
50
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
51
|
+
method: rpc_1.KeyringRpcMethod.CreateAccount,
|
|
52
|
+
params: { options },
|
|
53
|
+
}), api_1.CreateAccountResponseStruct);
|
|
54
|
+
}
|
|
55
|
+
async filterAccountChains(id, chains) {
|
|
56
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
57
|
+
method: rpc_1.KeyringRpcMethod.FilterAccountChains,
|
|
58
|
+
params: { id, chains },
|
|
59
|
+
}), api_1.FilterAccountChainsResponseStruct);
|
|
60
|
+
}
|
|
61
|
+
async updateAccount(account) {
|
|
62
|
+
(0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
63
|
+
method: rpc_1.KeyringRpcMethod.UpdateAccount,
|
|
64
|
+
params: { account },
|
|
65
|
+
}), api_1.UpdateAccountResponseStruct);
|
|
66
|
+
}
|
|
67
|
+
async deleteAccount(id) {
|
|
68
|
+
(0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
69
|
+
method: rpc_1.KeyringRpcMethod.DeleteAccount,
|
|
70
|
+
params: { id },
|
|
71
|
+
}), api_1.DeleteAccountResponseStruct);
|
|
72
|
+
}
|
|
73
|
+
async exportAccount(id) {
|
|
74
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
75
|
+
method: rpc_1.KeyringRpcMethod.ExportAccount,
|
|
76
|
+
params: { id },
|
|
77
|
+
}), api_1.ExportAccountResponseStruct);
|
|
78
|
+
}
|
|
79
|
+
async listRequests() {
|
|
80
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
81
|
+
method: rpc_1.KeyringRpcMethod.ListRequests,
|
|
82
|
+
}), api_1.ListRequestsResponseStruct);
|
|
83
|
+
}
|
|
84
|
+
async getRequest(id) {
|
|
85
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
86
|
+
method: rpc_1.KeyringRpcMethod.GetRequest,
|
|
87
|
+
params: { id },
|
|
88
|
+
}), api_1.GetRequestResponseStruct);
|
|
89
|
+
}
|
|
90
|
+
async submitRequest(request) {
|
|
91
|
+
return (0, superstruct_2.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
92
|
+
method: rpc_1.KeyringRpcMethod.SubmitRequest,
|
|
93
|
+
params: request,
|
|
94
|
+
}), api_1.SubmitRequestResponseStruct);
|
|
95
|
+
}
|
|
96
|
+
async approveRequest(id, data = {}) {
|
|
97
|
+
(0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
98
|
+
method: rpc_1.KeyringRpcMethod.ApproveRequest,
|
|
99
|
+
params: { id, data },
|
|
100
|
+
}), api_1.ApproveRequestResponseStruct);
|
|
101
|
+
}
|
|
102
|
+
async rejectRequest(id) {
|
|
103
|
+
(0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
|
104
|
+
method: rpc_1.KeyringRpcMethod.RejectRequest,
|
|
105
|
+
params: { id },
|
|
106
|
+
}), api_1.RejectRequestResponseStruct);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.KeyringClient = KeyringClient;
|
|
110
|
+
_KeyringClient_sender = new WeakMap(), _KeyringClient_instances = new WeakSet(), _KeyringClient_send =
|
|
111
|
+
/**
|
|
112
|
+
* Send a request to the snap and return the response.
|
|
113
|
+
*
|
|
114
|
+
* @param partial - A partial JSON-RPC request (method and params).
|
|
115
|
+
* @returns A promise that resolves to the response to the request.
|
|
116
|
+
*/
|
|
117
|
+
async function _KeyringClient_send(partial) {
|
|
118
|
+
return __classPrivateFieldGet(this, _KeyringClient_sender, "f").send({
|
|
119
|
+
jsonrpc: '2.0',
|
|
120
|
+
id: (0, uuid_1.v4)(),
|
|
121
|
+
...partial,
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=KeyringClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KeyringClient.js","sourceRoot":"","sources":["../src/KeyringClient.ts"],"names":[],"mappings":"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","sourcesContent":["import { assert } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { v4 as uuid } from 'uuid';\n\nimport type {\n Keyring,\n KeyringAccount,\n KeyringRequest,\n KeyringAccountData,\n KeyringResponse,\n CaipAssetType,\n Balance,\n} from './api';\nimport {\n ApproveRequestResponseStruct,\n CreateAccountResponseStruct,\n DeleteAccountResponseStruct,\n ExportAccountResponseStruct,\n FilterAccountChainsResponseStruct,\n GetAccountBalancesResponseStruct,\n GetAccountResponseStruct,\n GetRequestResponseStruct,\n ListAccountsResponseStruct,\n ListRequestsResponseStruct,\n RejectRequestResponseStruct,\n SubmitRequestResponseStruct,\n UpdateAccountResponseStruct,\n} from './internal/api';\nimport { KeyringRpcMethod } from './internal/rpc';\nimport type { JsonRpcRequest } from './JsonRpcRequest';\nimport { strictMask } from './superstruct';\n\nexport type Sender = {\n send(request: JsonRpcRequest): Promise<Json>;\n};\n\nexport class KeyringClient implements Keyring {\n #sender: Sender;\n\n /**\n * Create a new instance of `KeyringClient`.\n *\n * @param sender - The `Sender` instance to use to send requests to the snap.\n */\n constructor(sender: Sender) {\n this.#sender = sender;\n }\n\n /**\n * Send a request to the snap and return the response.\n *\n * @param partial - A partial JSON-RPC request (method and params).\n * @returns A promise that resolves to the response to the request.\n */\n async #send(partial: Omit<JsonRpcRequest, 'jsonrpc' | 'id'>): Promise<Json> {\n return this.#sender.send({\n jsonrpc: '2.0',\n id: uuid(),\n ...partial,\n });\n }\n\n async listAccounts(): Promise<KeyringAccount[]> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.ListAccounts,\n }),\n ListAccountsResponseStruct,\n );\n }\n\n async getAccount(id: string): Promise<KeyringAccount> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.GetAccount,\n params: { id },\n }),\n GetAccountResponseStruct,\n );\n }\n\n async getAccountBalances(\n id: string,\n assets: CaipAssetType[],\n ): Promise<Record<CaipAssetType, Balance>> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.GetAccountBalances,\n params: { id, assets },\n }),\n GetAccountBalancesResponseStruct,\n );\n }\n\n async createAccount(\n options: Record<string, Json> = {},\n ): Promise<KeyringAccount> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.CreateAccount,\n params: { options },\n }),\n CreateAccountResponseStruct,\n );\n }\n\n async filterAccountChains(id: string, chains: string[]): Promise<string[]> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.FilterAccountChains,\n params: { id, chains },\n }),\n FilterAccountChainsResponseStruct,\n );\n }\n\n async updateAccount(account: KeyringAccount): Promise<void> {\n assert(\n await this.#send({\n method: KeyringRpcMethod.UpdateAccount,\n params: { account },\n }),\n UpdateAccountResponseStruct,\n );\n }\n\n async deleteAccount(id: string): Promise<void> {\n assert(\n await this.#send({\n method: KeyringRpcMethod.DeleteAccount,\n params: { id },\n }),\n DeleteAccountResponseStruct,\n );\n }\n\n async exportAccount(id: string): Promise<KeyringAccountData> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.ExportAccount,\n params: { id },\n }),\n ExportAccountResponseStruct,\n );\n }\n\n async listRequests(): Promise<KeyringRequest[]> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.ListRequests,\n }),\n ListRequestsResponseStruct,\n );\n }\n\n async getRequest(id: string): Promise<KeyringRequest> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.GetRequest,\n params: { id },\n }),\n GetRequestResponseStruct,\n );\n }\n\n async submitRequest(request: KeyringRequest): Promise<KeyringResponse> {\n return strictMask(\n await this.#send({\n method: KeyringRpcMethod.SubmitRequest,\n params: request,\n }),\n SubmitRequestResponseStruct,\n );\n }\n\n async approveRequest(\n id: string,\n data: Record<string, Json> = {},\n ): Promise<void> {\n assert(\n await this.#send({\n method: KeyringRpcMethod.ApproveRequest,\n params: { id, data },\n }),\n ApproveRequestResponseStruct,\n );\n }\n\n async rejectRequest(id: string): Promise<void> {\n assert(\n await this.#send({\n method: KeyringRpcMethod.RejectRequest,\n params: { id },\n }),\n RejectRequestResponseStruct,\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { MetaMaskInpageProvider } from '@metamask/providers';
|
|
2
|
+
import type { Json } from '@metamask/utils';
|
|
3
|
+
import type { JsonRpcRequest } from './JsonRpcRequest';
|
|
4
|
+
import type { Sender } from './KeyringClient';
|
|
5
|
+
import { KeyringClient } from './KeyringClient';
|
|
6
|
+
/**
|
|
7
|
+
* Implementation of the `Sender` interface that can be used to send requests
|
|
8
|
+
* to a snap through the snap JSON-RPC API.
|
|
9
|
+
*/
|
|
10
|
+
export declare class SnapRpcSender implements Sender {
|
|
11
|
+
#private;
|
|
12
|
+
/**
|
|
13
|
+
* Create a new instance of `SnapRpcSender`.
|
|
14
|
+
*
|
|
15
|
+
* @param origin - The caller's origin.
|
|
16
|
+
* @param provider - The `MetaMaskInpageProvider` instance to use.
|
|
17
|
+
*/
|
|
18
|
+
constructor(origin: string, provider: MetaMaskInpageProvider);
|
|
19
|
+
/**
|
|
20
|
+
* Send a request to the snap and return the response.
|
|
21
|
+
*
|
|
22
|
+
* @param request - The JSON-RPC request to send to the snap.
|
|
23
|
+
* @returns A promise that resolves to the response of the request.
|
|
24
|
+
*/
|
|
25
|
+
send(request: JsonRpcRequest): Promise<Json>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A `KeyringClient` that allows the communication with a snap through the snap
|
|
29
|
+
* JSON-RPC API.
|
|
30
|
+
*/
|
|
31
|
+
export declare class KeyringSnapRpcClient extends KeyringClient {
|
|
32
|
+
/**
|
|
33
|
+
* Create a new instance of `KeyringSnapRpcClient`.
|
|
34
|
+
*
|
|
35
|
+
* @param origin - Caller's origin.
|
|
36
|
+
* @param provider - The `MetaMaskInpageProvider` instance to use.
|
|
37
|
+
*/
|
|
38
|
+
constructor(origin: string, provider: MetaMaskInpageProvider);
|
|
39
|
+
}
|