@metamask/keyring-api 21.3.0 → 21.5.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 (163) hide show
  1. package/CHANGELOG.md +49 -2
  2. package/README.md +4 -0
  3. package/dist/api/account-options.cjs +44 -7
  4. package/dist/api/account-options.cjs.map +1 -1
  5. package/dist/api/account-options.d.cts +41 -3
  6. package/dist/api/account-options.d.cts.map +1 -1
  7. package/dist/api/account-options.d.mts +41 -3
  8. package/dist/api/account-options.d.mts.map +1 -1
  9. package/dist/api/account-options.mjs +43 -6
  10. package/dist/api/account-options.mjs.map +1 -1
  11. package/dist/api/account.d.cts +4 -0
  12. package/dist/api/account.d.cts.map +1 -1
  13. package/dist/api/account.d.mts +4 -0
  14. package/dist/api/account.d.mts.map +1 -1
  15. package/dist/api/keyring.cjs +0 -2
  16. package/dist/api/keyring.cjs.map +1 -1
  17. package/dist/api/keyring.d.cts +13 -0
  18. package/dist/api/keyring.d.cts.map +1 -1
  19. package/dist/api/keyring.d.mts +13 -0
  20. package/dist/api/keyring.d.mts.map +1 -1
  21. package/dist/api/keyring.mjs +0 -2
  22. package/dist/api/keyring.mjs.map +1 -1
  23. package/dist/api/v2/create-account/bip44.cjs +27 -1
  24. package/dist/api/v2/create-account/bip44.cjs.map +1 -1
  25. package/dist/api/v2/create-account/bip44.d.cts +46 -0
  26. package/dist/api/v2/create-account/bip44.d.cts.map +1 -1
  27. package/dist/api/v2/create-account/bip44.d.mts +46 -0
  28. package/dist/api/v2/create-account/bip44.d.mts.map +1 -1
  29. package/dist/api/v2/create-account/bip44.mjs +26 -0
  30. package/dist/api/v2/create-account/bip44.mjs.map +1 -1
  31. package/dist/api/v2/create-account/custom.cjs +14 -0
  32. package/dist/api/v2/create-account/custom.cjs.map +1 -0
  33. package/dist/api/v2/create-account/custom.d.cts +25 -0
  34. package/dist/api/v2/create-account/custom.d.cts.map +1 -0
  35. package/dist/api/v2/create-account/custom.d.mts +25 -0
  36. package/dist/api/v2/create-account/custom.d.mts.map +1 -0
  37. package/dist/api/v2/create-account/custom.mjs +11 -0
  38. package/dist/api/v2/create-account/custom.mjs.map +1 -0
  39. package/dist/api/v2/create-account/index.cjs +55 -0
  40. package/dist/api/v2/create-account/index.cjs.map +1 -1
  41. package/dist/api/v2/create-account/index.d.cts +55 -1
  42. package/dist/api/v2/create-account/index.d.cts.map +1 -1
  43. package/dist/api/v2/create-account/index.d.mts +55 -1
  44. package/dist/api/v2/create-account/index.d.mts.map +1 -1
  45. package/dist/api/v2/create-account/index.mjs +55 -1
  46. package/dist/api/v2/create-account/index.mjs.map +1 -1
  47. package/dist/api/v2/index.cjs +1 -0
  48. package/dist/api/v2/index.cjs.map +1 -1
  49. package/dist/api/v2/index.d.cts +1 -0
  50. package/dist/api/v2/index.d.cts.map +1 -1
  51. package/dist/api/v2/index.d.mts +1 -0
  52. package/dist/api/v2/index.d.mts.map +1 -1
  53. package/dist/api/v2/index.mjs +1 -0
  54. package/dist/api/v2/index.mjs.map +1 -1
  55. package/dist/api/v2/keyring-capabilities.cjs +17 -3
  56. package/dist/api/v2/keyring-capabilities.cjs.map +1 -1
  57. package/dist/api/v2/keyring-capabilities.d.cts +31 -9
  58. package/dist/api/v2/keyring-capabilities.d.cts.map +1 -1
  59. package/dist/api/v2/keyring-capabilities.d.mts +31 -9
  60. package/dist/api/v2/keyring-capabilities.d.mts.map +1 -1
  61. package/dist/api/v2/keyring-capabilities.mjs +18 -4
  62. package/dist/api/v2/keyring-capabilities.mjs.map +1 -1
  63. package/dist/api/v2/wrapper/index.cjs +19 -0
  64. package/dist/api/v2/wrapper/index.cjs.map +1 -0
  65. package/dist/api/v2/wrapper/index.d.cts +3 -0
  66. package/dist/api/v2/wrapper/index.d.cts.map +1 -0
  67. package/dist/api/v2/wrapper/index.d.mts +3 -0
  68. package/dist/api/v2/wrapper/index.d.mts.map +1 -0
  69. package/dist/api/v2/wrapper/index.mjs +3 -0
  70. package/dist/api/v2/wrapper/index.mjs.map +1 -0
  71. package/dist/api/v2/wrapper/keyring-account-registry.cjs +135 -0
  72. package/dist/api/v2/wrapper/keyring-account-registry.cjs.map +1 -0
  73. package/dist/api/v2/wrapper/keyring-account-registry.d.cts +88 -0
  74. package/dist/api/v2/wrapper/keyring-account-registry.d.cts.map +1 -0
  75. package/dist/api/v2/wrapper/keyring-account-registry.d.mts +88 -0
  76. package/dist/api/v2/wrapper/keyring-account-registry.d.mts.map +1 -0
  77. package/dist/api/v2/wrapper/keyring-account-registry.mjs +131 -0
  78. package/dist/api/v2/wrapper/keyring-account-registry.mjs.map +1 -0
  79. package/dist/api/v2/wrapper/keyring-wrapper.cjs +134 -0
  80. package/dist/api/v2/wrapper/keyring-wrapper.cjs.map +1 -0
  81. package/dist/api/v2/wrapper/keyring-wrapper.d.cts +154 -0
  82. package/dist/api/v2/wrapper/keyring-wrapper.d.cts.map +1 -0
  83. package/dist/api/v2/wrapper/keyring-wrapper.d.mts +154 -0
  84. package/dist/api/v2/wrapper/keyring-wrapper.d.mts.map +1 -0
  85. package/dist/api/v2/wrapper/keyring-wrapper.mjs +130 -0
  86. package/dist/api/v2/wrapper/keyring-wrapper.mjs.map +1 -0
  87. package/dist/btc/types.d.cts +16 -0
  88. package/dist/btc/types.d.cts.map +1 -1
  89. package/dist/btc/types.d.mts +16 -0
  90. package/dist/btc/types.d.mts.map +1 -1
  91. package/dist/eth/index.cjs +2 -0
  92. package/dist/eth/index.cjs.map +1 -1
  93. package/dist/eth/index.d.cts +2 -0
  94. package/dist/eth/index.d.cts.map +1 -1
  95. package/dist/eth/index.d.mts +2 -0
  96. package/dist/eth/index.d.mts.map +1 -1
  97. package/dist/eth/index.mjs +2 -0
  98. package/dist/eth/index.mjs.map +1 -1
  99. package/dist/eth/rpc/index.cjs +18 -0
  100. package/dist/eth/rpc/index.cjs.map +1 -0
  101. package/dist/eth/rpc/index.d.cts +2 -0
  102. package/dist/eth/rpc/index.d.cts.map +1 -0
  103. package/dist/eth/rpc/index.d.mts +2 -0
  104. package/dist/eth/rpc/index.d.mts.map +1 -0
  105. package/dist/eth/rpc/index.mjs +2 -0
  106. package/dist/eth/rpc/index.mjs.map +1 -0
  107. package/dist/eth/rpc/params.cjs +151 -0
  108. package/dist/eth/rpc/params.cjs.map +1 -0
  109. package/dist/eth/rpc/params.d.cts +215 -0
  110. package/dist/eth/rpc/params.d.cts.map +1 -0
  111. package/dist/eth/rpc/params.d.mts +215 -0
  112. package/dist/eth/rpc/params.d.mts.map +1 -0
  113. package/dist/eth/rpc/params.mjs +148 -0
  114. package/dist/eth/rpc/params.mjs.map +1 -0
  115. package/dist/eth/types.cjs +5 -2
  116. package/dist/eth/types.cjs.map +1 -1
  117. package/dist/eth/types.d.cts +9 -0
  118. package/dist/eth/types.d.cts.map +1 -1
  119. package/dist/eth/types.d.mts +9 -0
  120. package/dist/eth/types.d.mts.map +1 -1
  121. package/dist/eth/types.mjs +4 -1
  122. package/dist/eth/types.mjs.map +1 -1
  123. package/dist/eth/v2/eth-keyring-wrapper.cjs +165 -0
  124. package/dist/eth/v2/eth-keyring-wrapper.cjs.map +1 -0
  125. package/dist/eth/v2/eth-keyring-wrapper.d.cts +54 -0
  126. package/dist/eth/v2/eth-keyring-wrapper.d.cts.map +1 -0
  127. package/dist/eth/v2/eth-keyring-wrapper.d.mts +54 -0
  128. package/dist/eth/v2/eth-keyring-wrapper.d.mts.map +1 -0
  129. package/dist/eth/v2/eth-keyring-wrapper.mjs +161 -0
  130. package/dist/eth/v2/eth-keyring-wrapper.mjs.map +1 -0
  131. package/dist/eth/v2/index.cjs +18 -0
  132. package/dist/eth/v2/index.cjs.map +1 -0
  133. package/dist/eth/v2/index.d.cts +2 -0
  134. package/dist/eth/v2/index.d.cts.map +1 -0
  135. package/dist/eth/v2/index.d.mts +2 -0
  136. package/dist/eth/v2/index.d.mts.map +1 -0
  137. package/dist/eth/v2/index.mjs +2 -0
  138. package/dist/eth/v2/index.mjs.map +1 -0
  139. package/dist/events.cjs +0 -2
  140. package/dist/events.cjs.map +1 -1
  141. package/dist/events.d.cts +16 -0
  142. package/dist/events.d.cts.map +1 -1
  143. package/dist/events.d.mts +16 -0
  144. package/dist/events.d.mts.map +1 -1
  145. package/dist/events.mjs +0 -2
  146. package/dist/events.mjs.map +1 -1
  147. package/dist/rpc.cjs +27 -12
  148. package/dist/rpc.cjs.map +1 -1
  149. package/dist/rpc.d.cts +200 -12
  150. package/dist/rpc.d.cts.map +1 -1
  151. package/dist/rpc.d.mts +200 -12
  152. package/dist/rpc.d.mts.map +1 -1
  153. package/dist/rpc.mjs +27 -12
  154. package/dist/rpc.mjs.map +1 -1
  155. package/dist/sol/types.d.cts +4 -0
  156. package/dist/sol/types.d.cts.map +1 -1
  157. package/dist/sol/types.d.mts +4 -0
  158. package/dist/sol/types.d.mts.map +1 -1
  159. package/dist/trx/types.d.cts +4 -0
  160. package/dist/trx/types.d.cts.map +1 -1
  161. package/dist/trx/types.d.mts +4 -0
  162. package/dist/trx/types.d.mts.map +1 -1
  163. package/package.json +9 -4
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EthKeyringWrapper = exports.EthKeyringMethod = void 0;
4
+ const tx_1 = require("@ethereumjs/tx");
5
+ const eth_sig_util_1 = require("@metamask/eth-sig-util");
6
+ const superstruct_1 = require("@metamask/superstruct");
7
+ const utils_1 = require("@metamask/utils");
8
+ const keyring_wrapper_1 = require("../../api/v2/wrapper/keyring-wrapper.cjs");
9
+ const rpc_1 = require("../rpc/index.cjs");
10
+ const types_1 = require("../types.cjs");
11
+ /**
12
+ * Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.
13
+ * These are primarily encryption and utility methods.
14
+ */
15
+ var EthKeyringMethod;
16
+ (function (EthKeyringMethod) {
17
+ EthKeyringMethod["Decrypt"] = "eth_decrypt";
18
+ EthKeyringMethod["GetEncryptionPublicKey"] = "eth_getEncryptionPublicKey";
19
+ EthKeyringMethod["GetAppKeyAddress"] = "eth_getAppKeyAddress";
20
+ EthKeyringMethod["SignEip7702Authorization"] = "eth_signEip7702Authorization";
21
+ })(EthKeyringMethod || (exports.EthKeyringMethod = EthKeyringMethod = {}));
22
+ /**
23
+ * Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.
24
+ *
25
+ * This class provides common functionality for all Ethereum keyrings including:
26
+ * - Request handling for standard Ethereum signing methods
27
+ * - Helper methods for Hex address conversion
28
+ *
29
+ * Subclasses must implement:
30
+ * - `getAccounts()`: Return all managed accounts
31
+ * - `createAccounts()`: Create new accounts based on options
32
+ * - `deleteAccount()`: Remove an account from the keyring
33
+ * - `exportAccount()` (optional): Export private key in specified format
34
+ */
35
+ class EthKeyringWrapper extends keyring_wrapper_1.KeyringWrapper {
36
+ /**
37
+ * Helper method to safely cast a KeyringAccount address to Hex type.
38
+ * The KeyringAccount.address is typed as string, but for Ethereum accounts
39
+ * it should always be a valid Hex address.
40
+ *
41
+ * @param address - The address from a KeyringAccount.
42
+ * @returns The address as Hex type.
43
+ */
44
+ toHexAddress(address) {
45
+ return (0, utils_1.add0x)(address);
46
+ }
47
+ /**
48
+ * Handle an Ethereum signing request.
49
+ *
50
+ * Routes the request to the appropriate legacy keyring method based on
51
+ * the RPC method name.
52
+ *
53
+ * @param request - The keyring request containing method and params.
54
+ * @returns The result of the signing operation.
55
+ */
56
+ async submitRequest(request) {
57
+ const { method, params } = request.request;
58
+ const { address, methods } = await this.getAccount(request.account);
59
+ const hexAddress = this.toHexAddress(address);
60
+ // Validate account can handle the method
61
+ if (!methods.includes(method)) {
62
+ throw new Error(`Account ${request.account} cannot handle method: ${method}`);
63
+ }
64
+ switch (method) {
65
+ case `${types_1.EthMethod.SignTransaction}`: {
66
+ if (!this.inner.signTransaction) {
67
+ throw new Error('Keyring does not support signTransaction');
68
+ }
69
+ (0, superstruct_1.assert)(params, rpc_1.EthSignTransactionParamsStruct);
70
+ const [txData] = params;
71
+ // Convert validated transaction data to TypedTransaction
72
+ // TODO: Improve typing to ensure txData matches TypedTxData
73
+ const tx = tx_1.TransactionFactory.fromTxData(txData);
74
+ // Note: Bigints are not directly representable in JSON
75
+ return (await this.inner.signTransaction(hexAddress, tx)); // FIXME: Should return type be unknown?
76
+ }
77
+ case `${types_1.EthMethod.Sign}`: {
78
+ if (!this.inner.signMessage) {
79
+ throw new Error('Keyring does not support signMessage');
80
+ }
81
+ (0, superstruct_1.assert)(params, rpc_1.EthSignParamsStruct);
82
+ const [, data] = params;
83
+ return this.inner.signMessage(hexAddress, data);
84
+ }
85
+ case `${types_1.EthMethod.PersonalSign}`: {
86
+ if (!this.inner.signPersonalMessage) {
87
+ throw new Error('Keyring does not support signPersonalMessage');
88
+ }
89
+ (0, superstruct_1.assert)(params, rpc_1.EthPersonalSignParamsStruct);
90
+ const [data] = params;
91
+ return this.inner.signPersonalMessage(hexAddress, data);
92
+ }
93
+ case `${types_1.EthMethod.SignTypedDataV1}`: {
94
+ if (!this.inner.signTypedData) {
95
+ throw new Error('Keyring does not support signTypedData');
96
+ }
97
+ (0, superstruct_1.assert)(params, rpc_1.EthSignTypedDataV1ParamsStruct);
98
+ const [, data] = params;
99
+ return this.inner.signTypedData(hexAddress, data, {
100
+ version: eth_sig_util_1.SignTypedDataVersion.V1,
101
+ });
102
+ }
103
+ case `${types_1.EthMethod.SignTypedDataV3}`: {
104
+ if (!this.inner.signTypedData) {
105
+ throw new Error('Keyring does not support signTypedData');
106
+ }
107
+ (0, superstruct_1.assert)(params, rpc_1.EthSignTypedDataParamsStruct);
108
+ const [, data] = params;
109
+ return this.inner.signTypedData(hexAddress,
110
+ // TODO: Improve typing to ensure data matches MessageTypes
111
+ data, {
112
+ version: eth_sig_util_1.SignTypedDataVersion.V3,
113
+ });
114
+ }
115
+ case `${types_1.EthMethod.SignTypedDataV4}`: {
116
+ if (!this.inner.signTypedData) {
117
+ throw new Error('Keyring does not support signTypedData');
118
+ }
119
+ (0, superstruct_1.assert)(params, rpc_1.EthSignTypedDataParamsStruct);
120
+ const [, data] = params;
121
+ return this.inner.signTypedData(hexAddress,
122
+ // TODO: Improve typing to ensure data matches MessageTypes
123
+ data, {
124
+ version: eth_sig_util_1.SignTypedDataVersion.V4,
125
+ });
126
+ }
127
+ case `${EthKeyringMethod.Decrypt}`: {
128
+ if (!this.inner.decryptMessage) {
129
+ throw new Error('Keyring does not support decryptMessage');
130
+ }
131
+ (0, superstruct_1.assert)(params, rpc_1.EthDecryptParamsStruct);
132
+ const [encryptedData] = params;
133
+ return this.inner.decryptMessage(hexAddress, encryptedData);
134
+ }
135
+ case `${EthKeyringMethod.GetEncryptionPublicKey}`: {
136
+ if (!this.inner.getEncryptionPublicKey) {
137
+ throw new Error('Keyring does not support getEncryptionPublicKey');
138
+ }
139
+ (0, superstruct_1.assert)(params, rpc_1.EthGetEncryptionPublicKeyParamsStruct);
140
+ const [, options] = params;
141
+ return this.inner.getEncryptionPublicKey(hexAddress, options);
142
+ }
143
+ case `${EthKeyringMethod.GetAppKeyAddress}`: {
144
+ if (!this.inner.getAppKeyAddress) {
145
+ throw new Error('Keyring does not support getAppKeyAddress');
146
+ }
147
+ (0, superstruct_1.assert)(params, rpc_1.EthGetAppKeyAddressParamsStruct);
148
+ const [origin] = params;
149
+ return this.inner.getAppKeyAddress(hexAddress, origin);
150
+ }
151
+ case `${EthKeyringMethod.SignEip7702Authorization}`: {
152
+ if (!this.inner.signEip7702Authorization) {
153
+ throw new Error('Keyring does not support signEip7702Authorization');
154
+ }
155
+ (0, superstruct_1.assert)(params, rpc_1.EthSignEip7702AuthorizationParamsStruct);
156
+ const [authorization] = params;
157
+ return this.inner.signEip7702Authorization(hexAddress, authorization);
158
+ }
159
+ default:
160
+ throw new Error(`Unsupported method for EthKeyringWrapper: ${method}`);
161
+ }
162
+ }
163
+ }
164
+ exports.EthKeyringWrapper = EthKeyringWrapper;
165
+ //# sourceMappingURL=eth-keyring-wrapper.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-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D,CAAC;YAED,KAAK,GAAG,gBAAgB,CAAC,wBAAwB,EAAE,CAAC,CAAC,CAAC;gBACpD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,wBAAwB,EAAE,CAAC;oBACzC,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;gBACvE,CAAC;gBACD,IAAA,oBAAM,EAAC,MAAM,EAAE,6CAAuC,CAAC,CAAC;gBACxD,MAAM,CAAC,aAAa,CAAC,GAAG,MAAM,CAAC;gBAC/B,OAAO,IAAI,CAAC,KAAK,CAAC,wBAAwB,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;YACxE,CAAC;YAED;gBACE,MAAM,IAAI,KAAK,CAAC,6CAA6C,MAAM,EAAE,CAAC,CAAC;QAC3E,CAAC;IACH,CAAC;CACF;AA5JD,8CA4JC","sourcesContent":["import { TransactionFactory, type TypedTxData } from '@ethereumjs/tx';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport { SignTypedDataVersion } from '@metamask/eth-sig-util';\nimport type { EthKeyring } from '@metamask/keyring-utils';\nimport { assert } from '@metamask/superstruct';\nimport { add0x, type Hex, type Json } from '@metamask/utils';\n\nimport type { KeyringAccount } from '../../api/account';\nimport type { KeyringRequest } from '../../api/request';\nimport {\n KeyringWrapper,\n type KeyringWrapperOptions,\n} from '../../api/v2/wrapper/keyring-wrapper';\nimport {\n EthDecryptParamsStruct,\n EthGetAppKeyAddressParamsStruct,\n EthGetEncryptionPublicKeyParamsStruct,\n EthPersonalSignParamsStruct,\n EthSignEip7702AuthorizationParamsStruct,\n EthSignParamsStruct,\n EthSignTransactionParamsStruct,\n EthSignTypedDataParamsStruct,\n EthSignTypedDataV1ParamsStruct,\n} from '../rpc';\nimport { EthMethod } from '../types';\n\n/**\n * Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.\n * These are primarily encryption and utility methods.\n */\nexport enum EthKeyringMethod {\n Decrypt = 'eth_decrypt',\n GetEncryptionPublicKey = 'eth_getEncryptionPublicKey',\n GetAppKeyAddress = 'eth_getAppKeyAddress',\n SignEip7702Authorization = 'eth_signEip7702Authorization',\n}\n\n/**\n * Options for constructing an EthKeyringWrapper.\n */\nexport type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> =\n KeyringWrapperOptions<InnerKeyring>;\n\n/**\n * Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.\n *\n * This class provides common functionality for all Ethereum keyrings including:\n * - Request handling for standard Ethereum signing methods\n * - Helper methods for Hex address conversion\n *\n * Subclasses must implement:\n * - `getAccounts()`: Return all managed accounts\n * - `createAccounts()`: Create new accounts based on options\n * - `deleteAccount()`: Remove an account from the keyring\n * - `exportAccount()` (optional): Export private key in specified format\n */\nexport abstract class EthKeyringWrapper<\n InnerKeyring extends EthKeyring,\n KeyringAccountType extends KeyringAccount = KeyringAccount,\n> extends KeyringWrapper<InnerKeyring, KeyringAccountType> {\n /**\n * Helper method to safely cast a KeyringAccount address to Hex type.\n * The KeyringAccount.address is typed as string, but for Ethereum accounts\n * it should always be a valid Hex address.\n *\n * @param address - The address from a KeyringAccount.\n * @returns The address as Hex type.\n */\n protected toHexAddress(address: string): Hex {\n return add0x(address);\n }\n\n /**\n * Handle an Ethereum signing request.\n *\n * Routes the request to the appropriate legacy keyring method based on\n * the RPC method name.\n *\n * @param request - The keyring request containing method and params.\n * @returns The result of the signing operation.\n */\n async submitRequest(request: KeyringRequest): Promise<Json> {\n const { method, params } = request.request;\n\n const { address, methods } = await this.getAccount(request.account);\n const hexAddress = this.toHexAddress(address);\n\n // Validate account can handle the method\n if (!methods.includes(method)) {\n throw new Error(\n `Account ${request.account} cannot handle method: ${method}`,\n );\n }\n\n switch (method) {\n case `${EthMethod.SignTransaction}`: {\n if (!this.inner.signTransaction) {\n throw new Error('Keyring does not support signTransaction');\n }\n assert(params, EthSignTransactionParamsStruct);\n const [txData] = params;\n // Convert validated transaction data to TypedTransaction\n // TODO: Improve typing to ensure txData matches TypedTxData\n const tx = TransactionFactory.fromTxData(txData as TypedTxData);\n // Note: Bigints are not directly representable in JSON\n return (await this.inner.signTransaction(\n hexAddress,\n tx,\n )) as unknown as Json; // FIXME: Should return type be unknown?\n }\n\n case `${EthMethod.Sign}`: {\n if (!this.inner.signMessage) {\n throw new Error('Keyring does not support signMessage');\n }\n assert(params, EthSignParamsStruct);\n const [, data] = params;\n return this.inner.signMessage(hexAddress, data);\n }\n\n case `${EthMethod.PersonalSign}`: {\n if (!this.inner.signPersonalMessage) {\n throw new Error('Keyring does not support signPersonalMessage');\n }\n assert(params, EthPersonalSignParamsStruct);\n const [data] = params;\n return this.inner.signPersonalMessage(hexAddress, data as Hex);\n }\n\n case `${EthMethod.SignTypedDataV1}`: {\n if (!this.inner.signTypedData) {\n throw new Error('Keyring does not support signTypedData');\n }\n assert(params, EthSignTypedDataV1ParamsStruct);\n const [, data] = params;\n return this.inner.signTypedData(hexAddress, data, {\n version: SignTypedDataVersion.V1,\n });\n }\n\n case `${EthMethod.SignTypedDataV3}`: {\n if (!this.inner.signTypedData) {\n throw new Error('Keyring does not support signTypedData');\n }\n assert(params, EthSignTypedDataParamsStruct);\n const [, data] = params;\n return this.inner.signTypedData(\n hexAddress,\n // TODO: Improve typing to ensure data matches MessageTypes\n data as TypedMessage<MessageTypes>,\n {\n version: SignTypedDataVersion.V3,\n },\n );\n }\n\n case `${EthMethod.SignTypedDataV4}`: {\n if (!this.inner.signTypedData) {\n throw new Error('Keyring does not support signTypedData');\n }\n assert(params, EthSignTypedDataParamsStruct);\n const [, data] = params;\n return this.inner.signTypedData(\n hexAddress,\n // TODO: Improve typing to ensure data matches MessageTypes\n data as TypedMessage<MessageTypes>,\n {\n version: SignTypedDataVersion.V4,\n },\n );\n }\n\n case `${EthKeyringMethod.Decrypt}`: {\n if (!this.inner.decryptMessage) {\n throw new Error('Keyring does not support decryptMessage');\n }\n assert(params, EthDecryptParamsStruct);\n const [encryptedData] = params;\n return this.inner.decryptMessage(hexAddress, encryptedData);\n }\n\n case `${EthKeyringMethod.GetEncryptionPublicKey}`: {\n if (!this.inner.getEncryptionPublicKey) {\n throw new Error('Keyring does not support getEncryptionPublicKey');\n }\n assert(params, EthGetEncryptionPublicKeyParamsStruct);\n const [, options] = params;\n return this.inner.getEncryptionPublicKey(hexAddress, options);\n }\n\n case `${EthKeyringMethod.GetAppKeyAddress}`: {\n if (!this.inner.getAppKeyAddress) {\n throw new Error('Keyring does not support getAppKeyAddress');\n }\n assert(params, EthGetAppKeyAddressParamsStruct);\n const [origin] = params;\n return this.inner.getAppKeyAddress(hexAddress, origin);\n }\n\n case `${EthKeyringMethod.SignEip7702Authorization}`: {\n if (!this.inner.signEip7702Authorization) {\n throw new Error('Keyring does not support signEip7702Authorization');\n }\n assert(params, EthSignEip7702AuthorizationParamsStruct);\n const [authorization] = params;\n return this.inner.signEip7702Authorization(hexAddress, authorization);\n }\n\n default:\n throw new Error(`Unsupported method for EthKeyringWrapper: ${method}`);\n }\n }\n}\n"]}
@@ -0,0 +1,54 @@
1
+ import type { EthKeyring } from "@metamask/keyring-utils";
2
+ import { type Hex, type Json } from "@metamask/utils";
3
+ import type { KeyringAccount } from "../../api/account.cjs";
4
+ import type { KeyringRequest } from "../../api/request.cjs";
5
+ import { KeyringWrapper, type KeyringWrapperOptions } from "../../api/v2/wrapper/keyring-wrapper.cjs";
6
+ /**
7
+ * Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.
8
+ * These are primarily encryption and utility methods.
9
+ */
10
+ export declare enum EthKeyringMethod {
11
+ Decrypt = "eth_decrypt",
12
+ GetEncryptionPublicKey = "eth_getEncryptionPublicKey",
13
+ GetAppKeyAddress = "eth_getAppKeyAddress",
14
+ SignEip7702Authorization = "eth_signEip7702Authorization"
15
+ }
16
+ /**
17
+ * Options for constructing an EthKeyringWrapper.
18
+ */
19
+ export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = KeyringWrapperOptions<InnerKeyring>;
20
+ /**
21
+ * Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.
22
+ *
23
+ * This class provides common functionality for all Ethereum keyrings including:
24
+ * - Request handling for standard Ethereum signing methods
25
+ * - Helper methods for Hex address conversion
26
+ *
27
+ * Subclasses must implement:
28
+ * - `getAccounts()`: Return all managed accounts
29
+ * - `createAccounts()`: Create new accounts based on options
30
+ * - `deleteAccount()`: Remove an account from the keyring
31
+ * - `exportAccount()` (optional): Export private key in specified format
32
+ */
33
+ export declare abstract class EthKeyringWrapper<InnerKeyring extends EthKeyring, KeyringAccountType extends KeyringAccount = KeyringAccount> extends KeyringWrapper<InnerKeyring, KeyringAccountType> {
34
+ /**
35
+ * Helper method to safely cast a KeyringAccount address to Hex type.
36
+ * The KeyringAccount.address is typed as string, but for Ethereum accounts
37
+ * it should always be a valid Hex address.
38
+ *
39
+ * @param address - The address from a KeyringAccount.
40
+ * @returns The address as Hex type.
41
+ */
42
+ protected toHexAddress(address: string): Hex;
43
+ /**
44
+ * Handle an Ethereum signing request.
45
+ *
46
+ * Routes the request to the appropriate legacy keyring method based on
47
+ * the RPC method name.
48
+ *
49
+ * @param request - The keyring request containing method and params.
50
+ * @returns The result of the signing operation.
51
+ */
52
+ submitRequest(request: KeyringRequest): Promise<Json>;
53
+ }
54
+ //# sourceMappingURL=eth-keyring-wrapper.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-keyring-wrapper.d.cts","sourceRoot":"","sources":["../../../src/eth/v2/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAE1D,OAAO,EAAS,KAAK,GAAG,EAAE,KAAK,IAAI,EAAE,wBAAwB;AAE7D,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA0B;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA0B;AACxD,OAAO,EACL,cAAc,EACd,KAAK,qBAAqB,EAC3B,iDAA6C;AAc9C;;;GAGG;AACH,oBAAY,gBAAgB;IAC1B,OAAO,gBAAgB;IACvB,sBAAsB,+BAA+B;IACrD,gBAAgB,yBAAyB;IACzC,wBAAwB,iCAAiC;CAC1D;AAED;;GAEG;AACH,MAAM,MAAM,wBAAwB,CAAC,YAAY,SAAS,UAAU,IAClE,qBAAqB,CAAC,YAAY,CAAC,CAAC;AAEtC;;;;;;;;;;;;GAYG;AACH,8BAAsB,iBAAiB,CACrC,YAAY,SAAS,UAAU,EAC/B,kBAAkB,SAAS,cAAc,GAAG,cAAc,CAC1D,SAAQ,cAAc,CAAC,YAAY,EAAE,kBAAkB,CAAC;IACxD;;;;;;;OAOG;IACH,SAAS,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG;IAI5C;;;;;;;;OAQG;IACG,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAmI5D"}
@@ -0,0 +1,54 @@
1
+ import type { EthKeyring } from "@metamask/keyring-utils";
2
+ import { type Hex, type Json } from "@metamask/utils";
3
+ import type { KeyringAccount } from "../../api/account.mjs";
4
+ import type { KeyringRequest } from "../../api/request.mjs";
5
+ import { KeyringWrapper, type KeyringWrapperOptions } from "../../api/v2/wrapper/keyring-wrapper.mjs";
6
+ /**
7
+ * Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.
8
+ * These are primarily encryption and utility methods.
9
+ */
10
+ export declare enum EthKeyringMethod {
11
+ Decrypt = "eth_decrypt",
12
+ GetEncryptionPublicKey = "eth_getEncryptionPublicKey",
13
+ GetAppKeyAddress = "eth_getAppKeyAddress",
14
+ SignEip7702Authorization = "eth_signEip7702Authorization"
15
+ }
16
+ /**
17
+ * Options for constructing an EthKeyringWrapper.
18
+ */
19
+ export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = KeyringWrapperOptions<InnerKeyring>;
20
+ /**
21
+ * Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.
22
+ *
23
+ * This class provides common functionality for all Ethereum keyrings including:
24
+ * - Request handling for standard Ethereum signing methods
25
+ * - Helper methods for Hex address conversion
26
+ *
27
+ * Subclasses must implement:
28
+ * - `getAccounts()`: Return all managed accounts
29
+ * - `createAccounts()`: Create new accounts based on options
30
+ * - `deleteAccount()`: Remove an account from the keyring
31
+ * - `exportAccount()` (optional): Export private key in specified format
32
+ */
33
+ export declare abstract class EthKeyringWrapper<InnerKeyring extends EthKeyring, KeyringAccountType extends KeyringAccount = KeyringAccount> extends KeyringWrapper<InnerKeyring, KeyringAccountType> {
34
+ /**
35
+ * Helper method to safely cast a KeyringAccount address to Hex type.
36
+ * The KeyringAccount.address is typed as string, but for Ethereum accounts
37
+ * it should always be a valid Hex address.
38
+ *
39
+ * @param address - The address from a KeyringAccount.
40
+ * @returns The address as Hex type.
41
+ */
42
+ protected toHexAddress(address: string): Hex;
43
+ /**
44
+ * Handle an Ethereum signing request.
45
+ *
46
+ * Routes the request to the appropriate legacy keyring method based on
47
+ * the RPC method name.
48
+ *
49
+ * @param request - The keyring request containing method and params.
50
+ * @returns The result of the signing operation.
51
+ */
52
+ submitRequest(request: KeyringRequest): Promise<Json>;
53
+ }
54
+ //# sourceMappingURL=eth-keyring-wrapper.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-keyring-wrapper.d.mts","sourceRoot":"","sources":["../../../src/eth/v2/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAE1D,OAAO,EAAS,KAAK,GAAG,EAAE,KAAK,IAAI,EAAE,wBAAwB;AAE7D,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA0B;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA0B;AACxD,OAAO,EACL,cAAc,EACd,KAAK,qBAAqB,EAC3B,iDAA6C;AAc9C;;;GAGG;AACH,oBAAY,gBAAgB;IAC1B,OAAO,gBAAgB;IACvB,sBAAsB,+BAA+B;IACrD,gBAAgB,yBAAyB;IACzC,wBAAwB,iCAAiC;CAC1D;AAED;;GAEG;AACH,MAAM,MAAM,wBAAwB,CAAC,YAAY,SAAS,UAAU,IAClE,qBAAqB,CAAC,YAAY,CAAC,CAAC;AAEtC;;;;;;;;;;;;GAYG;AACH,8BAAsB,iBAAiB,CACrC,YAAY,SAAS,UAAU,EAC/B,kBAAkB,SAAS,cAAc,GAAG,cAAc,CAC1D,SAAQ,cAAc,CAAC,YAAY,EAAE,kBAAkB,CAAC;IACxD;;;;;;;OAOG;IACH,SAAS,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG;IAI5C;;;;;;;;OAQG;IACG,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAmI5D"}
@@ -0,0 +1,161 @@
1
+ import { TransactionFactory } from "@ethereumjs/tx";
2
+ import { SignTypedDataVersion } from "@metamask/eth-sig-util";
3
+ import { assert } from "@metamask/superstruct";
4
+ import { add0x } from "@metamask/utils";
5
+ import { KeyringWrapper } from "../../api/v2/wrapper/keyring-wrapper.mjs";
6
+ import { EthDecryptParamsStruct, EthGetAppKeyAddressParamsStruct, EthGetEncryptionPublicKeyParamsStruct, EthPersonalSignParamsStruct, EthSignEip7702AuthorizationParamsStruct, EthSignParamsStruct, EthSignTransactionParamsStruct, EthSignTypedDataParamsStruct, EthSignTypedDataV1ParamsStruct } from "../rpc/index.mjs";
7
+ import { EthMethod } from "../types.mjs";
8
+ /**
9
+ * Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.
10
+ * These are primarily encryption and utility methods.
11
+ */
12
+ export var EthKeyringMethod;
13
+ (function (EthKeyringMethod) {
14
+ EthKeyringMethod["Decrypt"] = "eth_decrypt";
15
+ EthKeyringMethod["GetEncryptionPublicKey"] = "eth_getEncryptionPublicKey";
16
+ EthKeyringMethod["GetAppKeyAddress"] = "eth_getAppKeyAddress";
17
+ EthKeyringMethod["SignEip7702Authorization"] = "eth_signEip7702Authorization";
18
+ })(EthKeyringMethod || (EthKeyringMethod = {}));
19
+ /**
20
+ * Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.
21
+ *
22
+ * This class provides common functionality for all Ethereum keyrings including:
23
+ * - Request handling for standard Ethereum signing methods
24
+ * - Helper methods for Hex address conversion
25
+ *
26
+ * Subclasses must implement:
27
+ * - `getAccounts()`: Return all managed accounts
28
+ * - `createAccounts()`: Create new accounts based on options
29
+ * - `deleteAccount()`: Remove an account from the keyring
30
+ * - `exportAccount()` (optional): Export private key in specified format
31
+ */
32
+ export class EthKeyringWrapper extends KeyringWrapper {
33
+ /**
34
+ * Helper method to safely cast a KeyringAccount address to Hex type.
35
+ * The KeyringAccount.address is typed as string, but for Ethereum accounts
36
+ * it should always be a valid Hex address.
37
+ *
38
+ * @param address - The address from a KeyringAccount.
39
+ * @returns The address as Hex type.
40
+ */
41
+ toHexAddress(address) {
42
+ return add0x(address);
43
+ }
44
+ /**
45
+ * Handle an Ethereum signing request.
46
+ *
47
+ * Routes the request to the appropriate legacy keyring method based on
48
+ * the RPC method name.
49
+ *
50
+ * @param request - The keyring request containing method and params.
51
+ * @returns The result of the signing operation.
52
+ */
53
+ async submitRequest(request) {
54
+ const { method, params } = request.request;
55
+ const { address, methods } = await this.getAccount(request.account);
56
+ const hexAddress = this.toHexAddress(address);
57
+ // Validate account can handle the method
58
+ if (!methods.includes(method)) {
59
+ throw new Error(`Account ${request.account} cannot handle method: ${method}`);
60
+ }
61
+ switch (method) {
62
+ case `${EthMethod.SignTransaction}`: {
63
+ if (!this.inner.signTransaction) {
64
+ throw new Error('Keyring does not support signTransaction');
65
+ }
66
+ assert(params, EthSignTransactionParamsStruct);
67
+ const [txData] = params;
68
+ // Convert validated transaction data to TypedTransaction
69
+ // TODO: Improve typing to ensure txData matches TypedTxData
70
+ const tx = TransactionFactory.fromTxData(txData);
71
+ // Note: Bigints are not directly representable in JSON
72
+ return (await this.inner.signTransaction(hexAddress, tx)); // FIXME: Should return type be unknown?
73
+ }
74
+ case `${EthMethod.Sign}`: {
75
+ if (!this.inner.signMessage) {
76
+ throw new Error('Keyring does not support signMessage');
77
+ }
78
+ assert(params, EthSignParamsStruct);
79
+ const [, data] = params;
80
+ return this.inner.signMessage(hexAddress, data);
81
+ }
82
+ case `${EthMethod.PersonalSign}`: {
83
+ if (!this.inner.signPersonalMessage) {
84
+ throw new Error('Keyring does not support signPersonalMessage');
85
+ }
86
+ assert(params, EthPersonalSignParamsStruct);
87
+ const [data] = params;
88
+ return this.inner.signPersonalMessage(hexAddress, data);
89
+ }
90
+ case `${EthMethod.SignTypedDataV1}`: {
91
+ if (!this.inner.signTypedData) {
92
+ throw new Error('Keyring does not support signTypedData');
93
+ }
94
+ assert(params, EthSignTypedDataV1ParamsStruct);
95
+ const [, data] = params;
96
+ return this.inner.signTypedData(hexAddress, data, {
97
+ version: SignTypedDataVersion.V1,
98
+ });
99
+ }
100
+ case `${EthMethod.SignTypedDataV3}`: {
101
+ if (!this.inner.signTypedData) {
102
+ throw new Error('Keyring does not support signTypedData');
103
+ }
104
+ assert(params, EthSignTypedDataParamsStruct);
105
+ const [, data] = params;
106
+ return this.inner.signTypedData(hexAddress,
107
+ // TODO: Improve typing to ensure data matches MessageTypes
108
+ data, {
109
+ version: SignTypedDataVersion.V3,
110
+ });
111
+ }
112
+ case `${EthMethod.SignTypedDataV4}`: {
113
+ if (!this.inner.signTypedData) {
114
+ throw new Error('Keyring does not support signTypedData');
115
+ }
116
+ assert(params, EthSignTypedDataParamsStruct);
117
+ const [, data] = params;
118
+ return this.inner.signTypedData(hexAddress,
119
+ // TODO: Improve typing to ensure data matches MessageTypes
120
+ data, {
121
+ version: SignTypedDataVersion.V4,
122
+ });
123
+ }
124
+ case `${EthKeyringMethod.Decrypt}`: {
125
+ if (!this.inner.decryptMessage) {
126
+ throw new Error('Keyring does not support decryptMessage');
127
+ }
128
+ assert(params, EthDecryptParamsStruct);
129
+ const [encryptedData] = params;
130
+ return this.inner.decryptMessage(hexAddress, encryptedData);
131
+ }
132
+ case `${EthKeyringMethod.GetEncryptionPublicKey}`: {
133
+ if (!this.inner.getEncryptionPublicKey) {
134
+ throw new Error('Keyring does not support getEncryptionPublicKey');
135
+ }
136
+ assert(params, EthGetEncryptionPublicKeyParamsStruct);
137
+ const [, options] = params;
138
+ return this.inner.getEncryptionPublicKey(hexAddress, options);
139
+ }
140
+ case `${EthKeyringMethod.GetAppKeyAddress}`: {
141
+ if (!this.inner.getAppKeyAddress) {
142
+ throw new Error('Keyring does not support getAppKeyAddress');
143
+ }
144
+ assert(params, EthGetAppKeyAddressParamsStruct);
145
+ const [origin] = params;
146
+ return this.inner.getAppKeyAddress(hexAddress, origin);
147
+ }
148
+ case `${EthKeyringMethod.SignEip7702Authorization}`: {
149
+ if (!this.inner.signEip7702Authorization) {
150
+ throw new Error('Keyring does not support signEip7702Authorization');
151
+ }
152
+ assert(params, EthSignEip7702AuthorizationParamsStruct);
153
+ const [authorization] = params;
154
+ return this.inner.signEip7702Authorization(hexAddress, authorization);
155
+ }
156
+ default:
157
+ throw new Error(`Unsupported method for EthKeyringWrapper: ${method}`);
158
+ }
159
+ }
160
+ }
161
+ //# sourceMappingURL=eth-keyring-wrapper.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-keyring-wrapper.mjs","sourceRoot":"","sources":["../../../src/eth/v2/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAoB,uBAAuB;AAEtE,OAAO,EAAE,oBAAoB,EAAE,+BAA+B;AAE9D,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAC/C,OAAO,EAAE,KAAK,EAAuB,wBAAwB;AAI7D,OAAO,EACL,cAAc,EAEf,iDAA6C;AAC9C,OAAO,EACL,sBAAsB,EACtB,+BAA+B,EAC/B,qCAAqC,EACrC,2BAA2B,EAC3B,uCAAuC,EACvC,mBAAmB,EACnB,8BAA8B,EAC9B,4BAA4B,EAC5B,8BAA8B,EAC/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gBACD,MAAM,CAAC,MAAM,EAAE,uCAAuC,CAAC,CAAC;gBACxD,MAAM,CAAC,aAAa,CAAC,GAAG,MAAM,CAAC;gBAC/B,OAAO,IAAI,CAAC,KAAK,CAAC,wBAAwB,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;YACxE,CAAC;YAED;gBACE,MAAM,IAAI,KAAK,CAAC,6CAA6C,MAAM,EAAE,CAAC,CAAC;QAC3E,CAAC;IACH,CAAC;CACF","sourcesContent":["import { TransactionFactory, type TypedTxData } from '@ethereumjs/tx';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport { SignTypedDataVersion } from '@metamask/eth-sig-util';\nimport type { EthKeyring } from '@metamask/keyring-utils';\nimport { assert } from '@metamask/superstruct';\nimport { add0x, type Hex, type Json } from '@metamask/utils';\n\nimport type { KeyringAccount } from '../../api/account';\nimport type { KeyringRequest } from '../../api/request';\nimport {\n KeyringWrapper,\n type KeyringWrapperOptions,\n} from '../../api/v2/wrapper/keyring-wrapper';\nimport {\n EthDecryptParamsStruct,\n EthGetAppKeyAddressParamsStruct,\n EthGetEncryptionPublicKeyParamsStruct,\n EthPersonalSignParamsStruct,\n EthSignEip7702AuthorizationParamsStruct,\n EthSignParamsStruct,\n EthSignTransactionParamsStruct,\n EthSignTypedDataParamsStruct,\n EthSignTypedDataV1ParamsStruct,\n} from '../rpc';\nimport { EthMethod } from '../types';\n\n/**\n * Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.\n * These are primarily encryption and utility methods.\n */\nexport enum EthKeyringMethod {\n Decrypt = 'eth_decrypt',\n GetEncryptionPublicKey = 'eth_getEncryptionPublicKey',\n GetAppKeyAddress = 'eth_getAppKeyAddress',\n SignEip7702Authorization = 'eth_signEip7702Authorization',\n}\n\n/**\n * Options for constructing an EthKeyringWrapper.\n */\nexport type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> =\n KeyringWrapperOptions<InnerKeyring>;\n\n/**\n * Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.\n *\n * This class provides common functionality for all Ethereum keyrings including:\n * - Request handling for standard Ethereum signing methods\n * - Helper methods for Hex address conversion\n *\n * Subclasses must implement:\n * - `getAccounts()`: Return all managed accounts\n * - `createAccounts()`: Create new accounts based on options\n * - `deleteAccount()`: Remove an account from the keyring\n * - `exportAccount()` (optional): Export private key in specified format\n */\nexport abstract class EthKeyringWrapper<\n InnerKeyring extends EthKeyring,\n KeyringAccountType extends KeyringAccount = KeyringAccount,\n> extends KeyringWrapper<InnerKeyring, KeyringAccountType> {\n /**\n * Helper method to safely cast a KeyringAccount address to Hex type.\n * The KeyringAccount.address is typed as string, but for Ethereum accounts\n * it should always be a valid Hex address.\n *\n * @param address - The address from a KeyringAccount.\n * @returns The address as Hex type.\n */\n protected toHexAddress(address: string): Hex {\n return add0x(address);\n }\n\n /**\n * Handle an Ethereum signing request.\n *\n * Routes the request to the appropriate legacy keyring method based on\n * the RPC method name.\n *\n * @param request - The keyring request containing method and params.\n * @returns The result of the signing operation.\n */\n async submitRequest(request: KeyringRequest): Promise<Json> {\n const { method, params } = request.request;\n\n const { address, methods } = await this.getAccount(request.account);\n const hexAddress = this.toHexAddress(address);\n\n // Validate account can handle the method\n if (!methods.includes(method)) {\n throw new Error(\n `Account ${request.account} cannot handle method: ${method}`,\n );\n }\n\n switch (method) {\n case `${EthMethod.SignTransaction}`: {\n if (!this.inner.signTransaction) {\n throw new Error('Keyring does not support signTransaction');\n }\n assert(params, EthSignTransactionParamsStruct);\n const [txData] = params;\n // Convert validated transaction data to TypedTransaction\n // TODO: Improve typing to ensure txData matches TypedTxData\n const tx = TransactionFactory.fromTxData(txData as TypedTxData);\n // Note: Bigints are not directly representable in JSON\n return (await this.inner.signTransaction(\n hexAddress,\n tx,\n )) as unknown as Json; // FIXME: Should return type be unknown?\n }\n\n case `${EthMethod.Sign}`: {\n if (!this.inner.signMessage) {\n throw new Error('Keyring does not support signMessage');\n }\n assert(params, EthSignParamsStruct);\n const [, data] = params;\n return this.inner.signMessage(hexAddress, data);\n }\n\n case `${EthMethod.PersonalSign}`: {\n if (!this.inner.signPersonalMessage) {\n throw new Error('Keyring does not support signPersonalMessage');\n }\n assert(params, EthPersonalSignParamsStruct);\n const [data] = params;\n return this.inner.signPersonalMessage(hexAddress, data as Hex);\n }\n\n case `${EthMethod.SignTypedDataV1}`: {\n if (!this.inner.signTypedData) {\n throw new Error('Keyring does not support signTypedData');\n }\n assert(params, EthSignTypedDataV1ParamsStruct);\n const [, data] = params;\n return this.inner.signTypedData(hexAddress, data, {\n version: SignTypedDataVersion.V1,\n });\n }\n\n case `${EthMethod.SignTypedDataV3}`: {\n if (!this.inner.signTypedData) {\n throw new Error('Keyring does not support signTypedData');\n }\n assert(params, EthSignTypedDataParamsStruct);\n const [, data] = params;\n return this.inner.signTypedData(\n hexAddress,\n // TODO: Improve typing to ensure data matches MessageTypes\n data as TypedMessage<MessageTypes>,\n {\n version: SignTypedDataVersion.V3,\n },\n );\n }\n\n case `${EthMethod.SignTypedDataV4}`: {\n if (!this.inner.signTypedData) {\n throw new Error('Keyring does not support signTypedData');\n }\n assert(params, EthSignTypedDataParamsStruct);\n const [, data] = params;\n return this.inner.signTypedData(\n hexAddress,\n // TODO: Improve typing to ensure data matches MessageTypes\n data as TypedMessage<MessageTypes>,\n {\n version: SignTypedDataVersion.V4,\n },\n );\n }\n\n case `${EthKeyringMethod.Decrypt}`: {\n if (!this.inner.decryptMessage) {\n throw new Error('Keyring does not support decryptMessage');\n }\n assert(params, EthDecryptParamsStruct);\n const [encryptedData] = params;\n return this.inner.decryptMessage(hexAddress, encryptedData);\n }\n\n case `${EthKeyringMethod.GetEncryptionPublicKey}`: {\n if (!this.inner.getEncryptionPublicKey) {\n throw new Error('Keyring does not support getEncryptionPublicKey');\n }\n assert(params, EthGetEncryptionPublicKeyParamsStruct);\n const [, options] = params;\n return this.inner.getEncryptionPublicKey(hexAddress, options);\n }\n\n case `${EthKeyringMethod.GetAppKeyAddress}`: {\n if (!this.inner.getAppKeyAddress) {\n throw new Error('Keyring does not support getAppKeyAddress');\n }\n assert(params, EthGetAppKeyAddressParamsStruct);\n const [origin] = params;\n return this.inner.getAppKeyAddress(hexAddress, origin);\n }\n\n case `${EthKeyringMethod.SignEip7702Authorization}`: {\n if (!this.inner.signEip7702Authorization) {\n throw new Error('Keyring does not support signEip7702Authorization');\n }\n assert(params, EthSignEip7702AuthorizationParamsStruct);\n const [authorization] = params;\n return this.inner.signEip7702Authorization(hexAddress, authorization);\n }\n\n default:\n throw new Error(`Unsupported method for EthKeyringWrapper: ${method}`);\n }\n }\n}\n"]}
@@ -0,0 +1,18 @@
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("./eth-keyring-wrapper.cjs"), exports);
18
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/eth/v2/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAAsC","sourcesContent":["export * from './eth-keyring-wrapper';\n"]}
@@ -0,0 +1,2 @@
1
+ export * from "./eth-keyring-wrapper.cjs";
2
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/eth/v2/index.ts"],"names":[],"mappings":"AAAA,0CAAsC"}
@@ -0,0 +1,2 @@
1
+ export * from "./eth-keyring-wrapper.mjs";
2
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/eth/v2/index.ts"],"names":[],"mappings":"AAAA,0CAAsC"}
@@ -0,0 +1,2 @@
1
+ export * from "./eth-keyring-wrapper.mjs";
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/eth/v2/index.ts"],"names":[],"mappings":"AAAA,0CAAsC","sourcesContent":["export * from './eth-keyring-wrapper';\n"]}
package/dist/events.cjs CHANGED
@@ -1,6 +1,4 @@
1
1
  "use strict";
2
- /* eslint-disable @typescript-eslint/no-redundant-type-constituents */
3
- // FIXME: This rule seems to be triggering a false positive on the `KeyringEvents`.
4
2
  Object.defineProperty(exports, "__esModule", { value: true });
5
3
  exports.AccountAssetListUpdatedEventStruct = exports.AccountTransactionsUpdatedEventStruct = exports.AccountBalancesUpdatedEventStruct = exports.RequestRejectedEventStruct = exports.RequestApprovedEventStruct = exports.AccountDeletedEventStruct = exports.AccountUpdatedEventStruct = exports.AccountCreatedEventStruct = exports.KeyringEvent = void 0;
6
4
  const keyring_utils_1 = require("@metamask/keyring-utils");
@@ -1 +1 @@
1
- {"version":3,"file":"events.cjs","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":";AAAA,sEAAsE;AACtE,mFAAmF;;;AAEnF,2DAKiC;AAEjC,uDAAgF;AAChF,2CAIyB;AAEzB,yCAKe;AAEf;;GAEG;AACH,IAAY,YAcX;AAdD,WAAY,YAAY;IACtB,iBAAiB;IACjB,wDAAwC,CAAA;IACxC,wDAAwC,CAAA;IACxC,wDAAwC,CAAA;IAExC,iBAAiB;IACjB,0DAA0C,CAAA;IAC1C,0DAA0C,CA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,sBAAM,EAAC;QACb;;WAEG;QACH,MAAM,EAAE,IAAA,oBAAM;QACZ;;WAEG;QACH,+BAAe;QAEf;;WAEG;QACH,IAAA,sBAAM,EAAC;YACL;;eAEG;YACH,KAAK,EAAE,IAAA,mBAAK,EAAC,+BAAuB,CAAC;YAErC;;eAEG;YACH,OAAO,EAAE,IAAA,mBAAK,EAAC,+BAAuB,CAAC;SACxC,CAAC,CACH;KACF,CAAC;CACH,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-redundant-type-constituents */\n// FIXME: This rule seems to be triggering a false positive on the `KeyringEvents`.\n\nimport {\n exactOptional,\n object,\n UuidStruct,\n AccountIdStruct,\n} from '@metamask/keyring-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { array, boolean, literal, record, string } from '@metamask/superstruct';\nimport {\n CaipAssetTypeStruct,\n CaipAssetTypeOrIdStruct,\n JsonStruct,\n} from '@metamask/utils';\n\nimport {\n FungibleAssetAmountStruct,\n KeyringAccountStruct,\n TransactionStruct,\n MetaMaskOptionsStruct,\n} from './api';\n\n/**\n * Supported keyring events.\n */\nexport enum KeyringEvent {\n // Account events\n AccountCreated = 'notify:accountCreated',\n AccountUpdated = 'notify:accountUpdated',\n AccountDeleted = 'notify:accountDeleted',\n\n // Request events\n RequestApproved = 'notify:requestApproved',\n RequestRejected = 'notify:requestRejected',\n\n // Assets related events\n AccountBalancesUpdated = 'notify:accountBalancesUpdated',\n AccountAssetListUpdated = 'notify:accountAssetListUpdated',\n AccountTransactionsUpdated = 'notify:accountTransactionsUpdated',\n}\n\nexport const AccountCreatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountCreated}`),\n params: object({\n /**\n * New account object.\n */\n account: KeyringAccountStruct,\n\n /**\n * Account name suggestion provided to the MetaMask client.\n *\n * The keyring can suggest a name for the account, but it's up to the\n * client to decide whether to use it. The keyring won't be informed if the\n * client decides to use a different name.\n */\n accountNameSuggestion: exactOptional(string()),\n\n /**\n * Instructs MetaMask to display the add account confirmation dialog in the UI.\n *\n * **Note:** This is not guaranteed to be honored by the MetaMask client.\n */\n displayConfirmation: exactOptional(boolean()),\n\n /**\n * Instructs MetaMask to display the name confirmation dialog in the UI.\n * Otherwise, the account will be added with the suggested name, if it's not\n * already in use; if it is, a suffix will be appended to the name to make it\n * unique.\n *\n * **Note:** This is not guaranteed to be honored by the MetaMask client.\n */\n displayAccountNameSuggestion: exactOptional(boolean()),\n\n /**\n * Metamask internal options.\n */\n ...MetaMaskOptionsStruct.schema,\n }),\n});\nexport type AccountCreatedEvent = Infer<typeof AccountCreatedEventStruct>;\nexport type AccountCreatedEventPayload = AccountCreatedEvent['params'];\n\nexport const AccountUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountUpdated}`),\n params: object({\n /**\n * Updated account object.\n */\n account: KeyringAccountStruct,\n }),\n});\nexport type AccountUpdatedEvent = Infer<typeof AccountUpdatedEventStruct>;\nexport type AccountUpdatedEventPayload = AccountUpdatedEvent['params'];\n\nexport const AccountDeletedEventStruct = object({\n method: literal(`${KeyringEvent.AccountDeleted}`),\n params: object({\n /**\n * Deleted account ID.\n */\n id: UuidStruct,\n }),\n});\nexport type AccountDeletedEvent = Infer<typeof AccountDeletedEventStruct>;\nexport type AccountDeletedEventPayload = AccountDeletedEvent['params'];\n\nexport const RequestApprovedEventStruct = object({\n method: literal(`${KeyringEvent.RequestApproved}`),\n params: object({\n /**\n * Request ID.\n */\n id: UuidStruct,\n\n /**\n * Request result.\n */\n result: JsonStruct,\n }),\n});\nexport type RequestApprovedEvent = Infer<typeof RequestApprovedEventStruct>;\nexport type RequestApprovedEventPayload = RequestApprovedEvent['params'];\n\nexport const RequestRejectedEventStruct = object({\n method: literal(`${KeyringEvent.RequestRejected}`),\n params: object({\n /**\n * Request ID.\n */\n id: UuidStruct,\n }),\n});\nexport type RequestRejectedEvent = Infer<typeof RequestRejectedEventStruct>;\nexport type RequestRejectedEventPayload = RequestRejectedEvent['params'];\n\n// Assets related events:\n// -----------------------------------------------------------------------------------------------\n\n/**\n * Event emitted when the balances of an account are updated.\n *\n * Only changes are reported.\n *\n * The Snap can choose to emit this event for multiple accounts at once.\n */\nexport const AccountBalancesUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountBalancesUpdated}`),\n params: object({\n /**\n * Balances updates of accounts owned by the Snap.\n */\n balances: record(\n /**\n * Account ID.\n */\n AccountIdStruct,\n\n /**\n * Mapping of each owned assets and their respective balances for that account.\n */\n record(\n /**\n * Asset type (CAIP-19).\n */\n CaipAssetTypeStruct,\n\n /**\n * Balance information for a given asset.\n */\n FungibleAssetAmountStruct,\n ),\n ),\n }),\n});\nexport type AccountBalancesUpdatedEvent = Infer<\n typeof AccountBalancesUpdatedEventStruct\n>;\nexport type AccountBalancesUpdatedEventPayload =\n AccountBalancesUpdatedEvent['params'];\n\n/**\n * Event emitted when the transactions of an account are updated (added or\n * changed).\n *\n * Only changes are reported.\n *\n * The Snap can choose to emit this event for multiple accounts at once.\n */\nexport const AccountTransactionsUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountTransactionsUpdated}`),\n params: object({\n /**\n * Transactions updates of accounts owned by the Snap.\n */\n transactions: record(\n /**\n * Account ID.\n */\n AccountIdStruct,\n\n /**\n * List of updated transactions for that account.\n */\n array(TransactionStruct),\n ),\n }),\n});\nexport type AccountTransactionsUpdatedEvent = Infer<\n typeof AccountTransactionsUpdatedEventStruct\n>;\nexport type AccountTransactionsUpdatedEventPayload =\n AccountTransactionsUpdatedEvent['params'];\n\n/**\n * Event emitted when the assets of an account are updated.\n *\n * Only changes are reported.\n *\n * The Snap can choose to emit this event for multiple accounts at once.\n */\nexport const AccountAssetListUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountAssetListUpdated}`),\n params: object({\n /**\n * Asset list update of accounts owned by the Snap.\n */\n assets: record(\n /**\n * Account ID.\n */\n AccountIdStruct,\n\n /**\n * Asset list changes for that account.\n */\n object({\n /**\n * New assets detected.\n */\n added: array(CaipAssetTypeOrIdStruct),\n\n /**\n * Assets no longer available on that account.\n */\n removed: array(CaipAssetTypeOrIdStruct),\n }),\n ),\n }),\n});\nexport type AccountAssetListUpdatedEvent = Infer<\n typeof AccountAssetListUpdatedEventStruct\n>;\nexport type AccountAssetListUpdatedEventPayload =\n AccountAssetListUpdatedEvent['params'];\n\n/**\n * Keyring events.\n */\n// For some reason, eslint sometimes infer one of those members as `any`...\ntype KeyringEvents =\n | AccountCreatedEvent\n | AccountUpdatedEvent\n | AccountDeletedEvent\n | AccountAssetListUpdatedEvent\n | AccountBalancesUpdatedEvent\n | AccountTransactionsUpdatedEvent\n | RequestApprovedEvent\n | RequestRejectedEvent;\n\n/**\n * Extract the payload for a given `KeyringEvent` event.\n */\nexport type KeyringEventPayload<Event extends KeyringEvent> = Extract<\n KeyringEvents,\n // We need to use a literal string here, since that is what `KeyringEvents`\n // is using (probably because of `superstruct`.\n { method: `${Event}` }\n>['params'];\n"]}
1
+ {"version":3,"file":"events.cjs","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":";;;AAAA,2DAKiC;AAEjC,uDAAgF;AAChF,2CAIyB;AAEzB,yCAKe;AAEf;;GAEG;AACH,IAAY,YAcX;AAdD,WAAY,YAAY;IACtB,iBAAiB;IACjB,wDAAwC,CAAA;IACxC,wDAAwC,CAAA;IACxC,wDAAwC,CAAA;IAExC,iBAAiB;IACjB,0DAA0C,CAAA;IAC1C,0DAA0C,CAAA;IAE1C,wBAAwB;IACxB,wEAAwD,CAAA;IACxD,0EAA0D,CAAA;IAC1D,gFAAgE,CAAA;AAClE,CAAC,EAdW,YAAY,4BAAZ,YAAY,QAcvB;AAEY,QAAA,yBAAyB,GAAG,IAAA,sBAAM,EAAC;IAC9C,MAAM,EAAE,IAAA,qBAAO,EAAC,GAAG,YAAY,CAAC,cAAc,EAAE,CAAC;IACjD,MAAM,EAAE,IAAA,sBAAM,EAAC;QACb;;WAEG;QACH,OAAO,EAAE,0BAAoB;QAE7B;;;;;;WAMG;QACH,q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metamask/keyring-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { array, boolean, literal, record, string } from '@metamask/superstruct';\nimport {\n CaipAssetTypeStruct,\n CaipAssetTypeOrIdStruct,\n JsonStruct,\n} from '@metamask/utils';\n\nimport {\n FungibleAssetAmountStruct,\n KeyringAccountStruct,\n TransactionStruct,\n MetaMaskOptionsStruct,\n} from './api';\n\n/**\n * Supported keyring events.\n */\nexport enum KeyringEvent {\n // Account events\n AccountCreated = 'notify:accountCreated',\n AccountUpdated = 'notify:accountUpdated',\n AccountDeleted = 'notify:accountDeleted',\n\n // Request events\n RequestApproved = 'notify:requestApproved',\n RequestRejected = 'notify:requestRejected',\n\n // Assets related events\n AccountBalancesUpdated = 'notify:accountBalancesUpdated',\n AccountAssetListUpdated = 'notify:accountAssetListUpdated',\n AccountTransactionsUpdated = 'notify:accountTransactionsUpdated',\n}\n\nexport const AccountCreatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountCreated}`),\n params: object({\n /**\n * New account object.\n */\n account: KeyringAccountStruct,\n\n /**\n * Account name suggestion provided to the MetaMask client.\n *\n * The keyring can suggest a name for the account, but it's up to the\n * client to decide whether to use it. The keyring won't be informed if the\n * client decides to use a different name.\n */\n accountNameSuggestion: exactOptional(string()),\n\n /**\n * Instructs MetaMask to display the add account confirmation dialog in the UI.\n *\n * **Note:** This is not guaranteed to be honored by the MetaMask client.\n */\n displayConfirmation: exactOptional(boolean()),\n\n /**\n * Instructs MetaMask to display the name confirmation dialog in the UI.\n * Otherwise, the account will be added with the suggested name, if it's not\n * already in use; if it is, a suffix will be appended to the name to make it\n * unique.\n *\n * **Note:** This is not guaranteed to be honored by the MetaMask client.\n */\n displayAccountNameSuggestion: exactOptional(boolean()),\n\n /**\n * Metamask internal options.\n */\n ...MetaMaskOptionsStruct.schema,\n }),\n});\nexport type AccountCreatedEvent = Infer<typeof AccountCreatedEventStruct>;\nexport type AccountCreatedEventPayload = AccountCreatedEvent['params'];\n\nexport const AccountUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountUpdated}`),\n params: object({\n /**\n * Updated account object.\n */\n account: KeyringAccountStruct,\n }),\n});\nexport type AccountUpdatedEvent = Infer<typeof AccountUpdatedEventStruct>;\nexport type AccountUpdatedEventPayload = AccountUpdatedEvent['params'];\n\nexport const AccountDeletedEventStruct = object({\n method: literal(`${KeyringEvent.AccountDeleted}`),\n params: object({\n /**\n * Deleted account ID.\n */\n id: UuidStruct,\n }),\n});\nexport type AccountDeletedEvent = Infer<typeof AccountDeletedEventStruct>;\nexport type AccountDeletedEventPayload = AccountDeletedEvent['params'];\n\nexport const RequestApprovedEventStruct = object({\n method: literal(`${KeyringEvent.RequestApproved}`),\n params: object({\n /**\n * Request ID.\n */\n id: UuidStruct,\n\n /**\n * Request result.\n */\n result: JsonStruct,\n }),\n});\nexport type RequestApprovedEvent = Infer<typeof RequestApprovedEventStruct>;\nexport type RequestApprovedEventPayload = RequestApprovedEvent['params'];\n\nexport const RequestRejectedEventStruct = object({\n method: literal(`${KeyringEvent.RequestRejected}`),\n params: object({\n /**\n * Request ID.\n */\n id: UuidStruct,\n }),\n});\nexport type RequestRejectedEvent = Infer<typeof RequestRejectedEventStruct>;\nexport type RequestRejectedEventPayload = RequestRejectedEvent['params'];\n\n// Assets related events:\n// -----------------------------------------------------------------------------------------------\n\n/**\n * Event emitted when the balances of an account are updated.\n *\n * Only changes are reported.\n *\n * The Snap can choose to emit this event for multiple accounts at once.\n */\nexport const AccountBalancesUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountBalancesUpdated}`),\n params: object({\n /**\n * Balances updates of accounts owned by the Snap.\n */\n balances: record(\n /**\n * Account ID.\n */\n AccountIdStruct,\n\n /**\n * Mapping of each owned assets and their respective balances for that account.\n */\n record(\n /**\n * Asset type (CAIP-19).\n */\n CaipAssetTypeStruct,\n\n /**\n * Balance information for a given asset.\n */\n FungibleAssetAmountStruct,\n ),\n ),\n }),\n});\nexport type AccountBalancesUpdatedEvent = Infer<\n typeof AccountBalancesUpdatedEventStruct\n>;\nexport type AccountBalancesUpdatedEventPayload =\n AccountBalancesUpdatedEvent['params'];\n\n/**\n * Event emitted when the transactions of an account are updated (added or\n * changed).\n *\n * Only changes are reported.\n *\n * The Snap can choose to emit this event for multiple accounts at once.\n */\nexport const AccountTransactionsUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountTransactionsUpdated}`),\n params: object({\n /**\n * Transactions updates of accounts owned by the Snap.\n */\n transactions: record(\n /**\n * Account ID.\n */\n AccountIdStruct,\n\n /**\n * List of updated transactions for that account.\n */\n array(TransactionStruct),\n ),\n }),\n});\nexport type AccountTransactionsUpdatedEvent = Infer<\n typeof AccountTransactionsUpdatedEventStruct\n>;\nexport type AccountTransactionsUpdatedEventPayload =\n AccountTransactionsUpdatedEvent['params'];\n\n/**\n * Event emitted when the assets of an account are updated.\n *\n * Only changes are reported.\n *\n * The Snap can choose to emit this event for multiple accounts at once.\n */\nexport const AccountAssetListUpdatedEventStruct = object({\n method: literal(`${KeyringEvent.AccountAssetListUpdated}`),\n params: object({\n /**\n * Asset list update of accounts owned by the Snap.\n */\n assets: record(\n /**\n * Account ID.\n */\n AccountIdStruct,\n\n /**\n * Asset list changes for that account.\n */\n object({\n /**\n * New assets detected.\n */\n added: array(CaipAssetTypeOrIdStruct),\n\n /**\n * Assets no longer available on that account.\n */\n removed: array(CaipAssetTypeOrIdStruct),\n }),\n ),\n }),\n});\nexport type AccountAssetListUpdatedEvent = Infer<\n typeof AccountAssetListUpdatedEventStruct\n>;\nexport type AccountAssetListUpdatedEventPayload =\n AccountAssetListUpdatedEvent['params'];\n\n/**\n * Keyring events.\n */\n// For some reason, eslint sometimes infer one of those members as `any`...\ntype KeyringEvents =\n | AccountCreatedEvent\n | AccountUpdatedEvent\n | AccountDeletedEvent\n | AccountAssetListUpdatedEvent\n | AccountBalancesUpdatedEvent\n | AccountTransactionsUpdatedEvent\n | RequestApprovedEvent\n | RequestRejectedEvent;\n\n/**\n * Extract the payload for a given `KeyringEvent` event.\n */\nexport type KeyringEventPayload<Event extends KeyringEvent> = Extract<\n KeyringEvents,\n // We need to use a literal string here, since that is what `KeyringEvents`\n // is using (probably because of `superstruct`.\n { method: `${Event}` }\n>['params'];\n"]}