@metamask/keyring-sdk 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/CHANGELOG.md +20 -2
  2. package/dist/eth/eth-4337-keyring.cjs +3 -0
  3. package/dist/eth/eth-4337-keyring.cjs.map +1 -0
  4. package/dist/eth/eth-4337-keyring.d.cts +35 -0
  5. package/dist/eth/eth-4337-keyring.d.cts.map +1 -0
  6. package/dist/eth/eth-4337-keyring.d.mts +35 -0
  7. package/dist/eth/eth-4337-keyring.d.mts.map +1 -0
  8. package/dist/eth/eth-4337-keyring.mjs +2 -0
  9. package/dist/eth/eth-4337-keyring.mjs.map +1 -0
  10. package/dist/eth/index.cjs.map +1 -1
  11. package/dist/eth/index.d.cts +1 -0
  12. package/dist/eth/index.d.cts.map +1 -1
  13. package/dist/eth/index.d.mts +1 -0
  14. package/dist/eth/index.d.mts.map +1 -1
  15. package/dist/eth/index.mjs.map +1 -1
  16. package/dist/v2/eth/eth-keyring-v1-adapter.cjs +320 -0
  17. package/dist/v2/eth/eth-keyring-v1-adapter.cjs.map +1 -0
  18. package/dist/v2/eth/eth-keyring-v1-adapter.d.cts +156 -0
  19. package/dist/v2/eth/eth-keyring-v1-adapter.d.cts.map +1 -0
  20. package/dist/v2/eth/eth-keyring-v1-adapter.d.mts +156 -0
  21. package/dist/v2/eth/eth-keyring-v1-adapter.d.mts.map +1 -0
  22. package/dist/v2/eth/eth-keyring-v1-adapter.mjs +314 -0
  23. package/dist/v2/eth/eth-keyring-v1-adapter.mjs.map +1 -0
  24. package/dist/v2/eth/index.cjs +1 -0
  25. package/dist/v2/eth/index.cjs.map +1 -1
  26. package/dist/v2/eth/index.d.cts +1 -0
  27. package/dist/v2/eth/index.d.cts.map +1 -1
  28. package/dist/v2/eth/index.d.mts +1 -0
  29. package/dist/v2/eth/index.d.mts.map +1 -1
  30. package/dist/v2/eth/index.mjs +1 -0
  31. package/dist/v2/eth/index.mjs.map +1 -1
  32. package/dist/v2/keyring-v1-adapter.cjs +5 -19
  33. package/dist/v2/keyring-v1-adapter.cjs.map +1 -1
  34. package/dist/v2/keyring-v1-adapter.d.cts +1 -1
  35. package/dist/v2/keyring-v1-adapter.d.cts.map +1 -1
  36. package/dist/v2/keyring-v1-adapter.d.mts +1 -1
  37. package/dist/v2/keyring-v1-adapter.d.mts.map +1 -1
  38. package/dist/v2/keyring-v1-adapter.mjs +5 -19
  39. package/dist/v2/keyring-v1-adapter.mjs.map +1 -1
  40. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [2.2.0]
11
+
12
+ ### Added
13
+
14
+ - Add `EthKeyringV1Adapter` and typed errors for adapting v2 ETH keyrings to legacy ETH keyring signing flows ([#555](https://github.com/MetaMask/accounts/pull/555))
15
+
16
+ ### Changed
17
+
18
+ - Bump `@metamask/keyring-api` from `^23.1.0` to `^23.2.0` ([#562](https://github.com/MetaMask/accounts/pull/562))
19
+
20
+ ## [2.1.1]
21
+
22
+ ### Changed
23
+
24
+ - Bump `@metamask/keyring-utils` from `^3.3.0` to `^3.3.1` ([#546](https://github.com/MetaMask/accounts/pull/546))
25
+
10
26
  ## [2.1.0]
11
27
 
12
28
  ### Added
@@ -70,9 +86,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
70
86
 
71
87
  ### Added
72
88
 
73
- - Initial release, extracted from `@metamask/keyring-api` ([#478](https://github.com/MetaMask/accounts/pull/478)), ([#482](https://github.com/MetaMask/accounts/pull/482))
89
+ - Initial release, extracted from `@metamask/keyring-api` ([#478](https://github.com/MetaMask/accounts/pull/478), [#482](https://github.com/MetaMask/accounts/pull/482))
74
90
 
75
- [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@2.1.0...HEAD
91
+ [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@2.2.0...HEAD
92
+ [2.2.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@2.1.1...@metamask/keyring-sdk@2.2.0
93
+ [2.1.1]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@2.1.0...@metamask/keyring-sdk@2.1.1
76
94
  [2.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@2.0.2...@metamask/keyring-sdk@2.1.0
77
95
  [2.0.2]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@2.0.1...@metamask/keyring-sdk@2.0.2
78
96
  [2.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@2.0.0...@metamask/keyring-sdk@2.0.1
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=eth-4337-keyring.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-4337-keyring.cjs","sourceRoot":"","sources":["../../src/eth/eth-4337-keyring.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n EthBaseTransaction,\n EthBaseUserOperation,\n EthUserOperation,\n EthUserOperationPatch,\n KeyringExecutionContext,\n} from '@metamask/keyring-api';\n\n/**\n * Legacy ETH keyring methods for ERC-4337 accounts.\n */\nexport type Eth4337Keyring = {\n /**\n * Convert base transactions to a base UserOperation.\n *\n * @param address - Address of the account to prepare the user operation for.\n * @param transactions - Base transactions to include in the UserOperation.\n * @param context - Keyring execution context.\n * @returns A pseudo-UserOperation that can be used to construct a real one.\n */\n prepareUserOperation?(\n address: string,\n transactions: EthBaseTransaction[],\n context: KeyringExecutionContext,\n ): Promise<EthBaseUserOperation>;\n\n /**\n * Patches properties of a UserOperation. Currently, only the\n * `paymasterAndData` can be patched.\n *\n * @param address - Address of the account to patch the user operation for.\n * @param userOperation - UserOperation to patch.\n * @param context - Keyring execution context.\n * @returns A patch to apply to the UserOperation.\n */\n patchUserOperation?(\n address: string,\n userOperation: EthUserOperation,\n context: KeyringExecutionContext,\n ): Promise<EthUserOperationPatch>;\n\n /**\n * Signs a UserOperation.\n *\n * @param address - Address of the account to sign the user operation for.\n * @param userOperation - UserOperation to sign.\n * @param context - Keyring execution context.\n * @returns A promise that resolves to the signature.\n */\n signUserOperation?(\n address: string,\n userOperation: EthUserOperation,\n context: KeyringExecutionContext,\n ): Promise<string>;\n};\n"]}
@@ -0,0 +1,35 @@
1
+ import type { EthBaseTransaction, EthBaseUserOperation, EthUserOperation, EthUserOperationPatch, KeyringExecutionContext } from "@metamask/keyring-api";
2
+ /**
3
+ * Legacy ETH keyring methods for ERC-4337 accounts.
4
+ */
5
+ export type Eth4337Keyring = {
6
+ /**
7
+ * Convert base transactions to a base UserOperation.
8
+ *
9
+ * @param address - Address of the account to prepare the user operation for.
10
+ * @param transactions - Base transactions to include in the UserOperation.
11
+ * @param context - Keyring execution context.
12
+ * @returns A pseudo-UserOperation that can be used to construct a real one.
13
+ */
14
+ prepareUserOperation?(address: string, transactions: EthBaseTransaction[], context: KeyringExecutionContext): Promise<EthBaseUserOperation>;
15
+ /**
16
+ * Patches properties of a UserOperation. Currently, only the
17
+ * `paymasterAndData` can be patched.
18
+ *
19
+ * @param address - Address of the account to patch the user operation for.
20
+ * @param userOperation - UserOperation to patch.
21
+ * @param context - Keyring execution context.
22
+ * @returns A patch to apply to the UserOperation.
23
+ */
24
+ patchUserOperation?(address: string, userOperation: EthUserOperation, context: KeyringExecutionContext): Promise<EthUserOperationPatch>;
25
+ /**
26
+ * Signs a UserOperation.
27
+ *
28
+ * @param address - Address of the account to sign the user operation for.
29
+ * @param userOperation - UserOperation to sign.
30
+ * @param context - Keyring execution context.
31
+ * @returns A promise that resolves to the signature.
32
+ */
33
+ signUserOperation?(address: string, userOperation: EthUserOperation, context: KeyringExecutionContext): Promise<string>;
34
+ };
35
+ //# sourceMappingURL=eth-4337-keyring.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-4337-keyring.d.cts","sourceRoot":"","sources":["../../src/eth/eth-4337-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,qBAAqB,EACrB,uBAAuB,EACxB,8BAA8B;AAE/B;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;;;;;;OAOG;IACH,oBAAoB,CAAC,CACnB,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,kBAAkB,EAAE,EAClC,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAEjC;;;;;;;;OAQG;IACH,kBAAkB,CAAC,CACjB,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,gBAAgB,EAC/B,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAElC;;;;;;;OAOG;IACH,iBAAiB,CAAC,CAChB,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,gBAAgB,EAC/B,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,MAAM,CAAC,CAAC;CACpB,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { EthBaseTransaction, EthBaseUserOperation, EthUserOperation, EthUserOperationPatch, KeyringExecutionContext } from "@metamask/keyring-api";
2
+ /**
3
+ * Legacy ETH keyring methods for ERC-4337 accounts.
4
+ */
5
+ export type Eth4337Keyring = {
6
+ /**
7
+ * Convert base transactions to a base UserOperation.
8
+ *
9
+ * @param address - Address of the account to prepare the user operation for.
10
+ * @param transactions - Base transactions to include in the UserOperation.
11
+ * @param context - Keyring execution context.
12
+ * @returns A pseudo-UserOperation that can be used to construct a real one.
13
+ */
14
+ prepareUserOperation?(address: string, transactions: EthBaseTransaction[], context: KeyringExecutionContext): Promise<EthBaseUserOperation>;
15
+ /**
16
+ * Patches properties of a UserOperation. Currently, only the
17
+ * `paymasterAndData` can be patched.
18
+ *
19
+ * @param address - Address of the account to patch the user operation for.
20
+ * @param userOperation - UserOperation to patch.
21
+ * @param context - Keyring execution context.
22
+ * @returns A patch to apply to the UserOperation.
23
+ */
24
+ patchUserOperation?(address: string, userOperation: EthUserOperation, context: KeyringExecutionContext): Promise<EthUserOperationPatch>;
25
+ /**
26
+ * Signs a UserOperation.
27
+ *
28
+ * @param address - Address of the account to sign the user operation for.
29
+ * @param userOperation - UserOperation to sign.
30
+ * @param context - Keyring execution context.
31
+ * @returns A promise that resolves to the signature.
32
+ */
33
+ signUserOperation?(address: string, userOperation: EthUserOperation, context: KeyringExecutionContext): Promise<string>;
34
+ };
35
+ //# sourceMappingURL=eth-4337-keyring.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-4337-keyring.d.mts","sourceRoot":"","sources":["../../src/eth/eth-4337-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,qBAAqB,EACrB,uBAAuB,EACxB,8BAA8B;AAE/B;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;;;;;;OAOG;IACH,oBAAoB,CAAC,CACnB,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,kBAAkB,EAAE,EAClC,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAEjC;;;;;;;;OAQG;IACH,kBAAkB,CAAC,CACjB,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,gBAAgB,EAC/B,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAElC;;;;;;;OAOG;IACH,iBAAiB,CAAC,CAChB,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,gBAAgB,EAC/B,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,MAAM,CAAC,CAAC;CACpB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=eth-4337-keyring.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-4337-keyring.mjs","sourceRoot":"","sources":["../../src/eth/eth-4337-keyring.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n EthBaseTransaction,\n EthBaseUserOperation,\n EthUserOperation,\n EthUserOperationPatch,\n KeyringExecutionContext,\n} from '@metamask/keyring-api';\n\n/**\n * Legacy ETH keyring methods for ERC-4337 accounts.\n */\nexport type Eth4337Keyring = {\n /**\n * Convert base transactions to a base UserOperation.\n *\n * @param address - Address of the account to prepare the user operation for.\n * @param transactions - Base transactions to include in the UserOperation.\n * @param context - Keyring execution context.\n * @returns A pseudo-UserOperation that can be used to construct a real one.\n */\n prepareUserOperation?(\n address: string,\n transactions: EthBaseTransaction[],\n context: KeyringExecutionContext,\n ): Promise<EthBaseUserOperation>;\n\n /**\n * Patches properties of a UserOperation. Currently, only the\n * `paymasterAndData` can be patched.\n *\n * @param address - Address of the account to patch the user operation for.\n * @param userOperation - UserOperation to patch.\n * @param context - Keyring execution context.\n * @returns A patch to apply to the UserOperation.\n */\n patchUserOperation?(\n address: string,\n userOperation: EthUserOperation,\n context: KeyringExecutionContext,\n ): Promise<EthUserOperationPatch>;\n\n /**\n * Signs a UserOperation.\n *\n * @param address - Address of the account to sign the user operation for.\n * @param userOperation - UserOperation to sign.\n * @param context - Keyring execution context.\n * @returns A promise that resolves to the signature.\n */\n signUserOperation?(\n address: string,\n userOperation: EthUserOperation,\n context: KeyringExecutionContext,\n ): Promise<string>;\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAA6B","sourcesContent":["export * from './account-id';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAA6B","sourcesContent":["export * from './account-id';\nexport type * from './eth-4337-keyring';\n"]}
@@ -1,2 +1,3 @@
1
1
  export * from "./account-id.cjs";
2
+ export type * from "./eth-4337-keyring.cjs";
2
3
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,4CAAwC"}
@@ -1,2 +1,3 @@
1
1
  export * from "./account-id.mjs";
2
+ export type * from "./eth-4337-keyring.mjs";
2
3
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,4CAAwC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B","sourcesContent":["export * from './account-id';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B","sourcesContent":["export * from './account-id';\nexport type * from './eth-4337-keyring';\n"]}
@@ -0,0 +1,320 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var _EthKeyringV1Adapter_instances, _EthKeyringV1Adapter_getAccount, _EthKeyringV1Adapter_getAccountForMethod, _EthKeyringV1Adapter_getSignTypedDataMethod, _EthKeyringV1Adapter_submitRequest;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.EthKeyringV1Adapter = exports.EthKeyringV1MethodNotSupportedError = exports.EthKeyringV1AccountNotFoundError = void 0;
10
+ const tx_1 = require("@ethereumjs/tx");
11
+ const eth_sig_util_1 = require("@metamask/eth-sig-util");
12
+ const keyring_api_1 = require("@metamask/keyring-api");
13
+ const v2_1 = require("@metamask/keyring-api/v2");
14
+ const keyring_utils_1 = require("@metamask/keyring-utils");
15
+ const superstruct_1 = require("@metamask/superstruct");
16
+ const utils_1 = require("@metamask/utils");
17
+ const uuid_1 = require("uuid");
18
+ const keyring_v1_adapter_1 = require("../keyring-v1-adapter.cjs");
19
+ const eth_keyring_wrapper_1 = require("./eth-keyring-wrapper.cjs");
20
+ const METAMASK_ORIGIN = 'metamask';
21
+ /**
22
+ * Convert a value to a valid JSON object.
23
+ *
24
+ * The function chains JSON.stringify and JSON.parse to ensure that the result
25
+ * is a valid JSON object.
26
+ *
27
+ * We assume that the input value is JSON-serializable.
28
+ *
29
+ * However, some transformations can occur like: in objects, undefined values
30
+ * are removed, and in arrays, they are replaced with null.
31
+ *
32
+ * @param value - Value to convert to JSON - Assumed to be JSON-serializable.
33
+ * @returns JSON representation of the value.
34
+ */
35
+ function toJson(value) {
36
+ return JSON.parse(JSON.stringify(value));
37
+ }
38
+ /**
39
+ * Convert a chain ID (number or hex) to an EIP-155 CAIP chain ID string for keyring request scoping.
40
+ *
41
+ * @param chainId - The chain ID to convert.
42
+ * @returns The EIP-155 CAIP chain ID string.
43
+ */
44
+ function toEip155Scope(chainId) {
45
+ return (0, utils_1.toCaipChainId)(utils_1.KnownCaipNamespace.Eip155, BigInt(chainId).toString());
46
+ }
47
+ /**
48
+ * Error thrown when an account cannot be resolved from the requested address.
49
+ */
50
+ class EthKeyringV1AccountNotFoundError extends Error {
51
+ constructor(address) {
52
+ super(`Account not found for address: ${address}`);
53
+ this.name = 'EthKeyringV1AccountNotFoundError';
54
+ this.address = address;
55
+ Object.setPrototypeOf(this, EthKeyringV1AccountNotFoundError.prototype);
56
+ }
57
+ }
58
+ exports.EthKeyringV1AccountNotFoundError = EthKeyringV1AccountNotFoundError;
59
+ /**
60
+ * Error thrown when a resolved account does not support a requested method.
61
+ */
62
+ class EthKeyringV1MethodNotSupportedError extends Error {
63
+ constructor(method, address) {
64
+ super(`Account ${address} does not support method: ${method}`);
65
+ this.name = 'EthKeyringV1MethodNotSupportedError';
66
+ this.address = address;
67
+ this.method = method;
68
+ Object.setPrototypeOf(this, EthKeyringV1MethodNotSupportedError.prototype);
69
+ }
70
+ }
71
+ exports.EthKeyringV1MethodNotSupportedError = EthKeyringV1MethodNotSupportedError;
72
+ /**
73
+ * Adapts a `KeyringV2` instance to the subset of the legacy ETH keyring API
74
+ * used by existing ETH export and signing flows.
75
+ *
76
+ * The adapter is account-agnostic: each method resolves the requested address
77
+ * against the wrapped v2 keyring's accounts, verifies that the resolved account
78
+ * supports the requested method, then forwards the request through the v2
79
+ * `submitRequest` or `exportAccount` APIs.
80
+ */
81
+ class EthKeyringV1Adapter extends keyring_v1_adapter_1.KeyringV1Adapter {
82
+ constructor() {
83
+ super(...arguments);
84
+ _EthKeyringV1Adapter_instances.add(this);
85
+ }
86
+ /**
87
+ * Gets the private data associated with the given address so
88
+ * that it may be exported.
89
+ *
90
+ * Used by the UI to export an account.
91
+ *
92
+ * @param address - Address of the account to export.
93
+ * @returns A promise that resolves to the exported private key.
94
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
95
+ * address.
96
+ */
97
+ async exportAccount(address) {
98
+ const { account } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccount).call(this, address);
99
+ if (!this.inner.exportAccount) {
100
+ throw new Error('Keyring does not support exportAccount');
101
+ }
102
+ const exportedAccount = await this.inner.exportAccount(account.id, {
103
+ type: v2_1.AccountExportType.PrivateKey,
104
+ encoding: v2_1.PrivateKeyEncoding.Hexadecimal,
105
+ });
106
+ return (0, utils_1.remove0x)(exportedAccount.privateKey);
107
+ }
108
+ /**
109
+ * Sign a transaction.
110
+ *
111
+ * @param address - Sender's address.
112
+ * @param transaction - Transaction.
113
+ * @returns A promise that resolves to the signed transaction.
114
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
115
+ * address.
116
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
117
+ * not support transaction signing.
118
+ */
119
+ async signTransaction(address, transaction) {
120
+ const { account, normalizedAddress } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, keyring_api_1.EthMethod.SignTransaction);
121
+ const chainId = transaction.common.chainId();
122
+ const tx = toJson({
123
+ ...transaction.toJSON(),
124
+ from: normalizedAddress,
125
+ type: `0x${transaction.type.toString(16)}`,
126
+ chainId: (0, utils_1.bigIntToHex)(chainId),
127
+ });
128
+ const signedTx = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, keyring_api_1.EthMethod.SignTransaction, toJson([tx]), toEip155Scope(chainId));
129
+ // Only take the signature fields from the keyring response. This prevents
130
+ // the keyring from overwriting the transaction payload we submitted.
131
+ const signature = (0, superstruct_1.mask)(signedTx, (0, superstruct_1.object)({
132
+ r: keyring_api_1.EthBytesStrictStruct,
133
+ s: keyring_api_1.EthBytesStrictStruct,
134
+ v: keyring_api_1.EthBytesStrictStruct,
135
+ }));
136
+ return tx_1.TransactionFactory.fromTxData({
137
+ ...tx,
138
+ r: signature.r,
139
+ s: signature.s,
140
+ v: signature.v,
141
+ });
142
+ }
143
+ /**
144
+ * Sign a message.
145
+ *
146
+ * @param address - Signer's address.
147
+ * @param data - Data to sign.
148
+ * @returns A promise that resolves to the signature.
149
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
150
+ * address.
151
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
152
+ * not support message signing.
153
+ */
154
+ async signMessage(address, data) {
155
+ const { account, normalizedAddress } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, keyring_api_1.EthMethod.Sign);
156
+ return (0, keyring_utils_1.strictMask)(await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, keyring_api_1.EthMethod.Sign, toJson([normalizedAddress, data]), ''), keyring_api_1.EthBytesStruct);
157
+ }
158
+ /**
159
+ * Sign an EIP-7702 authorization.
160
+ *
161
+ * @param address - Signer's address.
162
+ * @param authorization - Authorization tuple to sign.
163
+ * @returns A promise that resolves to the signature.
164
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
165
+ * address.
166
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
167
+ * not support EIP-7702 authorization signing.
168
+ */
169
+ async signEip7702Authorization(address, authorization) {
170
+ const { account } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, eth_keyring_wrapper_1.EthKeyringMethod.SignEip7702Authorization);
171
+ const [chainId] = authorization;
172
+ return (0, keyring_utils_1.strictMask)(await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, eth_keyring_wrapper_1.EthKeyringMethod.SignEip7702Authorization, [authorization], toEip155Scope(chainId)), keyring_api_1.EthBytesStruct);
173
+ }
174
+ /**
175
+ * Sign a personal message.
176
+ *
177
+ * Note: KeyringController says this should return a Buffer but it actually
178
+ * expects a string.
179
+ *
180
+ * @param address - Signer's address.
181
+ * @param message - Data to sign.
182
+ * @returns A promise that resolves to the signature.
183
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
184
+ * address.
185
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
186
+ * not support personal message signing.
187
+ */
188
+ async signPersonalMessage(address, message) {
189
+ const { account, normalizedAddress } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, keyring_api_1.EthMethod.PersonalSign);
190
+ return (0, keyring_utils_1.strictMask)(await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, keyring_api_1.EthMethod.PersonalSign, toJson([message, normalizedAddress]), ''), keyring_api_1.EthBytesStruct);
191
+ }
192
+ /**
193
+ * Sign a typed data message.
194
+ *
195
+ * @param address - Signer's address.
196
+ * @param typedData - Typed data to sign.
197
+ * @param options - Signing options.
198
+ * @param options.version - The typed data version to use.
199
+ * @returns A promise that resolves to the signature.
200
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
201
+ * address.
202
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
203
+ * not support the requested typed data signing method.
204
+ */
205
+ async signTypedData(address, typedData, options) {
206
+ const version = options?.version;
207
+ const method = __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getSignTypedDataMethod).call(this, version);
208
+ const { account, normalizedAddress } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, method);
209
+ // Extract chain ID as if it was a typed message (as defined by EIP-712), if
210
+ // input is not a typed message, then chain ID will be undefined!
211
+ const chainId = typedData.domain?.chainId;
212
+ return (0, keyring_utils_1.strictMask)(await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, method, toJson([normalizedAddress, typedData]), chainId === undefined ? '' : toEip155Scope(chainId)), keyring_api_1.EthBytesStruct);
213
+ }
214
+ /**
215
+ * Convert base transactions to a base UserOperation.
216
+ *
217
+ * @param address - Address of the sender.
218
+ * @param transactions - Base transactions to include in the UserOperation.
219
+ * @param context - Keyring execution context.
220
+ * @returns A pseudo-UserOperation that can be used to construct a real one.
221
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
222
+ * address.
223
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
224
+ * not support UserOperation preparation.
225
+ */
226
+ async prepareUserOperation(address, transactions, context) {
227
+ const { account } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, keyring_api_1.EthMethod.PrepareUserOperation);
228
+ return (0, keyring_utils_1.strictMask)(await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, keyring_api_1.EthMethod.PrepareUserOperation,
229
+ // NOTE: This is inconsistent with the other methods. We are not wrapping `transactions` in an array.
230
+ // Which means the receiver of the `submitRequest` call would see individual transactions spread as
231
+ // separate params rather than a single transactions array as the first param, which doesn't match
232
+ // the method signature of `prepareUserOperation(address, transactions, context)`.
233
+ // This was already implemented like this in the legacy Snap keyring, so we keep the same logic here
234
+ // too!
235
+ toJson(transactions), toEip155Scope(context.chainId)), keyring_api_1.EthBaseUserOperationStruct);
236
+ }
237
+ /**
238
+ * Patches properties of a UserOperation. Currently, only the
239
+ * `paymasterAndData` can be patched.
240
+ *
241
+ * @param address - Address of the sender.
242
+ * @param userOperation - UserOperation to patch.
243
+ * @param context - Keyring execution context.
244
+ * @returns A patch to apply to the UserOperation.
245
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
246
+ * address.
247
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
248
+ * not support UserOperation patching.
249
+ */
250
+ async patchUserOperation(address, userOperation, context) {
251
+ const { account } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, keyring_api_1.EthMethod.PatchUserOperation);
252
+ return (0, keyring_utils_1.strictMask)(await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, keyring_api_1.EthMethod.PatchUserOperation, toJson([userOperation]), toEip155Scope(context.chainId)), keyring_api_1.EthUserOperationPatchStruct);
253
+ }
254
+ /**
255
+ * Signs a UserOperation.
256
+ *
257
+ * @param address - Address of the sender.
258
+ * @param userOperation - UserOperation to sign.
259
+ * @param context - Keyring execution context.
260
+ * @returns The signature of the UserOperation.
261
+ * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the
262
+ * address.
263
+ * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does
264
+ * not support UserOperation signing.
265
+ */
266
+ async signUserOperation(address, userOperation, context) {
267
+ const { account } = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccountForMethod).call(this, address, keyring_api_1.EthMethod.SignUserOperation);
268
+ return (0, keyring_utils_1.strictMask)(await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_submitRequest).call(this, account, keyring_api_1.EthMethod.SignUserOperation, toJson([userOperation]), toEip155Scope(context.chainId)), keyring_api_1.EthBytesStruct);
269
+ }
270
+ }
271
+ exports.EthKeyringV1Adapter = EthKeyringV1Adapter;
272
+ _EthKeyringV1Adapter_instances = new WeakSet(), _EthKeyringV1Adapter_getAccount = async function _EthKeyringV1Adapter_getAccount(address) {
273
+ const normalizedAddress = (0, eth_sig_util_1.normalize)(address);
274
+ const normalizedAddressLowerCase = normalizedAddress?.toLowerCase();
275
+ if (!normalizedAddress || !normalizedAddressLowerCase) {
276
+ throw new EthKeyringV1AccountNotFoundError(address);
277
+ }
278
+ const accounts = await this.inner.getAccounts();
279
+ const account = accounts.find((keyringAccount) => {
280
+ const accountAddress = (0, eth_sig_util_1.normalize)(keyringAccount.address);
281
+ return accountAddress?.toLowerCase() === normalizedAddressLowerCase;
282
+ });
283
+ if (!account) {
284
+ throw new EthKeyringV1AccountNotFoundError(address);
285
+ }
286
+ return {
287
+ account,
288
+ normalizedAddress: normalizedAddress,
289
+ };
290
+ }, _EthKeyringV1Adapter_getAccountForMethod = async function _EthKeyringV1Adapter_getAccountForMethod(address, method) {
291
+ const resolvedAccount = await __classPrivateFieldGet(this, _EthKeyringV1Adapter_instances, "m", _EthKeyringV1Adapter_getAccount).call(this, address);
292
+ if (!resolvedAccount.account.methods.includes(method)) {
293
+ throw new EthKeyringV1MethodNotSupportedError(method, address);
294
+ }
295
+ return resolvedAccount;
296
+ }, _EthKeyringV1Adapter_getSignTypedDataMethod = function _EthKeyringV1Adapter_getSignTypedDataMethod(version) {
297
+ switch (version) {
298
+ case 'V3':
299
+ return keyring_api_1.EthMethod.SignTypedDataV3;
300
+ case 'V4':
301
+ return keyring_api_1.EthMethod.SignTypedDataV4;
302
+ case 'V1':
303
+ default:
304
+ // Use 'V1' by default to match other keyring implementations. V1 will be
305
+ // used if the version is not specified or not supported.
306
+ return keyring_api_1.EthMethod.SignTypedDataV1;
307
+ }
308
+ }, _EthKeyringV1Adapter_submitRequest = async function _EthKeyringV1Adapter_submitRequest(account, method, params, scope) {
309
+ return (await this.inner.submitRequest({
310
+ id: (0, uuid_1.v4)(),
311
+ origin: METAMASK_ORIGIN,
312
+ scope,
313
+ account: account.id,
314
+ request: {
315
+ method,
316
+ params,
317
+ },
318
+ }));
319
+ };
320
+ //# sourceMappingURL=eth-keyring-v1-adapter.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eth-keyring-v1-adapter.cjs","sourceRoot":"","sources":["../../../src/v2/eth/eth-keyring-v1-adapter.ts"],"names":[],"mappings":";;;;;;;;;AACA,uCAAoD;AAEpD,yDAAmE;AACnE,uDAM+B;AAW/B,iDAGkC;AAElC,2DAAqD;AACrD,uDAAqD;AAErD,2CAKyB;AACzB,+BAAkC;AAGlC,kEAAyD;AACzD,mEAAyD;AAEzD,MAAM,eAAe,GAAG,UAAU,CAAC;AAenC;;;;;;;;;;;;;GAaG;AACH,SAAS,MAAM,CAA2B,KAAc;IACtD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAS,CAAC;AACnD,C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,CAAC;IACrC,CAAC;AACH,CAAC,uCAED,KAAK,6CACH,OAAuB,EACvB,MAAc,EACd,MAAqB,EACrB,KAAa;IAEb,OAAO,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC;QACrC,EAAE,EAAE,IAAA,SAAI,GAAE;QACV,MAAM,EAAE,eAAe;QACvB,KAAK;QACL,OAAO,EAAE,OAAO,CAAC,EAAE;QACnB,OAAO,EAAE;YACP,MAAM;YACN,MAAM;SACP;KACF,CAAC,CAAW,CAAC;AAChB,CAAC","sourcesContent":["import type { TypedTransaction } from '@ethereumjs/tx';\nimport { TransactionFactory } from '@ethereumjs/tx';\nimport type { SignTypedDataVersion } from '@metamask/eth-sig-util';\nimport { normalize as ethNormalize } from '@metamask/eth-sig-util';\nimport {\n EthBaseUserOperationStruct,\n EthBytesStrictStruct,\n EthBytesStruct,\n EthMethod,\n EthUserOperationPatchStruct,\n} from '@metamask/keyring-api';\nimport type {\n EthBaseTransaction,\n EthBaseUserOperation,\n EthUserOperation,\n EthUserOperationPatch,\n KeyringAccount,\n KeyringExecutionContext,\n KeyringRequest,\n} from '@metamask/keyring-api';\nimport type { Keyring as KeyringV2 } from '@metamask/keyring-api/v2';\nimport {\n AccountExportType,\n PrivateKeyEncoding,\n} from '@metamask/keyring-api/v2';\nimport type { BaseKeyring, EthKeyring } from '@metamask/keyring-utils';\nimport { strictMask } from '@metamask/keyring-utils';\nimport { mask, object } from '@metamask/superstruct';\nimport type { Hex, Json } from '@metamask/utils';\nimport {\n bigIntToHex,\n KnownCaipNamespace,\n remove0x,\n toCaipChainId,\n} from '@metamask/utils';\nimport { v4 as uuid } from 'uuid';\n\nimport type { Eth4337Keyring } from '../../eth';\nimport { KeyringV1Adapter } from '../keyring-v1-adapter';\nimport { EthKeyringMethod } from './eth-keyring-wrapper';\n\nconst METAMASK_ORIGIN = 'metamask';\n\ntype RequestParams = NonNullable<KeyringRequest['request']['params']>;\n\ntype ResolvedAccount = {\n account: KeyringAccount;\n normalizedAddress: Hex;\n};\n\ntype TypedDataWithDomain = {\n domain?: {\n chainId?: string | number;\n };\n};\n\n/**\n * Convert a value to a valid JSON object.\n *\n * The function chains JSON.stringify and JSON.parse to ensure that the result\n * is a valid JSON object.\n *\n * We assume that the input value is JSON-serializable.\n *\n * However, some transformations can occur like: in objects, undefined values\n * are removed, and in arrays, they are replaced with null.\n *\n * @param value - Value to convert to JSON - Assumed to be JSON-serializable.\n * @returns JSON representation of the value.\n */\nfunction toJson<Type extends Json = Json>(value: unknown): Type {\n return JSON.parse(JSON.stringify(value)) as Type;\n}\n\n/**\n * Convert a chain ID (number or hex) to an EIP-155 CAIP chain ID string for keyring request scoping.\n *\n * @param chainId - The chain ID to convert.\n * @returns The EIP-155 CAIP chain ID string.\n */\nfunction toEip155Scope(chainId: string | number | bigint): string {\n return toCaipChainId(KnownCaipNamespace.Eip155, BigInt(chainId).toString());\n}\n\n/**\n * Legacy ETH keyring API surface exposed by {@link EthKeyringV1Adapter}.\n */\nexport type BaseEthKeyring = BaseKeyring &\n // Only pick account-related methods (signing and exporting).\n Pick<\n EthKeyring,\n | 'exportAccount'\n | 'signMessage'\n | 'signEip7702Authorization'\n | 'signPersonalMessage'\n | 'signTypedData'\n | 'signTransaction'\n > &\n Eth4337Keyring;\n\n/**\n * Error thrown when an account cannot be resolved from the requested address.\n */\nexport class EthKeyringV1AccountNotFoundError extends Error {\n readonly address: string;\n\n constructor(address: string) {\n super(`Account not found for address: ${address}`);\n this.name = 'EthKeyringV1AccountNotFoundError';\n this.address = address;\n Object.setPrototypeOf(this, EthKeyringV1AccountNotFoundError.prototype);\n }\n}\n\n/**\n * Error thrown when a resolved account does not support a requested method.\n */\nexport class EthKeyringV1MethodNotSupportedError extends Error {\n readonly address: string;\n\n readonly method: string;\n\n constructor(method: string, address: string) {\n super(`Account ${address} does not support method: ${method}`);\n this.name = 'EthKeyringV1MethodNotSupportedError';\n this.address = address;\n this.method = method;\n Object.setPrototypeOf(this, EthKeyringV1MethodNotSupportedError.prototype);\n }\n}\n\n/**\n * Adapts a `KeyringV2` instance to the subset of the legacy ETH keyring API\n * used by existing ETH export and signing flows.\n *\n * The adapter is account-agnostic: each method resolves the requested address\n * against the wrapped v2 keyring's accounts, verifies that the resolved account\n * supports the requested method, then forwards the request through the v2\n * `submitRequest` or `exportAccount` APIs.\n */\nexport class EthKeyringV1Adapter<InnerKeyring extends KeyringV2 = KeyringV2>\n extends KeyringV1Adapter<InnerKeyring>\n implements BaseEthKeyring\n{\n /**\n * Gets the private data associated with the given address so\n * that it may be exported.\n *\n * Used by the UI to export an account.\n *\n * @param address - Address of the account to export.\n * @returns A promise that resolves to the exported private key.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n */\n async exportAccount(address: Hex): Promise<string> {\n const { account } = await this.#getAccount(address);\n\n if (!this.inner.exportAccount) {\n throw new Error('Keyring does not support exportAccount');\n }\n\n const exportedAccount = await this.inner.exportAccount(account.id, {\n type: AccountExportType.PrivateKey,\n encoding: PrivateKeyEncoding.Hexadecimal,\n });\n\n return remove0x(exportedAccount.privateKey);\n }\n\n /**\n * Sign a transaction.\n *\n * @param address - Sender's address.\n * @param transaction - Transaction.\n * @returns A promise that resolves to the signed transaction.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support transaction signing.\n */\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n ): Promise<TypedTransaction> {\n const { account, normalizedAddress } = await this.#getAccountForMethod(\n address,\n EthMethod.SignTransaction,\n );\n\n const chainId = transaction.common.chainId();\n const tx = toJson({\n ...transaction.toJSON(),\n from: normalizedAddress,\n type: `0x${transaction.type.toString(16)}`,\n chainId: bigIntToHex(chainId),\n });\n\n const signedTx = await this.#submitRequest(\n account,\n EthMethod.SignTransaction,\n toJson<Json[]>([tx]),\n toEip155Scope(chainId),\n );\n\n // Only take the signature fields from the keyring response. This prevents\n // the keyring from overwriting the transaction payload we submitted.\n const signature = mask(\n signedTx,\n object({\n r: EthBytesStrictStruct,\n s: EthBytesStrictStruct,\n v: EthBytesStrictStruct,\n }),\n );\n\n return TransactionFactory.fromTxData({\n ...(tx as Record<string, Json>),\n r: signature.r,\n s: signature.s,\n v: signature.v,\n });\n }\n\n /**\n * Sign a message.\n *\n * @param address - Signer's address.\n * @param data - Data to sign.\n * @returns A promise that resolves to the signature.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support message signing.\n */\n async signMessage(address: Hex, data: string): Promise<string> {\n const { account, normalizedAddress } = await this.#getAccountForMethod(\n address,\n EthMethod.Sign,\n );\n\n return strictMask(\n await this.#submitRequest(\n account,\n EthMethod.Sign,\n toJson<Json[]>([normalizedAddress, data]),\n '',\n ),\n EthBytesStruct,\n );\n }\n\n /**\n * Sign an EIP-7702 authorization.\n *\n * @param address - Signer's address.\n * @param authorization - Authorization tuple to sign.\n * @returns A promise that resolves to the signature.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support EIP-7702 authorization signing.\n */\n async signEip7702Authorization(\n address: Hex,\n authorization: [chainId: number, contractAddress: Hex, nonce: number],\n ): Promise<string> {\n const { account } = await this.#getAccountForMethod(\n address,\n EthKeyringMethod.SignEip7702Authorization,\n );\n\n const [chainId] = authorization;\n\n return strictMask(\n await this.#submitRequest(\n account,\n EthKeyringMethod.SignEip7702Authorization,\n [authorization],\n toEip155Scope(chainId),\n ),\n EthBytesStruct,\n );\n }\n\n /**\n * Sign a personal message.\n *\n * Note: KeyringController says this should return a Buffer but it actually\n * expects a string.\n *\n * @param address - Signer's address.\n * @param message - Data to sign.\n * @returns A promise that resolves to the signature.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support personal message signing.\n */\n async signPersonalMessage(address: Hex, message: Hex): Promise<string> {\n const { account, normalizedAddress } = await this.#getAccountForMethod(\n address,\n EthMethod.PersonalSign,\n );\n\n return strictMask(\n await this.#submitRequest(\n account,\n EthMethod.PersonalSign,\n toJson<Json[]>([message, normalizedAddress]),\n '',\n ),\n EthBytesStruct,\n );\n }\n\n /**\n * Sign a typed data message.\n *\n * @param address - Signer's address.\n * @param typedData - Typed data to sign.\n * @param options - Signing options.\n * @param options.version - The typed data version to use.\n * @returns A promise that resolves to the signature.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support the requested typed data signing method.\n */\n async signTypedData(\n address: Hex,\n typedData: unknown[] | Record<string, unknown>,\n options?: Record<string, unknown>,\n ): Promise<string> {\n const version = options?.version as SignTypedDataVersion | undefined;\n const method = this.#getSignTypedDataMethod(version);\n const { account, normalizedAddress } = await this.#getAccountForMethod(\n address,\n method,\n );\n\n // Extract chain ID as if it was a typed message (as defined by EIP-712), if\n // input is not a typed message, then chain ID will be undefined!\n const chainId = (typedData as TypedDataWithDomain).domain?.chainId;\n\n return strictMask(\n await this.#submitRequest(\n account,\n method,\n toJson<Json[]>([normalizedAddress, typedData]),\n chainId === undefined ? '' : toEip155Scope(chainId),\n ),\n EthBytesStruct,\n );\n }\n\n /**\n * Convert base transactions to a base UserOperation.\n *\n * @param address - Address of the sender.\n * @param transactions - Base transactions to include in the UserOperation.\n * @param context - Keyring execution context.\n * @returns A pseudo-UserOperation that can be used to construct a real one.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support UserOperation preparation.\n */\n async prepareUserOperation(\n address: string,\n transactions: EthBaseTransaction[],\n context: KeyringExecutionContext,\n ): Promise<EthBaseUserOperation> {\n const { account } = await this.#getAccountForMethod(\n address,\n EthMethod.PrepareUserOperation,\n );\n\n return strictMask(\n await this.#submitRequest(\n account,\n EthMethod.PrepareUserOperation,\n // NOTE: This is inconsistent with the other methods. We are not wrapping `transactions` in an array.\n // Which means the receiver of the `submitRequest` call would see individual transactions spread as\n // separate params rather than a single transactions array as the first param, which doesn't match\n // the method signature of `prepareUserOperation(address, transactions, context)`.\n // This was already implemented like this in the legacy Snap keyring, so we keep the same logic here\n // too!\n toJson<Json[]>(transactions),\n toEip155Scope(context.chainId),\n ),\n EthBaseUserOperationStruct,\n );\n }\n\n /**\n * Patches properties of a UserOperation. Currently, only the\n * `paymasterAndData` can be patched.\n *\n * @param address - Address of the sender.\n * @param userOperation - UserOperation to patch.\n * @param context - Keyring execution context.\n * @returns A patch to apply to the UserOperation.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support UserOperation patching.\n */\n async patchUserOperation(\n address: string,\n userOperation: EthUserOperation,\n context: KeyringExecutionContext,\n ): Promise<EthUserOperationPatch> {\n const { account } = await this.#getAccountForMethod(\n address,\n EthMethod.PatchUserOperation,\n );\n\n return strictMask(\n await this.#submitRequest(\n account,\n EthMethod.PatchUserOperation,\n toJson<Json[]>([userOperation]),\n toEip155Scope(context.chainId),\n ),\n EthUserOperationPatchStruct,\n );\n }\n\n /**\n * Signs a UserOperation.\n *\n * @param address - Address of the sender.\n * @param userOperation - UserOperation to sign.\n * @param context - Keyring execution context.\n * @returns The signature of the UserOperation.\n * @throws {@link EthKeyringV1AccountNotFoundError} if no account matches the\n * address.\n * @throws {@link EthKeyringV1MethodNotSupportedError} if the account does\n * not support UserOperation signing.\n */\n async signUserOperation(\n address: string,\n userOperation: EthUserOperation,\n context: KeyringExecutionContext,\n ): Promise<string> {\n const { account } = await this.#getAccountForMethod(\n address,\n EthMethod.SignUserOperation,\n );\n\n return strictMask(\n await this.#submitRequest(\n account,\n EthMethod.SignUserOperation,\n toJson<Json[]>([userOperation]),\n toEip155Scope(context.chainId),\n ),\n EthBytesStruct,\n );\n }\n\n async #getAccount(address: string): Promise<ResolvedAccount> {\n const normalizedAddress = ethNormalize(address);\n const normalizedAddressLowerCase = normalizedAddress?.toLowerCase();\n\n if (!normalizedAddress || !normalizedAddressLowerCase) {\n throw new EthKeyringV1AccountNotFoundError(address);\n }\n\n const accounts = await this.inner.getAccounts();\n const account = accounts.find((keyringAccount) => {\n const accountAddress = ethNormalize(keyringAccount.address);\n return accountAddress?.toLowerCase() === normalizedAddressLowerCase;\n });\n\n if (!account) {\n throw new EthKeyringV1AccountNotFoundError(address);\n }\n\n return {\n account,\n normalizedAddress: normalizedAddress as Hex,\n };\n }\n\n async #getAccountForMethod(\n address: string,\n method: string,\n ): Promise<ResolvedAccount> {\n const resolvedAccount = await this.#getAccount(address);\n\n if (!resolvedAccount.account.methods.includes(method)) {\n throw new EthKeyringV1MethodNotSupportedError(method, address);\n }\n\n return resolvedAccount;\n }\n\n #getSignTypedDataMethod(\n version: SignTypedDataVersion | undefined,\n ): EthMethod {\n switch (version) {\n case 'V3':\n return EthMethod.SignTypedDataV3;\n case 'V4':\n return EthMethod.SignTypedDataV4;\n case 'V1':\n default:\n // Use 'V1' by default to match other keyring implementations. V1 will be\n // used if the version is not specified or not supported.\n return EthMethod.SignTypedDataV1;\n }\n }\n\n async #submitRequest<Result extends Json = Json>(\n account: KeyringAccount,\n method: string,\n params: RequestParams,\n scope: string,\n ): Promise<Result> {\n return (await this.inner.submitRequest({\n id: uuid(),\n origin: METAMASK_ORIGIN,\n scope,\n account: account.id,\n request: {\n method,\n params,\n },\n })) as Result;\n }\n}\n"]}