@metamask-previews/keyring-api 8.1.0-preview-46bb5ff → 8.1.3-84e4c35

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 (76) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/JsonRpcRequest.d.ts +2 -2
  3. package/dist/JsonRpcRequest.js +3 -3
  4. package/dist/JsonRpcRequest.js.map +1 -1
  5. package/dist/KeyringClient.d.ts +1 -1
  6. package/dist/KeyringClient.js +10 -10
  7. package/dist/KeyringClient.js.map +1 -1
  8. package/dist/KeyringSnapRpcClient.js.map +1 -1
  9. package/dist/api/account.d.ts +3 -3
  10. package/dist/api/account.js +6 -5
  11. package/dist/api/account.js.map +1 -1
  12. package/dist/api/balance.d.ts +1 -1
  13. package/dist/api/balance.js +4 -3
  14. package/dist/api/balance.js.map +1 -1
  15. package/dist/api/caip.d.ts +2 -2
  16. package/dist/api/caip.js +3 -3
  17. package/dist/api/caip.js.map +1 -1
  18. package/dist/api/export.d.ts +1 -1
  19. package/dist/api/index.d.ts +1 -1
  20. package/dist/api/index.js +0 -1
  21. package/dist/api/index.js.map +1 -1
  22. package/dist/api/keyring.d.ts +1 -1
  23. package/dist/api/request.d.ts +2 -2
  24. package/dist/api/request.js +7 -6
  25. package/dist/api/request.js.map +1 -1
  26. package/dist/api/response.d.ts +1 -1
  27. package/dist/api/response.js +6 -6
  28. package/dist/api/response.js.map +1 -1
  29. package/dist/btc/types.d.ts +2 -2
  30. package/dist/btc/types.js +3 -3
  31. package/dist/btc/types.js.map +1 -1
  32. package/dist/contexts.d.ts +1 -1
  33. package/dist/eth/erc4337/types.d.ts +10 -9
  34. package/dist/eth/erc4337/types.js +12 -10
  35. package/dist/eth/erc4337/types.js.map +1 -1
  36. package/dist/eth/types.d.ts +4 -4
  37. package/dist/eth/types.js +7 -7
  38. package/dist/eth/types.js.map +1 -1
  39. package/dist/events.js +1 -1
  40. package/dist/events.js.map +1 -1
  41. package/dist/index.d.ts +2 -1
  42. package/dist/index.js +1 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/internal/api.d.ts +36 -36
  45. package/dist/internal/api.js +34 -33
  46. package/dist/internal/api.js.map +1 -1
  47. package/dist/internal/eth/EthKeyring.d.ts +1 -1
  48. package/dist/internal/eth/index.d.ts +1 -1
  49. package/dist/internal/eth/index.js +0 -15
  50. package/dist/internal/eth/index.js.map +1 -1
  51. package/dist/internal/events.d.ts +8 -8
  52. package/dist/internal/events.js +17 -16
  53. package/dist/internal/events.js.map +1 -1
  54. package/dist/internal/index.d.ts +1 -1
  55. package/dist/internal/index.js +0 -1
  56. package/dist/internal/index.js.map +1 -1
  57. package/dist/internal/rpc.js +1 -1
  58. package/dist/internal/rpc.js.map +1 -1
  59. package/dist/internal/types.d.ts +58 -61
  60. package/dist/internal/types.js +20 -23
  61. package/dist/internal/types.js.map +1 -1
  62. package/dist/rpc-handler.js +13 -13
  63. package/dist/rpc-handler.js.map +1 -1
  64. package/dist/superstruct.d.ts +83 -0
  65. package/dist/superstruct.js +81 -0
  66. package/dist/superstruct.js.map +1 -0
  67. package/dist/utils/index.d.ts +2 -0
  68. package/dist/utils/index.js +19 -0
  69. package/dist/utils/index.js.map +1 -0
  70. package/dist/utils/types.d.ts +17 -0
  71. package/dist/utils/types.js +29 -0
  72. package/dist/utils/types.js.map +1 -0
  73. package/dist/utils/typing.d.ts +32 -0
  74. package/dist/utils/typing.js +21 -0
  75. package/dist/utils/typing.js.map +1 -0
  76. package/package.json +16 -14
@@ -1,6 +1,6 @@
1
1
  import type { Infer, Struct } from '@metamask/superstruct';
2
2
  import { BtcAccountType, EthAccountType } from '../api';
3
- export declare type InternalAccountType = EthAccountType | BtcAccountType;
3
+ export type InternalAccountType = EthAccountType | BtcAccountType;
4
4
  export declare const InternalAccountMetadataStruct: Struct<{
5
5
  metadata: {
6
6
  name: string;
@@ -32,8 +32,8 @@ export declare const InternalAccountMetadataStruct: Struct<{
32
32
  lastSelected?: number;
33
33
  }, {
34
34
  name: Struct<string, null>;
35
- nameLastUpdatedAt: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
36
- snap: Struct<import("@metamask/keyring-utils").ExactOptionalTag | {
35
+ nameLastUpdatedAt: Struct<number | import("../superstruct").ExactOptionalTag, null>;
36
+ snap: Struct<import("../superstruct").ExactOptionalTag | {
37
37
  id: string;
38
38
  name: string;
39
39
  enabled: boolean;
@@ -42,7 +42,7 @@ export declare const InternalAccountMetadataStruct: Struct<{
42
42
  enabled: Struct<boolean, null>;
43
43
  name: Struct<string, null>;
44
44
  }>;
45
- lastSelected: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
45
+ lastSelected: Struct<number | import("../superstruct").ExactOptionalTag, null>;
46
46
  importTime: Struct<number, null>;
47
47
  keyring: Struct<{
48
48
  type: string;
@@ -52,8 +52,8 @@ export declare const InternalAccountMetadataStruct: Struct<{
52
52
  }>;
53
53
  }>;
54
54
  export declare const InternalEthEoaAccountStruct: Struct<{
55
- id: string;
56
55
  type: "eip155:eoa";
56
+ id: string;
57
57
  address: string;
58
58
  options: Record<string, import("@metamask/utils").Json>;
59
59
  methods: ("personal_sign" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4")[];
@@ -72,19 +72,6 @@ export declare const InternalEthEoaAccountStruct: Struct<{
72
72
  lastSelected?: number;
73
73
  };
74
74
  }, {
75
- address: Struct<string, null>;
76
- type: Struct<"eip155:eoa", "eip155:eoa">;
77
- methods: Struct<("personal_sign" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4")[], Struct<"personal_sign" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4", {
78
- personal_sign: "personal_sign";
79
- eth_sign: "eth_sign";
80
- eth_signTransaction: "eth_signTransaction";
81
- eth_signTypedData_v1: "eth_signTypedData_v1";
82
- eth_signTypedData_v3: "eth_signTypedData_v3";
83
- eth_signTypedData_v4: "eth_signTypedData_v4";
84
- }>>;
85
- id: Struct<string, null>;
86
- options: Struct<Record<string, import("@metamask/utils").Json>, null>;
87
- } & {
88
75
  metadata: Struct<{
89
76
  name: string;
90
77
  importTime: number;
@@ -100,8 +87,8 @@ export declare const InternalEthEoaAccountStruct: Struct<{
100
87
  lastSelected?: number;
101
88
  }, {
102
89
  name: Struct<string, null>;
103
- nameLastUpdatedAt: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
104
- snap: Struct<import("@metamask/keyring-utils").ExactOptionalTag | {
90
+ nameLastUpdatedAt: Struct<number | import("../superstruct").ExactOptionalTag, null>;
91
+ snap: Struct<import("../superstruct").ExactOptionalTag | {
105
92
  id: string;
106
93
  name: string;
107
94
  enabled: boolean;
@@ -110,7 +97,7 @@ export declare const InternalEthEoaAccountStruct: Struct<{
110
97
  enabled: Struct<boolean, null>;
111
98
  name: Struct<string, null>;
112
99
  }>;
113
- lastSelected: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
100
+ lastSelected: Struct<number | import("../superstruct").ExactOptionalTag, null>;
114
101
  importTime: Struct<number, null>;
115
102
  keyring: Struct<{
116
103
  type: string;
@@ -118,10 +105,22 @@ export declare const InternalEthEoaAccountStruct: Struct<{
118
105
  type: Struct<string, null>;
119
106
  }>;
120
107
  }>;
108
+ address: Struct<string, null>;
109
+ type: Struct<"eip155:eoa", "eip155:eoa">;
110
+ methods: Struct<("personal_sign" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4")[], Struct<"personal_sign" | "eth_sign" | "eth_signTransaction" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4", {
111
+ personal_sign: "personal_sign";
112
+ eth_sign: "eth_sign";
113
+ eth_signTransaction: "eth_signTransaction";
114
+ eth_signTypedData_v1: "eth_signTypedData_v1";
115
+ eth_signTypedData_v3: "eth_signTypedData_v3";
116
+ eth_signTypedData_v4: "eth_signTypedData_v4";
117
+ }>>;
118
+ id: Struct<string, null>;
119
+ options: Struct<Record<string, import("@metamask/utils").Json>, null>;
121
120
  }>;
122
121
  export declare const InternalEthErc4337AccountStruct: Struct<{
123
- id: string;
124
122
  type: "eip155:erc4337";
123
+ id: string;
125
124
  address: string;
126
125
  options: Record<string, import("@metamask/utils").Json>;
127
126
  methods: ("personal_sign" | "eth_sign" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4" | "eth_prepareUserOperation" | "eth_patchUserOperation" | "eth_signUserOperation")[];
@@ -140,21 +139,6 @@ export declare const InternalEthErc4337AccountStruct: Struct<{
140
139
  lastSelected?: number;
141
140
  };
142
141
  }, {
143
- address: Struct<string, null>;
144
- type: Struct<"eip155:erc4337", "eip155:erc4337">;
145
- methods: Struct<("personal_sign" | "eth_sign" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4" | "eth_prepareUserOperation" | "eth_patchUserOperation" | "eth_signUserOperation")[], Struct<"personal_sign" | "eth_sign" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4" | "eth_prepareUserOperation" | "eth_patchUserOperation" | "eth_signUserOperation", {
146
- personal_sign: "personal_sign";
147
- eth_sign: "eth_sign";
148
- eth_signTypedData_v1: "eth_signTypedData_v1";
149
- eth_signTypedData_v3: "eth_signTypedData_v3";
150
- eth_signTypedData_v4: "eth_signTypedData_v4";
151
- eth_prepareUserOperation: "eth_prepareUserOperation";
152
- eth_patchUserOperation: "eth_patchUserOperation";
153
- eth_signUserOperation: "eth_signUserOperation";
154
- }>>;
155
- id: Struct<string, null>;
156
- options: Struct<Record<string, import("@metamask/utils").Json>, null>;
157
- } & {
158
142
  metadata: Struct<{
159
143
  name: string;
160
144
  importTime: number;
@@ -170,8 +154,8 @@ export declare const InternalEthErc4337AccountStruct: Struct<{
170
154
  lastSelected?: number;
171
155
  }, {
172
156
  name: Struct<string, null>;
173
- nameLastUpdatedAt: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
174
- snap: Struct<import("@metamask/keyring-utils").ExactOptionalTag | {
157
+ nameLastUpdatedAt: Struct<number | import("../superstruct").ExactOptionalTag, null>;
158
+ snap: Struct<import("../superstruct").ExactOptionalTag | {
175
159
  id: string;
176
160
  name: string;
177
161
  enabled: boolean;
@@ -180,7 +164,7 @@ export declare const InternalEthErc4337AccountStruct: Struct<{
180
164
  enabled: Struct<boolean, null>;
181
165
  name: Struct<string, null>;
182
166
  }>;
183
- lastSelected: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
167
+ lastSelected: Struct<number | import("../superstruct").ExactOptionalTag, null>;
184
168
  importTime: Struct<number, null>;
185
169
  keyring: Struct<{
186
170
  type: string;
@@ -188,10 +172,24 @@ export declare const InternalEthErc4337AccountStruct: Struct<{
188
172
  type: Struct<string, null>;
189
173
  }>;
190
174
  }>;
175
+ address: Struct<string, null>;
176
+ type: Struct<"eip155:erc4337", "eip155:erc4337">;
177
+ methods: Struct<("personal_sign" | "eth_sign" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4" | "eth_prepareUserOperation" | "eth_patchUserOperation" | "eth_signUserOperation")[], Struct<"personal_sign" | "eth_sign" | "eth_signTypedData_v1" | "eth_signTypedData_v3" | "eth_signTypedData_v4" | "eth_prepareUserOperation" | "eth_patchUserOperation" | "eth_signUserOperation", {
178
+ personal_sign: "personal_sign";
179
+ eth_sign: "eth_sign";
180
+ eth_signTypedData_v1: "eth_signTypedData_v1";
181
+ eth_signTypedData_v3: "eth_signTypedData_v3";
182
+ eth_signTypedData_v4: "eth_signTypedData_v4";
183
+ eth_prepareUserOperation: "eth_prepareUserOperation";
184
+ eth_patchUserOperation: "eth_patchUserOperation";
185
+ eth_signUserOperation: "eth_signUserOperation";
186
+ }>>;
187
+ id: Struct<string, null>;
188
+ options: Struct<Record<string, import("@metamask/utils").Json>, null>;
191
189
  }>;
192
190
  export declare const InternalBtcP2wpkhAccountStruct: Struct<{
193
- id: string;
194
191
  type: "bip122:p2wpkh";
192
+ id: string;
195
193
  address: string;
196
194
  options: Record<string, import("@metamask/utils").Json>;
197
195
  methods: "btc_sendmany"[];
@@ -210,14 +208,6 @@ export declare const InternalBtcP2wpkhAccountStruct: Struct<{
210
208
  lastSelected?: number;
211
209
  };
212
210
  }, {
213
- address: Struct<string, null>;
214
- type: Struct<"bip122:p2wpkh", "bip122:p2wpkh">;
215
- methods: Struct<"btc_sendmany"[], Struct<"btc_sendmany", {
216
- btc_sendmany: "btc_sendmany";
217
- }>>;
218
- id: Struct<string, null>;
219
- options: Struct<Record<string, import("@metamask/utils").Json>, null>;
220
- } & {
221
211
  metadata: Struct<{
222
212
  name: string;
223
213
  importTime: number;
@@ -233,8 +223,8 @@ export declare const InternalBtcP2wpkhAccountStruct: Struct<{
233
223
  lastSelected?: number;
234
224
  }, {
235
225
  name: Struct<string, null>;
236
- nameLastUpdatedAt: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
237
- snap: Struct<import("@metamask/keyring-utils").ExactOptionalTag | {
226
+ nameLastUpdatedAt: Struct<number | import("../superstruct").ExactOptionalTag, null>;
227
+ snap: Struct<import("../superstruct").ExactOptionalTag | {
238
228
  id: string;
239
229
  name: string;
240
230
  enabled: boolean;
@@ -243,7 +233,7 @@ export declare const InternalBtcP2wpkhAccountStruct: Struct<{
243
233
  enabled: Struct<boolean, null>;
244
234
  name: Struct<string, null>;
245
235
  }>;
246
- lastSelected: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
236
+ lastSelected: Struct<number | import("../superstruct").ExactOptionalTag, null>;
247
237
  importTime: Struct<number, null>;
248
238
  keyring: Struct<{
249
239
  type: string;
@@ -251,15 +241,22 @@ export declare const InternalBtcP2wpkhAccountStruct: Struct<{
251
241
  type: Struct<string, null>;
252
242
  }>;
253
243
  }>;
244
+ address: Struct<string, null>;
245
+ type: Struct<"bip122:p2wpkh", "bip122:p2wpkh">;
246
+ methods: Struct<"btc_sendmany"[], Struct<"btc_sendmany", {
247
+ btc_sendmany: "btc_sendmany";
248
+ }>>;
249
+ id: Struct<string, null>;
250
+ options: Struct<Record<string, import("@metamask/utils").Json>, null>;
254
251
  }>;
255
- export declare type InternalEthEoaAccount = Infer<typeof InternalEthEoaAccountStruct>;
256
- export declare type InternalEthErc4337Account = Infer<typeof InternalEthErc4337AccountStruct>;
257
- export declare type InternalBtcP2wpkhAccount = Infer<typeof InternalBtcP2wpkhAccountStruct>;
252
+ export type InternalEthEoaAccount = Infer<typeof InternalEthEoaAccountStruct>;
253
+ export type InternalEthErc4337Account = Infer<typeof InternalEthErc4337AccountStruct>;
254
+ export type InternalBtcP2wpkhAccount = Infer<typeof InternalBtcP2wpkhAccountStruct>;
258
255
  export declare const InternalAccountStructs: Record<string, Struct<InternalEthEoaAccount> | Struct<InternalEthErc4337Account> | Struct<InternalBtcP2wpkhAccount>>;
259
- export declare type InternalAccountTypes = InternalEthEoaAccount | InternalEthErc4337Account | InternalBtcP2wpkhAccount;
256
+ export type InternalAccountTypes = InternalEthEoaAccount | InternalEthErc4337Account | InternalBtcP2wpkhAccount;
260
257
  export declare const InternalAccountStruct: Struct<{
261
- id: string;
262
258
  type: "eip155:eoa" | "eip155:erc4337" | "bip122:p2wpkh";
259
+ id: string;
263
260
  address: string;
264
261
  options: Record<string, import("@metamask/utils").Json>;
265
262
  methods: string[];
@@ -293,8 +290,8 @@ export declare const InternalAccountStruct: Struct<{
293
290
  lastSelected?: number;
294
291
  }, {
295
292
  name: Struct<string, null>;
296
- nameLastUpdatedAt: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
297
- snap: Struct<import("@metamask/keyring-utils").ExactOptionalTag | {
293
+ nameLastUpdatedAt: Struct<number | import("../superstruct").ExactOptionalTag, null>;
294
+ snap: Struct<import("../superstruct").ExactOptionalTag | {
298
295
  id: string;
299
296
  name: string;
300
297
  enabled: boolean;
@@ -303,7 +300,7 @@ export declare const InternalAccountStruct: Struct<{
303
300
  enabled: Struct<boolean, null>;
304
301
  name: Struct<string, null>;
305
302
  }>;
306
- lastSelected: Struct<number | import("@metamask/keyring-utils").ExactOptionalTag, null>;
303
+ lastSelected: Struct<number | import("../superstruct").ExactOptionalTag, null>;
307
304
  importTime: Struct<number, null>;
308
305
  keyring: Struct<{
309
306
  type: string;
@@ -327,4 +324,4 @@ export declare const InternalAccountStruct: Struct<{
327
324
  * This type is used internally by MetaMask to add additional metadata to the
328
325
  * account object. It's should not be used by external applications.
329
326
  */
330
- export declare type InternalAccount = Infer<typeof InternalAccountStruct>;
327
+ export type InternalAccount = Infer<typeof InternalAccountStruct>;
@@ -5,44 +5,41 @@ const superstruct_1 = require("@metamask/superstruct");
5
5
  const api_1 = require("../api");
6
6
  const types_1 = require("../btc/types");
7
7
  const types_2 = require("../eth/types");
8
- const keyring_utils_1 = require("@metamask/keyring-utils");
9
- exports.InternalAccountMetadataStruct = (0, keyring_utils_1.object)({
10
- metadata: (0, keyring_utils_1.object)({
8
+ const superstruct_2 = require("../superstruct");
9
+ exports.InternalAccountMetadataStruct = (0, superstruct_2.object)({
10
+ metadata: (0, superstruct_2.object)({
11
11
  name: (0, superstruct_1.string)(),
12
- nameLastUpdatedAt: (0, keyring_utils_1.exactOptional)((0, superstruct_1.number)()),
13
- snap: (0, keyring_utils_1.exactOptional)((0, keyring_utils_1.object)({
12
+ nameLastUpdatedAt: (0, superstruct_2.exactOptional)((0, superstruct_1.number)()),
13
+ snap: (0, superstruct_2.exactOptional)((0, superstruct_2.object)({
14
14
  id: (0, superstruct_1.string)(),
15
15
  enabled: (0, superstruct_1.boolean)(),
16
16
  name: (0, superstruct_1.string)(),
17
17
  })),
18
- lastSelected: (0, keyring_utils_1.exactOptional)((0, superstruct_1.number)()),
18
+ lastSelected: (0, superstruct_2.exactOptional)((0, superstruct_1.number)()),
19
19
  importTime: (0, superstruct_1.number)(),
20
- keyring: (0, keyring_utils_1.object)({
20
+ keyring: (0, superstruct_2.object)({
21
21
  type: (0, superstruct_1.string)(),
22
22
  }),
23
23
  }),
24
24
  });
25
- /**
26
- * Creates an `InternalAccount` from an existing account `superstruct` object.
27
- *
28
- * @param accountStruct - An account `superstruct` object.
29
- * @returns The `InternalAccount` associated to `accountStruct`.
30
- */
31
- function asInternalAccountStruct(accountStruct) {
32
- return (0, keyring_utils_1.object)({
33
- ...accountStruct.schema,
34
- ...exports.InternalAccountMetadataStruct.schema,
35
- });
36
- }
37
- exports.InternalEthEoaAccountStruct = asInternalAccountStruct(types_2.EthEoaAccountStruct);
38
- exports.InternalEthErc4337AccountStruct = asInternalAccountStruct(types_2.EthErc4337AccountStruct);
39
- exports.InternalBtcP2wpkhAccountStruct = asInternalAccountStruct(types_1.BtcP2wpkhAccountStruct);
25
+ exports.InternalEthEoaAccountStruct = (0, superstruct_2.object)({
26
+ ...types_2.EthEoaAccountStruct.schema,
27
+ ...exports.InternalAccountMetadataStruct.schema,
28
+ });
29
+ exports.InternalEthErc4337AccountStruct = (0, superstruct_2.object)({
30
+ ...types_2.EthErc4337AccountStruct.schema,
31
+ ...exports.InternalAccountMetadataStruct.schema,
32
+ });
33
+ exports.InternalBtcP2wpkhAccountStruct = (0, superstruct_2.object)({
34
+ ...types_1.BtcP2wpkhAccountStruct.schema,
35
+ ...exports.InternalAccountMetadataStruct.schema,
36
+ });
40
37
  exports.InternalAccountStructs = {
41
38
  [`${api_1.EthAccountType.Eoa}`]: exports.InternalEthEoaAccountStruct,
42
39
  [`${api_1.EthAccountType.Erc4337}`]: exports.InternalEthErc4337AccountStruct,
43
40
  [`${api_1.BtcAccountType.P2wpkh}`]: exports.InternalBtcP2wpkhAccountStruct,
44
41
  };
45
- exports.InternalAccountStruct = (0, keyring_utils_1.object)({
42
+ exports.InternalAccountStruct = (0, superstruct_2.object)({
46
43
  ...api_1.KeyringAccountStruct.schema,
47
44
  ...exports.InternalAccountMetadataStruct.schema,
48
45
  });
@@ -1 +1 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A+B;IAC9D,CAAC,GAAG,oBAAc,CAAC,MAAM,EAAE,CAAC,EAAE,sCAA8B;CAC7D,CAAC;AAOW,QAAA,qBAAqB,GAAG,IAAA,sBAAM,EAAC;IAC1C,GAAG,0BAAoB,CAAC,MAAM;IAC9B,GAAG,qCAA6B,CAAC,MAAM;CACxC,CAAC,CAAC","sourcesContent":["import type { Infer, Struct } from '@metamask/superstruct';\nimport { boolean, string, number } from '@metamask/superstruct';\n\nimport { BtcAccountType, EthAccountType, KeyringAccountStruct } from '../api';\nimport { BtcP2wpkhAccountStruct } from '../btc/types';\nimport { EthEoaAccountStruct, EthErc4337AccountStruct } from '../eth/types';\nimport { exactOptional, object } from '@metamask/keyring-utils';\n\nexport type InternalAccountType = EthAccountType | BtcAccountType;\n\nexport const InternalAccountMetadataStruct = object({\n metadata: object({\n name: string(),\n nameLastUpdatedAt: exactOptional(number()),\n snap: exactOptional(\n object({\n id: string(),\n enabled: boolean(),\n name: string(),\n }),\n ),\n lastSelected: exactOptional(number()),\n importTime: number(),\n keyring: object({\n type: string(),\n }),\n }),\n});\n\n/**\n * Creates an `InternalAccount` from an existing account `superstruct` object.\n *\n * @param accountStruct - An account `superstruct` object.\n * @returns The `InternalAccount` associated to `accountStruct`.\n */\nfunction asInternalAccountStruct<Account, AccountSchema>(\n accountStruct: Struct<Account, AccountSchema>,\n) {\n return object({\n ...accountStruct.schema,\n ...InternalAccountMetadataStruct.schema,\n });\n}\n\nexport const InternalEthEoaAccountStruct =\n asInternalAccountStruct(EthEoaAccountStruct);\n\nexport const InternalEthErc4337AccountStruct = asInternalAccountStruct(\n EthErc4337AccountStruct,\n);\n\nexport const InternalBtcP2wpkhAccountStruct = asInternalAccountStruct(\n BtcP2wpkhAccountStruct,\n);\n\nexport type InternalEthEoaAccount = Infer<typeof InternalEthEoaAccountStruct>;\n\nexport type InternalEthErc4337Account = Infer<\n typeof InternalEthErc4337AccountStruct\n>;\n\nexport type InternalBtcP2wpkhAccount = Infer<\n typeof InternalBtcP2wpkhAccountStruct\n>;\n\nexport const InternalAccountStructs: Record<\n string,\n | Struct<InternalEthEoaAccount>\n | Struct<InternalEthErc4337Account>\n | Struct<InternalBtcP2wpkhAccount>\n> = {\n [`${EthAccountType.Eoa}`]: InternalEthEoaAccountStruct,\n [`${EthAccountType.Erc4337}`]: InternalEthErc4337AccountStruct,\n [`${BtcAccountType.P2wpkh}`]: InternalBtcP2wpkhAccountStruct,\n};\n\nexport type InternalAccountTypes =\n | InternalEthEoaAccount\n | InternalEthErc4337Account\n | InternalBtcP2wpkhAccount;\n\nexport const InternalAccountStruct = object({\n ...KeyringAccountStruct.schema,\n ...InternalAccountMetadataStruct.schema,\n});\n\n/**\n * Internal account representation.\n *\n * This type is used internally by MetaMask to add additional metadata to the\n * account object. It's should not be used by external applications.\n */\nexport type InternalAccount = Infer<typeof InternalAccountStruct>;\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/internal/types.ts"],"names":[],"mappings":";;;AACA,uDAAgE;AAEhE,gCAA8E;AAC9E,wCAAsD;AACtD,wCAA4E;AAC5E,gDAAuD;AAI1C,QAAA,6BAA6B,GAAG,IAAA,oBAAM,EAAC;IAClD,QAAQ,EAAE,IAAA,oBAAM,EAAC;QACf,IAAI,EAAE,IAAA,oBAAM,GAAE;QACd,iBAAiB,EAAE,IAAA,2BAAa,EAAC,IAAA,oBAAM,GAAE,CAAC;QAC1C,IAAI,EAAE,IAAA,2BAAa,EACjB,IAAA,oBAAM,EAAC;YACL,EAAE,EAAE,IAAA,oBAAM,GAAE;YACZ,OAAO,EAAE,IAAA,qBAAO,GAAE;YAClB,IAAI,EAAE,IAAA,oBAAM,GAAE;SACf,CAAC,CACH;QACD,YAAY,EAAE,IAAA,2BAAa,EAAC,IAAA,oBAAM,GAAE,CAAC;QACrC,UAAU,EAAE,IAAA,oBAAM,GAAE;QACpB,OAAO,EAAE,IAAA,oBAAM,EAAC;YACd,IAAI,EAAE,IAAA,oBAAM,GAAE;SACf,CAAC;KACH,CAAC;CACH,CAAC,CAAC;AAEU,QAAA,2BAA2B,GAAG,IAAA,oBAAM,EAAC;IAChD,GAAG,2BAAmB,CAAC,MAAM;IAC7B,GAAG,qCAA6B,CAAC,MAAM;CACxC,CAAC,CAAC;AAEU,QAAA,+BAA+B,GAAG,IAAA,oBAAM,EAAC;IACpD,GAAG,+BAAuB,CAAC,MAAM;IACjC,GAAG,qCAA6B,CAAC,MAAM;CACxC,CAAC,CAAC;AAEU,QAAA,8BAA8B,GAAG,IAAA,oBAAM,EAAC;IACnD,GAAG,8BAAsB,CAAC,MAAM;IAChC,GAAG,qCAA6B,CAAC,MAAM;CACxC,CAAC,CAAC;AAYU,QAAA,sBAAsB,GAK/B;IACF,CAAC,GAAG,oBAAc,CAAC,GAAG,EAAE,CAAC,EAAE,mCAA2B;IACtD,CAAC,GAAG,oBAAc,CAAC,OAAO,EAAE,CAAC,EAAE,uCAA+B;IAC9D,CAAC,GAAG,oBAAc,CAAC,MAAM,EAAE,CAAC,EAAE,sCAA8B;CAC7D,CAAC;AAOW,QAAA,qBAAqB,GAAG,IAAA,oBAAM,EAAC;IAC1C,GAAG,0BAAoB,CAAC,MAAM;IAC9B,GAAG,qCAA6B,CAAC,MAAM;CACxC,CAAC,CAAC","sourcesContent":["import type { Infer, Struct } from '@metamask/superstruct';\nimport { boolean, string, number } from '@metamask/superstruct';\n\nimport { BtcAccountType, EthAccountType, KeyringAccountStruct } from '../api';\nimport { BtcP2wpkhAccountStruct } from '../btc/types';\nimport { EthEoaAccountStruct, EthErc4337AccountStruct } from '../eth/types';\nimport { exactOptional, object } from '../superstruct';\n\nexport type InternalAccountType = EthAccountType | BtcAccountType;\n\nexport const InternalAccountMetadataStruct = object({\n metadata: object({\n name: string(),\n nameLastUpdatedAt: exactOptional(number()),\n snap: exactOptional(\n object({\n id: string(),\n enabled: boolean(),\n name: string(),\n }),\n ),\n lastSelected: exactOptional(number()),\n importTime: number(),\n keyring: object({\n type: string(),\n }),\n }),\n});\n\nexport const InternalEthEoaAccountStruct = object({\n ...EthEoaAccountStruct.schema,\n ...InternalAccountMetadataStruct.schema,\n});\n\nexport const InternalEthErc4337AccountStruct = object({\n ...EthErc4337AccountStruct.schema,\n ...InternalAccountMetadataStruct.schema,\n});\n\nexport const InternalBtcP2wpkhAccountStruct = object({\n ...BtcP2wpkhAccountStruct.schema,\n ...InternalAccountMetadataStruct.schema,\n});\n\nexport type InternalEthEoaAccount = Infer<typeof InternalEthEoaAccountStruct>;\n\nexport type InternalEthErc4337Account = Infer<\n typeof InternalEthErc4337AccountStruct\n>;\n\nexport type InternalBtcP2wpkhAccount = Infer<\n typeof InternalBtcP2wpkhAccountStruct\n>;\n\nexport const InternalAccountStructs: Record<\n string,\n | Struct<InternalEthEoaAccount>\n | Struct<InternalEthErc4337Account>\n | Struct<InternalBtcP2wpkhAccount>\n> = {\n [`${EthAccountType.Eoa}`]: InternalEthEoaAccountStruct,\n [`${EthAccountType.Erc4337}`]: InternalEthErc4337AccountStruct,\n [`${BtcAccountType.P2wpkh}`]: InternalBtcP2wpkhAccountStruct,\n};\n\nexport type InternalAccountTypes =\n | InternalEthEoaAccount\n | InternalEthErc4337Account\n | InternalBtcP2wpkhAccount;\n\nexport const InternalAccountStruct = object({\n ...KeyringAccountStruct.schema,\n ...InternalAccountMetadataStruct.schema,\n});\n\n/**\n * Internal account representation.\n *\n * This type is used internally by MetaMask to add additional metadata to the\n * account object. It's should not be used by external applications.\n */\nexport type InternalAccount = Infer<typeof InternalAccountStruct>;\n"]}
@@ -27,70 +27,70 @@ async function dispatchRequest(keyring, request) {
27
27
  // we can check its method name.
28
28
  (0, superstruct_1.assert)(request, JsonRpcRequest_1.JsonRpcRequestStruct);
29
29
  switch (request.method) {
30
- case rpc_1.KeyringRpcMethod.ListAccounts: {
30
+ case `${rpc_1.KeyringRpcMethod.ListAccounts}`: {
31
31
  (0, superstruct_1.assert)(request, api_1.ListAccountsRequestStruct);
32
32
  return keyring.listAccounts();
33
33
  }
34
- case rpc_1.KeyringRpcMethod.GetAccount: {
34
+ case `${rpc_1.KeyringRpcMethod.GetAccount}`: {
35
35
  (0, superstruct_1.assert)(request, api_1.GetAccountRequestStruct);
36
36
  return keyring.getAccount(request.params.id);
37
37
  }
38
- case rpc_1.KeyringRpcMethod.CreateAccount: {
38
+ case `${rpc_1.KeyringRpcMethod.CreateAccount}`: {
39
39
  (0, superstruct_1.assert)(request, api_1.CreateAccountRequestStruct);
40
40
  return keyring.createAccount(request.params.options);
41
41
  }
42
- case rpc_1.KeyringRpcMethod.GetAccountBalances: {
42
+ case `${rpc_1.KeyringRpcMethod.GetAccountBalances}`: {
43
43
  if (keyring.getAccountBalances === undefined) {
44
44
  throw new MethodNotSupportedError(request.method);
45
45
  }
46
46
  (0, superstruct_1.assert)(request, api_1.GetAccountBalancesRequestStruct);
47
47
  return keyring.getAccountBalances(request.params.id, request.params.assets);
48
48
  }
49
- case rpc_1.KeyringRpcMethod.FilterAccountChains: {
49
+ case `${rpc_1.KeyringRpcMethod.FilterAccountChains}`: {
50
50
  (0, superstruct_1.assert)(request, api_1.FilterAccountChainsStruct);
51
51
  return keyring.filterAccountChains(request.params.id, request.params.chains);
52
52
  }
53
- case rpc_1.KeyringRpcMethod.UpdateAccount: {
53
+ case `${rpc_1.KeyringRpcMethod.UpdateAccount}`: {
54
54
  (0, superstruct_1.assert)(request, api_1.UpdateAccountRequestStruct);
55
55
  return keyring.updateAccount(request.params.account);
56
56
  }
57
- case rpc_1.KeyringRpcMethod.DeleteAccount: {
57
+ case `${rpc_1.KeyringRpcMethod.DeleteAccount}`: {
58
58
  (0, superstruct_1.assert)(request, api_1.DeleteAccountRequestStruct);
59
59
  return keyring.deleteAccount(request.params.id);
60
60
  }
61
- case rpc_1.KeyringRpcMethod.ExportAccount: {
61
+ case `${rpc_1.KeyringRpcMethod.ExportAccount}`: {
62
62
  if (keyring.exportAccount === undefined) {
63
63
  throw new MethodNotSupportedError(request.method);
64
64
  }
65
65
  (0, superstruct_1.assert)(request, api_1.ExportAccountRequestStruct);
66
66
  return keyring.exportAccount(request.params.id);
67
67
  }
68
- case rpc_1.KeyringRpcMethod.ListRequests: {
68
+ case `${rpc_1.KeyringRpcMethod.ListRequests}`: {
69
69
  if (keyring.listRequests === undefined) {
70
70
  throw new MethodNotSupportedError(request.method);
71
71
  }
72
72
  (0, superstruct_1.assert)(request, api_1.ListRequestsRequestStruct);
73
73
  return keyring.listRequests();
74
74
  }
75
- case rpc_1.KeyringRpcMethod.GetRequest: {
75
+ case `${rpc_1.KeyringRpcMethod.GetRequest}`: {
76
76
  if (keyring.getRequest === undefined) {
77
77
  throw new MethodNotSupportedError(request.method);
78
78
  }
79
79
  (0, superstruct_1.assert)(request, api_1.GetRequestRequestStruct);
80
80
  return keyring.getRequest(request.params.id);
81
81
  }
82
- case rpc_1.KeyringRpcMethod.SubmitRequest: {
82
+ case `${rpc_1.KeyringRpcMethod.SubmitRequest}`: {
83
83
  (0, superstruct_1.assert)(request, api_1.SubmitRequestRequestStruct);
84
84
  return keyring.submitRequest(request.params);
85
85
  }
86
- case rpc_1.KeyringRpcMethod.ApproveRequest: {
86
+ case `${rpc_1.KeyringRpcMethod.ApproveRequest}`: {
87
87
  if (keyring.approveRequest === undefined) {
88
88
  throw new MethodNotSupportedError(request.method);
89
89
  }
90
90
  (0, superstruct_1.assert)(request, api_1.ApproveRequestRequestStruct);
91
91
  return keyring.approveRequest(request.params.id, request.params.data);
92
92
  }
93
- case rpc_1.KeyringRpcMethod.RejectRequest: {
93
+ case `${rpc_1.KeyringRpcMethod.RejectRequest}`: {
94
94
  if (keyring.rejectRequest === undefined) {
95
95
  throw new MethodNotSupportedError(request.method);
96
96
  }
@@ -1 +1 @@
1
- {"version":3,"file":"rpc-handler.js","sourceRoot":"","sources":["../src/rpc-handler.ts"],"names":[],"mappings":";;;AAAA,uDAA+C;AAI/C,wCAcwB;AACxB,wCAAkD;AAElD,qDAAwD;AAExD;;GAEG;AACH,MAAa,uBAAwB,SAAQ,KA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OAAO,EAAE,OAAO,CAAC,CAAC;KAChD;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,OAAO,GACX,KAAK,YAAY,KAAK,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;YACzD,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,8DAA8D,CAAC;QAErE,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;KAC1B;AACH,CAAC;AAdD,oDAcC","sourcesContent":["import { assert } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\n\nimport type { Keyring } from './api';\nimport {\n GetAccountRequestStruct,\n CreateAccountRequestStruct,\n ApproveRequestRequestStruct,\n DeleteAccountRequestStruct,\n ExportAccountRequestStruct,\n GetRequestRequestStruct,\n RejectRequestRequestStruct,\n SubmitRequestRequestStruct,\n UpdateAccountRequestStruct,\n FilterAccountChainsStruct,\n ListAccountsRequestStruct,\n ListRequestsRequestStruct,\n GetAccountBalancesRequestStruct,\n} from './internal/api';\nimport { KeyringRpcMethod } from './internal/rpc';\nimport type { JsonRpcRequest } from './JsonRpcRequest';\nimport { JsonRpcRequestStruct } from './JsonRpcRequest';\n\n/**\n * Error thrown when a keyring JSON-RPC method is not supported.\n */\nexport class MethodNotSupportedError extends Error {\n constructor(method: string) {\n super(`Method not supported: ${method}`);\n }\n}\n\n/**\n * Inner function that dispatches JSON-RPC request to the associated Keyring\n * methods.\n *\n * @param keyring - Keyring instance.\n * @param request - Keyring JSON-RPC request.\n * @returns A promise that resolves to the keyring response.\n */\nasync function dispatchRequest(\n keyring: Keyring,\n request: JsonRpcRequest,\n): Promise<Json | void> {\n // We first have to make sure that the request is a valid JSON-RPC request so\n // we can check its method name.\n assert(request, JsonRpcRequestStruct);\n\n switch (request.method) {\n case KeyringRpcMethod.ListAccounts: {\n assert(request, ListAccountsRequestStruct);\n return keyring.listAccounts();\n }\n\n case KeyringRpcMethod.GetAccount: {\n assert(request, GetAccountRequestStruct);\n return keyring.getAccount(request.params.id);\n }\n\n case KeyringRpcMethod.CreateAccount: {\n assert(request, CreateAccountRequestStruct);\n return keyring.createAccount(request.params.options);\n }\n\n case KeyringRpcMethod.GetAccountBalances: {\n if (keyring.getAccountBalances === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, GetAccountBalancesRequestStruct);\n return keyring.getAccountBalances(\n request.params.id,\n request.params.assets,\n );\n }\n\n case KeyringRpcMethod.FilterAccountChains: {\n assert(request, FilterAccountChainsStruct);\n return keyring.filterAccountChains(\n request.params.id,\n request.params.chains,\n );\n }\n\n case KeyringRpcMethod.UpdateAccount: {\n assert(request, UpdateAccountRequestStruct);\n return keyring.updateAccount(request.params.account);\n }\n\n case KeyringRpcMethod.DeleteAccount: {\n assert(request, DeleteAccountRequestStruct);\n return keyring.deleteAccount(request.params.id);\n }\n\n case KeyringRpcMethod.ExportAccount: {\n if (keyring.exportAccount === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ExportAccountRequestStruct);\n return keyring.exportAccount(request.params.id);\n }\n\n case KeyringRpcMethod.ListRequests: {\n if (keyring.listRequests === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ListRequestsRequestStruct);\n return keyring.listRequests();\n }\n\n case KeyringRpcMethod.GetRequest: {\n if (keyring.getRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, GetRequestRequestStruct);\n return keyring.getRequest(request.params.id);\n }\n\n case KeyringRpcMethod.SubmitRequest: {\n assert(request, SubmitRequestRequestStruct);\n return keyring.submitRequest(request.params);\n }\n\n case KeyringRpcMethod.ApproveRequest: {\n if (keyring.approveRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ApproveRequestRequestStruct);\n return keyring.approveRequest(request.params.id, request.params.data);\n }\n\n case KeyringRpcMethod.RejectRequest: {\n if (keyring.rejectRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, RejectRequestRequestStruct);\n return keyring.rejectRequest(request.params.id);\n }\n\n default: {\n throw new MethodNotSupportedError(request.method);\n }\n }\n}\n\n/**\n * Handles a keyring JSON-RPC request.\n *\n * This function is meant to be used as a handler for Keyring JSON-RPC requests\n * in an Accounts Snap.\n *\n * @param keyring - Keyring instance.\n * @param request - Keyring JSON-RPC request.\n * @returns A promise that resolves to the keyring response.\n * @example\n * ```ts\n * export const onKeyringRequest: OnKeyringRequestHandler = async ({\n * origin,\n * request,\n * }) => {\n * return await handleKeyringRequest(keyring, request);\n * };\n * ```\n */\nexport async function handleKeyringRequest(\n keyring: Keyring,\n request: JsonRpcRequest,\n): Promise<Json | void> {\n try {\n return await dispatchRequest(keyring, request);\n } catch (error) {\n const message =\n error instanceof Error && typeof error.message === 'string'\n ? error.message\n : 'An unknown error occurred while handling the keyring request';\n\n throw new Error(message);\n }\n}\n"]}
1
+ {"version":3,"file":"rpc-handler.js","sourceRoot":"","sources":["../src/rpc-handler.ts"],"names":[],"mappings":";;;AAAA,uDAA+C;AAI/C,wCAcwB;AACxB,wCAAkD;AAElD,qDAAwD;AAExD;;GAEG;AACH,MAAa,uBAAwB,SAAQ,KAAK;IAChD,YAAY,MAAc;QACxB,KAAK,CAAC,yBAAyB,MAAM,EAAE,CAAC,CAAC;IAC3C,CAAC;CACF;AAJD,0DAIC;AAED;;;;;;;GAOG;AACH,KAAK,UAAU,eAAe,CAC5B,OAAgB,EAChB,OAAuB;IAEvB,6EAA6E;IAC7E,gCAAgC;IAChC,IAAA,oBAAM,EAAC,OAAO,EAAE,qCAAoB,CAAC,CAAC;IAEtC,QAAQ,OAAO,CAAC,MAAM,EAAE,CAAC;QACvB,KAAK,GAAG,sBAAgB,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC;YACxC,IAAA,oBAAM,EAAC,OAAO,EAAE,+BAAyB,CAAC,CAAC;YAC3C,OAAO,OAAO,CAAC,YAAY,EAAE,CAAC;QAChC,CAAC;QAED,KAAK,GAAG,sBAAgB,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;Y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AAK,CAAC,OAAO,KAAK,QAAQ;YACzD,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,8DAA8D,CAAC;QAErE,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;AACH,CAAC;AAdD,oDAcC","sourcesContent":["import { assert } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\n\nimport type { Keyring } from './api';\nimport {\n GetAccountRequestStruct,\n CreateAccountRequestStruct,\n ApproveRequestRequestStruct,\n DeleteAccountRequestStruct,\n ExportAccountRequestStruct,\n GetRequestRequestStruct,\n RejectRequestRequestStruct,\n SubmitRequestRequestStruct,\n UpdateAccountRequestStruct,\n FilterAccountChainsStruct,\n ListAccountsRequestStruct,\n ListRequestsRequestStruct,\n GetAccountBalancesRequestStruct,\n} from './internal/api';\nimport { KeyringRpcMethod } from './internal/rpc';\nimport type { JsonRpcRequest } from './JsonRpcRequest';\nimport { JsonRpcRequestStruct } from './JsonRpcRequest';\n\n/**\n * Error thrown when a keyring JSON-RPC method is not supported.\n */\nexport class MethodNotSupportedError extends Error {\n constructor(method: string) {\n super(`Method not supported: ${method}`);\n }\n}\n\n/**\n * Inner function that dispatches JSON-RPC request to the associated Keyring\n * methods.\n *\n * @param keyring - Keyring instance.\n * @param request - Keyring JSON-RPC request.\n * @returns A promise that resolves to the keyring response.\n */\nasync function dispatchRequest(\n keyring: Keyring,\n request: JsonRpcRequest,\n): Promise<Json | void> {\n // We first have to make sure that the request is a valid JSON-RPC request so\n // we can check its method name.\n assert(request, JsonRpcRequestStruct);\n\n switch (request.method) {\n case `${KeyringRpcMethod.ListAccounts}`: {\n assert(request, ListAccountsRequestStruct);\n return keyring.listAccounts();\n }\n\n case `${KeyringRpcMethod.GetAccount}`: {\n assert(request, GetAccountRequestStruct);\n return keyring.getAccount(request.params.id);\n }\n\n case `${KeyringRpcMethod.CreateAccount}`: {\n assert(request, CreateAccountRequestStruct);\n return keyring.createAccount(request.params.options);\n }\n\n case `${KeyringRpcMethod.GetAccountBalances}`: {\n if (keyring.getAccountBalances === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, GetAccountBalancesRequestStruct);\n return keyring.getAccountBalances(\n request.params.id,\n request.params.assets,\n );\n }\n\n case `${KeyringRpcMethod.FilterAccountChains}`: {\n assert(request, FilterAccountChainsStruct);\n return keyring.filterAccountChains(\n request.params.id,\n request.params.chains,\n );\n }\n\n case `${KeyringRpcMethod.UpdateAccount}`: {\n assert(request, UpdateAccountRequestStruct);\n return keyring.updateAccount(request.params.account);\n }\n\n case `${KeyringRpcMethod.DeleteAccount}`: {\n assert(request, DeleteAccountRequestStruct);\n return keyring.deleteAccount(request.params.id);\n }\n\n case `${KeyringRpcMethod.ExportAccount}`: {\n if (keyring.exportAccount === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ExportAccountRequestStruct);\n return keyring.exportAccount(request.params.id);\n }\n\n case `${KeyringRpcMethod.ListRequests}`: {\n if (keyring.listRequests === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ListRequestsRequestStruct);\n return keyring.listRequests();\n }\n\n case `${KeyringRpcMethod.GetRequest}`: {\n if (keyring.getRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, GetRequestRequestStruct);\n return keyring.getRequest(request.params.id);\n }\n\n case `${KeyringRpcMethod.SubmitRequest}`: {\n assert(request, SubmitRequestRequestStruct);\n return keyring.submitRequest(request.params);\n }\n\n case `${KeyringRpcMethod.ApproveRequest}`: {\n if (keyring.approveRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ApproveRequestRequestStruct);\n return keyring.approveRequest(request.params.id, request.params.data);\n }\n\n case `${KeyringRpcMethod.RejectRequest}`: {\n if (keyring.rejectRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, RejectRequestRequestStruct);\n return keyring.rejectRequest(request.params.id);\n }\n\n default: {\n throw new MethodNotSupportedError(request.method);\n }\n }\n}\n\n/**\n * Handles a keyring JSON-RPC request.\n *\n * This function is meant to be used as a handler for Keyring JSON-RPC requests\n * in an Accounts Snap.\n *\n * @param keyring - Keyring instance.\n * @param request - Keyring JSON-RPC request.\n * @returns A promise that resolves to the keyring response.\n * @example\n * ```ts\n * export const onKeyringRequest: OnKeyringRequestHandler = async ({\n * origin,\n * request,\n * }) => {\n * return await handleKeyringRequest(keyring, request);\n * };\n * ```\n */\nexport async function handleKeyringRequest(\n keyring: Keyring,\n request: JsonRpcRequest,\n): Promise<Json | void> {\n try {\n return await dispatchRequest(keyring, request);\n } catch (error) {\n const message =\n error instanceof Error && typeof error.message === 'string'\n ? error.message\n : 'An unknown error occurred while handling the keyring request';\n\n throw new Error(message);\n }\n}\n"]}
@@ -0,0 +1,83 @@
1
+ import { Struct } from '@metamask/superstruct';
2
+ import type { Infer, ObjectSchema, OmitBy, Optionalize, PickBy, Simplify } from '@metamask/superstruct';
3
+ declare const ExactOptionalSymbol: unique symbol;
4
+ export type ExactOptionalTag = {
5
+ type: typeof ExactOptionalSymbol;
6
+ };
7
+ /**
8
+ * Exclude type `Type` from the properties of `Obj`.
9
+ *
10
+ * ```ts
11
+ * type Foo = { a: string | null; b: number };
12
+ * type Bar = ExcludeType<Foo, null>;
13
+ * // Bar = { a: string, b: number }
14
+ * ```
15
+ */
16
+ export type ExcludeType<Obj, Type> = {
17
+ [K in keyof Obj]: Exclude<Obj[K], Type>;
18
+ };
19
+ /**
20
+ * Make optional all properties that have the `ExactOptionalTag` type.
21
+ *
22
+ * ```ts
23
+ * type Foo = { a: string | ExactOptionalTag; b: number};
24
+ * type Bar = ExactOptionalize<Foo>;
25
+ * // Bar = { a?: string; b: number}
26
+ * ```
27
+ */
28
+ export type ExactOptionalize<Schema extends object> = OmitBy<Schema, ExactOptionalTag> & Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;
29
+ /**
30
+ * Infer a type from an superstruct object schema.
31
+ */
32
+ export type ObjectType<Schema extends ObjectSchema> = Simplify<ExactOptionalize<Optionalize<{
33
+ [K in keyof Schema]: Infer<Schema[K]>;
34
+ }>>>;
35
+ /**
36
+ * Change the return type of a superstruct object struct to support exact
37
+ * optional properties.
38
+ *
39
+ * @param schema - The object schema.
40
+ * @returns A struct representing an object with a known set of properties.
41
+ */
42
+ export declare function object<Schema extends ObjectSchema>(schema: Schema): Struct<ObjectType<Schema>, Schema>;
43
+ /**
44
+ * Augment a struct to allow exact-optional values. Exact-optional values can
45
+ * be omitted but cannot be `undefined`.
46
+ *
47
+ * ```ts
48
+ * const foo = object({ bar: exactOptional(string()) });
49
+ * type Foo = Infer<typeof foo>;
50
+ * // Foo = { bar?: string }
51
+ * ```
52
+ *
53
+ * @param struct - The struct to augment.
54
+ * @returns The augmented struct.
55
+ */
56
+ export declare function exactOptional<Type, Schema>(struct: Struct<Type, Schema>): Struct<Type | ExactOptionalTag, Schema>;
57
+ /**
58
+ * Defines a new string-struct matching a regular expression.
59
+ *
60
+ * Example:
61
+ *
62
+ * ```ts
63
+ * const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
64
+ * ```
65
+ *
66
+ * @param name - Type name.
67
+ * @param pattern - Regular expression to match.
68
+ * @returns A new string-struct that matches the given pattern.
69
+ */
70
+ export declare function definePattern(name: string, pattern: RegExp): Struct<string, null>;
71
+ /**
72
+ * Assert that a value is valid according to a struct.
73
+ *
74
+ * It is similar to superstruct's mask function, but it does not ignore extra
75
+ * properties.
76
+ *
77
+ * @param value - Value to check.
78
+ * @param struct - Struct to validate the value against.
79
+ * @param message - Error message to throw if the value is not valid.
80
+ * @returns The value if it is valid.
81
+ */
82
+ export declare function strictMask<Type, Schema>(value: unknown, struct: Struct<Type, Schema>, message?: string): Type;
83
+ export {};
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.strictMask = exports.definePattern = exports.exactOptional = exports.object = void 0;
4
+ const superstruct_1 = require("@metamask/superstruct");
5
+ /**
6
+ * Change the return type of a superstruct object struct to support exact
7
+ * optional properties.
8
+ *
9
+ * @param schema - The object schema.
10
+ * @returns A struct representing an object with a known set of properties.
11
+ */
12
+ function object(schema) {
13
+ return (0, superstruct_1.object)(schema);
14
+ }
15
+ exports.object = object;
16
+ /**
17
+ * Check if the current property is present in its parent object.
18
+ *
19
+ * @param ctx - The context to check.
20
+ * @returns `true` if the property is present, `false` otherwise.
21
+ */
22
+ function hasOptional(ctx) {
23
+ const property = ctx.path[ctx.path.length - 1];
24
+ const parent = ctx.branch[ctx.branch.length - 2];
25
+ return property in parent;
26
+ }
27
+ /**
28
+ * Augment a struct to allow exact-optional values. Exact-optional values can
29
+ * be omitted but cannot be `undefined`.
30
+ *
31
+ * ```ts
32
+ * const foo = object({ bar: exactOptional(string()) });
33
+ * type Foo = Infer<typeof foo>;
34
+ * // Foo = { bar?: string }
35
+ * ```
36
+ *
37
+ * @param struct - The struct to augment.
38
+ * @returns The augmented struct.
39
+ */
40
+ function exactOptional(struct) {
41
+ return new superstruct_1.Struct({
42
+ ...struct,
43
+ validator: (value, ctx) => !hasOptional(ctx) || struct.validator(value, ctx),
44
+ refiner: (value, ctx) => !hasOptional(ctx) || struct.refiner(value, ctx),
45
+ });
46
+ }
47
+ exports.exactOptional = exactOptional;
48
+ /**
49
+ * Defines a new string-struct matching a regular expression.
50
+ *
51
+ * Example:
52
+ *
53
+ * ```ts
54
+ * const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
55
+ * ```
56
+ *
57
+ * @param name - Type name.
58
+ * @param pattern - Regular expression to match.
59
+ * @returns A new string-struct that matches the given pattern.
60
+ */
61
+ function definePattern(name, pattern) {
62
+ return (0, superstruct_1.define)(name, (value) => typeof value === 'string' && pattern.test(value));
63
+ }
64
+ exports.definePattern = definePattern;
65
+ /**
66
+ * Assert that a value is valid according to a struct.
67
+ *
68
+ * It is similar to superstruct's mask function, but it does not ignore extra
69
+ * properties.
70
+ *
71
+ * @param value - Value to check.
72
+ * @param struct - Struct to validate the value against.
73
+ * @param message - Error message to throw if the value is not valid.
74
+ * @returns The value if it is valid.
75
+ */
76
+ function strictMask(value, struct, message) {
77
+ (0, superstruct_1.assert)(value, struct, message);
78
+ return value;
79
+ }
80
+ exports.strictMask = strictMask;
81
+ //# sourceMappingURL=superstruct.js.map