@metamask/keyring-api 0.1.0
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 +18 -0
- package/README.md +141 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/keyring-api.d.ts +241 -0
- package/dist/keyring-api.js +81 -0
- package/dist/keyring-api.js.map +1 -0
- package/dist/keyring-client.d.ts +26 -0
- package/dist/keyring-client.js +112 -0
- package/dist/keyring-client.js.map +1 -0
- package/dist/keyring-internal-api.d.ts +588 -0
- package/dist/keyring-internal-api.js +166 -0
- package/dist/keyring-internal-api.js.map +1 -0
- package/dist/keyring-rpc-dispatcher.d.ts +30 -0
- package/dist/keyring-rpc-dispatcher.js +98 -0
- package/dist/keyring-rpc-dispatcher.js.map +1 -0
- package/dist/keyring-snap-controller-client.d.ts +38 -0
- package/dist/keyring-snap-controller-client.js +99 -0
- package/dist/keyring-snap-controller-client.js.map +1 -0
- package/dist/keyring-snap-rpc-client.d.ts +38 -0
- package/dist/keyring-snap-rpc-client.js +69 -0
- package/dist/keyring-snap-rpc-client.js.map +1 -0
- package/dist/utils.d.ts +13 -0
- package/dist/utils.js +9 -0
- package/dist/utils.js.map +1 -0
- package/package.json +91 -0
package/CHANGELOG.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [Unreleased]
|
8
|
+
|
9
|
+
## [0.1.0] - 2023-06-20
|
10
|
+
### Added
|
11
|
+
- Usage examples to [`README.md`](./README.md).
|
12
|
+
- Keyring API definition.
|
13
|
+
- JSON-RPC snap keyring client. It is intended to be used by a snap's companion dApp to send requests to the snap.
|
14
|
+
- SnapController keyring client. It is intended to be used by MetaMask to talk to the snap.
|
15
|
+
- Helper functions to create keyring handler in the snap.
|
16
|
+
|
17
|
+
[Unreleased]: https://github.com/MetaMask/keyring-api/compare/v0.1.0...HEAD
|
18
|
+
[0.1.0]: https://github.com/MetaMask/keyring-api/releases/tag/v0.1.0
|
package/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# MetaMask Keyring API
|
2
|
+
|
3
|
+
> This TypeScript module is maintained in the style of the MetaMask team.
|
4
|
+
|
5
|
+
This TypeScript module simplifies the integration of snaps with MetaMask using
|
6
|
+
the Keyring API.
|
7
|
+
|
8
|
+
Features:
|
9
|
+
|
10
|
+
- **Keyring API Interface**: The module exposes an interface representing the
|
11
|
+
Keyring API. Snaps can implement this interface to seamlessly interact with
|
12
|
+
MetaMask and leverage its functionality.
|
13
|
+
|
14
|
+
- **DApp Client**: The module includes a client that enables dApps to
|
15
|
+
communicate with the Keyring snap. This client allows dApps to send requests
|
16
|
+
to the snap, such as retrieving account information or submitting requests.
|
17
|
+
|
18
|
+
- **MetaMask Client**: The module provides a client specifically designed for
|
19
|
+
MetaMask integration. This client enables MetaMask to send requests directly
|
20
|
+
to the Keyring snap, facilitating smooth interoperability between the two
|
21
|
+
applications.
|
22
|
+
|
23
|
+
- **Request Handler Helper Functions**: The module offers a set of helper
|
24
|
+
functions to simplify the implementation of the request handler in the
|
25
|
+
Keyring snap. These functions assist in processing incoming requests,
|
26
|
+
validating data, and handling various request types from dApps and MetaMask.
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
`yarn add @metamask/keyring-api`
|
31
|
+
|
32
|
+
or
|
33
|
+
|
34
|
+
`npm install @metamask/keyring-api`
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
### In a snap
|
39
|
+
|
40
|
+
Inside the snap, implement the `Keyring` API:
|
41
|
+
|
42
|
+
```typescript
|
43
|
+
class MySnapKeyring implements Keyring {
|
44
|
+
// Implement the required methods.
|
45
|
+
}
|
46
|
+
```
|
47
|
+
|
48
|
+
Then create a handler that uses an instance of your keyring:
|
49
|
+
|
50
|
+
```typescript
|
51
|
+
import { keyringRpcDispatcher } from '@metamask/keyring-api';
|
52
|
+
|
53
|
+
// Create a new MySnapKeyring instance
|
54
|
+
keyring = new MySnapKeyring(keyringState);
|
55
|
+
// ...
|
56
|
+
|
57
|
+
// And wrap it in a handler
|
58
|
+
const keyringHandler: OnRpcRequestHandler = async ({ request }) => {
|
59
|
+
// Load the keyring state if needed
|
60
|
+
// ...
|
61
|
+
return await keyringRpcDispatcher(keyring, request);
|
62
|
+
};
|
63
|
+
```
|
64
|
+
|
65
|
+
Now expose this handler:
|
66
|
+
|
67
|
+
```typescript
|
68
|
+
export const onRpcRequest: OnRpcRequestHandler = keyringHandler;
|
69
|
+
```
|
70
|
+
|
71
|
+
Or chain it with other handlers:
|
72
|
+
|
73
|
+
```typescript
|
74
|
+
import { chainHandlers } from '@metamask/keyring-api';
|
75
|
+
|
76
|
+
export const onRpcRequest: OnRpcRequestHandler = chainHandlers(
|
77
|
+
// Other handlers...
|
78
|
+
keyringHandler,
|
79
|
+
// Other handlers...
|
80
|
+
);
|
81
|
+
```
|
82
|
+
|
83
|
+
## API
|
84
|
+
|
85
|
+
See our documentation:
|
86
|
+
|
87
|
+
- [Latest published API documentation](https://metamask.github.io/keyring-api/latest/)
|
88
|
+
- [Latest development API documentation](https://metamask.github.io/keyring-api/staging/)
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
### Setup
|
93
|
+
|
94
|
+
- Install [Node.js](https://nodejs.org) version 16
|
95
|
+
- If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
|
96
|
+
- Install [Yarn v3](https://yarnpkg.com/getting-started/install)
|
97
|
+
- Run `yarn install` to install dependencies and run any required post-install scripts
|
98
|
+
|
99
|
+
### Testing and Linting
|
100
|
+
|
101
|
+
Run `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.
|
102
|
+
|
103
|
+
Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.
|
104
|
+
|
105
|
+
### Release & Publishing
|
106
|
+
|
107
|
+
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.
|
108
|
+
|
109
|
+
1. Choose a release version.
|
110
|
+
|
111
|
+
- 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.
|
112
|
+
|
113
|
+
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).
|
114
|
+
|
115
|
+
- 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.
|
116
|
+
|
117
|
+
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.
|
118
|
+
|
119
|
+
- 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).
|
120
|
+
- This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.
|
121
|
+
|
122
|
+
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.
|
123
|
+
|
124
|
+
- 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.).
|
125
|
+
- Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
|
126
|
+
- Consolidate related changes into one change entry if it makes it easier to explain.
|
127
|
+
- Run `yarn auto-changelog validate --rc` to check that the changelog is correctly formatted.
|
128
|
+
|
129
|
+
5. Review and QA the release.
|
130
|
+
|
131
|
+
- 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.
|
132
|
+
|
133
|
+
6. Squash & Merge the release.
|
134
|
+
|
135
|
+
- 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.
|
136
|
+
|
137
|
+
7. Publish the release on npm.
|
138
|
+
|
139
|
+
- 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.
|
140
|
+
- Approve the `publish-npm` job (or ask somebody on the npm publishers team to approve it for you).
|
141
|
+
- Once the `publish-npm` job has finished, check npm to verify that it has been published.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./keyring-api"), exports);
|
18
|
+
__exportStar(require("./keyring-client"), exports);
|
19
|
+
__exportStar(require("./keyring-rpc-dispatcher"), exports);
|
20
|
+
__exportStar(require("./keyring-snap-controller-client"), exports);
|
21
|
+
__exportStar(require("./keyring-snap-rpc-client"), exports);
|
22
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,mDAAiC;AACjC,2DAAyC;AACzC,mEAAiD;AACjD,4DAA0C","sourcesContent":["export * from './keyring-api';\nexport * from './keyring-client';\nexport * from './keyring-rpc-dispatcher';\nexport * from './keyring-snap-controller-client';\nexport * from './keyring-snap-rpc-client';\n"]}
|
@@ -0,0 +1,241 @@
|
|
1
|
+
import { Json } from '@metamask/utils';
|
2
|
+
import { Infer } from 'superstruct';
|
3
|
+
export declare const KeyringAccountStruct: import("superstruct").Struct<{
|
4
|
+
id: string;
|
5
|
+
name: string;
|
6
|
+
address: string;
|
7
|
+
options: Record<string, Json> | null;
|
8
|
+
supportedMethods: ("personal_sign" | "eth_sendTransaction" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData" | "eth_signTypedData_v1" | "eth_signTypedData_v2" | "eth_signTypedData_v3" | "eth_signTypedData_v4")[];
|
9
|
+
type: "eip155:eoa" | "eip155:erc4337";
|
10
|
+
}, {
|
11
|
+
/**
|
12
|
+
* Account ID (UUIDv4).
|
13
|
+
*/
|
14
|
+
id: import("superstruct").Struct<string, null>;
|
15
|
+
/**
|
16
|
+
* User-chosen account name.
|
17
|
+
*/
|
18
|
+
name: import("superstruct").Struct<string, null>;
|
19
|
+
/**
|
20
|
+
* Account address or next receive address (UTXO).
|
21
|
+
*/
|
22
|
+
address: import("superstruct").Struct<string, null>;
|
23
|
+
/**
|
24
|
+
* Keyring-dependent account options.
|
25
|
+
*/
|
26
|
+
options: import("superstruct").Struct<Record<string, Json> | null, null>;
|
27
|
+
/**
|
28
|
+
* Account supported methods.
|
29
|
+
*/
|
30
|
+
supportedMethods: import("superstruct").Struct<("personal_sign" | "eth_sendTransaction" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData" | "eth_signTypedData_v1" | "eth_signTypedData_v2" | "eth_signTypedData_v3" | "eth_signTypedData_v4")[], import("superstruct").Struct<"personal_sign" | "eth_sendTransaction" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData" | "eth_signTypedData_v1" | "eth_signTypedData_v2" | "eth_signTypedData_v3" | "eth_signTypedData_v4", {
|
31
|
+
personal_sign: "personal_sign";
|
32
|
+
eth_sendTransaction: "eth_sendTransaction";
|
33
|
+
eth_sign: "eth_sign";
|
34
|
+
eth_signTransaction: "eth_signTransaction";
|
35
|
+
eth_signTypedData: "eth_signTypedData";
|
36
|
+
eth_signTypedData_v1: "eth_signTypedData_v1";
|
37
|
+
eth_signTypedData_v2: "eth_signTypedData_v2";
|
38
|
+
eth_signTypedData_v3: "eth_signTypedData_v3";
|
39
|
+
eth_signTypedData_v4: "eth_signTypedData_v4";
|
40
|
+
}>>;
|
41
|
+
/**
|
42
|
+
* Account type.
|
43
|
+
*/
|
44
|
+
type: import("superstruct").Struct<"eip155:eoa" | "eip155:erc4337", {
|
45
|
+
"eip155:eoa": "eip155:eoa";
|
46
|
+
"eip155:erc4337": "eip155:erc4337";
|
47
|
+
}>;
|
48
|
+
}>;
|
49
|
+
/**
|
50
|
+
* Account object.
|
51
|
+
*
|
52
|
+
* Represents an account with its properties and capabilities.
|
53
|
+
*/
|
54
|
+
export declare type KeyringAccount = Infer<typeof KeyringAccountStruct>;
|
55
|
+
export declare const KeyringJsonRpcRequestStruct: import("superstruct").Struct<{
|
56
|
+
id: string;
|
57
|
+
jsonrpc: "2.0";
|
58
|
+
method: string;
|
59
|
+
} | {
|
60
|
+
id: string;
|
61
|
+
jsonrpc: "2.0";
|
62
|
+
method: string;
|
63
|
+
params: Record<string, Json> | Json[];
|
64
|
+
}, null>;
|
65
|
+
/**
|
66
|
+
* JSON-RPC request type.
|
67
|
+
*
|
68
|
+
* Represents a JSON-RPC request sent by a dApp. The request ID must be a
|
69
|
+
* string and the params field cannot be undefined.
|
70
|
+
*/
|
71
|
+
export declare type KeyringJsonRpcRequest = Infer<typeof KeyringJsonRpcRequestStruct>;
|
72
|
+
export declare const KeyringRequestStruct: import("superstruct").Struct<{
|
73
|
+
account: string;
|
74
|
+
scope: string;
|
75
|
+
request: {
|
76
|
+
id: string;
|
77
|
+
jsonrpc: "2.0";
|
78
|
+
method: string;
|
79
|
+
} | {
|
80
|
+
id: string;
|
81
|
+
jsonrpc: "2.0";
|
82
|
+
method: string;
|
83
|
+
params: Record<string, Json> | Json[];
|
84
|
+
};
|
85
|
+
}, {
|
86
|
+
/**
|
87
|
+
* Account ID (UUIDv4).
|
88
|
+
*/
|
89
|
+
account: import("superstruct").Struct<string, null>;
|
90
|
+
/**
|
91
|
+
* Request's scope (CAIP-2 chain ID).
|
92
|
+
*/
|
93
|
+
scope: import("superstruct").Struct<string, null>;
|
94
|
+
/**
|
95
|
+
* JSON-RPC request sent by the client application.
|
96
|
+
*
|
97
|
+
* Note: The request ID must be a string.
|
98
|
+
*/
|
99
|
+
request: import("superstruct").Struct<{
|
100
|
+
id: string;
|
101
|
+
jsonrpc: "2.0";
|
102
|
+
method: string;
|
103
|
+
} | {
|
104
|
+
id: string;
|
105
|
+
jsonrpc: "2.0";
|
106
|
+
method: string;
|
107
|
+
params: Record<string, Json> | Json[];
|
108
|
+
}, null>;
|
109
|
+
}>;
|
110
|
+
/**
|
111
|
+
* Keyring request.
|
112
|
+
*
|
113
|
+
* Represents a request made to the keyring for account-related operations.
|
114
|
+
*/
|
115
|
+
export declare type KeyringRequest = Infer<typeof KeyringRequestStruct>;
|
116
|
+
export declare const SubmitRequestResponseStruct: import("superstruct").Struct<{
|
117
|
+
pending: true;
|
118
|
+
} | {
|
119
|
+
pending: false;
|
120
|
+
result: Json;
|
121
|
+
}, null>;
|
122
|
+
/**
|
123
|
+
* Response returned when submitting a request to the Keyring.
|
124
|
+
*/
|
125
|
+
export declare type SubmitRequestResponse = Infer<typeof SubmitRequestResponseStruct>;
|
126
|
+
/**
|
127
|
+
* Keyring interface.
|
128
|
+
*
|
129
|
+
* Represents the functionality and operations related to managing accounts and
|
130
|
+
* handling requests.
|
131
|
+
*/
|
132
|
+
export declare type Keyring = {
|
133
|
+
/**
|
134
|
+
* List accounts.
|
135
|
+
*
|
136
|
+
* Retrieves an array of KeyringAccount objects representing the available
|
137
|
+
* accounts.
|
138
|
+
*
|
139
|
+
* @returns A promise that resolves to an array of KeyringAccount objects.
|
140
|
+
*/
|
141
|
+
listAccounts(): Promise<KeyringAccount[]>;
|
142
|
+
/**
|
143
|
+
* Get an account.
|
144
|
+
*
|
145
|
+
* Retrieves the KeyringAccount object for the given account ID.
|
146
|
+
*
|
147
|
+
* @param id - The ID of the account to retrieve.
|
148
|
+
* @returns A promise that resolves to the KeyringAccount object if found, or
|
149
|
+
* undefined otherwise.
|
150
|
+
*/
|
151
|
+
getAccount(id: string): Promise<KeyringAccount | undefined>;
|
152
|
+
/**
|
153
|
+
* Create an account.
|
154
|
+
*
|
155
|
+
* Creates a new account with the given name, supported chains, and optional
|
156
|
+
* account options.
|
157
|
+
*
|
158
|
+
* @param name - The name of the account.
|
159
|
+
* @param options - Keyring-defined options for the account (optional).
|
160
|
+
* @returns A promise that resolves to the newly created KeyringAccount
|
161
|
+
* object without any private information.
|
162
|
+
*/
|
163
|
+
createAccount(name: string, options?: Record<string, Json> | null): Promise<KeyringAccount>;
|
164
|
+
/**
|
165
|
+
* Filter supported chains for a given account.
|
166
|
+
*
|
167
|
+
* @param id - ID of the account to be checked.
|
168
|
+
* @param chains - List of chains (CAIP-2) to be checked.
|
169
|
+
* @returns A Promise that resolves to a filtered list of CAIP-2 IDs
|
170
|
+
* representing the supported chains.
|
171
|
+
*/
|
172
|
+
filterAccountChains(id: string, chains: string[]): Promise<string[]>;
|
173
|
+
/**
|
174
|
+
* Update an account.
|
175
|
+
*
|
176
|
+
* Updates the account with the given account object. Does nothing if the
|
177
|
+
* account does not exist.
|
178
|
+
*
|
179
|
+
* @param account - The updated account object.
|
180
|
+
* @returns A promise that resolves when the account is successfully updated.
|
181
|
+
*/
|
182
|
+
updateAccount(account: KeyringAccount): Promise<void>;
|
183
|
+
/**
|
184
|
+
* Delete an account from the keyring.
|
185
|
+
*
|
186
|
+
* Deletes the account with the given ID from the keyring.
|
187
|
+
*
|
188
|
+
* @param id - The ID of the account to delete.
|
189
|
+
* @returns A promise that resolves when the account is successfully deleted.
|
190
|
+
*/
|
191
|
+
deleteAccount(id: string): Promise<void>;
|
192
|
+
/**
|
193
|
+
* List all submitted requests.
|
194
|
+
*
|
195
|
+
* Retrieves an array of KeyringRequest objects representing the submitted
|
196
|
+
* requests.
|
197
|
+
*
|
198
|
+
* @returns A promise that resolves to an array of KeyringRequest objects.
|
199
|
+
*/
|
200
|
+
listRequests(): Promise<KeyringRequest[]>;
|
201
|
+
/**
|
202
|
+
* Get a request.
|
203
|
+
*
|
204
|
+
* Retrieves the KeyringRequest object for the given request ID.
|
205
|
+
*
|
206
|
+
* @param id - The ID of the request to retrieve.
|
207
|
+
* @returns A promise that resolves to the KeyringRequest object if found, or
|
208
|
+
* undefined otherwise.
|
209
|
+
*/
|
210
|
+
getRequest(id: string): Promise<KeyringRequest | undefined>;
|
211
|
+
/**
|
212
|
+
* Submit a request.
|
213
|
+
*
|
214
|
+
* Submits the given KeyringRequest object.
|
215
|
+
*
|
216
|
+
* @param request - The KeyringRequest object to submit.
|
217
|
+
* @returns A promise that resolves to the request response.
|
218
|
+
*/
|
219
|
+
submitRequest(request: KeyringRequest): Promise<SubmitRequestResponse>;
|
220
|
+
/**
|
221
|
+
* Approve a request.
|
222
|
+
*
|
223
|
+
* Approves the request with the given ID and sets the response if provided.
|
224
|
+
*
|
225
|
+
* @param id - The ID of the request to approve.
|
226
|
+
* @param result - The response to the request (optional).
|
227
|
+
* @returns A promise that resolves when the request is successfully
|
228
|
+
* approved.
|
229
|
+
*/
|
230
|
+
approveRequest(id: string, result?: Json): Promise<void>;
|
231
|
+
/**
|
232
|
+
* Reject a request.
|
233
|
+
*
|
234
|
+
* Rejects the request with the given ID.
|
235
|
+
*
|
236
|
+
* @param id - The ID of the request to reject.
|
237
|
+
* @returns A promise that resolves when the request is successfully
|
238
|
+
* rejected.
|
239
|
+
*/
|
240
|
+
rejectRequest(id: string): Promise<void>;
|
241
|
+
};
|
@@ -0,0 +1,81 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.SubmitRequestResponseStruct = exports.KeyringRequestStruct = exports.KeyringJsonRpcRequestStruct = exports.KeyringAccountStruct = void 0;
|
4
|
+
const utils_1 = require("@metamask/utils");
|
5
|
+
const superstruct_1 = require("superstruct");
|
6
|
+
const utils_2 = require("./utils");
|
7
|
+
exports.KeyringAccountStruct = (0, superstruct_1.object)({
|
8
|
+
/**
|
9
|
+
* Account ID (UUIDv4).
|
10
|
+
*/
|
11
|
+
id: utils_2.UuidStruct,
|
12
|
+
/**
|
13
|
+
* User-chosen account name.
|
14
|
+
*/
|
15
|
+
name: (0, superstruct_1.string)(),
|
16
|
+
/**
|
17
|
+
* Account address or next receive address (UTXO).
|
18
|
+
*/
|
19
|
+
address: (0, superstruct_1.string)(),
|
20
|
+
/**
|
21
|
+
* Keyring-dependent account options.
|
22
|
+
*/
|
23
|
+
options: (0, superstruct_1.nullable)((0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct)),
|
24
|
+
/**
|
25
|
+
* Account supported methods.
|
26
|
+
*/
|
27
|
+
supportedMethods: (0, superstruct_1.array)((0, superstruct_1.enums)([
|
28
|
+
'personal_sign',
|
29
|
+
'eth_sendTransaction',
|
30
|
+
'eth_sign',
|
31
|
+
'eth_signTransaction',
|
32
|
+
'eth_signTypedData',
|
33
|
+
'eth_signTypedData_v1',
|
34
|
+
'eth_signTypedData_v2',
|
35
|
+
'eth_signTypedData_v3',
|
36
|
+
'eth_signTypedData_v4',
|
37
|
+
])),
|
38
|
+
/**
|
39
|
+
* Account type.
|
40
|
+
*/
|
41
|
+
type: (0, superstruct_1.enums)(['eip155:eoa', 'eip155:erc4337']),
|
42
|
+
});
|
43
|
+
exports.KeyringJsonRpcRequestStruct = (0, superstruct_1.union)([
|
44
|
+
(0, superstruct_1.object)({
|
45
|
+
jsonrpc: (0, superstruct_1.literal)('2.0'),
|
46
|
+
id: (0, superstruct_1.string)(),
|
47
|
+
method: (0, superstruct_1.string)(),
|
48
|
+
}),
|
49
|
+
(0, superstruct_1.object)({
|
50
|
+
jsonrpc: (0, superstruct_1.literal)('2.0'),
|
51
|
+
id: (0, superstruct_1.string)(),
|
52
|
+
method: (0, superstruct_1.string)(),
|
53
|
+
params: (0, superstruct_1.union)([(0, superstruct_1.array)(utils_1.JsonStruct), (0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct)]),
|
54
|
+
}),
|
55
|
+
]);
|
56
|
+
exports.KeyringRequestStruct = (0, superstruct_1.object)({
|
57
|
+
/**
|
58
|
+
* Account ID (UUIDv4).
|
59
|
+
*/
|
60
|
+
account: utils_2.UuidStruct,
|
61
|
+
/**
|
62
|
+
* Request's scope (CAIP-2 chain ID).
|
63
|
+
*/
|
64
|
+
scope: (0, superstruct_1.string)(),
|
65
|
+
/**
|
66
|
+
* JSON-RPC request sent by the client application.
|
67
|
+
*
|
68
|
+
* Note: The request ID must be a string.
|
69
|
+
*/
|
70
|
+
request: exports.KeyringJsonRpcRequestStruct,
|
71
|
+
});
|
72
|
+
exports.SubmitRequestResponseStruct = (0, superstruct_1.union)([
|
73
|
+
(0, superstruct_1.object)({
|
74
|
+
pending: (0, superstruct_1.literal)(true),
|
75
|
+
}),
|
76
|
+
(0, superstruct_1.object)({
|
77
|
+
pending: (0, superstruct_1.literal)(false),
|
78
|
+
result: (0, superstruct_1.nullable)(utils_1.JsonStruct),
|
79
|
+
}),
|
80
|
+
]);
|
81
|
+
//# sourceMappingURL=keyring-api.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"keyring-api.js","sourceRoot":"","sources":["../src/keyring-api.ts"],"names":[],"mappings":";;;AAAA,2CAAmD;AACnD,6CAUqB;AAErB,mCAAqC;AAExB,QAAA,oBAAoB,GAAG,IAAA,oBAAM,EAAC;IACzC;;OAEG;IACH,EAAE,EAAE,kBAAU;IAEd;;OAEG;IACH,IAAI,EAAE,IAAA,oBAAM,GAAE;IAEd;;OAEG;IACH,OAAO,EAAE,IAAA,oBAAM,GAAE;IAEjB;;OAEG;IACH,OAAO,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC;IAE/C;;OAEG;IACH,gBAAgB,EAAE,IAAA,mBAAK,EACrB,IAAA,mBAAK,EAAC;QACJ,eAAe;QACf,qBAAqB;QACrB,UAAU;QACV,qBAAqB;QACrB,mBAAmB;QACnB,sBAAsB;QACtB,sBAAsB;QACtB,sBAAsB;QACtB,sBAAsB;KACvB,CAAC,CACH;IAED;;OAEG;IACH,IAAI,EAAE,IAAA,mBAAK,EAAC,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;CAC9C,CAAC,CAAC;AASU,QAAA,2BAA2B,GAAG,IAAA,mBAAK,EAAC;IAC/C,IAAA,oBAAM,EAAC;QACL,OAAO,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;QACvB,EAAE,EAAE,IAAA,oBAAM,GAAE;QACZ,MAAM,EAAE,IAAA,oBAAM,GAAE;KACjB,CAAC;IACF,IAAA,oBAAM,EAAC;QACL,OAAO,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;QACvB,EAAE,EAAE,IAAA,oBAAM,GAAE;QACZ,MAAM,EAAE,IAAA,oBAAM,GAAE;QAChB,MAAM,EAAE,IAAA,mBAAK,EAAC,CAAC,IAAA,mBAAK,EAAC,kBAAU,CAAC,EAAE,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC;KACjE,CAAC;CACH,CAAC,CAAC;AAUU,QAAA,oBAAoB,GAAG,IAAA,oBAAM,EAAC;IACzC;;OAEG;IACH,OAAO,EAAE,kBAAU;IAEnB;;OAEG;IACH,KAAK,EAAE,IAAA,oBAAM,GAAE;IAEf;;;;OAIG;IACH,OAAO,EAAE,mCAA2B;CACrC,CAAC,CAAC;AASU,QAAA,2BAA2B,GAAG,IAAA,mBAAK,EAAC;IAC/C,IAAA,oBAAM,EAAC;QACL,OAAO,EAAE,IAAA,qBAAO,EAAC,IAAI,CAAC;KACvB,CAAC;IACF,IAAA,oBAAM,EAAC;QACL,OAAO,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;QACvB,MAAM,EAAE,IAAA,sBAAQ,EAAC,kBAAU,CAAC;KAC7B,CAAC;CACH,CAAC,CAAC","sourcesContent":["import { Json, JsonStruct } from '@metamask/utils';\nimport {\n literal,\n union,\n nullable,\n object,\n string,\n enums,\n Infer,\n record,\n array,\n} from 'superstruct';\n\nimport { UuidStruct } from './utils';\n\nexport const KeyringAccountStruct = object({\n /**\n * Account ID (UUIDv4).\n */\n id: UuidStruct,\n\n /**\n * User-chosen account name.\n */\n name: string(),\n\n /**\n * Account address or next receive address (UTXO).\n */\n address: string(),\n\n /**\n * Keyring-dependent account options.\n */\n options: nullable(record(string(), JsonStruct)),\n\n /**\n * Account supported methods.\n */\n supportedMethods: array(\n enums([\n 'personal_sign',\n 'eth_sendTransaction',\n 'eth_sign',\n 'eth_signTransaction',\n 'eth_signTypedData',\n 'eth_signTypedData_v1',\n 'eth_signTypedData_v2',\n 'eth_signTypedData_v3',\n 'eth_signTypedData_v4',\n ]),\n ),\n\n /**\n * Account type.\n */\n type: enums(['eip155:eoa', 'eip155:erc4337']),\n});\n\n/**\n * Account object.\n *\n * Represents an account with its properties and capabilities.\n */\nexport type KeyringAccount = Infer<typeof KeyringAccountStruct>;\n\nexport const KeyringJsonRpcRequestStruct = union([\n object({\n jsonrpc: literal('2.0'),\n id: string(),\n method: string(),\n }),\n object({\n jsonrpc: literal('2.0'),\n id: string(),\n method: string(),\n params: union([array(JsonStruct), record(string(), JsonStruct)]),\n }),\n]);\n\n/**\n * JSON-RPC request type.\n *\n * Represents a JSON-RPC request sent by a dApp. The request ID must be a\n * string and the params field cannot be undefined.\n */\nexport type KeyringJsonRpcRequest = Infer<typeof KeyringJsonRpcRequestStruct>;\n\nexport const KeyringRequestStruct = object({\n /**\n * Account ID (UUIDv4).\n */\n account: UuidStruct,\n\n /**\n * Request's scope (CAIP-2 chain ID).\n */\n scope: string(),\n\n /**\n * JSON-RPC request sent by the client application.\n *\n * Note: The request ID must be a string.\n */\n request: KeyringJsonRpcRequestStruct,\n});\n\n/**\n * Keyring request.\n *\n * Represents a request made to the keyring for account-related operations.\n */\nexport type KeyringRequest = Infer<typeof KeyringRequestStruct>;\n\nexport const SubmitRequestResponseStruct = union([\n object({\n pending: literal(true),\n }),\n object({\n pending: literal(false),\n result: nullable(JsonStruct),\n }),\n]);\n\n/**\n * Response returned when submitting a request to the Keyring.\n */\nexport type SubmitRequestResponse = Infer<typeof SubmitRequestResponseStruct>;\n\n/**\n * Keyring interface.\n *\n * Represents the functionality and operations related to managing accounts and\n * handling requests.\n */\nexport type Keyring = {\n /**\n * List accounts.\n *\n * Retrieves an array of KeyringAccount objects representing the available\n * accounts.\n *\n * @returns A promise that resolves to an array of KeyringAccount objects.\n */\n listAccounts(): Promise<KeyringAccount[]>;\n\n /**\n * Get an account.\n *\n * Retrieves the KeyringAccount object for the given account ID.\n *\n * @param id - The ID of the account to retrieve.\n * @returns A promise that resolves to the KeyringAccount object if found, or\n * undefined otherwise.\n */\n getAccount(id: string): Promise<KeyringAccount | undefined>;\n\n /**\n * Create an account.\n *\n * Creates a new account with the given name, supported chains, and optional\n * account options.\n *\n * @param name - The name of the account.\n * @param options - Keyring-defined options for the account (optional).\n * @returns A promise that resolves to the newly created KeyringAccount\n * object without any private information.\n */\n createAccount(\n name: string,\n options?: Record<string, Json> | null,\n ): Promise<KeyringAccount>;\n\n /**\n * Filter supported chains for a given account.\n *\n * @param id - ID of the account to be checked.\n * @param chains - List of chains (CAIP-2) to be checked.\n * @returns A Promise that resolves to a filtered list of CAIP-2 IDs\n * representing the supported chains.\n */\n filterAccountChains(id: string, chains: string[]): Promise<string[]>;\n\n /**\n * Update an account.\n *\n * Updates the account with the given account object. Does nothing if the\n * account does not exist.\n *\n * @param account - The updated account object.\n * @returns A promise that resolves when the account is successfully updated.\n */\n updateAccount(account: KeyringAccount): Promise<void>;\n\n /**\n * Delete an account from the keyring.\n *\n * Deletes the account with the given ID from the keyring.\n *\n * @param id - The ID of the account to delete.\n * @returns A promise that resolves when the account is successfully deleted.\n */\n deleteAccount(id: string): Promise<void>;\n\n /**\n * List all submitted requests.\n *\n * Retrieves an array of KeyringRequest objects representing the submitted\n * requests.\n *\n * @returns A promise that resolves to an array of KeyringRequest objects.\n */\n listRequests(): Promise<KeyringRequest[]>;\n\n /**\n * Get a request.\n *\n * Retrieves the KeyringRequest object for the given request ID.\n *\n * @param id - The ID of the request to retrieve.\n * @returns A promise that resolves to the KeyringRequest object if found, or\n * undefined otherwise.\n */\n getRequest(id: string): Promise<KeyringRequest | undefined>;\n\n /**\n * Submit a request.\n *\n * Submits the given KeyringRequest object.\n *\n * @param request - The KeyringRequest object to submit.\n * @returns A promise that resolves to the request response.\n */\n submitRequest(request: KeyringRequest): Promise<SubmitRequestResponse>;\n\n /**\n * Approve a request.\n *\n * Approves the request with the given ID and sets the response if provided.\n *\n * @param id - The ID of the request to approve.\n * @param result - The response to the request (optional).\n * @returns A promise that resolves when the request is successfully\n * approved.\n */\n approveRequest(id: string, result?: Json): Promise<void>;\n\n /**\n * Reject a request.\n *\n * Rejects the request with the given ID.\n *\n * @param id - The ID of the request to reject.\n * @returns A promise that resolves when the request is successfully\n * rejected.\n */\n rejectRequest(id: string): Promise<void>;\n};\n"]}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import type { Json } from '@metamask/utils';
|
2
|
+
import { Keyring, KeyringAccount, KeyringRequest, SubmitRequestResponse } from './keyring-api';
|
3
|
+
import { InternalRequest } from './keyring-internal-api';
|
4
|
+
export declare type Sender = {
|
5
|
+
send<Response extends Json>(request: InternalRequest): Promise<Response>;
|
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
|
+
createAccount(name: string, options?: Record<string, Json> | null): Promise<KeyringAccount>;
|
18
|
+
filterAccountChains(id: string, chains: string[]): Promise<string[]>;
|
19
|
+
updateAccount(account: KeyringAccount): Promise<void>;
|
20
|
+
deleteAccount(id: string): Promise<void>;
|
21
|
+
listRequests(): Promise<KeyringRequest[]>;
|
22
|
+
getRequest(id: string): Promise<KeyringRequest>;
|
23
|
+
submitRequest(request: KeyringRequest): Promise<SubmitRequestResponse>;
|
24
|
+
approveRequest(id: string): Promise<void>;
|
25
|
+
rejectRequest(id: string): Promise<void>;
|
26
|
+
}
|
@@ -0,0 +1,112 @@
|
|
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("superstruct");
|
17
|
+
const uuid_1 = require("uuid");
|
18
|
+
const keyring_internal_api_1 = require("./keyring-internal-api");
|
19
|
+
class KeyringClient {
|
20
|
+
/**
|
21
|
+
* Create a new instance of `KeyringClient`.
|
22
|
+
*
|
23
|
+
* @param sender - The `Sender` instance to use to send requests to the snap.
|
24
|
+
*/
|
25
|
+
constructor(sender) {
|
26
|
+
_KeyringClient_instances.add(this);
|
27
|
+
_KeyringClient_sender.set(this, void 0);
|
28
|
+
__classPrivateFieldSet(this, _KeyringClient_sender, sender, "f");
|
29
|
+
}
|
30
|
+
async listAccounts() {
|
31
|
+
return await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
32
|
+
method: 'keyring_listAccounts',
|
33
|
+
});
|
34
|
+
}
|
35
|
+
async getAccount(id) {
|
36
|
+
return await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
37
|
+
method: 'keyring_getAccount',
|
38
|
+
params: { id },
|
39
|
+
});
|
40
|
+
}
|
41
|
+
async createAccount(name, options = null) {
|
42
|
+
return await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
43
|
+
method: 'keyring_createAccount',
|
44
|
+
params: { name, options },
|
45
|
+
});
|
46
|
+
}
|
47
|
+
async filterAccountChains(id, chains) {
|
48
|
+
return await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
49
|
+
method: 'keyring_filterAccountChains',
|
50
|
+
params: { id, chains },
|
51
|
+
});
|
52
|
+
}
|
53
|
+
async updateAccount(account) {
|
54
|
+
await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
55
|
+
method: 'keyring_updateAccount',
|
56
|
+
params: { account },
|
57
|
+
});
|
58
|
+
}
|
59
|
+
async deleteAccount(id) {
|
60
|
+
await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
61
|
+
method: 'keyring_deleteAccount',
|
62
|
+
params: { id },
|
63
|
+
});
|
64
|
+
}
|
65
|
+
async listRequests() {
|
66
|
+
return await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
67
|
+
method: 'keyring_listRequests',
|
68
|
+
});
|
69
|
+
}
|
70
|
+
async getRequest(id) {
|
71
|
+
return await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
72
|
+
method: 'keyring_getRequest',
|
73
|
+
params: { id },
|
74
|
+
});
|
75
|
+
}
|
76
|
+
async submitRequest(request) {
|
77
|
+
return await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
78
|
+
method: 'keyring_submitRequest',
|
79
|
+
params: request,
|
80
|
+
});
|
81
|
+
}
|
82
|
+
async approveRequest(id) {
|
83
|
+
await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
84
|
+
method: 'keyring_approveRequest',
|
85
|
+
params: { id },
|
86
|
+
});
|
87
|
+
}
|
88
|
+
async rejectRequest(id) {
|
89
|
+
await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
|
90
|
+
method: 'keyring_rejectRequest',
|
91
|
+
params: { id },
|
92
|
+
});
|
93
|
+
}
|
94
|
+
}
|
95
|
+
exports.KeyringClient = KeyringClient;
|
96
|
+
_KeyringClient_sender = new WeakMap(), _KeyringClient_instances = new WeakSet(), _KeyringClient_send =
|
97
|
+
/**
|
98
|
+
* Send a request to the snap and return the response.
|
99
|
+
*
|
100
|
+
* @param partial - Partial internal request (method and params).
|
101
|
+
* @returns A promise that resolves to the response to the request.
|
102
|
+
*/
|
103
|
+
async function _KeyringClient_send(partial) {
|
104
|
+
const request = {
|
105
|
+
jsonrpc: '2.0',
|
106
|
+
id: (0, uuid_1.v4)(),
|
107
|
+
...partial,
|
108
|
+
};
|
109
|
+
(0, superstruct_1.assert)(request, keyring_internal_api_1.InternalRequestStruct);
|
110
|
+
return await __classPrivateFieldGet(this, _KeyringClient_sender, "f").send(request);
|
111
|
+
};
|
112
|
+
//# sourceMappingURL=keyring-client.js.map
|