@metamask/keyring-api 0.1.2 → 0.2.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/JsonRpcRequest.d.ts +15 -0
  3. package/dist/JsonRpcRequest.js +18 -0
  4. package/dist/JsonRpcRequest.js.map +1 -0
  5. package/dist/{keyring-client.d.ts → KeyringClient.d.ts} +7 -6
  6. package/dist/{keyring-client.js → KeyringClient.js} +25 -20
  7. package/dist/KeyringClient.js.map +1 -0
  8. package/dist/{keyring-snap-controller-client.d.ts → KeyringSnapControllerClient.d.ts} +7 -1
  9. package/dist/{keyring-snap-controller-client.js → KeyringSnapControllerClient.js} +13 -7
  10. package/dist/KeyringSnapControllerClient.js.map +1 -0
  11. package/dist/{keyring-snap-rpc-client.d.ts → KeyringSnapRpcClient.d.ts} +4 -3
  12. package/dist/{keyring-snap-rpc-client.js → KeyringSnapRpcClient.js} +5 -7
  13. package/dist/KeyringSnapRpcClient.js.map +1 -0
  14. package/dist/{keyring-api.d.ts → api.d.ts} +72 -52
  15. package/dist/api.js +110 -0
  16. package/dist/api.js.map +1 -0
  17. package/dist/events.d.ts +10 -0
  18. package/dist/events.js +17 -0
  19. package/dist/events.js.map +1 -0
  20. package/dist/index.d.ts +6 -5
  21. package/dist/index.js +6 -5
  22. package/dist/index.js.map +1 -1
  23. package/dist/{keyring-internal-api.d.ts → internal/api.d.ts} +129 -260
  24. package/dist/internal/api.js +126 -0
  25. package/dist/internal/api.js.map +1 -0
  26. package/dist/internal/events.d.ts +162 -0
  27. package/dist/internal/events.js +58 -0
  28. package/dist/internal/events.js.map +1 -0
  29. package/dist/internal/index.d.ts +2 -0
  30. package/dist/internal/index.js +19 -0
  31. package/dist/internal/index.js.map +1 -0
  32. package/dist/internal/types.d.ts +67 -0
  33. package/dist/internal/types.js +19 -0
  34. package/dist/internal/types.js.map +1 -0
  35. package/dist/{keyring-rpc-dispatcher.d.ts → rpc-handler.d.ts} +17 -2
  36. package/dist/rpc-handler.js +141 -0
  37. package/dist/rpc-handler.js.map +1 -0
  38. package/dist/snap-utils.d.ts +11 -0
  39. package/dist/snap-utils.js +21 -0
  40. package/dist/snap-utils.js.map +1 -0
  41. package/dist/utils.d.ts +3 -3
  42. package/dist/utils.js.map +1 -1
  43. package/package.json +19 -10
  44. package/dist/keyring-api.js +0 -81
  45. package/dist/keyring-api.js.map +0 -1
  46. package/dist/keyring-client.js.map +0 -1
  47. package/dist/keyring-internal-api.js +0 -154
  48. package/dist/keyring-internal-api.js.map +0 -1
  49. package/dist/keyring-rpc-dispatcher.js +0 -107
  50. package/dist/keyring-rpc-dispatcher.js.map +0 -1
  51. package/dist/keyring-snap-controller-client.js.map +0 -1
  52. package/dist/keyring-snap-rpc-client.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -6,6 +6,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.2.0]
10
+ ### Added
11
+ - Add `InternalAccount` type and create submodule `internal` ([#65](https://github.com/MetaMask/keyring-api/pull/65))
12
+ - Add keyring events and helper functions ([#74](https://github.com/MetaMask/keyring-api/pull/74))
13
+ - Add a `redirect` field to asynchronous request responses ([#75](https://github.com/MetaMask/keyring-api/pull/75))
14
+ - Add `exportAccount` method ([#60](https://github.com/MetaMask/keyring-api/pull/60))
15
+ - Add `getController` to client ([#43](https://github.com/MetaMask/keyring-api/pull/43))
16
+
17
+ ### Changed
18
+ - Rename `erc4337` -> `eip4337` ([#42](https://github.com/MetaMask/keyring-api/pull/42))
19
+ - Make `options` a mandatory field of `KeyringAccount` ([#30](https://github.com/MetaMask/keyring-api/pull/30))
20
+ - Make `approveRequest` and `rejectRequest` optional ([#63](https://github.com/MetaMask/keyring-api/pull/63))
21
+ - Make `exportAccount`, `listRequests` and `getRequest` optional ([#73](https://github.com/MetaMask/keyring-api/pull/73))
22
+ - Export enums with account methods and types ([#66](https://github.com/MetaMask/keyring-api/pull/66))
23
+ - Make `approveRequest` accept a `result` argument ([#59](https://github.com/MetaMask/keyring-api/pull/59))
24
+ - Remove account name from `KeyringAccount` type ([#55](https://github.com/MetaMask/keyring-api/pull/55))
25
+ - Remove `eth_sendTransaction` method ([#50](https://github.com/MetaMask/keyring-api/pull/50))
26
+ - Rename `supportedMethods` to `methods` ([#35](https://github.com/MetaMask/keyring-api/pull/35))
27
+
28
+ ## [0.1.3]
29
+ ### Changed
30
+ - Downgrade snaps dependencies to `0.35.2-flask.1`. ([#25](https://github.com/MetaMask/keyring-api/pull/25))
31
+
9
32
  ## [0.1.2]
10
33
  ### Changed
11
34
  - Update snaps dependencies. ([#21](https://github.com/MetaMask/keyring-api/pull/21))
@@ -25,7 +48,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
25
48
  - SnapController keyring client. It is intended to be used by MetaMask to talk to the snap.
26
49
  - Helper functions to create keyring handler in the snap.
27
50
 
28
- [Unreleased]: https://github.com/MetaMask/keyring-api/compare/v0.1.2...HEAD
51
+ [Unreleased]: https://github.com/MetaMask/keyring-api/compare/v0.2.0...HEAD
52
+ [0.2.0]: https://github.com/MetaMask/keyring-api/compare/v0.1.3...v0.2.0
53
+ [0.1.3]: https://github.com/MetaMask/keyring-api/compare/v0.1.2...v0.1.3
29
54
  [0.1.2]: https://github.com/MetaMask/keyring-api/compare/v0.1.1...v0.1.2
30
55
  [0.1.1]: https://github.com/MetaMask/keyring-api/compare/v0.1.0...v0.1.1
31
56
  [0.1.0]: https://github.com/MetaMask/keyring-api/releases/tag/v0.1.0
@@ -0,0 +1,15 @@
1
+ import { type Infer } from 'superstruct';
2
+ export declare const JsonRpcRequestStruct: import("superstruct").Struct<{
3
+ jsonrpc: "2.0";
4
+ id: string | number | null;
5
+ method: string;
6
+ } | {
7
+ jsonrpc: "2.0";
8
+ id: string | number | null;
9
+ method: string;
10
+ params: import("@metamask/utils").Json[] | Record<string, import("@metamask/utils").Json>;
11
+ }, null>;
12
+ /**
13
+ * JSON-RPC request type.
14
+ */
15
+ export declare type JsonRpcRequest = Infer<typeof JsonRpcRequestStruct>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonRpcRequestStruct = void 0;
4
+ const utils_1 = require("@metamask/utils");
5
+ const superstruct_1 = require("superstruct");
6
+ const Common = {
7
+ jsonrpc: (0, superstruct_1.literal)('2.0'),
8
+ id: (0, superstruct_1.nullable)((0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)()])),
9
+ method: (0, superstruct_1.string)(),
10
+ };
11
+ const Params = {
12
+ params: (0, superstruct_1.union)([(0, superstruct_1.array)(utils_1.JsonStruct), (0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct)]),
13
+ };
14
+ exports.JsonRpcRequestStruct = (0, superstruct_1.union)([
15
+ (0, superstruct_1.object)({ ...Common }),
16
+ (0, superstruct_1.object)({ ...Common, ...Params }),
17
+ ]);
18
+ //# sourceMappingURL=JsonRpcRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonRpcRequest.js","sourceRoot":"","sources":["../src/JsonRpcRequest.ts"],"names":[],"mappings":";;;AAAA,2CAA6C;AAC7C,6CAUqB;AAErB,MAAM,MAAM,GAAG;IACb,OAAO,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;IACvB,EAAE,EAAE,IAAA,sBAAQ,EAAC,IAAA,mBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,oBAAM,GAAE,CAAC,CAAC,CAAC;IACzC,MAAM,EAAE,IAAA,oBAAM,GAAE;CACjB,CAAC;AAEF,MAAM,MAAM,GAAG;IACb,MAAM,EAAE,IAAA,mBAAK,EAAC,CAAC,IAAA,mBAAK,EAAC,kBAAU,CAAC,EAAE,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC;CACjE,CAAC;AAEW,QAAA,oBAAoB,GAAG,IAAA,mBAAK,EAAC;IACxC,IAAA,oBAAM,EAAC,EAAE,GAAG,MAAM,EAAE,CAAC;IACrB,IAAA,oBAAM,EAAC,EAAE,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;CACjC,CAAC,CAAC","sourcesContent":["import { JsonStruct } from '@metamask/utils';\nimport {\n type Infer,\n array,\n literal,\n nullable,\n number,\n object,\n record,\n string,\n union,\n} from 'superstruct';\n\nconst Common = {\n jsonrpc: literal('2.0'),\n id: nullable(union([string(), number()])),\n method: string(),\n};\n\nconst Params = {\n params: union([array(JsonStruct), record(string(), JsonStruct)]),\n};\n\nexport const JsonRpcRequestStruct = union([\n object({ ...Common }),\n object({ ...Common, ...Params }),\n]);\n\n/**\n * JSON-RPC request type.\n */\nexport type JsonRpcRequest = Infer<typeof JsonRpcRequestStruct>;\n"]}
@@ -1,8 +1,8 @@
1
1
  import type { Json } from '@metamask/utils';
2
- import { Keyring, KeyringAccount, KeyringRequest, SubmitRequestResponse } from './keyring-api';
3
- import { InternalRequest, InternalResponse } from './keyring-internal-api';
2
+ import type { Keyring, KeyringAccount, KeyringRequest, KeyringAccountData, KeyringResponse } from './api';
3
+ import type { JsonRpcRequest } from './JsonRpcRequest';
4
4
  export declare type Sender = {
5
- send(request: InternalRequest): Promise<InternalResponse>;
5
+ send(request: JsonRpcRequest): Promise<Json>;
6
6
  };
7
7
  export declare class KeyringClient implements Keyring {
8
8
  #private;
@@ -14,13 +14,14 @@ export declare class KeyringClient implements Keyring {
14
14
  constructor(sender: Sender);
15
15
  listAccounts(): Promise<KeyringAccount[]>;
16
16
  getAccount(id: string): Promise<KeyringAccount>;
17
- createAccount(name: string, options?: Record<string, Json> | null): Promise<KeyringAccount>;
17
+ createAccount(options?: Record<string, Json>): Promise<KeyringAccount>;
18
18
  filterAccountChains(id: string, chains: string[]): Promise<string[]>;
19
19
  updateAccount(account: KeyringAccount): Promise<void>;
20
20
  deleteAccount(id: string): Promise<void>;
21
+ exportAccount(id: string): Promise<KeyringAccountData>;
21
22
  listRequests(): Promise<KeyringRequest[]>;
22
23
  getRequest(id: string): Promise<KeyringRequest>;
23
- submitRequest(request: KeyringRequest): Promise<SubmitRequestResponse>;
24
- approveRequest(id: string): Promise<void>;
24
+ submitRequest(request: KeyringRequest): Promise<KeyringResponse>;
25
+ approveRequest(id: string, data?: Record<string, Json>): Promise<void>;
25
26
  rejectRequest(id: string): Promise<void>;
26
27
  }
@@ -15,8 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.KeyringClient = void 0;
16
16
  const superstruct_1 = require("superstruct");
17
17
  const uuid_1 = require("uuid");
18
- const keyring_api_1 = require("./keyring-api");
19
- const keyring_internal_api_1 = require("./keyring-internal-api");
18
+ const api_1 = require("./internal/api");
20
19
  const utils_1 = require("./utils");
21
20
  class KeyringClient {
22
21
  /**
@@ -32,66 +31,72 @@ class KeyringClient {
32
31
  async listAccounts() {
33
32
  return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
34
33
  method: 'keyring_listAccounts',
35
- }), keyring_internal_api_1.ListAccountsResponseStruct);
34
+ }), api_1.ListAccountsResponseStruct);
36
35
  }
37
36
  async getAccount(id) {
38
37
  return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
39
38
  method: 'keyring_getAccount',
40
39
  params: { id },
41
- }), keyring_internal_api_1.GetAccountResponseStruct);
40
+ }), api_1.GetAccountResponseStruct);
42
41
  }
43
- async createAccount(name, options = null) {
42
+ async createAccount(options = {}) {
44
43
  return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
45
44
  method: 'keyring_createAccount',
46
- params: { name, options },
47
- }), keyring_internal_api_1.CreateAccountResponseStruct);
45
+ params: { options },
46
+ }), api_1.CreateAccountResponseStruct);
48
47
  }
49
48
  async filterAccountChains(id, chains) {
50
49
  return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
51
50
  method: 'keyring_filterAccountChains',
52
51
  params: { id, chains },
53
- }), keyring_internal_api_1.FilterAccountChainsResponseStruct);
52
+ }), api_1.FilterAccountChainsResponseStruct);
54
53
  }
55
54
  async updateAccount(account) {
56
55
  (0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
57
56
  method: 'keyring_updateAccount',
58
57
  params: { account },
59
- }), keyring_internal_api_1.UpdateAccountResponseStruct);
58
+ }), api_1.UpdateAccountResponseStruct);
60
59
  }
61
60
  async deleteAccount(id) {
62
61
  (0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
63
62
  method: 'keyring_deleteAccount',
64
63
  params: { id },
65
- }), keyring_internal_api_1.DeleteAccountResponseStruct);
64
+ }), api_1.DeleteAccountResponseStruct);
65
+ }
66
+ async exportAccount(id) {
67
+ return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
68
+ method: 'keyring_exportAccount',
69
+ params: { id },
70
+ }), api_1.ExportAccountResponseStruct);
66
71
  }
67
72
  async listRequests() {
68
73
  return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
69
74
  method: 'keyring_listRequests',
70
- }), keyring_internal_api_1.ListRequestsResponseStruct);
75
+ }), api_1.ListRequestsResponseStruct);
71
76
  }
72
77
  async getRequest(id) {
73
78
  return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
74
79
  method: 'keyring_getRequest',
75
80
  params: { id },
76
- }), keyring_internal_api_1.GetRequestResponseStruct);
81
+ }), api_1.GetRequestResponseStruct);
77
82
  }
78
83
  async submitRequest(request) {
79
84
  return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
80
85
  method: 'keyring_submitRequest',
81
86
  params: request,
82
- }), keyring_api_1.SubmitRequestResponseStruct);
87
+ }), api_1.SubmitRequestResponseStruct);
83
88
  }
84
- async approveRequest(id) {
89
+ async approveRequest(id, data = {}) {
85
90
  (0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
86
91
  method: 'keyring_approveRequest',
87
- params: { id },
88
- }), keyring_internal_api_1.ApproveRequestResponseStruct);
92
+ params: { id, data },
93
+ }), api_1.ApproveRequestResponseStruct);
89
94
  }
90
95
  async rejectRequest(id) {
91
96
  (0, superstruct_1.assert)(await __classPrivateFieldGet(this, _KeyringClient_instances, "m", _KeyringClient_send).call(this, {
92
97
  method: 'keyring_rejectRequest',
93
98
  params: { id },
94
- }), keyring_internal_api_1.RejectRequestResponseStruct);
99
+ }), api_1.RejectRequestResponseStruct);
95
100
  }
96
101
  }
97
102
  exports.KeyringClient = KeyringClient;
@@ -99,14 +104,14 @@ _KeyringClient_sender = new WeakMap(), _KeyringClient_instances = new WeakSet(),
99
104
  /**
100
105
  * Send a request to the snap and return the response.
101
106
  *
102
- * @param partial - Partial internal request (method and params).
107
+ * @param partial - A partial JSON-RPC request (method and params).
103
108
  * @returns A promise that resolves to the response to the request.
104
109
  */
105
110
  async function _KeyringClient_send(partial) {
106
- return await __classPrivateFieldGet(this, _KeyringClient_sender, "f").send({
111
+ return __classPrivateFieldGet(this, _KeyringClient_sender, "f").send({
107
112
  jsonrpc: '2.0',
108
113
  id: (0, uuid_1.v4)(),
109
114
  ...partial,
110
115
  });
111
116
  };
112
- //# sourceMappingURL=keyring-client.js.map
117
+ //# sourceMappingURL=KeyringClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringClient.js","sourceRoot":"","sources":["../src/KeyringClient.ts"],"names":[],"mappings":";;;;;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AAM,EAAE,wBAAwB;YAChC,MAAM,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE;SACrB,CAAC,EACF,kCAA4B,CAC7B,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,EAAU;QAC5B,IAAA,oBAAM,EACJ,MAAM,uBAAA,IAAI,qDAAM,MAAV,IAAI,EAAO;YACf,MAAM,EAAE,uBAAuB;YAC/B,MAAM,EAAE,EAAE,EAAE,EAAE;SACf,CAAC,EACF,iCAA2B,CAC5B,CAAC;IACJ,CAAC;CACF;AAtJD,sCAsJC;;AA1IC;;;;;GAKG;AACH,KAAK,8BACH,OAAoD;IAEpD,OAAO,uBAAA,IAAI,6BAAQ,CAAC,IAAI,CAAC;QACvB,OAAO,EAAE,KAAK;QACd,EAAE,EAAE,IAAA,SAAI,GAAE;QACV,GAAG,OAAO;KACX,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { Json } from '@metamask/utils';\nimport { assert } from 'superstruct';\nimport { v4 as uuid } from 'uuid';\n\nimport type {\n Keyring,\n KeyringAccount,\n KeyringRequest,\n KeyringAccountData,\n KeyringResponse,\n} from './api';\nimport {\n ApproveRequestResponseStruct,\n CreateAccountResponseStruct,\n DeleteAccountResponseStruct,\n ExportAccountResponseStruct,\n FilterAccountChainsResponseStruct,\n GetAccountResponseStruct,\n GetRequestResponseStruct,\n ListAccountsResponseStruct,\n ListRequestsResponseStruct,\n RejectRequestResponseStruct,\n SubmitRequestResponseStruct,\n UpdateAccountResponseStruct,\n} from './internal/api';\nimport type { JsonRpcRequest } from './JsonRpcRequest';\nimport { type OmitUnion, strictMask } from './utils';\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(\n partial: OmitUnion<JsonRpcRequest, 'jsonrpc' | 'id'>,\n ): 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: 'keyring_listAccounts',\n }),\n ListAccountsResponseStruct,\n );\n }\n\n async getAccount(id: string): Promise<KeyringAccount> {\n return strictMask(\n await this.#send({\n method: 'keyring_getAccount',\n params: { id },\n }),\n GetAccountResponseStruct,\n );\n }\n\n async createAccount(\n options: Record<string, Json> = {},\n ): Promise<KeyringAccount> {\n return strictMask(\n await this.#send({\n method: 'keyring_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: 'keyring_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: 'keyring_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: 'keyring_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: 'keyring_exportAccount',\n params: { id },\n }),\n ExportAccountResponseStruct,\n );\n }\n\n async listRequests(): Promise<KeyringRequest[]> {\n return strictMask(\n await this.#send({\n method: 'keyring_listRequests',\n }),\n ListRequestsResponseStruct,\n );\n }\n\n async getRequest(id: string): Promise<KeyringRequest> {\n return strictMask(\n await this.#send({\n method: 'keyring_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: 'keyring_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: 'keyring_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: 'keyring_rejectRequest',\n params: { id },\n }),\n RejectRequestResponseStruct,\n );\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import type { SnapController } from '@metamask/snaps-controllers';
2
2
  import type { HandlerType } from '@metamask/snaps-utils';
3
- import { KeyringClient } from './keyring-client';
3
+ import { KeyringClient } from './KeyringClient';
4
4
  /**
5
5
  * A `KeyringClient` that allows the communication with a snap through the
6
6
  * `SnapController`.
@@ -35,4 +35,10 @@ export declare class KeyringSnapControllerClient extends KeyringClient {
35
35
  * specified snap ID.
36
36
  */
37
37
  withSnapId(snapId: string): KeyringSnapControllerClient;
38
+ /**
39
+ * Get the `SnapController` instance used by this client.
40
+ *
41
+ * @returns The `SnapController` instance used by this client.
42
+ */
43
+ getController(): SnapController;
38
44
  }
@@ -13,9 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var _SnapControllerSender_snapId, _SnapControllerSender_origin, _SnapControllerSender_controller, _SnapControllerSender_handler, _KeyringSnapControllerClient_controller;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.KeyringSnapControllerClient = void 0;
16
- const keyring_client_1 = require("./keyring-client");
17
- const keyring_internal_api_1 = require("./keyring-internal-api");
18
- const utils_1 = require("./utils");
16
+ const KeyringClient_1 = require("./KeyringClient");
19
17
  /**
20
18
  * Implementation of the `Sender` interface that can be used to send requests
21
19
  * to a snap through a `SnapController`.
@@ -46,12 +44,12 @@ class SnapControllerSender {
46
44
  * @returns A promise that resolves to the response of the request.
47
45
  */
48
46
  async send(request) {
49
- return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _SnapControllerSender_controller, "f").handleRequest({
47
+ return __classPrivateFieldGet(this, _SnapControllerSender_controller, "f").handleRequest({
50
48
  snapId: __classPrivateFieldGet(this, _SnapControllerSender_snapId, "f"),
51
49
  origin: __classPrivateFieldGet(this, _SnapControllerSender_origin, "f"),
52
50
  handler: __classPrivateFieldGet(this, _SnapControllerSender_handler, "f"),
53
51
  request,
54
- }), keyring_internal_api_1.InternalResponseStruct);
52
+ });
55
53
  }
56
54
  }
57
55
  _SnapControllerSender_snapId = new WeakMap(), _SnapControllerSender_origin = new WeakMap(), _SnapControllerSender_controller = new WeakMap(), _SnapControllerSender_handler = new WeakMap();
@@ -59,7 +57,7 @@ _SnapControllerSender_snapId = new WeakMap(), _SnapControllerSender_origin = new
59
57
  * A `KeyringClient` that allows the communication with a snap through the
60
58
  * `SnapController`.
61
59
  */
62
- class KeyringSnapControllerClient extends keyring_client_1.KeyringClient {
60
+ class KeyringSnapControllerClient extends KeyringClient_1.KeyringClient {
63
61
  /**
64
62
  * Create a new instance of `KeyringSnapControllerClient`.
65
63
  *
@@ -92,7 +90,15 @@ class KeyringSnapControllerClient extends keyring_client_1.KeyringClient {
92
90
  snapId,
93
91
  });
94
92
  }
93
+ /**
94
+ * Get the `SnapController` instance used by this client.
95
+ *
96
+ * @returns The `SnapController` instance used by this client.
97
+ */
98
+ getController() {
99
+ return __classPrivateFieldGet(this, _KeyringSnapControllerClient_controller, "f");
100
+ }
95
101
  }
96
102
  exports.KeyringSnapControllerClient = KeyringSnapControllerClient;
97
103
  _KeyringSnapControllerClient_controller = new WeakMap();
98
- //# sourceMappingURL=keyring-snap-controller-client.js.map
104
+ //# sourceMappingURL=KeyringSnapControllerClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringSnapControllerClient.js","sourceRoot":"","sources":["../src/KeyringSnapControllerClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,mDAA6D;AAE7D;;;GAGG;AACH,MAAM,oBAAoB;IASxB;;;;;;;OAOG;IACH,YACE,UAAe,EACf,MAAc,EACd,MAAc,EACd,OAAoB;QApBtB,+CAAgB;QAEhB,+CAAgB;QAEhB,mDAA4B;QAE5B,gDAAsB;QAgBpB,uBAAA,IAAI,oCAAe,UAAU,MAAA,CAAC;QAC9B,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CAAC,OAAuB;QAChC,OAAO,uBAAA,IAAI,wCAAY,CAAC,aAAa,CAAC;YACpC,MAAM,EAAE,uBAAA,IAAI,oCAA2B;YACvC,MAAM,EAAE,uBAAA,IAAI,oCAAQ;YACpB,OAAO,EAAE,uBAAA,IAAI,qCAAS;YACtB,OAAO;SACR,CAAkB,CAAC;IACtB,CAAC;CACF;;AAED;;;GAGG;AACH,MAAa,2BAA4B,SAAQ,6BAAa;IAG5D;;;;;;;;;;;;OAYG;IACH,YAAY,EACV,UAAU,EACV,MAAM,GAAG,WAAW,EACpB,MAAM,GAAG,UAAU,EACnB,OAAO,GAAG,cAA6B,GAMxC;QACC,KAAK,CAAC,IAAI,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;QA1BvE,0DAA4B;QA2B1B,uBAAA,IAAI,2CAAe,UAAU,MAAA,CAAC;IAChC,CAAC;IAED;;;;;;;OAOG;IACH,UAAU,CAAC,MAAc;QACvB,OAAO,IAAI,2BAA2B,CAAC;YACrC,UAAU,EAAE,uBAAA,IAAI,+CAAY;YAC5B,MAAM;SACP,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,aAAa;QACX,OAAO,uBAAA,IAAI,+CAAY,CAAC;IAC1B,CAAC;CACF;AAtDD,kEAsDC","sourcesContent":["import type { SnapController } from '@metamask/snaps-controllers';\nimport type { HandlerType, ValidatedSnapId } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\n\nimport type { JsonRpcRequest } from './JsonRpcRequest';\nimport { KeyringClient, type Sender } from './KeyringClient';\n\n/**\n * Implementation of the `Sender` interface that can be used to send requests\n * to a snap through a `SnapController`.\n */\nclass SnapControllerSender implements Sender {\n #snapId: string;\n\n #origin: string;\n\n #controller: SnapController;\n\n #handler: HandlerType;\n\n /**\n * Create a new instance of `SnapControllerSender`.\n *\n * @param controller - The `SnapController` instance to send requests to.\n * @param snapId - The ID of the snap to use.\n * @param origin - The sender's origin.\n * @param handler - The handler type.\n */\n constructor(\n controller: any,\n snapId: string,\n origin: string,\n handler: HandlerType,\n ) {\n this.#controller = controller;\n this.#snapId = snapId;\n this.#origin = origin;\n this.#handler = handler;\n }\n\n /**\n * Send a request to the snap and return the response.\n *\n * @param request - JSON-RPC request to send to the snap.\n * @returns A promise that resolves to the response of the request.\n */\n async send(request: JsonRpcRequest): Promise<Json> {\n return this.#controller.handleRequest({\n snapId: this.#snapId as ValidatedSnapId,\n origin: this.#origin,\n handler: this.#handler,\n request,\n }) as Promise<Json>;\n }\n}\n\n/**\n * A `KeyringClient` that allows the communication with a snap through the\n * `SnapController`.\n */\nexport class KeyringSnapControllerClient extends KeyringClient {\n #controller: SnapController;\n\n /**\n * Create a new instance of `KeyringSnapControllerClient`.\n *\n * The `handlerType` argument has a hard-coded default `string` value instead\n * of a `HandlerType` value to prevent the `@metamask/snaps-utils` module\n * from being required at runtime.\n *\n * @param args - Constructor arguments.\n * @param args.controller - The `SnapController` instance to use.\n * @param args.snapId - The ID of the snap to use (default: `'undefined'`).\n * @param args.origin - The sender's origin (default: `'metamask'`).\n * @param args.handler - The handler type (default: `'onRpcRequest'`).\n */\n constructor({\n controller,\n snapId = 'undefined',\n origin = 'metamask',\n handler = 'onRpcRequest' as HandlerType,\n }: {\n controller: SnapController;\n snapId?: string;\n origin?: string;\n handler?: HandlerType;\n }) {\n super(new SnapControllerSender(controller, snapId, origin, handler));\n this.#controller = controller;\n }\n\n /**\n * Create a new instance of `KeyringSnapControllerClient` with the specified\n * `snapId`.\n *\n * @param snapId - The ID of the snap to use in the new instance.\n * @returns A new instance of `KeyringSnapControllerClient` with the\n * specified snap ID.\n */\n withSnapId(snapId: string): KeyringSnapControllerClient {\n return new KeyringSnapControllerClient({\n controller: this.#controller,\n snapId,\n });\n }\n\n /**\n * Get the `SnapController` instance used by this client.\n *\n * @returns The `SnapController` instance used by this client.\n */\n getController(): SnapController {\n return this.#controller;\n }\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import type { MetaMaskInpageProvider } from '@metamask/providers';
2
- import { KeyringClient, Sender } from './keyring-client';
3
- import { type InternalRequest, InternalResponse } from './keyring-internal-api';
2
+ import type { Json } from '@metamask/utils';
3
+ import type { JsonRpcRequest } from './JsonRpcRequest';
4
+ import { KeyringClient, type Sender } from './KeyringClient';
4
5
  /**
5
6
  * Implementation of the `Sender` interface that can be used to send requests
6
7
  * to a snap through the snap JSON-RPC API.
@@ -20,7 +21,7 @@ export declare class SnapRpcSender implements Sender {
20
21
  * @param request - The JSON-RPC request to send to the snap.
21
22
  * @returns A promise that resolves to the response of the request.
22
23
  */
23
- send(request: InternalRequest): Promise<InternalResponse>;
24
+ send(request: JsonRpcRequest): Promise<Json>;
24
25
  }
25
26
  /**
26
27
  * A `KeyringClient` that allows the communication with a snap through the snap
@@ -13,9 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var _SnapRpcSender_origin, _SnapRpcSender_provider;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.KeyringSnapRpcClient = exports.SnapRpcSender = void 0;
16
- const keyring_client_1 = require("./keyring-client");
17
- const keyring_internal_api_1 = require("./keyring-internal-api");
18
- const utils_1 = require("./utils");
16
+ const KeyringClient_1 = require("./KeyringClient");
19
17
  /**
20
18
  * Implementation of the `Sender` interface that can be used to send requests
21
19
  * to a snap through the snap JSON-RPC API.
@@ -40,13 +38,13 @@ class SnapRpcSender {
40
38
  * @returns A promise that resolves to the response of the request.
41
39
  */
42
40
  async send(request) {
43
- return (0, utils_1.strictMask)(await __classPrivateFieldGet(this, _SnapRpcSender_provider, "f").request({
41
+ return __classPrivateFieldGet(this, _SnapRpcSender_provider, "f").request({
44
42
  method: 'wallet_invokeSnap',
45
43
  params: {
46
44
  snapId: __classPrivateFieldGet(this, _SnapRpcSender_origin, "f"),
47
45
  request,
48
46
  },
49
- }), keyring_internal_api_1.InternalResponseStruct);
47
+ });
50
48
  }
51
49
  }
52
50
  exports.SnapRpcSender = SnapRpcSender;
@@ -55,7 +53,7 @@ _SnapRpcSender_origin = new WeakMap(), _SnapRpcSender_provider = new WeakMap();
55
53
  * A `KeyringClient` that allows the communication with a snap through the snap
56
54
  * JSON-RPC API.
57
55
  */
58
- class KeyringSnapRpcClient extends keyring_client_1.KeyringClient {
56
+ class KeyringSnapRpcClient extends KeyringClient_1.KeyringClient {
59
57
  /**
60
58
  * Create a new instance of `KeyringSnapRpcClient`.
61
59
  *
@@ -67,4 +65,4 @@ class KeyringSnapRpcClient extends keyring_client_1.KeyringClient {
67
65
  }
68
66
  }
69
67
  exports.KeyringSnapRpcClient = KeyringSnapRpcClient;
70
- //# sourceMappingURL=keyring-snap-rpc-client.js.map
68
+ //# sourceMappingURL=KeyringSnapRpcClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringSnapRpcClient.js","sourceRoot":"","sources":["../src/KeyringSnapRpcClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,mDAA6D;AAE7D;;;GAGG;AACH,MAAa,aAAa;IAKxB;;;;;OAKG;IACH,YAAY,MAAc,EAAE,QAAgC;QAV5D,wCAAgB;QAEhB,0CAAkC;QAShC,uBAAA,IAAI,yBAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,2BAAa,QAAQ,MAAA,CAAC;IAC5B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CAAC,OAAuB;QAChC,OAAO,uBAAA,IAAI,+BAAU,CAAC,OAAO,CAAC;YAC5B,MAAM,EAAE,mBAAmB;YAC3B,MAAM,EAAE;gBACN,MAAM,EAAE,uBAAA,IAAI,6BAAQ;gBACpB,OAAO;aACR;SACF,CAAkB,CAAC;IACtB,CAAC;CACF;AA/BD,sCA+BC;;AAED;;;GAGG;AACH,MAAa,oBAAqB,SAAQ,6BAAa;IACrD;;;;;OAKG;IACH,YAAY,MAAc,EAAE,QAAgC;QAC1D,KAAK,CAAC,IAAI,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC7C,CAAC;CACF;AAVD,oDAUC","sourcesContent":["import type { MetaMaskInpageProvider } from '@metamask/providers';\nimport type { Json } from '@metamask/utils';\n\nimport type { JsonRpcRequest } from './JsonRpcRequest';\nimport { KeyringClient, type Sender } from './KeyringClient';\n\n/**\n * Implementation of the `Sender` interface that can be used to send requests\n * to a snap through the snap JSON-RPC API.\n */\nexport class SnapRpcSender implements Sender {\n #origin: string;\n\n #provider: MetaMaskInpageProvider;\n\n /**\n * Create a new instance of `SnapRpcSender`.\n *\n * @param origin - The caller's origin.\n * @param provider - The `MetaMaskInpageProvider` instance to use.\n */\n constructor(origin: string, provider: MetaMaskInpageProvider) {\n this.#origin = origin;\n this.#provider = provider;\n }\n\n /**\n * Send a request to the snap and return the response.\n *\n * @param request - The JSON-RPC request to send to the snap.\n * @returns A promise that resolves to the response of the request.\n */\n async send(request: JsonRpcRequest): Promise<Json> {\n return this.#provider.request({\n method: 'wallet_invokeSnap',\n params: {\n snapId: this.#origin,\n request,\n },\n }) as Promise<Json>;\n }\n}\n\n/**\n * A `KeyringClient` that allows the communication with a snap through the snap\n * JSON-RPC API.\n */\nexport class KeyringSnapRpcClient extends KeyringClient {\n /**\n * Create a new instance of `KeyringSnapRpcClient`.\n *\n * @param origin - Caller's origin.\n * @param provider - The `MetaMaskInpageProvider` instance to use.\n */\n constructor(origin: string, provider: MetaMaskInpageProvider) {\n super(new SnapRpcSender(origin, provider));\n }\n}\n"]}