@metamask/keyring-sdk 1.1.0 → 1.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.
- package/CHANGELOG.md +9 -1
- package/dist/eth/account-id.cjs +27 -0
- package/dist/eth/account-id.cjs.map +1 -0
- package/dist/eth/account-id.d.cts +16 -0
- package/dist/eth/account-id.d.cts.map +1 -0
- package/dist/eth/account-id.d.mts +16 -0
- package/dist/eth/account-id.d.mts.map +1 -0
- package/dist/eth/account-id.mjs +24 -0
- package/dist/eth/account-id.mjs.map +1 -0
- package/dist/eth/eth-keyring-wrapper.cjs +9 -0
- package/dist/eth/eth-keyring-wrapper.cjs.map +1 -1
- package/dist/eth/eth-keyring-wrapper.d.cts +7 -2
- package/dist/eth/eth-keyring-wrapper.d.cts.map +1 -1
- package/dist/eth/eth-keyring-wrapper.d.mts +7 -2
- package/dist/eth/eth-keyring-wrapper.d.mts.map +1 -1
- package/dist/eth/eth-keyring-wrapper.mjs +9 -0
- package/dist/eth/eth-keyring-wrapper.mjs.map +1 -1
- package/dist/eth/index.cjs +1 -0
- package/dist/eth/index.cjs.map +1 -1
- package/dist/eth/index.d.cts +1 -0
- package/dist/eth/index.d.cts.map +1 -1
- package/dist/eth/index.d.mts +1 -0
- package/dist/eth/index.d.mts.map +1 -1
- package/dist/eth/index.mjs +1 -0
- package/dist/eth/index.mjs.map +1 -1
- package/dist/keyring-account-registry.cjs +18 -4
- package/dist/keyring-account-registry.cjs.map +1 -1
- package/dist/keyring-account-registry.d.cts +19 -0
- package/dist/keyring-account-registry.d.cts.map +1 -1
- package/dist/keyring-account-registry.d.mts +19 -0
- package/dist/keyring-account-registry.d.mts.map +1 -1
- package/dist/keyring-account-registry.mjs +18 -4
- package/dist/keyring-account-registry.mjs.map +1 -1
- package/dist/keyring-wrapper.cjs +1 -8
- package/dist/keyring-wrapper.cjs.map +1 -1
- package/dist/keyring-wrapper.d.cts +5 -1
- package/dist/keyring-wrapper.d.cts.map +1 -1
- package/dist/keyring-wrapper.d.mts +5 -1
- package/dist/keyring-wrapper.d.mts.map +1 -1
- package/dist/keyring-wrapper.mjs +1 -8
- package/dist/keyring-wrapper.mjs.map +1 -1
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.2.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add `generateId` option to `KeyringAccountRegistry` ([#503](https://github.com/MetaMask/accounts/pull/503))
|
|
15
|
+
- Add `generateEthAccountId` to generate deterministic account IDs for EVM addresses, and use it by default in `EthKeyringWrapper` ([#504](https://github.com/MetaMask/accounts/pull/504))
|
|
16
|
+
|
|
10
17
|
## [1.1.0]
|
|
11
18
|
|
|
12
19
|
### Added
|
|
@@ -23,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
23
30
|
|
|
24
31
|
- Initial release, extracted from `@metamask/keyring-api` ([#478](https://github.com/MetaMask/accounts/pull/478)), ([#482](https://github.com/MetaMask/accounts/pull/482))
|
|
25
32
|
|
|
26
|
-
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@1.
|
|
33
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@1.2.0...HEAD
|
|
34
|
+
[1.2.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@1.1.0...@metamask/keyring-sdk@1.2.0
|
|
27
35
|
[1.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-sdk@1.0.0...@metamask/keyring-sdk@1.1.0
|
|
28
36
|
[1.0.0]: https://github.com/MetaMask/accounts/releases/tag/@metamask/keyring-sdk@1.0.0
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateEthAccountId = generateEthAccountId;
|
|
4
|
+
const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
6
|
+
const sha256_1 = require("ethereum-cryptography/sha256");
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
8
|
+
/**
|
|
9
|
+
* Generates a deterministic account ID for a given Ethereum address.
|
|
10
|
+
*
|
|
11
|
+
* The address is first normalized (lowercased, `0x`-prefixed) so that
|
|
12
|
+
* checksum-cased and lowercase variants of the same address produce the
|
|
13
|
+
* same ID. The ID is then a UUID v4 derived by seeding the UUID generator
|
|
14
|
+
* with the first 16 bytes of the SHA-256 hash of the normalized address
|
|
15
|
+
* bytes.
|
|
16
|
+
*
|
|
17
|
+
* @param address - The Ethereum address (hex string) to generate the account
|
|
18
|
+
* ID from.
|
|
19
|
+
* @returns A deterministic UUID v4 string to use as the account ID.
|
|
20
|
+
*/
|
|
21
|
+
function generateEthAccountId(address) {
|
|
22
|
+
const normalized = (0, eth_sig_util_1.normalize)(address);
|
|
23
|
+
return (0, uuid_1.v4)({
|
|
24
|
+
random: (0, sha256_1.sha256)((0, utils_1.hexToBytes)(normalized)).slice(0, 16),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=account-id.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-id.cjs","sourceRoot":"","sources":["../../src/eth/account-id.ts"],"names":[],"mappings":";;AAmBA,oDAKC;AAxBD,yDAAmD;AAEnD,2CAA6C;AAC7C,yDAAsD;AACtD,+BAAoC;AAEpC;;;;;;;;;;;;GAYG;AACH,SAAgB,oBAAoB,CAAC,OAAe;IAClD,MAAM,UAAU,GAAG,IAAA,wBAAS,EAAC,OAAO,CAAW,CAAC;IAChD,OAAO,IAAA,SAAM,EAAC;QACZ,MAAM,EAAE,IAAA,eAAM,EAAC,IAAA,kBAAU,EAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC;KACpD,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { normalize } from '@metamask/eth-sig-util';\nimport type { AccountId } from '@metamask/keyring-utils';\nimport { hexToBytes } from '@metamask/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport { v4 as uuidv4 } from 'uuid';\n\n/**\n * Generates a deterministic account ID for a given Ethereum address.\n *\n * The address is first normalized (lowercased, `0x`-prefixed) so that\n * checksum-cased and lowercase variants of the same address produce the\n * same ID. The ID is then a UUID v4 derived by seeding the UUID generator\n * with the first 16 bytes of the SHA-256 hash of the normalized address\n * bytes.\n *\n * @param address - The Ethereum address (hex string) to generate the account\n * ID from.\n * @returns A deterministic UUID v4 string to use as the account ID.\n */\nexport function generateEthAccountId(address: string): AccountId {\n const normalized = normalize(address) as string;\n return uuidv4({\n random: sha256(hexToBytes(normalized)).slice(0, 16),\n });\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AccountId } from "@metamask/keyring-utils";
|
|
2
|
+
/**
|
|
3
|
+
* Generates a deterministic account ID for a given Ethereum address.
|
|
4
|
+
*
|
|
5
|
+
* The address is first normalized (lowercased, `0x`-prefixed) so that
|
|
6
|
+
* checksum-cased and lowercase variants of the same address produce the
|
|
7
|
+
* same ID. The ID is then a UUID v4 derived by seeding the UUID generator
|
|
8
|
+
* with the first 16 bytes of the SHA-256 hash of the normalized address
|
|
9
|
+
* bytes.
|
|
10
|
+
*
|
|
11
|
+
* @param address - The Ethereum address (hex string) to generate the account
|
|
12
|
+
* ID from.
|
|
13
|
+
* @returns A deterministic UUID v4 string to use as the account ID.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateEthAccountId(address: string): AccountId;
|
|
16
|
+
//# sourceMappingURL=account-id.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-id.d.cts","sourceRoot":"","sources":["../../src/eth/account-id.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,gCAAgC;AAKzD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAK/D"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AccountId } from "@metamask/keyring-utils";
|
|
2
|
+
/**
|
|
3
|
+
* Generates a deterministic account ID for a given Ethereum address.
|
|
4
|
+
*
|
|
5
|
+
* The address is first normalized (lowercased, `0x`-prefixed) so that
|
|
6
|
+
* checksum-cased and lowercase variants of the same address produce the
|
|
7
|
+
* same ID. The ID is then a UUID v4 derived by seeding the UUID generator
|
|
8
|
+
* with the first 16 bytes of the SHA-256 hash of the normalized address
|
|
9
|
+
* bytes.
|
|
10
|
+
*
|
|
11
|
+
* @param address - The Ethereum address (hex string) to generate the account
|
|
12
|
+
* ID from.
|
|
13
|
+
* @returns A deterministic UUID v4 string to use as the account ID.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateEthAccountId(address: string): AccountId;
|
|
16
|
+
//# sourceMappingURL=account-id.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-id.d.mts","sourceRoot":"","sources":["../../src/eth/account-id.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,gCAAgC;AAKzD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAK/D"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { normalize } from "@metamask/eth-sig-util";
|
|
2
|
+
import { hexToBytes } from "@metamask/utils";
|
|
3
|
+
import { sha256 } from "ethereum-cryptography/sha256";
|
|
4
|
+
import { v4 as uuidv4 } from "uuid";
|
|
5
|
+
/**
|
|
6
|
+
* Generates a deterministic account ID for a given Ethereum address.
|
|
7
|
+
*
|
|
8
|
+
* The address is first normalized (lowercased, `0x`-prefixed) so that
|
|
9
|
+
* checksum-cased and lowercase variants of the same address produce the
|
|
10
|
+
* same ID. The ID is then a UUID v4 derived by seeding the UUID generator
|
|
11
|
+
* with the first 16 bytes of the SHA-256 hash of the normalized address
|
|
12
|
+
* bytes.
|
|
13
|
+
*
|
|
14
|
+
* @param address - The Ethereum address (hex string) to generate the account
|
|
15
|
+
* ID from.
|
|
16
|
+
* @returns A deterministic UUID v4 string to use as the account ID.
|
|
17
|
+
*/
|
|
18
|
+
export function generateEthAccountId(address) {
|
|
19
|
+
const normalized = normalize(address);
|
|
20
|
+
return uuidv4({
|
|
21
|
+
random: sha256(hexToBytes(normalized)).slice(0, 16),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=account-id.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-id.mjs","sourceRoot":"","sources":["../../src/eth/account-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,+BAA+B;AAEnD,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAC7C,OAAO,EAAE,MAAM,EAAE,qCAAqC;AACtD,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAEpC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAe;IAClD,MAAM,UAAU,GAAG,SAAS,CAAC,OAAO,CAAW,CAAC;IAChD,OAAO,MAAM,CAAC;QACZ,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC;KACpD,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { normalize } from '@metamask/eth-sig-util';\nimport type { AccountId } from '@metamask/keyring-utils';\nimport { hexToBytes } from '@metamask/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport { v4 as uuidv4 } from 'uuid';\n\n/**\n * Generates a deterministic account ID for a given Ethereum address.\n *\n * The address is first normalized (lowercased, `0x`-prefixed) so that\n * checksum-cased and lowercase variants of the same address produce the\n * same ID. The ID is then a UUID v4 derived by seeding the UUID generator\n * with the first 16 bytes of the SHA-256 hash of the normalized address\n * bytes.\n *\n * @param address - The Ethereum address (hex string) to generate the account\n * ID from.\n * @returns A deterministic UUID v4 string to use as the account ID.\n */\nexport function generateEthAccountId(address: string): AccountId {\n const normalized = normalize(address) as string;\n return uuidv4({\n random: sha256(hexToBytes(normalized)).slice(0, 16),\n });\n}\n"]}
|
|
@@ -6,6 +6,7 @@ const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
|
6
6
|
const keyring_api_1 = require("@metamask/keyring-api");
|
|
7
7
|
const superstruct_1 = require("@metamask/superstruct");
|
|
8
8
|
const utils_1 = require("@metamask/utils");
|
|
9
|
+
const account_id_1 = require("./account-id.cjs");
|
|
9
10
|
const keyring_wrapper_1 = require("../keyring-wrapper.cjs");
|
|
10
11
|
/**
|
|
11
12
|
* Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.
|
|
@@ -32,6 +33,14 @@ var EthKeyringMethod;
|
|
|
32
33
|
* - `exportAccount()` (optional): Export private key in specified format
|
|
33
34
|
*/
|
|
34
35
|
class EthKeyringWrapper extends keyring_wrapper_1.KeyringWrapper {
|
|
36
|
+
constructor(options) {
|
|
37
|
+
super({
|
|
38
|
+
...options,
|
|
39
|
+
// We use a custom ID generator to ensure that account IDs are deterministic based on
|
|
40
|
+
// the Ethereum address.
|
|
41
|
+
registryOptions: { generateId: account_id_1.generateEthAccountId },
|
|
42
|
+
});
|
|
43
|
+
}
|
|
35
44
|
/**
|
|
36
45
|
* Helper method to safely cast a KeyringAccount address to Hex type.
|
|
37
46
|
* The KeyringAccount.address is typed as string, but for Ethereum accounts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth-keyring-wrapper.cjs","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":";;;AAAA,uCAAsE;AAEtE,yDAA8D;AAE9D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,CAAC;oBACzC,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;gBACvE,CAAC;gBACD,IAAA,oBAAM,EAAC,MAAM,EAAE,qDAAuC,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 { KeyringAccount, KeyringRequest } from '@metamask/keyring-api';\nimport {\n EthDecryptParamsStruct,\n EthGetAppKeyAddressParamsStruct,\n EthGetEncryptionPublicKeyParamsStruct,\n EthPersonalSignParamsStruct,\n EthSignEip7702AuthorizationParamsStruct,\n EthSignParamsStruct,\n EthSignTransactionParamsStruct,\n EthSignTypedDataParamsStruct,\n EthSignTypedDataV1ParamsStruct,\n EthMethod,\n} from '@metamask/keyring-api';\nimport type { EthKeyring } from '@metamask/keyring-utils';\nimport { assert } from '@metamask/superstruct';\nimport { add0x, type Hex, type Json } from '@metamask/utils';\n\nimport { KeyringWrapper, type KeyringWrapperOptions } from '../keyring-wrapper';\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"]}
|
|
1
|
+
{"version":3,"file":"eth-keyring-wrapper.cjs","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":";;;AAAA,uCAAsE;AAEtE,yDAA8D;AAE9D,uDAW+B;AAE/B,uDAA+C;AAC/C,2CAA6D;AAE7D,iDAAoD;AACpD,4DAAgF;AAEhF;;;GAGG;AACH,IAAY,gBAKX;AALD,WAAY,gBAAgB;IAC1B,2CAAuB,CAAA;IACvB,yEAAqD,CAAA;IACrD,6DAAyC,CAAA;IACzC,6EAAyD,CAAA;AAC3D,CAAC,EALW,gBAAgB,gCAAhB,gBAAgB,QAK3B;AAcD;;;;;;;;;;;;GAYG;AACH,MAAsB,iBAGpB,SAAQ,gCAAgD;IACxD,YAAY,OAA+C;QACzD,KAAK,CAAC;YACJ,GAAG,OAAO;YACV,qFAAqF;YACrF,wBAAwB;YACxB,eAAe,EAAE,EAAE,UAAU,EAAE,iCAAoB,EAAE;SACtD,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;OAOG;IACO,YAAY,CAAC,OAAe;QACpC,OAAO,IAAA,aAAK,EAAC,OAAO,CAAC,CAAC;IACxB,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,aAAa,CAAC,OAAuB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,KAAK,CAAC,iDAAiD,CAAC,CAAC;gBACrE,CAAC;gBACD,IAAA,oBAAM,EAAC,MAAM,EAAE,mDAAqC,CAAC,CAAC;gBACtD,MAAM,CAAC,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;gBAC3B,OAAO,IAAI,CAAC,KAAK,CAAC,sBAAsB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;YAChE,CAAC;YAED,KAAK,GAAG,gBAAgB,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;gBAC5C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,gBAAgB,EAAE,CAAC;oBACjC,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;gBAC/D,CAAC;gBACD,IAAA,oBAAM,EAAC,MAAM,EAAE,6CAA+B,CAAC,CAAC;gBAChD,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;gBACxB,OAAO,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;YACzD,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,qDAAuC,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;AArKD,8CAqKC","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 { KeyringAccount, KeyringRequest } from '@metamask/keyring-api';\nimport {\n EthDecryptParamsStruct,\n EthGetAppKeyAddressParamsStruct,\n EthGetEncryptionPublicKeyParamsStruct,\n EthPersonalSignParamsStruct,\n EthSignEip7702AuthorizationParamsStruct,\n EthSignParamsStruct,\n EthSignTransactionParamsStruct,\n EthSignTypedDataParamsStruct,\n EthSignTypedDataV1ParamsStruct,\n EthMethod,\n} from '@metamask/keyring-api';\nimport type { EthKeyring } from '@metamask/keyring-utils';\nimport { assert } from '@metamask/superstruct';\nimport { add0x, type Hex, type Json } from '@metamask/utils';\n\nimport { generateEthAccountId } from './account-id';\nimport { KeyringWrapper, type KeyringWrapperOptions } from '../keyring-wrapper';\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 {@link EthKeyringWrapper}.\n *\n * `registryOptions` is intentionally omitted: the registry is always\n * configured with {@link generateEthAccountId} to produce deterministic\n * account IDs for Ethereum addresses.\n */\nexport type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = Omit<\n KeyringWrapperOptions<InnerKeyring>,\n 'registryOptions'\n>;\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 constructor(options: EthKeyringWrapperOptions<InnerKeyring>) {\n super({\n ...options,\n // We use a custom ID generator to ensure that account IDs are deterministic based on\n // the Ethereum address.\n registryOptions: { generateId: generateEthAccountId },\n });\n }\n\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"]}
|
|
@@ -13,9 +13,13 @@ export declare enum EthKeyringMethod {
|
|
|
13
13
|
SignEip7702Authorization = "eth_signEip7702Authorization"
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* Options for constructing an EthKeyringWrapper.
|
|
16
|
+
* Options for constructing an {@link EthKeyringWrapper}.
|
|
17
|
+
*
|
|
18
|
+
* `registryOptions` is intentionally omitted: the registry is always
|
|
19
|
+
* configured with {@link generateEthAccountId} to produce deterministic
|
|
20
|
+
* account IDs for Ethereum addresses.
|
|
17
21
|
*/
|
|
18
|
-
export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = KeyringWrapperOptions<InnerKeyring>;
|
|
22
|
+
export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = Omit<KeyringWrapperOptions<InnerKeyring>, 'registryOptions'>;
|
|
19
23
|
/**
|
|
20
24
|
* Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.
|
|
21
25
|
*
|
|
@@ -30,6 +34,7 @@ export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = KeyringW
|
|
|
30
34
|
* - `exportAccount()` (optional): Export private key in specified format
|
|
31
35
|
*/
|
|
32
36
|
export declare abstract class EthKeyringWrapper<InnerKeyring extends EthKeyring, KeyringAccountType extends KeyringAccount = KeyringAccount> extends KeyringWrapper<InnerKeyring, KeyringAccountType> {
|
|
37
|
+
constructor(options: EthKeyringWrapperOptions<InnerKeyring>);
|
|
33
38
|
/**
|
|
34
39
|
* Helper method to safely cast a KeyringAccount address to Hex type.
|
|
35
40
|
* The KeyringAccount.address is typed as string, but for Ethereum accounts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth-keyring-wrapper.d.cts","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,8BAA8B;AAa5E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAE1D,OAAO,EAAS,KAAK,GAAG,EAAE,KAAK,IAAI,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"eth-keyring-wrapper.d.cts","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,8BAA8B;AAa5E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAE1D,OAAO,EAAS,KAAK,GAAG,EAAE,KAAK,IAAI,EAAE,wBAAwB;AAG7D,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,+BAA2B;AAEhF;;;GAGG;AACH,oBAAY,gBAAgB;IAC1B,OAAO,gBAAgB;IACvB,sBAAsB,+BAA+B;IACrD,gBAAgB,yBAAyB;IACzC,wBAAwB,iCAAiC;CAC1D;AAED;;;;;;GAMG;AACH,MAAM,MAAM,wBAAwB,CAAC,YAAY,SAAS,UAAU,IAAI,IAAI,CAC1E,qBAAqB,CAAC,YAAY,CAAC,EACnC,iBAAiB,CAClB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,8BAAsB,iBAAiB,CACrC,YAAY,SAAS,UAAU,EAC/B,kBAAkB,SAAS,cAAc,GAAG,cAAc,CAC1D,SAAQ,cAAc,CAAC,YAAY,EAAE,kBAAkB,CAAC;gBAC5C,OAAO,EAAE,wBAAwB,CAAC,YAAY,CAAC;IAS3D;;;;;;;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"}
|
|
@@ -13,9 +13,13 @@ export declare enum EthKeyringMethod {
|
|
|
13
13
|
SignEip7702Authorization = "eth_signEip7702Authorization"
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* Options for constructing an EthKeyringWrapper.
|
|
16
|
+
* Options for constructing an {@link EthKeyringWrapper}.
|
|
17
|
+
*
|
|
18
|
+
* `registryOptions` is intentionally omitted: the registry is always
|
|
19
|
+
* configured with {@link generateEthAccountId} to produce deterministic
|
|
20
|
+
* account IDs for Ethereum addresses.
|
|
17
21
|
*/
|
|
18
|
-
export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = KeyringWrapperOptions<InnerKeyring>;
|
|
22
|
+
export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = Omit<KeyringWrapperOptions<InnerKeyring>, 'registryOptions'>;
|
|
19
23
|
/**
|
|
20
24
|
* Abstract wrapper for Ethereum-based keyrings that extends KeyringWrapper, that itself implements KeyringV2.
|
|
21
25
|
*
|
|
@@ -30,6 +34,7 @@ export type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = KeyringW
|
|
|
30
34
|
* - `exportAccount()` (optional): Export private key in specified format
|
|
31
35
|
*/
|
|
32
36
|
export declare abstract class EthKeyringWrapper<InnerKeyring extends EthKeyring, KeyringAccountType extends KeyringAccount = KeyringAccount> extends KeyringWrapper<InnerKeyring, KeyringAccountType> {
|
|
37
|
+
constructor(options: EthKeyringWrapperOptions<InnerKeyring>);
|
|
33
38
|
/**
|
|
34
39
|
* Helper method to safely cast a KeyringAccount address to Hex type.
|
|
35
40
|
* The KeyringAccount.address is typed as string, but for Ethereum accounts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth-keyring-wrapper.d.mts","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,8BAA8B;AAa5E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAE1D,OAAO,EAAS,KAAK,GAAG,EAAE,KAAK,IAAI,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"eth-keyring-wrapper.d.mts","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,8BAA8B;AAa5E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAE1D,OAAO,EAAS,KAAK,GAAG,EAAE,KAAK,IAAI,EAAE,wBAAwB;AAG7D,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,+BAA2B;AAEhF;;;GAGG;AACH,oBAAY,gBAAgB;IAC1B,OAAO,gBAAgB;IACvB,sBAAsB,+BAA+B;IACrD,gBAAgB,yBAAyB;IACzC,wBAAwB,iCAAiC;CAC1D;AAED;;;;;;GAMG;AACH,MAAM,MAAM,wBAAwB,CAAC,YAAY,SAAS,UAAU,IAAI,IAAI,CAC1E,qBAAqB,CAAC,YAAY,CAAC,EACnC,iBAAiB,CAClB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,8BAAsB,iBAAiB,CACrC,YAAY,SAAS,UAAU,EAC/B,kBAAkB,SAAS,cAAc,GAAG,cAAc,CAC1D,SAAQ,cAAc,CAAC,YAAY,EAAE,kBAAkB,CAAC;gBAC5C,OAAO,EAAE,wBAAwB,CAAC,YAAY,CAAC;IAS3D;;;;;;;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"}
|
|
@@ -3,6 +3,7 @@ import { SignTypedDataVersion } from "@metamask/eth-sig-util";
|
|
|
3
3
|
import { EthDecryptParamsStruct, EthGetAppKeyAddressParamsStruct, EthGetEncryptionPublicKeyParamsStruct, EthPersonalSignParamsStruct, EthSignEip7702AuthorizationParamsStruct, EthSignParamsStruct, EthSignTransactionParamsStruct, EthSignTypedDataParamsStruct, EthSignTypedDataV1ParamsStruct, EthMethod } from "@metamask/keyring-api";
|
|
4
4
|
import { assert } from "@metamask/superstruct";
|
|
5
5
|
import { add0x } from "@metamask/utils";
|
|
6
|
+
import { generateEthAccountId } from "./account-id.mjs";
|
|
6
7
|
import { KeyringWrapper } from "../keyring-wrapper.mjs";
|
|
7
8
|
/**
|
|
8
9
|
* Additional Ethereum methods supported by Eth keyrings that are not in the standard EthMethod enum.
|
|
@@ -29,6 +30,14 @@ export var EthKeyringMethod;
|
|
|
29
30
|
* - `exportAccount()` (optional): Export private key in specified format
|
|
30
31
|
*/
|
|
31
32
|
export class EthKeyringWrapper extends KeyringWrapper {
|
|
33
|
+
constructor(options) {
|
|
34
|
+
super({
|
|
35
|
+
...options,
|
|
36
|
+
// We use a custom ID generator to ensure that account IDs are deterministic based on
|
|
37
|
+
// the Ethereum address.
|
|
38
|
+
registryOptions: { generateId: generateEthAccountId },
|
|
39
|
+
});
|
|
40
|
+
}
|
|
32
41
|
/**
|
|
33
42
|
* Helper method to safely cast a KeyringAccount address to Hex type.
|
|
34
43
|
* The KeyringAccount.address is typed as string, but for Ethereum accounts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth-keyring-wrapper.mjs","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAoB,uBAAuB;AAEtE,OAAO,EAAE,oBAAoB,EAAE,+BAA+B;AAE9D,OAAO,EACL,sBAAsB,EACtB,+BAA+B,EAC/B,qCAAqC,EACrC,2BAA2B,EAC3B,uCAAuC,EACvC,mBAAmB,EACnB,8BAA8B,EAC9B,4BAA4B,EAC5B,8BAA8B,EAC9B,SAAS,EACV,8BAA8B;AAE/B,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C;oBACzC,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;gBACvE,CAAC;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 { KeyringAccount, KeyringRequest } from '@metamask/keyring-api';\nimport {\n EthDecryptParamsStruct,\n EthGetAppKeyAddressParamsStruct,\n EthGetEncryptionPublicKeyParamsStruct,\n EthPersonalSignParamsStruct,\n EthSignEip7702AuthorizationParamsStruct,\n EthSignParamsStruct,\n EthSignTransactionParamsStruct,\n EthSignTypedDataParamsStruct,\n EthSignTypedDataV1ParamsStruct,\n EthMethod,\n} from '@metamask/keyring-api';\nimport type { EthKeyring } from '@metamask/keyring-utils';\nimport { assert } from '@metamask/superstruct';\nimport { add0x, type Hex, type Json } from '@metamask/utils';\n\nimport { KeyringWrapper, type KeyringWrapperOptions } from '../keyring-wrapper';\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"]}
|
|
1
|
+
{"version":3,"file":"eth-keyring-wrapper.mjs","sourceRoot":"","sources":["../../src/eth/eth-keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAoB,uBAAuB;AAEtE,OAAO,EAAE,oBAAoB,EAAE,+BAA+B;AAE9D,OAAO,EACL,sBAAsB,EACtB,+BAA+B,EAC/B,qCAAqC,EACrC,2BAA2B,EAC3B,uCAAuC,EACvC,mBAAmB,EACnB,8BAA8B,EAC9B,4BAA4B,EAC5B,8BAA8B,EAC9B,SAAS,EACV,8BAA8B;AAE/B,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAC/C,OAAO,EAAE,KAAK,EAAuB,wBAAwB;AAE7D,OAAO,EAAE,oBAAoB,EAAE,yBAAqB;AACpD,OAAO,EAAE,cAAc,EAA8B,+BAA2B;AAEhF;;;GAGG;AACH,MAAM,CAAN,IAAY,gBAKX;AALD,WAAY,gBAAgB;IAC1B,2CAAuB,CAAA;IACvB,yEAAqD,CAAA;IACrD,6DAAyC,CAAA;IACzC,6EAAyD,CAAA;AAC3D,CAAC,EALW,gBAAgB,KAAhB,gBAAgB,QAK3B;AAcD;;;;;;;;;;;;GAYG;AACH,MAAM,OAAgB,iBAGpB,SAAQ,cAAgD;IACxD,YAAY,OAA+C;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a,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 { KeyringAccount, KeyringRequest } from '@metamask/keyring-api';\nimport {\n EthDecryptParamsStruct,\n EthGetAppKeyAddressParamsStruct,\n EthGetEncryptionPublicKeyParamsStruct,\n EthPersonalSignParamsStruct,\n EthSignEip7702AuthorizationParamsStruct,\n EthSignParamsStruct,\n EthSignTransactionParamsStruct,\n EthSignTypedDataParamsStruct,\n EthSignTypedDataV1ParamsStruct,\n EthMethod,\n} from '@metamask/keyring-api';\nimport type { EthKeyring } from '@metamask/keyring-utils';\nimport { assert } from '@metamask/superstruct';\nimport { add0x, type Hex, type Json } from '@metamask/utils';\n\nimport { generateEthAccountId } from './account-id';\nimport { KeyringWrapper, type KeyringWrapperOptions } from '../keyring-wrapper';\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 {@link EthKeyringWrapper}.\n *\n * `registryOptions` is intentionally omitted: the registry is always\n * configured with {@link generateEthAccountId} to produce deterministic\n * account IDs for Ethereum addresses.\n */\nexport type EthKeyringWrapperOptions<InnerKeyring extends EthKeyring> = Omit<\n KeyringWrapperOptions<InnerKeyring>,\n 'registryOptions'\n>;\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 constructor(options: EthKeyringWrapperOptions<InnerKeyring>) {\n super({\n ...options,\n // We use a custom ID generator to ensure that account IDs are deterministic based on\n // the Ethereum address.\n registryOptions: { generateId: generateEthAccountId },\n });\n }\n\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"]}
|
package/dist/eth/index.cjs
CHANGED
|
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./account-id.cjs"), exports);
|
|
17
18
|
__exportStar(require("./eth-keyring-wrapper.cjs"), exports);
|
|
18
19
|
//# sourceMappingURL=index.cjs.map
|
package/dist/eth/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAAsC","sourcesContent":["export * from './eth-keyring-wrapper';\n"]}
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAA6B;AAC7B,4DAAsC","sourcesContent":["export * from './account-id';\nexport * from './eth-keyring-wrapper';\n"]}
|
package/dist/eth/index.d.cts
CHANGED
package/dist/eth/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,0CAAsC"}
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,0CAAsC"}
|
package/dist/eth/index.d.mts
CHANGED
package/dist/eth/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,0CAAsC"}
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,0CAAsC"}
|
package/dist/eth/index.mjs
CHANGED
package/dist/eth/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,0CAAsC","sourcesContent":["export * from './eth-keyring-wrapper';\n"]}
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,0CAAsC","sourcesContent":["export * from './account-id';\nexport * from './eth-keyring-wrapper';\n"]}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
2
8
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
9
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
12
|
};
|
|
7
|
-
var _KeyringAccountRegistry_accountById, _KeyringAccountRegistry_idByAddress;
|
|
13
|
+
var _KeyringAccountRegistry_accountById, _KeyringAccountRegistry_idByAddress, _KeyringAccountRegistry_generateId;
|
|
8
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
15
|
exports.KeyringAccountRegistry = void 0;
|
|
10
16
|
const uuid_1 = require("uuid");
|
|
@@ -15,9 +21,17 @@ const uuid_1 = require("uuid");
|
|
|
15
21
|
* KeyringAccount objects for efficient retrieval.
|
|
16
22
|
*/
|
|
17
23
|
class KeyringAccountRegistry {
|
|
18
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Create a new KeyringAccountRegistry.
|
|
26
|
+
*
|
|
27
|
+
* @param options - Optional configuration.
|
|
28
|
+
* @param options.generateId - Custom ID generator. Defaults to `uuidv4`.
|
|
29
|
+
*/
|
|
30
|
+
constructor(options) {
|
|
19
31
|
_KeyringAccountRegistry_accountById.set(this, new Map());
|
|
20
32
|
_KeyringAccountRegistry_idByAddress.set(this, new Map());
|
|
33
|
+
_KeyringAccountRegistry_generateId.set(this, void 0);
|
|
34
|
+
__classPrivateFieldSet(this, _KeyringAccountRegistry_generateId, options?.generateId ?? (() => (0, uuid_1.v4)()), "f");
|
|
21
35
|
}
|
|
22
36
|
/**
|
|
23
37
|
* Get an account by its account ID.
|
|
@@ -58,7 +72,7 @@ class KeyringAccountRegistry {
|
|
|
58
72
|
if (existing) {
|
|
59
73
|
return existing;
|
|
60
74
|
}
|
|
61
|
-
const id = (
|
|
75
|
+
const id = __classPrivateFieldGet(this, _KeyringAccountRegistry_generateId, "f").call(this, address);
|
|
62
76
|
__classPrivateFieldGet(this, _KeyringAccountRegistry_idByAddress, "f").set(address, id);
|
|
63
77
|
return id;
|
|
64
78
|
}
|
|
@@ -131,5 +145,5 @@ class KeyringAccountRegistry {
|
|
|
131
145
|
}
|
|
132
146
|
}
|
|
133
147
|
exports.KeyringAccountRegistry = KeyringAccountRegistry;
|
|
134
|
-
_KeyringAccountRegistry_accountById = new WeakMap(), _KeyringAccountRegistry_idByAddress = new WeakMap();
|
|
148
|
+
_KeyringAccountRegistry_accountById = new WeakMap(), _KeyringAccountRegistry_idByAddress = new WeakMap(), _KeyringAccountRegistry_generateId = new WeakMap();
|
|
135
149
|
//# sourceMappingURL=keyring-account-registry.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-account-registry.cjs","sourceRoot":"","sources":["../src/keyring-account-registry.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"keyring-account-registry.cjs","sourceRoot":"","sources":["../src/keyring-account-registry.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAEA,+BAAoC;AAepC;;;;;GAKG;AACH,MAAa,sBAAsB;IASjC;;;;;OAKG;IACH,YAAY,OAAuC;QAZ1C,8CAAe,IAAI,GAAG,EAAiC,EAAC;QAExD,8CAAe,IAAI,GAAG,EAAqB,EAAC;QAE5C,qDAA4C;QASnD,uBAAA,IAAI,sCAAe,OAAO,EAAE,UAAU,IAAI,CAAC,GAAc,EAAE,CAAC,IAAA,SAAM,GAAE,CAAC,MAAA,CAAC;IACxE,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,SAAoB;QACtB,OAAO,uBAAA,IAAI,2CAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,SAAoB;QAC7B,OAAO,uBAAA,IAAI,2CAAa,CAAC,GAAG,CAAC,SAAS,CAAC,EAA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IG;IACH,MAAM;QACJ,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,2CAAa,CAAC,MAAM,EAAE,CAAC,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,2CAAa,CAAC,IAAI,EAAE,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,SAAoB;QACtB,OAAO,uBAAA,IAAI,2CAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,2CAAa,CAAC,IAAI,CAAC;IAChC,CAAC;CACF;AA3ID,wDA2IC","sourcesContent":["import type { KeyringAccount } from '@metamask/keyring-api';\nimport type { AccountId } from '@metamask/keyring-utils';\nimport { v4 as uuidv4 } from 'uuid';\n\nexport type KeyringAccountRegistryOptions = {\n /**\n * Custom ID generator invoked when registering a new address.\n *\n * Receives the address being registered and must return a unique account ID.\n * Defaults to a random UUID v4 when not provided.\n *\n * @param address - The address for which to generate an account ID.\n * @returns A unique account ID string.\n */\n generateId?: (address: string) => AccountId;\n};\n\n/**\n * In-memory registry for KeyringAccount objects.\n *\n * Provides O(1) lookups by account ID or address, and stores the full\n * KeyringAccount objects for efficient retrieval.\n */\nexport class KeyringAccountRegistry<\n KeyringAccountType extends KeyringAccount = KeyringAccount,\n> {\n readonly #accountById = new Map<AccountId, KeyringAccountType>();\n\n readonly #idByAddress = new Map<string, AccountId>();\n\n readonly #generateId: (address: string) => AccountId;\n\n /**\n * Create a new KeyringAccountRegistry.\n *\n * @param options - Optional configuration.\n * @param options.generateId - Custom ID generator. Defaults to `uuidv4`.\n */\n constructor(options?: KeyringAccountRegistryOptions) {\n this.#generateId = options?.generateId ?? ((): AccountId => uuidv4());\n }\n\n /**\n * Get an account by its account ID.\n *\n * @param accountId - The account ID to look up.\n * @returns The KeyringAccount, or undefined if not found.\n */\n get(accountId: AccountId): KeyringAccountType | undefined {\n return this.#accountById.get(accountId);\n }\n\n /**\n * Get an account's address by its account ID.\n *\n * @param accountId - The account ID to look up.\n * @returns The address, or undefined if not found.\n */\n getAddress(accountId: AccountId): string | undefined {\n return this.#accountById.get(accountId)?.address;\n }\n\n /**\n * Get an account ID by the underlying address.\n *\n * @param address - The address to look up.\n * @returns The account ID, or undefined if not found.\n */\n getAccountId(address: string): AccountId | undefined {\n return this.#idByAddress.get(address);\n }\n\n /**\n * Register a new address and generate an account ID for it.\n * If the address is already registered, returns the existing account ID.\n *\n * @param address - The address to register.\n * @returns The account ID for this address.\n */\n register(address: string): AccountId {\n const existing = this.#idByAddress.get(address);\n if (existing) {\n return existing;\n }\n const id = this.#generateId(address);\n this.#idByAddress.set(address, id);\n return id;\n }\n\n /**\n * Add an account to the registry.\n * Also registers the address → account ID mapping.\n *\n * ⚠️ If an address was previously registered via `register()` with a\n * different account ID, calling `set()` with an account that has a new ID for\n * the same address will overwrite the address → ID mapping. The old ID\n * becomes \"dangling\" (i.e., `get(oldId)` will return undefined).\n *\n * @param account - The KeyringAccount to cache.\n */\n set(account: KeyringAccountType): void {\n this.#accountById.set(account.id, account);\n this.#idByAddress.set(account.address, account.id);\n }\n\n /**\n * Remove an account from the registry.\n *\n * @param accountId - The account ID to remove.\n */\n delete(accountId: AccountId): void {\n const account = this.#accountById.get(accountId);\n if (account) {\n this.#idByAddress.delete(account.address);\n this.#accountById.delete(accountId);\n }\n }\n\n /**\n * Clear all accounts from the registry.\n */\n clear(): void {\n this.#accountById.clear();\n this.#idByAddress.clear();\n }\n\n /**\n * Get all cached accounts as an array.\n *\n * @returns Array of all KeyringAccountType objects in the registry.\n */\n values(): KeyringAccountType[] {\n return Array.from(this.#accountById.values());\n }\n\n /**\n * Get all account IDs in the registry.\n *\n * @returns Array of all account IDs.\n */\n keys(): AccountId[] {\n return Array.from(this.#accountById.keys());\n }\n\n /**\n * Check if an account exists in the registry.\n *\n * @param accountId - The account ID to check.\n * @returns True if the account exists.\n */\n has(accountId: AccountId): boolean {\n return this.#accountById.has(accountId);\n }\n\n /**\n * Get the number of accounts in the registry.\n *\n * @returns The number of accounts.\n */\n get size(): number {\n return this.#accountById.size;\n }\n}\n"]}
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import type { KeyringAccount } from "@metamask/keyring-api";
|
|
2
2
|
import type { AccountId } from "@metamask/keyring-utils";
|
|
3
|
+
export type KeyringAccountRegistryOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Custom ID generator invoked when registering a new address.
|
|
6
|
+
*
|
|
7
|
+
* Receives the address being registered and must return a unique account ID.
|
|
8
|
+
* Defaults to a random UUID v4 when not provided.
|
|
9
|
+
*
|
|
10
|
+
* @param address - The address for which to generate an account ID.
|
|
11
|
+
* @returns A unique account ID string.
|
|
12
|
+
*/
|
|
13
|
+
generateId?: (address: string) => AccountId;
|
|
14
|
+
};
|
|
3
15
|
/**
|
|
4
16
|
* In-memory registry for KeyringAccount objects.
|
|
5
17
|
*
|
|
@@ -8,6 +20,13 @@ import type { AccountId } from "@metamask/keyring-utils";
|
|
|
8
20
|
*/
|
|
9
21
|
export declare class KeyringAccountRegistry<KeyringAccountType extends KeyringAccount = KeyringAccount> {
|
|
10
22
|
#private;
|
|
23
|
+
/**
|
|
24
|
+
* Create a new KeyringAccountRegistry.
|
|
25
|
+
*
|
|
26
|
+
* @param options - Optional configuration.
|
|
27
|
+
* @param options.generateId - Custom ID generator. Defaults to `uuidv4`.
|
|
28
|
+
*/
|
|
29
|
+
constructor(options?: KeyringAccountRegistryOptions);
|
|
11
30
|
/**
|
|
12
31
|
* Get an account by its account ID.
|
|
13
32
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-account-registry.d.cts","sourceRoot":"","sources":["../src/keyring-account-registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA8B;AAC5D,OAAO,KAAK,EAAE,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|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import type { KeyringAccount } from "@metamask/keyring-api";
|
|
2
2
|
import type { AccountId } from "@metamask/keyring-utils";
|
|
3
|
+
export type KeyringAccountRegistryOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Custom ID generator invoked when registering a new address.
|
|
6
|
+
*
|
|
7
|
+
* Receives the address being registered and must return a unique account ID.
|
|
8
|
+
* Defaults to a random UUID v4 when not provided.
|
|
9
|
+
*
|
|
10
|
+
* @param address - The address for which to generate an account ID.
|
|
11
|
+
* @returns A unique account ID string.
|
|
12
|
+
*/
|
|
13
|
+
generateId?: (address: string) => AccountId;
|
|
14
|
+
};
|
|
3
15
|
/**
|
|
4
16
|
* In-memory registry for KeyringAccount objects.
|
|
5
17
|
*
|
|
@@ -8,6 +20,13 @@ import type { AccountId } from "@metamask/keyring-utils";
|
|
|
8
20
|
*/
|
|
9
21
|
export declare class KeyringAccountRegistry<KeyringAccountType extends KeyringAccount = KeyringAccount> {
|
|
10
22
|
#private;
|
|
23
|
+
/**
|
|
24
|
+
* Create a new KeyringAccountRegistry.
|
|
25
|
+
*
|
|
26
|
+
* @param options - Optional configuration.
|
|
27
|
+
* @param options.generateId - Custom ID generator. Defaults to `uuidv4`.
|
|
28
|
+
*/
|
|
29
|
+
constructor(options?: KeyringAccountRegistryOptions);
|
|
11
30
|
/**
|
|
12
31
|
* Get an account by its account ID.
|
|
13
32
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-account-registry.d.mts","sourceRoot":"","sources":["../src/keyring-account-registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA8B;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,gCAAgC;AAGzD;;;;;GAKG;AACH,qBAAa,sBAAsB,CACjC,kBAAkB,SAAS,cAAc,GAAG,cAAc;;
|
|
1
|
+
{"version":3,"file":"keyring-account-registry.d.mts","sourceRoot":"","sources":["../src/keyring-account-registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA8B;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,gCAAgC;AAGzD,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;;;;;;OAQG;IACH,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,SAAS,CAAC;CAC7C,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,sBAAsB,CACjC,kBAAkB,SAAS,cAAc,GAAG,cAAc;;IAQ1D;;;;;OAKG;gBACS,OAAO,CAAC,EAAE,6BAA6B;IAInD;;;;;OAKG;IACH,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,kBAAkB,GAAG,SAAS;IAIzD;;;;;OAKG;IACH,UAAU,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM,GAAG,SAAS;IAIpD;;;;;OAKG;IACH,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;IAIpD;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS;IAUpC;;;;;;;;;;OAUG;IACH,GAAG,CAAC,OAAO,EAAE,kBAAkB,GAAG,IAAI;IAKtC;;;;OAIG;IACH,MAAM,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI;IAQlC;;OAEG;IACH,KAAK,IAAI,IAAI;IAKb;;;;OAIG;IACH,MAAM,IAAI,kBAAkB,EAAE;IAI9B;;;;OAIG;IACH,IAAI,IAAI,SAAS,EAAE;IAInB;;;;;OAKG;IACH,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO;IAIlC;;;;OAIG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF"}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
1
7
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
8
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
9
|
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");
|
|
4
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
11
|
};
|
|
6
|
-
var _KeyringAccountRegistry_accountById, _KeyringAccountRegistry_idByAddress;
|
|
12
|
+
var _KeyringAccountRegistry_accountById, _KeyringAccountRegistry_idByAddress, _KeyringAccountRegistry_generateId;
|
|
7
13
|
import { v4 as uuidv4 } from "uuid";
|
|
8
14
|
/**
|
|
9
15
|
* In-memory registry for KeyringAccount objects.
|
|
@@ -12,9 +18,17 @@ import { v4 as uuidv4 } from "uuid";
|
|
|
12
18
|
* KeyringAccount objects for efficient retrieval.
|
|
13
19
|
*/
|
|
14
20
|
export class KeyringAccountRegistry {
|
|
15
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Create a new KeyringAccountRegistry.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Optional configuration.
|
|
25
|
+
* @param options.generateId - Custom ID generator. Defaults to `uuidv4`.
|
|
26
|
+
*/
|
|
27
|
+
constructor(options) {
|
|
16
28
|
_KeyringAccountRegistry_accountById.set(this, new Map());
|
|
17
29
|
_KeyringAccountRegistry_idByAddress.set(this, new Map());
|
|
30
|
+
_KeyringAccountRegistry_generateId.set(this, void 0);
|
|
31
|
+
__classPrivateFieldSet(this, _KeyringAccountRegistry_generateId, options?.generateId ?? (() => uuidv4()), "f");
|
|
18
32
|
}
|
|
19
33
|
/**
|
|
20
34
|
* Get an account by its account ID.
|
|
@@ -55,7 +69,7 @@ export class KeyringAccountRegistry {
|
|
|
55
69
|
if (existing) {
|
|
56
70
|
return existing;
|
|
57
71
|
}
|
|
58
|
-
const id =
|
|
72
|
+
const id = __classPrivateFieldGet(this, _KeyringAccountRegistry_generateId, "f").call(this, address);
|
|
59
73
|
__classPrivateFieldGet(this, _KeyringAccountRegistry_idByAddress, "f").set(address, id);
|
|
60
74
|
return id;
|
|
61
75
|
}
|
|
@@ -127,5 +141,5 @@ export class KeyringAccountRegistry {
|
|
|
127
141
|
return __classPrivateFieldGet(this, _KeyringAccountRegistry_accountById, "f").size;
|
|
128
142
|
}
|
|
129
143
|
}
|
|
130
|
-
_KeyringAccountRegistry_accountById = new WeakMap(), _KeyringAccountRegistry_idByAddress = new WeakMap();
|
|
144
|
+
_KeyringAccountRegistry_accountById = new WeakMap(), _KeyringAccountRegistry_idByAddress = new WeakMap(), _KeyringAccountRegistry_generateId = new WeakMap();
|
|
131
145
|
//# sourceMappingURL=keyring-account-registry.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-account-registry.mjs","sourceRoot":"","sources":["../src/keyring-account-registry.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"keyring-account-registry.mjs","sourceRoot":"","sources":["../src/keyring-account-registry.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAepC;;;;;GAKG;AACH,MAAM,OAAO,sBAAsB;IASjC;;;;;OAKG;IACH,YAAY,OAAuC;QAZ1C,8CAAe,IAAI,GAAG,EAAiC,EAAC;QAExD,8CAAe,IAAI,GAAG,EAAqB,EAAC;QAE5C,qDAA4C;QASnD,uBAAA,IAAI,sCAAe,OAAO,EAAE,UAAU,IAAI,CAAC,GAAc,EAAE,CAAC,MAAM,EAAE,CAAC,MAAA,CAAC;IACxE,CAAC;I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uBAAA,IAAI,2CAAa,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAED;;OAEG;IACH,KAAK;QACH,uBAAA,IAAI,2CAAa,CAAC,KAAK,EAAE,CAAC;QAC1B,uBAAA,IAAI,2CAAa,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,2CAAa,CAAC,MAAM,EAAE,CAAC,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,2CAAa,CAAC,IAAI,EAAE,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,SAAoB;QACtB,OAAO,uBAAA,IAAI,2CAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,2CAAa,CAAC,IAAI,CAAC;IAChC,CAAC;CACF","sourcesContent":["import type { KeyringAccount } from '@metamask/keyring-api';\nimport type { AccountId } from '@metamask/keyring-utils';\nimport { v4 as uuidv4 } from 'uuid';\n\nexport type KeyringAccountRegistryOptions = {\n /**\n * Custom ID generator invoked when registering a new address.\n *\n * Receives the address being registered and must return a unique account ID.\n * Defaults to a random UUID v4 when not provided.\n *\n * @param address - The address for which to generate an account ID.\n * @returns A unique account ID string.\n */\n generateId?: (address: string) => AccountId;\n};\n\n/**\n * In-memory registry for KeyringAccount objects.\n *\n * Provides O(1) lookups by account ID or address, and stores the full\n * KeyringAccount objects for efficient retrieval.\n */\nexport class KeyringAccountRegistry<\n KeyringAccountType extends KeyringAccount = KeyringAccount,\n> {\n readonly #accountById = new Map<AccountId, KeyringAccountType>();\n\n readonly #idByAddress = new Map<string, AccountId>();\n\n readonly #generateId: (address: string) => AccountId;\n\n /**\n * Create a new KeyringAccountRegistry.\n *\n * @param options - Optional configuration.\n * @param options.generateId - Custom ID generator. Defaults to `uuidv4`.\n */\n constructor(options?: KeyringAccountRegistryOptions) {\n this.#generateId = options?.generateId ?? ((): AccountId => uuidv4());\n }\n\n /**\n * Get an account by its account ID.\n *\n * @param accountId - The account ID to look up.\n * @returns The KeyringAccount, or undefined if not found.\n */\n get(accountId: AccountId): KeyringAccountType | undefined {\n return this.#accountById.get(accountId);\n }\n\n /**\n * Get an account's address by its account ID.\n *\n * @param accountId - The account ID to look up.\n * @returns The address, or undefined if not found.\n */\n getAddress(accountId: AccountId): string | undefined {\n return this.#accountById.get(accountId)?.address;\n }\n\n /**\n * Get an account ID by the underlying address.\n *\n * @param address - The address to look up.\n * @returns The account ID, or undefined if not found.\n */\n getAccountId(address: string): AccountId | undefined {\n return this.#idByAddress.get(address);\n }\n\n /**\n * Register a new address and generate an account ID for it.\n * If the address is already registered, returns the existing account ID.\n *\n * @param address - The address to register.\n * @returns The account ID for this address.\n */\n register(address: string): AccountId {\n const existing = this.#idByAddress.get(address);\n if (existing) {\n return existing;\n }\n const id = this.#generateId(address);\n this.#idByAddress.set(address, id);\n return id;\n }\n\n /**\n * Add an account to the registry.\n * Also registers the address → account ID mapping.\n *\n * ⚠️ If an address was previously registered via `register()` with a\n * different account ID, calling `set()` with an account that has a new ID for\n * the same address will overwrite the address → ID mapping. The old ID\n * becomes \"dangling\" (i.e., `get(oldId)` will return undefined).\n *\n * @param account - The KeyringAccount to cache.\n */\n set(account: KeyringAccountType): void {\n this.#accountById.set(account.id, account);\n this.#idByAddress.set(account.address, account.id);\n }\n\n /**\n * Remove an account from the registry.\n *\n * @param accountId - The account ID to remove.\n */\n delete(accountId: AccountId): void {\n const account = this.#accountById.get(accountId);\n if (account) {\n this.#idByAddress.delete(account.address);\n this.#accountById.delete(accountId);\n }\n }\n\n /**\n * Clear all accounts from the registry.\n */\n clear(): void {\n this.#accountById.clear();\n this.#idByAddress.clear();\n }\n\n /**\n * Get all cached accounts as an array.\n *\n * @returns Array of all KeyringAccountType objects in the registry.\n */\n values(): KeyringAccountType[] {\n return Array.from(this.#accountById.values());\n }\n\n /**\n * Get all account IDs in the registry.\n *\n * @returns Array of all account IDs.\n */\n keys(): AccountId[] {\n return Array.from(this.#accountById.keys());\n }\n\n /**\n * Check if an account exists in the registry.\n *\n * @param accountId - The account ID to check.\n * @returns True if the account exists.\n */\n has(accountId: AccountId): boolean {\n return this.#accountById.has(accountId);\n }\n\n /**\n * Get the number of accounts in the registry.\n *\n * @returns The number of accounts.\n */\n get size(): number {\n return this.#accountById.size;\n }\n}\n"]}
|
package/dist/keyring-wrapper.cjs
CHANGED
|
@@ -33,17 +33,10 @@ class KeyringWrapper {
|
|
|
33
33
|
* operations that mutate its state.
|
|
34
34
|
*/
|
|
35
35
|
_KeyringWrapper_lock.set(this, new async_mutex_1.Mutex());
|
|
36
|
-
/**
|
|
37
|
-
* Registry for KeyringAccount objects.
|
|
38
|
-
* Provides O(1) lookups by AccountId or address.
|
|
39
|
-
*
|
|
40
|
-
* Subclasses should use this registry when creating accounts and
|
|
41
|
-
* clear/update it when deleting accounts or deserializing state.
|
|
42
|
-
*/
|
|
43
|
-
this.registry = new keyring_account_registry_1.KeyringAccountRegistry();
|
|
44
36
|
this.inner = options.inner;
|
|
45
37
|
this.type = `${options.type}`;
|
|
46
38
|
__classPrivateFieldSet(this, _KeyringWrapper_capabilities, options.capabilities, "f");
|
|
39
|
+
this.registry = new keyring_account_registry_1.KeyringAccountRegistry(options.registryOptions);
|
|
47
40
|
}
|
|
48
41
|
/**
|
|
49
42
|
* Get the capabilities of this keyring.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-wrapper.cjs","sourceRoot":"","sources":["../src/keyring-wrapper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAYA,6CAAoC;AAEpC,
|
|
1
|
+
{"version":3,"file":"keyring-wrapper.cjs","sourceRoot":"","sources":["../src/keyring-wrapper.ts"],"names":[],"m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AAoB;QACnC,IAAI,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC1C,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,MAAM,CAAC;QAChB,CAAC;QAED,4CAA4C;QAC5C,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QAEzB,mCAAmC;QACnC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACtC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,6BAA6B,SAAS,EAAE,CAAC,CAAC;QAC5D,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CA+CF;AA3LD,wCA2LC","sourcesContent":["import type {\n CreateAccountOptions,\n ExportAccountOptions,\n ExportedAccount,\n KeyringAccount,\n KeyringRequest,\n KeyringV2,\n KeyringCapabilities,\n KeyringType,\n} from '@metamask/keyring-api';\nimport type { Keyring, AccountId } from '@metamask/keyring-utils';\nimport type { Json } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport {\n KeyringAccountRegistry,\n type KeyringAccountRegistryOptions,\n} from './keyring-account-registry';\n\n/**\n * Basic options for constructing a {@link KeyringWrapper}.\n */\nexport type KeyringWrapperOptions<InnerKeyring extends Keyring> = {\n /**\n * The underlying \"old\" keyring instance that this wrapper adapts.\n */\n inner: InnerKeyring;\n\n /**\n * The concrete keyring type exposed through the V2 interface.\n */\n type: KeyringType;\n\n /**\n * Capabilities of the underlying keyring.\n */\n capabilities: KeyringCapabilities;\n\n /**\n * Options forwarded to the {@link KeyringAccountRegistry} constructor.\n */\n registryOptions?: KeyringAccountRegistryOptions;\n};\n\n/**\n * Generic adapter that turns an existing {@link Keyring} implementation into a\n * {@link KeyringV2} instance.\n *\n * Consumers are expected to provide concrete mappings between high-level V2\n * operations and the underlying keyring methods (for example BIP-44 account\n * creation, private-key import, and request handling). This class focuses on\n * the common mechanics required by all adapters: state serialization,\n * account-ID/address mapping and basic account management.\n */\nexport abstract class KeyringWrapper<\n InnerKeyring extends Keyring,\n KeyringAccountType extends KeyringAccount = KeyringAccount,\n> implements KeyringV2\n{\n readonly type: `${KeyringType}`;\n\n readonly #capabilities: KeyringCapabilities;\n\n protected readonly inner: InnerKeyring;\n\n /**\n * Mutex to ensure exclusive access to the inner keyring during\n * operations that mutate its state.\n */\n readonly #lock = new Mutex();\n\n /**\n * Registry for KeyringAccount objects.\n * Provides O(1) lookups by AccountId or address.\n *\n * Subclasses should use this registry when creating accounts and\n * clear/update it when deleting accounts or deserializing state.\n */\n protected readonly registry: KeyringAccountRegistry<KeyringAccountType>;\n\n constructor(options: KeyringWrapperOptions<InnerKeyring>) {\n this.inner = options.inner;\n this.type = `${options.type}`;\n this.#capabilities = options.capabilities;\n this.registry = new KeyringAccountRegistry(options.registryOptions);\n }\n\n /**\n * Get the capabilities of this keyring.\n *\n * Subclasses can override this getter to return capabilities dynamically\n * based on runtime state.\n *\n * @returns The keyring's capabilities.\n */\n get capabilities(): KeyringCapabilities {\n return this.#capabilities;\n }\n\n /**\n * Execute an operation with exclusive access to the inner keyring.\n *\n * This method ensures thread-safety for operations that read or mutate\n * the inner keyring state. All operations that modify the keyring\n * (createAccounts, deleteAccount, deserialize) should use this method\n * to prevent race conditions.\n *\n * Within the callback, use `this.inner` to access the inner keyring.\n *\n * @param callback - A function that performs the operation.\n * @returns The result of the callback.\n */\n protected async withLock<Result>(\n callback: () => Promise<Result>,\n ): Promise<Result> {\n return this.#lock.runExclusive(callback);\n }\n\n /**\n * Serialize the underlying keyring state to a JSON-serializable object.\n *\n * This simply delegates to the legacy keyring's {@link Keyring.serialize}\n * implementation.\n *\n * @returns The serialized keyring state.\n */\n async serialize(): Promise<Json> {\n return this.inner.serialize();\n }\n\n /**\n * Hydrate the underlying keyring from a previously serialized state.\n *\n * This clears the registry, delegates to the legacy keyring's\n * {@link Keyring.deserialize} implementation, and rebuilds the registry\n * by calling {@link getAccounts}.\n *\n * @param state - The serialized keyring state.\n */\n async deserialize(state: Json): Promise<void> {\n await this.withLock(async () => {\n // Clear the registry when deserializing\n this.registry.clear();\n\n // Deserialize the legacy keyring\n await this.inner.deserialize(state);\n\n // Rebuild the registry by calling getAccounts().\n // Subclass implementations of getAccounts() should populate the registry\n // as a side effect (see the abstract method's documentation).\n await this.getAccounts();\n });\n }\n\n /**\n * Return all accounts managed by this keyring.\n *\n * Concrete adapters are responsible for mapping the underlying keyring's\n * notion of accounts (typically addresses returned by\n * {@link Keyring.getAccounts}) into {@link KeyringAccount} objects.\n * Implementations should use the configured {@link KeyringAccountRegistry}\n * to establish the account ID/address mapping so that\n * {@link getAccount} works as expected.\n *\n * @returns The list of managed accounts.\n */\n abstract getAccounts(): Promise<KeyringAccount[]>;\n\n /**\n * Look up a single account by its {@link AccountId}.\n *\n * This method first checks the registry for O(1) lookup.\n * If not found, it falls back to calling {@link getAccounts} which\n * should populate the registry as a side effect.\n *\n * @param accountId - The AccountId to look up.\n * @returns The matching KeyringAccount.\n */\n async getAccount(accountId: AccountId): Promise<KeyringAccount> {\n let cached = this.registry.get(accountId);\n if (cached) {\n return cached;\n }\n\n // Prime the registry by calling getAccounts\n await this.getAccounts();\n\n // Try registry again after priming\n cached = this.registry.get(accountId);\n if (!cached) {\n throw new Error(`Account not found for id: ${accountId}`);\n }\n\n return cached;\n }\n\n /**\n * Create one or more new accounts managed by this keyring.\n *\n * Implementations are responsible for interpreting the\n * {@link CreateAccountOptions} (for example BIP-44 derivation or\n * private-key import) and returning the resulting {@link KeyringAccount}\n * objects. Implementors should also ensure that the registry is updated so\n * that {@link getAccount} works for newly created accounts.\n */\n abstract createAccounts(\n options: CreateAccountOptions,\n ): Promise<KeyringAccount[]>;\n\n /**\n * Remove the account associated with the given {@link AccountId} from this\n * keyring.\n *\n * Implementations are expected to translate the ID to an underlying\n * address (typically via the registry) and then invoke the appropriate\n * removal mechanism on the legacy keyring.\n */\n abstract deleteAccount(accountId: AccountId): Promise<void>;\n\n /**\n * Export the secrets associated with the given account in a format\n * described by {@link ExportAccountOptions}.\n *\n * This method is optional, and concrete adapters should only\n * implement it if the underlying keyring supports exporting\n * accounts.\n */\n exportAccount?(\n accountId: AccountId,\n options?: ExportAccountOptions,\n ): Promise<ExportedAccount>;\n\n /**\n * Handle a high-level {@link KeyringRequest} on behalf of this keyring.\n *\n * Concrete adapters are responsible for routing the request's method and\n * parameters to the appropriate legacy keyring APIs (for example signing\n * transactions or decrypting messages) and returning a JSON-serializable\n * result.\n */\n abstract submitRequest(request: KeyringRequest): Promise<Json>;\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CreateAccountOptions, ExportAccountOptions, ExportedAccount, KeyringAccount, KeyringRequest, KeyringV2, KeyringCapabilities, KeyringType } from "@metamask/keyring-api";
|
|
2
2
|
import type { Keyring, AccountId } from "@metamask/keyring-utils";
|
|
3
3
|
import type { Json } from "@metamask/utils";
|
|
4
|
-
import { KeyringAccountRegistry } from "./keyring-account-registry.cjs";
|
|
4
|
+
import { KeyringAccountRegistry, type KeyringAccountRegistryOptions } from "./keyring-account-registry.cjs";
|
|
5
5
|
/**
|
|
6
6
|
* Basic options for constructing a {@link KeyringWrapper}.
|
|
7
7
|
*/
|
|
@@ -18,6 +18,10 @@ export type KeyringWrapperOptions<InnerKeyring extends Keyring> = {
|
|
|
18
18
|
* Capabilities of the underlying keyring.
|
|
19
19
|
*/
|
|
20
20
|
capabilities: KeyringCapabilities;
|
|
21
|
+
/**
|
|
22
|
+
* Options forwarded to the {@link KeyringAccountRegistry} constructor.
|
|
23
|
+
*/
|
|
24
|
+
registryOptions?: KeyringAccountRegistryOptions;
|
|
21
25
|
};
|
|
22
26
|
/**
|
|
23
27
|
* Generic adapter that turns an existing {@link Keyring} implementation into a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-wrapper.d.cts","sourceRoot":"","sources":["../src/keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OA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EAAE,qBAAqB,CAAC,YAAY,CAAC;IAOxD;;;;;;;OAOG;IACH,IAAI,YAAY,IAAI,mBAAmB,CAEtC;IAED;;;;;;;;;;;;OAYG;cACa,QAAQ,CAAC,MAAM,EAC7B,QAAQ,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,GAC9B,OAAO,CAAC,MAAM,CAAC;IAIlB;;;;;;;OAOG;IACG,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAIhC;;;;;;;;OAQG;IACG,WAAW,CAAC,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAe7C;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,WAAW,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAEjD;;;;;;;;;OASG;IACG,UAAU,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,cAAc,CAAC;IAkB/D;;;;;;;;OAQG;IACH,QAAQ,CAAC,cAAc,CACrB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,cAAc,EAAE,CAAC;IAE5B;;;;;;;OAOG;IACH,QAAQ,CAAC,aAAa,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAE3D;;;;;;;OAOG;IACH,aAAa,CAAC,CACZ,SAAS,EAAE,SAAS,EACpB,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,eAAe,CAAC;IAE3B;;;;;;;OAOG;IACH,QAAQ,CAAC,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAC/D"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CreateAccountOptions, ExportAccountOptions, ExportedAccount, KeyringAccount, KeyringRequest, KeyringV2, KeyringCapabilities, KeyringType } from "@metamask/keyring-api";
|
|
2
2
|
import type { Keyring, AccountId } from "@metamask/keyring-utils";
|
|
3
3
|
import type { Json } from "@metamask/utils";
|
|
4
|
-
import { KeyringAccountRegistry } from "./keyring-account-registry.mjs";
|
|
4
|
+
import { KeyringAccountRegistry, type KeyringAccountRegistryOptions } from "./keyring-account-registry.mjs";
|
|
5
5
|
/**
|
|
6
6
|
* Basic options for constructing a {@link KeyringWrapper}.
|
|
7
7
|
*/
|
|
@@ -18,6 +18,10 @@ export type KeyringWrapperOptions<InnerKeyring extends Keyring> = {
|
|
|
18
18
|
* Capabilities of the underlying keyring.
|
|
19
19
|
*/
|
|
20
20
|
capabilities: KeyringCapabilities;
|
|
21
|
+
/**
|
|
22
|
+
* Options forwarded to the {@link KeyringAccountRegistry} constructor.
|
|
23
|
+
*/
|
|
24
|
+
registryOptions?: KeyringAccountRegistryOptions;
|
|
21
25
|
};
|
|
22
26
|
/**
|
|
23
27
|
* Generic adapter that turns an existing {@link Keyring} implementation into a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-wrapper.d.mts","sourceRoot":"","sources":["../src/keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EACf,cAAc,EACd,cAAc,EACd,SAAS,EACT,mBAAmB,EACnB,WAAW,EACZ,8BAA8B;AAC/B,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,gCAAgC;AAClE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,
|
|
1
|
+
{"version":3,"file":"keyring-wrapper.d.mts","sourceRoot":"","sources":["../src/keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EACf,cAAc,EACd,cAAc,EACd,SAAS,EACT,mBAAmB,EACnB,WAAW,EACZ,8BAA8B;AAC/B,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,gCAAgC;AAClE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,EACL,sBAAsB,EACtB,KAAK,6BAA6B,EACnC,uCAAm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5B,OAAO,CAAC,cAAc,EAAE,CAAC;IAE5B;;;;;;;OAOG;IACH,QAAQ,CAAC,aAAa,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAE3D;;;;;;;OAOG;IACH,aAAa,CAAC,CACZ,SAAS,EAAE,SAAS,EACpB,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,eAAe,CAAC;IAE3B;;;;;;;OAOG;IACH,QAAQ,CAAC,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAC/D"}
|
package/dist/keyring-wrapper.mjs
CHANGED
|
@@ -30,17 +30,10 @@ export class KeyringWrapper {
|
|
|
30
30
|
* operations that mutate its state.
|
|
31
31
|
*/
|
|
32
32
|
_KeyringWrapper_lock.set(this, new Mutex());
|
|
33
|
-
/**
|
|
34
|
-
* Registry for KeyringAccount objects.
|
|
35
|
-
* Provides O(1) lookups by AccountId or address.
|
|
36
|
-
*
|
|
37
|
-
* Subclasses should use this registry when creating accounts and
|
|
38
|
-
* clear/update it when deleting accounts or deserializing state.
|
|
39
|
-
*/
|
|
40
|
-
this.registry = new KeyringAccountRegistry();
|
|
41
33
|
this.inner = options.inner;
|
|
42
34
|
this.type = `${options.type}`;
|
|
43
35
|
__classPrivateFieldSet(this, _KeyringWrapper_capabilities, options.capabilities, "f");
|
|
36
|
+
this.registry = new KeyringAccountRegistry(options.registryOptions);
|
|
44
37
|
}
|
|
45
38
|
/**
|
|
46
39
|
* Get the capabilities of this keyring.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring-wrapper.mjs","sourceRoot":"","sources":["../src/keyring-wrapper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAYA,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAEpC,OAAO,
|
|
1
|
+
{"version":3,"file":"keyring-wrapper.mjs","sourceRoot":"","sources":["../src/keyring-wrapper.ts"],"names":[],"mappings":";;;;;;;;;;;;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ASG;IACH,KAAK,CAAC,UAAU,CAAC,SAAoB;QACnC,IAAI,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC1C,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,MAAM,CAAC;QAChB,CAAC;QAED,4CAA4C;QAC5C,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QAEzB,mCAAmC;QACnC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACtC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,6BAA6B,SAAS,EAAE,CAAC,CAAC;QAC5D,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CA+CF","sourcesContent":["import type {\n CreateAccountOptions,\n ExportAccountOptions,\n ExportedAccount,\n KeyringAccount,\n KeyringRequest,\n KeyringV2,\n KeyringCapabilities,\n KeyringType,\n} from '@metamask/keyring-api';\nimport type { Keyring, AccountId } from '@metamask/keyring-utils';\nimport type { Json } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport {\n KeyringAccountRegistry,\n type KeyringAccountRegistryOptions,\n} from './keyring-account-registry';\n\n/**\n * Basic options for constructing a {@link KeyringWrapper}.\n */\nexport type KeyringWrapperOptions<InnerKeyring extends Keyring> = {\n /**\n * The underlying \"old\" keyring instance that this wrapper adapts.\n */\n inner: InnerKeyring;\n\n /**\n * The concrete keyring type exposed through the V2 interface.\n */\n type: KeyringType;\n\n /**\n * Capabilities of the underlying keyring.\n */\n capabilities: KeyringCapabilities;\n\n /**\n * Options forwarded to the {@link KeyringAccountRegistry} constructor.\n */\n registryOptions?: KeyringAccountRegistryOptions;\n};\n\n/**\n * Generic adapter that turns an existing {@link Keyring} implementation into a\n * {@link KeyringV2} instance.\n *\n * Consumers are expected to provide concrete mappings between high-level V2\n * operations and the underlying keyring methods (for example BIP-44 account\n * creation, private-key import, and request handling). This class focuses on\n * the common mechanics required by all adapters: state serialization,\n * account-ID/address mapping and basic account management.\n */\nexport abstract class KeyringWrapper<\n InnerKeyring extends Keyring,\n KeyringAccountType extends KeyringAccount = KeyringAccount,\n> implements KeyringV2\n{\n readonly type: `${KeyringType}`;\n\n readonly #capabilities: KeyringCapabilities;\n\n protected readonly inner: InnerKeyring;\n\n /**\n * Mutex to ensure exclusive access to the inner keyring during\n * operations that mutate its state.\n */\n readonly #lock = new Mutex();\n\n /**\n * Registry for KeyringAccount objects.\n * Provides O(1) lookups by AccountId or address.\n *\n * Subclasses should use this registry when creating accounts and\n * clear/update it when deleting accounts or deserializing state.\n */\n protected readonly registry: KeyringAccountRegistry<KeyringAccountType>;\n\n constructor(options: KeyringWrapperOptions<InnerKeyring>) {\n this.inner = options.inner;\n this.type = `${options.type}`;\n this.#capabilities = options.capabilities;\n this.registry = new KeyringAccountRegistry(options.registryOptions);\n }\n\n /**\n * Get the capabilities of this keyring.\n *\n * Subclasses can override this getter to return capabilities dynamically\n * based on runtime state.\n *\n * @returns The keyring's capabilities.\n */\n get capabilities(): KeyringCapabilities {\n return this.#capabilities;\n }\n\n /**\n * Execute an operation with exclusive access to the inner keyring.\n *\n * This method ensures thread-safety for operations that read or mutate\n * the inner keyring state. All operations that modify the keyring\n * (createAccounts, deleteAccount, deserialize) should use this method\n * to prevent race conditions.\n *\n * Within the callback, use `this.inner` to access the inner keyring.\n *\n * @param callback - A function that performs the operation.\n * @returns The result of the callback.\n */\n protected async withLock<Result>(\n callback: () => Promise<Result>,\n ): Promise<Result> {\n return this.#lock.runExclusive(callback);\n }\n\n /**\n * Serialize the underlying keyring state to a JSON-serializable object.\n *\n * This simply delegates to the legacy keyring's {@link Keyring.serialize}\n * implementation.\n *\n * @returns The serialized keyring state.\n */\n async serialize(): Promise<Json> {\n return this.inner.serialize();\n }\n\n /**\n * Hydrate the underlying keyring from a previously serialized state.\n *\n * This clears the registry, delegates to the legacy keyring's\n * {@link Keyring.deserialize} implementation, and rebuilds the registry\n * by calling {@link getAccounts}.\n *\n * @param state - The serialized keyring state.\n */\n async deserialize(state: Json): Promise<void> {\n await this.withLock(async () => {\n // Clear the registry when deserializing\n this.registry.clear();\n\n // Deserialize the legacy keyring\n await this.inner.deserialize(state);\n\n // Rebuild the registry by calling getAccounts().\n // Subclass implementations of getAccounts() should populate the registry\n // as a side effect (see the abstract method's documentation).\n await this.getAccounts();\n });\n }\n\n /**\n * Return all accounts managed by this keyring.\n *\n * Concrete adapters are responsible for mapping the underlying keyring's\n * notion of accounts (typically addresses returned by\n * {@link Keyring.getAccounts}) into {@link KeyringAccount} objects.\n * Implementations should use the configured {@link KeyringAccountRegistry}\n * to establish the account ID/address mapping so that\n * {@link getAccount} works as expected.\n *\n * @returns The list of managed accounts.\n */\n abstract getAccounts(): Promise<KeyringAccount[]>;\n\n /**\n * Look up a single account by its {@link AccountId}.\n *\n * This method first checks the registry for O(1) lookup.\n * If not found, it falls back to calling {@link getAccounts} which\n * should populate the registry as a side effect.\n *\n * @param accountId - The AccountId to look up.\n * @returns The matching KeyringAccount.\n */\n async getAccount(accountId: AccountId): Promise<KeyringAccount> {\n let cached = this.registry.get(accountId);\n if (cached) {\n return cached;\n }\n\n // Prime the registry by calling getAccounts\n await this.getAccounts();\n\n // Try registry again after priming\n cached = this.registry.get(accountId);\n if (!cached) {\n throw new Error(`Account not found for id: ${accountId}`);\n }\n\n return cached;\n }\n\n /**\n * Create one or more new accounts managed by this keyring.\n *\n * Implementations are responsible for interpreting the\n * {@link CreateAccountOptions} (for example BIP-44 derivation or\n * private-key import) and returning the resulting {@link KeyringAccount}\n * objects. Implementors should also ensure that the registry is updated so\n * that {@link getAccount} works for newly created accounts.\n */\n abstract createAccounts(\n options: CreateAccountOptions,\n ): Promise<KeyringAccount[]>;\n\n /**\n * Remove the account associated with the given {@link AccountId} from this\n * keyring.\n *\n * Implementations are expected to translate the ID to an underlying\n * address (typically via the registry) and then invoke the appropriate\n * removal mechanism on the legacy keyring.\n */\n abstract deleteAccount(accountId: AccountId): Promise<void>;\n\n /**\n * Export the secrets associated with the given account in a format\n * described by {@link ExportAccountOptions}.\n *\n * This method is optional, and concrete adapters should only\n * implement it if the underlying keyring supports exporting\n * accounts.\n */\n exportAccount?(\n accountId: AccountId,\n options?: ExportAccountOptions,\n ): Promise<ExportedAccount>;\n\n /**\n * Handle a high-level {@link KeyringRequest} on behalf of this keyring.\n *\n * Concrete adapters are responsible for routing the request's method and\n * parameters to the appropriate legacy keyring APIs (for example signing\n * transactions or decrypting messages) and returning a JSON-serializable\n * result.\n */\n abstract submitRequest(request: KeyringRequest): Promise<Json>;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/keyring-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "MetaMask Keyring SDK",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"metamask",
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
"@metamask/superstruct": "^3.1.0",
|
|
54
54
|
"@metamask/utils": "^11.10.0",
|
|
55
55
|
"async-mutex": "^0.5.0",
|
|
56
|
+
"ethereum-cryptography": "^2.1.2",
|
|
56
57
|
"uuid": "^9.0.1"
|
|
57
58
|
},
|
|
58
59
|
"devDependencies": {
|