@metamask/keyring-api 21.3.0 → 21.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/CHANGELOG.md +49 -2
  2. package/README.md +4 -0
  3. package/dist/api/account-options.cjs +44 -7
  4. package/dist/api/account-options.cjs.map +1 -1
  5. package/dist/api/account-options.d.cts +41 -3
  6. package/dist/api/account-options.d.cts.map +1 -1
  7. package/dist/api/account-options.d.mts +41 -3
  8. package/dist/api/account-options.d.mts.map +1 -1
  9. package/dist/api/account-options.mjs +43 -6
  10. package/dist/api/account-options.mjs.map +1 -1
  11. package/dist/api/account.d.cts +4 -0
  12. package/dist/api/account.d.cts.map +1 -1
  13. package/dist/api/account.d.mts +4 -0
  14. package/dist/api/account.d.mts.map +1 -1
  15. package/dist/api/keyring.cjs +0 -2
  16. package/dist/api/keyring.cjs.map +1 -1
  17. package/dist/api/keyring.d.cts +13 -0
  18. package/dist/api/keyring.d.cts.map +1 -1
  19. package/dist/api/keyring.d.mts +13 -0
  20. package/dist/api/keyring.d.mts.map +1 -1
  21. package/dist/api/keyring.mjs +0 -2
  22. package/dist/api/keyring.mjs.map +1 -1
  23. package/dist/api/v2/create-account/bip44.cjs +27 -1
  24. package/dist/api/v2/create-account/bip44.cjs.map +1 -1
  25. package/dist/api/v2/create-account/bip44.d.cts +46 -0
  26. package/dist/api/v2/create-account/bip44.d.cts.map +1 -1
  27. package/dist/api/v2/create-account/bip44.d.mts +46 -0
  28. package/dist/api/v2/create-account/bip44.d.mts.map +1 -1
  29. package/dist/api/v2/create-account/bip44.mjs +26 -0
  30. package/dist/api/v2/create-account/bip44.mjs.map +1 -1
  31. package/dist/api/v2/create-account/custom.cjs +14 -0
  32. package/dist/api/v2/create-account/custom.cjs.map +1 -0
  33. package/dist/api/v2/create-account/custom.d.cts +25 -0
  34. package/dist/api/v2/create-account/custom.d.cts.map +1 -0
  35. package/dist/api/v2/create-account/custom.d.mts +25 -0
  36. package/dist/api/v2/create-account/custom.d.mts.map +1 -0
  37. package/dist/api/v2/create-account/custom.mjs +11 -0
  38. package/dist/api/v2/create-account/custom.mjs.map +1 -0
  39. package/dist/api/v2/create-account/index.cjs +55 -0
  40. package/dist/api/v2/create-account/index.cjs.map +1 -1
  41. package/dist/api/v2/create-account/index.d.cts +55 -1
  42. package/dist/api/v2/create-account/index.d.cts.map +1 -1
  43. package/dist/api/v2/create-account/index.d.mts +55 -1
  44. package/dist/api/v2/create-account/index.d.mts.map +1 -1
  45. package/dist/api/v2/create-account/index.mjs +55 -1
  46. package/dist/api/v2/create-account/index.mjs.map +1 -1
  47. package/dist/api/v2/index.cjs +1 -0
  48. package/dist/api/v2/index.cjs.map +1 -1
  49. package/dist/api/v2/index.d.cts +1 -0
  50. package/dist/api/v2/index.d.cts.map +1 -1
  51. package/dist/api/v2/index.d.mts +1 -0
  52. package/dist/api/v2/index.d.mts.map +1 -1
  53. package/dist/api/v2/index.mjs +1 -0
  54. package/dist/api/v2/index.mjs.map +1 -1
  55. package/dist/api/v2/keyring-capabilities.cjs +17 -3
  56. package/dist/api/v2/keyring-capabilities.cjs.map +1 -1
  57. package/dist/api/v2/keyring-capabilities.d.cts +31 -9
  58. package/dist/api/v2/keyring-capabilities.d.cts.map +1 -1
  59. package/dist/api/v2/keyring-capabilities.d.mts +31 -9
  60. package/dist/api/v2/keyring-capabilities.d.mts.map +1 -1
  61. package/dist/api/v2/keyring-capabilities.mjs +18 -4
  62. package/dist/api/v2/keyring-capabilities.mjs.map +1 -1
  63. package/dist/api/v2/wrapper/index.cjs +19 -0
  64. package/dist/api/v2/wrapper/index.cjs.map +1 -0
  65. package/dist/api/v2/wrapper/index.d.cts +3 -0
  66. package/dist/api/v2/wrapper/index.d.cts.map +1 -0
  67. package/dist/api/v2/wrapper/index.d.mts +3 -0
  68. package/dist/api/v2/wrapper/index.d.mts.map +1 -0
  69. package/dist/api/v2/wrapper/index.mjs +3 -0
  70. package/dist/api/v2/wrapper/index.mjs.map +1 -0
  71. package/dist/api/v2/wrapper/keyring-account-registry.cjs +135 -0
  72. package/dist/api/v2/wrapper/keyring-account-registry.cjs.map +1 -0
  73. package/dist/api/v2/wrapper/keyring-account-registry.d.cts +88 -0
  74. package/dist/api/v2/wrapper/keyring-account-registry.d.cts.map +1 -0
  75. package/dist/api/v2/wrapper/keyring-account-registry.d.mts +88 -0
  76. package/dist/api/v2/wrapper/keyring-account-registry.d.mts.map +1 -0
  77. package/dist/api/v2/wrapper/keyring-account-registry.mjs +131 -0
  78. package/dist/api/v2/wrapper/keyring-account-registry.mjs.map +1 -0
  79. package/dist/api/v2/wrapper/keyring-wrapper.cjs +134 -0
  80. package/dist/api/v2/wrapper/keyring-wrapper.cjs.map +1 -0
  81. package/dist/api/v2/wrapper/keyring-wrapper.d.cts +154 -0
  82. package/dist/api/v2/wrapper/keyring-wrapper.d.cts.map +1 -0
  83. package/dist/api/v2/wrapper/keyring-wrapper.d.mts +154 -0
  84. package/dist/api/v2/wrapper/keyring-wrapper.d.mts.map +1 -0
  85. package/dist/api/v2/wrapper/keyring-wrapper.mjs +130 -0
  86. package/dist/api/v2/wrapper/keyring-wrapper.mjs.map +1 -0
  87. package/dist/btc/types.d.cts +16 -0
  88. package/dist/btc/types.d.cts.map +1 -1
  89. package/dist/btc/types.d.mts +16 -0
  90. package/dist/btc/types.d.mts.map +1 -1
  91. package/dist/eth/index.cjs +2 -0
  92. package/dist/eth/index.cjs.map +1 -1
  93. package/dist/eth/index.d.cts +2 -0
  94. package/dist/eth/index.d.cts.map +1 -1
  95. package/dist/eth/index.d.mts +2 -0
  96. package/dist/eth/index.d.mts.map +1 -1
  97. package/dist/eth/index.mjs +2 -0
  98. package/dist/eth/index.mjs.map +1 -1
  99. package/dist/eth/rpc/index.cjs +18 -0
  100. package/dist/eth/rpc/index.cjs.map +1 -0
  101. package/dist/eth/rpc/index.d.cts +2 -0
  102. package/dist/eth/rpc/index.d.cts.map +1 -0
  103. package/dist/eth/rpc/index.d.mts +2 -0
  104. package/dist/eth/rpc/index.d.mts.map +1 -0
  105. package/dist/eth/rpc/index.mjs +2 -0
  106. package/dist/eth/rpc/index.mjs.map +1 -0
  107. package/dist/eth/rpc/params.cjs +151 -0
  108. package/dist/eth/rpc/params.cjs.map +1 -0
  109. package/dist/eth/rpc/params.d.cts +215 -0
  110. package/dist/eth/rpc/params.d.cts.map +1 -0
  111. package/dist/eth/rpc/params.d.mts +215 -0
  112. package/dist/eth/rpc/params.d.mts.map +1 -0
  113. package/dist/eth/rpc/params.mjs +148 -0
  114. package/dist/eth/rpc/params.mjs.map +1 -0
  115. package/dist/eth/types.cjs +5 -2
  116. package/dist/eth/types.cjs.map +1 -1
  117. package/dist/eth/types.d.cts +9 -0
  118. package/dist/eth/types.d.cts.map +1 -1
  119. package/dist/eth/types.d.mts +9 -0
  120. package/dist/eth/types.d.mts.map +1 -1
  121. package/dist/eth/types.mjs +4 -1
  122. package/dist/eth/types.mjs.map +1 -1
  123. package/dist/eth/v2/eth-keyring-wrapper.cjs +165 -0
  124. package/dist/eth/v2/eth-keyring-wrapper.cjs.map +1 -0
  125. package/dist/eth/v2/eth-keyring-wrapper.d.cts +54 -0
  126. package/dist/eth/v2/eth-keyring-wrapper.d.cts.map +1 -0
  127. package/dist/eth/v2/eth-keyring-wrapper.d.mts +54 -0
  128. package/dist/eth/v2/eth-keyring-wrapper.d.mts.map +1 -0
  129. package/dist/eth/v2/eth-keyring-wrapper.mjs +161 -0
  130. package/dist/eth/v2/eth-keyring-wrapper.mjs.map +1 -0
  131. package/dist/eth/v2/index.cjs +18 -0
  132. package/dist/eth/v2/index.cjs.map +1 -0
  133. package/dist/eth/v2/index.d.cts +2 -0
  134. package/dist/eth/v2/index.d.cts.map +1 -0
  135. package/dist/eth/v2/index.d.mts +2 -0
  136. package/dist/eth/v2/index.d.mts.map +1 -0
  137. package/dist/eth/v2/index.mjs +2 -0
  138. package/dist/eth/v2/index.mjs.map +1 -0
  139. package/dist/events.cjs +0 -2
  140. package/dist/events.cjs.map +1 -1
  141. package/dist/events.d.cts +16 -0
  142. package/dist/events.d.cts.map +1 -1
  143. package/dist/events.d.mts +16 -0
  144. package/dist/events.d.mts.map +1 -1
  145. package/dist/events.mjs +0 -2
  146. package/dist/events.mjs.map +1 -1
  147. package/dist/rpc.cjs +27 -12
  148. package/dist/rpc.cjs.map +1 -1
  149. package/dist/rpc.d.cts +200 -12
  150. package/dist/rpc.d.cts.map +1 -1
  151. package/dist/rpc.d.mts +200 -12
  152. package/dist/rpc.d.mts.map +1 -1
  153. package/dist/rpc.mjs +27 -12
  154. package/dist/rpc.mjs.map +1 -1
  155. package/dist/sol/types.d.cts +4 -0
  156. package/dist/sol/types.d.cts.map +1 -1
  157. package/dist/sol/types.d.mts +4 -0
  158. package/dist/sol/types.d.mts.map +1 -1
  159. package/dist/trx/types.d.cts +4 -0
  160. package/dist/trx/types.d.cts.map +1 -1
  161. package/dist/trx/types.d.mts +4 -0
  162. package/dist/trx/types.d.mts.map +1 -1
  163. package/package.json +9 -4
@@ -0,0 +1,134 @@
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
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _KeyringWrapper_capabilities, _KeyringWrapper_lock;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.KeyringWrapper = void 0;
16
+ const async_mutex_1 = require("async-mutex");
17
+ const keyring_account_registry_1 = require("./keyring-account-registry.cjs");
18
+ /**
19
+ * Generic adapter that turns an existing {@link Keyring} implementation into a
20
+ * {@link KeyringV2} instance.
21
+ *
22
+ * Consumers are expected to provide concrete mappings between high-level V2
23
+ * operations and the underlying keyring methods (for example BIP-44 account
24
+ * creation, private-key import, and request handling). This class focuses on
25
+ * the common mechanics required by all adapters: state serialization,
26
+ * account-ID/address mapping and basic account management.
27
+ */
28
+ class KeyringWrapper {
29
+ constructor(options) {
30
+ _KeyringWrapper_capabilities.set(this, void 0);
31
+ /**
32
+ * Mutex to ensure exclusive access to the inner keyring during
33
+ * operations that mutate its state.
34
+ */
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
+ this.inner = options.inner;
45
+ this.type = `${options.type}`;
46
+ __classPrivateFieldSet(this, _KeyringWrapper_capabilities, options.capabilities, "f");
47
+ }
48
+ /**
49
+ * Get the capabilities of this keyring.
50
+ *
51
+ * Subclasses can override this getter to return capabilities dynamically
52
+ * based on runtime state.
53
+ *
54
+ * @returns The keyring's capabilities.
55
+ */
56
+ get capabilities() {
57
+ return __classPrivateFieldGet(this, _KeyringWrapper_capabilities, "f");
58
+ }
59
+ /**
60
+ * Execute an operation with exclusive access to the inner keyring.
61
+ *
62
+ * This method ensures thread-safety for operations that read or mutate
63
+ * the inner keyring state. All operations that modify the keyring
64
+ * (createAccounts, deleteAccount, deserialize) should use this method
65
+ * to prevent race conditions.
66
+ *
67
+ * Within the callback, use `this.inner` to access the inner keyring.
68
+ *
69
+ * @param callback - A function that performs the operation.
70
+ * @returns The result of the callback.
71
+ */
72
+ async withLock(callback) {
73
+ return __classPrivateFieldGet(this, _KeyringWrapper_lock, "f").runExclusive(callback);
74
+ }
75
+ /**
76
+ * Serialize the underlying keyring state to a JSON-serializable object.
77
+ *
78
+ * This simply delegates to the legacy keyring's {@link Keyring.serialize}
79
+ * implementation.
80
+ *
81
+ * @returns The serialized keyring state.
82
+ */
83
+ async serialize() {
84
+ return this.inner.serialize();
85
+ }
86
+ /**
87
+ * Hydrate the underlying keyring from a previously serialized state.
88
+ *
89
+ * This clears the registry, delegates to the legacy keyring's
90
+ * {@link Keyring.deserialize} implementation, and rebuilds the registry
91
+ * by calling {@link getAccounts}.
92
+ *
93
+ * @param state - The serialized keyring state.
94
+ */
95
+ async deserialize(state) {
96
+ await this.withLock(async () => {
97
+ // Clear the registry when deserializing
98
+ this.registry.clear();
99
+ // Deserialize the legacy keyring
100
+ await this.inner.deserialize(state);
101
+ // Rebuild the registry by calling getAccounts().
102
+ // Subclass implementations of getAccounts() should populate the registry
103
+ // as a side effect (see the abstract method's documentation).
104
+ await this.getAccounts();
105
+ });
106
+ }
107
+ /**
108
+ * Look up a single account by its {@link AccountId}.
109
+ *
110
+ * This method first checks the registry for O(1) lookup.
111
+ * If not found, it falls back to calling {@link getAccounts} which
112
+ * should populate the registry as a side effect.
113
+ *
114
+ * @param accountId - The AccountId to look up.
115
+ * @returns The matching KeyringAccount.
116
+ */
117
+ async getAccount(accountId) {
118
+ let cached = this.registry.get(accountId);
119
+ if (cached) {
120
+ return cached;
121
+ }
122
+ // Prime the registry by calling getAccounts
123
+ await this.getAccounts();
124
+ // Try registry again after priming
125
+ cached = this.registry.get(accountId);
126
+ if (!cached) {
127
+ throw new Error(`Account not found for id: ${accountId}`);
128
+ }
129
+ return cached;
130
+ }
131
+ }
132
+ exports.KeyringWrapper = KeyringWrapper;
133
+ _KeyringWrapper_capabilities = new WeakMap(), _KeyringWrapper_lock = new WeakMap();
134
+ //# sourceMapp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I,CAAC,WAAW,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;IAgBD;;;;;;;;;OASG;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;AA3LD,wCA2LC","sourcesContent":["import type { Keyring, AccountId } from '@metamask/keyring-utils';\nimport type { Json } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport { KeyringAccountRegistry } from './keyring-account-registry';\nimport type {\n CreateAccountOptions,\n ExportAccountOptions,\n ExportedAccount,\n} from '..';\nimport type { KeyringAccount } from '../../account';\nimport type { KeyringRequest } from '../../request';\nimport type { KeyringV2 } from '../keyring';\nimport type { KeyringCapabilities } from '../keyring-capabilities';\nimport type { KeyringType } from '../keyring-type';\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/**\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 =\n new KeyringAccountRegistry<KeyringAccountType>();\n\n constructor(options: KeyringWrapperOptions<InnerKeyring>) {\n this.inner = options.inner;\n this.type = `${options.type}`;\n this.#capabilities = options.capabilities;\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"]}
@@ -0,0 +1,154 @@
1
+ import type { Keyring, AccountId } from "@metamask/keyring-utils";
2
+ import type { Json } from "@metamask/utils";
3
+ import { KeyringAccountRegistry } from "./keyring-account-registry.cjs";
4
+ import type { CreateAccountOptions, ExportAccountOptions, ExportedAccount } from "../index.cjs";
5
+ import type { KeyringAccount } from "../../account.cjs";
6
+ import type { KeyringRequest } from "../../request.cjs";
7
+ import type { KeyringV2 } from "../keyring.cjs";
8
+ import type { KeyringCapabilities } from "../keyring-capabilities.cjs";
9
+ import type { KeyringType } from "../keyring-type.cjs";
10
+ /**
11
+ * Basic options for constructing a {@link KeyringWrapper}.
12
+ */
13
+ export type KeyringWrapperOptions<InnerKeyring extends Keyring> = {
14
+ /**
15
+ * The underlying "old" keyring instance that this wrapper adapts.
16
+ */
17
+ inner: InnerKeyring;
18
+ /**
19
+ * The concrete keyring type exposed through the V2 interface.
20
+ */
21
+ type: KeyringType;
22
+ /**
23
+ * Capabilities of the underlying keyring.
24
+ */
25
+ capabilities: KeyringCapabilities;
26
+ };
27
+ /**
28
+ * Generic adapter that turns an existing {@link Keyring} implementation into a
29
+ * {@link KeyringV2} instance.
30
+ *
31
+ * Consumers are expected to provide concrete mappings between high-level V2
32
+ * operations and the underlying keyring methods (for example BIP-44 account
33
+ * creation, private-key import, and request handling). This class focuses on
34
+ * the common mechanics required by all adapters: state serialization,
35
+ * account-ID/address mapping and basic account management.
36
+ */
37
+ export declare abstract class KeyringWrapper<InnerKeyring extends Keyring, KeyringAccountType extends KeyringAccount = KeyringAccount> implements KeyringV2 {
38
+ #private;
39
+ readonly type: `${KeyringType}`;
40
+ protected readonly inner: InnerKeyring;
41
+ /**
42
+ * Registry for KeyringAccount objects.
43
+ * Provides O(1) lookups by AccountId or address.
44
+ *
45
+ * Subclasses should use this registry when creating accounts and
46
+ * clear/update it when deleting accounts or deserializing state.
47
+ */
48
+ protected readonly registry: KeyringAccountRegistry<KeyringAccountType>;
49
+ constructor(options: KeyringWrapperOptions<InnerKeyring>);
50
+ /**
51
+ * Get the capabilities of this keyring.
52
+ *
53
+ * Subclasses can override this getter to return capabilities dynamically
54
+ * based on runtime state.
55
+ *
56
+ * @returns The keyring's capabilities.
57
+ */
58
+ get capabilities(): KeyringCapabilities;
59
+ /**
60
+ * Execute an operation with exclusive access to the inner keyring.
61
+ *
62
+ * This method ensures thread-safety for operations that read or mutate
63
+ * the inner keyring state. All operations that modify the keyring
64
+ * (createAccounts, deleteAccount, deserialize) should use this method
65
+ * to prevent race conditions.
66
+ *
67
+ * Within the callback, use `this.inner` to access the inner keyring.
68
+ *
69
+ * @param callback - A function that performs the operation.
70
+ * @returns The result of the callback.
71
+ */
72
+ protected withLock<Result>(callback: () => Promise<Result>): Promise<Result>;
73
+ /**
74
+ * Serialize the underlying keyring state to a JSON-serializable object.
75
+ *
76
+ * This simply delegates to the legacy keyring's {@link Keyring.serialize}
77
+ * implementation.
78
+ *
79
+ * @returns The serialized keyring state.
80
+ */
81
+ serialize(): Promise<Json>;
82
+ /**
83
+ * Hydrate the underlying keyring from a previously serialized state.
84
+ *
85
+ * This clears the registry, delegates to the legacy keyring's
86
+ * {@link Keyring.deserialize} implementation, and rebuilds the registry
87
+ * by calling {@link getAccounts}.
88
+ *
89
+ * @param state - The serialized keyring state.
90
+ */
91
+ deserialize(state: Json): Promise<void>;
92
+ /**
93
+ * Return all accounts managed by this keyring.
94
+ *
95
+ * Concrete adapters are responsible for mapping the underlying keyring's
96
+ * notion of accounts (typically addresses returned by
97
+ * {@link Keyring.getAccounts}) into {@link KeyringAccount} objects.
98
+ * Implementations should use the configured {@link KeyringAccountRegistry}
99
+ * to establish the account ID/address mapping so that
100
+ * {@link getAccount} works as expected.
101
+ *
102
+ * @returns The list of managed accounts.
103
+ */
104
+ abstract getAccounts(): Promise<KeyringAccount[]>;
105
+ /**
106
+ * Look up a single account by its {@link AccountId}.
107
+ *
108
+ * This method first checks the registry for O(1) lookup.
109
+ * If not found, it falls back to calling {@link getAccounts} which
110
+ * should populate the registry as a side effect.
111
+ *
112
+ * @param accountId - The AccountId to look up.
113
+ * @returns The matching KeyringAccount.
114
+ */
115
+ getAccount(accountId: AccountId): Promise<KeyringAccount>;
116
+ /**
117
+ * Create one or more new accounts managed by this keyring.
118
+ *
119
+ * Implementations are responsible for interpreting the
120
+ * {@link CreateAccountOptions} (for example BIP-44 derivation or
121
+ * private-key import) and returning the resulting {@link KeyringAccount}
122
+ * objects. Implementors should also ensure that the registry is updated so
123
+ * that {@link getAccount} works for newly created accounts.
124
+ */
125
+ abstract createAccounts(options: CreateAccountOptions): Promise<KeyringAccount[]>;
126
+ /**
127
+ * Remove the account associated with the given {@link AccountId} from this
128
+ * keyring.
129
+ *
130
+ * Implementations are expected to translate the ID to an underlying
131
+ * address (typically via the registry) and then invoke the appropriate
132
+ * removal mechanism on the legacy keyring.
133
+ */
134
+ abstract deleteAccount(accountId: AccountId): Promise<void>;
135
+ /**
136
+ * Export the secrets associated with the given account in a format
137
+ * described by {@link ExportAccountOptions}.
138
+ *
139
+ * This method is optional, and concrete adapters should only
140
+ * implement it if the underlying keyring supports exporting
141
+ * accounts.
142
+ */
143
+ exportAccount?(accountId: AccountId, options?: ExportAccountOptions): Promise<ExportedAccount>;
144
+ /**
145
+ * Handle a high-level {@link KeyringRequest} on behalf of this keyring.
146
+ *
147
+ * Concrete adapters are responsible for routing the request's method and
148
+ * parameters to the appropriate legacy keyring APIs (for example signing
149
+ * transactions or decrypting messages) and returning a JSON-serializable
150
+ * result.
151
+ */
152
+ abstract submitRequest(request: KeyringRequest): Promise<Json>;
153
+ }
154
+ //# sourceMappingURL=keyring-wrapper.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyring-wrapper.d.cts","sourceRoot":"","sources":["../../../../src/api/v2/wrapper/keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,gCAAgC;AAClE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,EAAE,sBAAsB,EAAE,uCAAmC;AACpE,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EAChB,qBAAW;AACZ,OAAO,KAAK,EAAE,cAAc,EAAE,0BAAsB;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,0BAAsB;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,uBAAmB;AAC5C,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAgC;AACnE,OAAO,KAAK,EAAE,WAAW,EAAE,4BAAwB;AAEnD;;GAEG;AACH,MAAM,MAAM,qBAAqB,CAAC,YAAY,SAAS,OAAO,IAAI;IAChE;;OAEG;IACH,KAAK,EAAE,YAAY,CAAC;IAEpB;;OAEG;IACH,IAAI,EAAE,WAAW,CAAC;IAElB;;OAEG;IACH,YAAY,EAAE,mBAAmB,CAAC;CACnC,CAAC;AAEF;;;;;;;;;GASG;AACH,8BAAsB,cAAc,CAClC,YAAY,SAAS,OAAO,EAC5B,kBAAkB,SAAS,cAAc,GAAG,cAAc,CAC1D,YAAW,SAAS;;IAEpB,QAAQ,CAAC,IAAI,EAAE,GAAG,WAAW,EAAE,CAAC;IAIhC,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAQvC;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,6CACwB;gBAEvC,OAAO,EAAE,qBAAqB,CAAC,YAAY,CAAC;IAMxD;;;;;;;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"}
@@ -0,0 +1,154 @@
1
+ import type { Keyring, AccountId } from "@metamask/keyring-utils";
2
+ import type { Json } from "@metamask/utils";
3
+ import { KeyringAccountRegistry } from "./keyring-account-registry.mjs";
4
+ import type { CreateAccountOptions, ExportAccountOptions, ExportedAccount } from "../index.mjs";
5
+ import type { KeyringAccount } from "../../account.mjs";
6
+ import type { KeyringRequest } from "../../request.mjs";
7
+ import type { KeyringV2 } from "../keyring.mjs";
8
+ import type { KeyringCapabilities } from "../keyring-capabilities.mjs";
9
+ import type { KeyringType } from "../keyring-type.mjs";
10
+ /**
11
+ * Basic options for constructing a {@link KeyringWrapper}.
12
+ */
13
+ export type KeyringWrapperOptions<InnerKeyring extends Keyring> = {
14
+ /**
15
+ * The underlying "old" keyring instance that this wrapper adapts.
16
+ */
17
+ inner: InnerKeyring;
18
+ /**
19
+ * The concrete keyring type exposed through the V2 interface.
20
+ */
21
+ type: KeyringType;
22
+ /**
23
+ * Capabilities of the underlying keyring.
24
+ */
25
+ capabilities: KeyringCapabilities;
26
+ };
27
+ /**
28
+ * Generic adapter that turns an existing {@link Keyring} implementation into a
29
+ * {@link KeyringV2} instance.
30
+ *
31
+ * Consumers are expected to provide concrete mappings between high-level V2
32
+ * operations and the underlying keyring methods (for example BIP-44 account
33
+ * creation, private-key import, and request handling). This class focuses on
34
+ * the common mechanics required by all adapters: state serialization,
35
+ * account-ID/address mapping and basic account management.
36
+ */
37
+ export declare abstract class KeyringWrapper<InnerKeyring extends Keyring, KeyringAccountType extends KeyringAccount = KeyringAccount> implements KeyringV2 {
38
+ #private;
39
+ readonly type: `${KeyringType}`;
40
+ protected readonly inner: InnerKeyring;
41
+ /**
42
+ * Registry for KeyringAccount objects.
43
+ * Provides O(1) lookups by AccountId or address.
44
+ *
45
+ * Subclasses should use this registry when creating accounts and
46
+ * clear/update it when deleting accounts or deserializing state.
47
+ */
48
+ protected readonly registry: KeyringAccountRegistry<KeyringAccountType>;
49
+ constructor(options: KeyringWrapperOptions<InnerKeyring>);
50
+ /**
51
+ * Get the capabilities of this keyring.
52
+ *
53
+ * Subclasses can override this getter to return capabilities dynamically
54
+ * based on runtime state.
55
+ *
56
+ * @returns The keyring's capabilities.
57
+ */
58
+ get capabilities(): KeyringCapabilities;
59
+ /**
60
+ * Execute an operation with exclusive access to the inner keyring.
61
+ *
62
+ * This method ensures thread-safety for operations that read or mutate
63
+ * the inner keyring state. All operations that modify the keyring
64
+ * (createAccounts, deleteAccount, deserialize) should use this method
65
+ * to prevent race conditions.
66
+ *
67
+ * Within the callback, use `this.inner` to access the inner keyring.
68
+ *
69
+ * @param callback - A function that performs the operation.
70
+ * @returns The result of the callback.
71
+ */
72
+ protected withLock<Result>(callback: () => Promise<Result>): Promise<Result>;
73
+ /**
74
+ * Serialize the underlying keyring state to a JSON-serializable object.
75
+ *
76
+ * This simply delegates to the legacy keyring's {@link Keyring.serialize}
77
+ * implementation.
78
+ *
79
+ * @returns The serialized keyring state.
80
+ */
81
+ serialize(): Promise<Json>;
82
+ /**
83
+ * Hydrate the underlying keyring from a previously serialized state.
84
+ *
85
+ * This clears the registry, delegates to the legacy keyring's
86
+ * {@link Keyring.deserialize} implementation, and rebuilds the registry
87
+ * by calling {@link getAccounts}.
88
+ *
89
+ * @param state - The serialized keyring state.
90
+ */
91
+ deserialize(state: Json): Promise<void>;
92
+ /**
93
+ * Return all accounts managed by this keyring.
94
+ *
95
+ * Concrete adapters are responsible for mapping the underlying keyring's
96
+ * notion of accounts (typically addresses returned by
97
+ * {@link Keyring.getAccounts}) into {@link KeyringAccount} objects.
98
+ * Implementations should use the configured {@link KeyringAccountRegistry}
99
+ * to establish the account ID/address mapping so that
100
+ * {@link getAccount} works as expected.
101
+ *
102
+ * @returns The list of managed accounts.
103
+ */
104
+ abstract getAccounts(): Promise<KeyringAccount[]>;
105
+ /**
106
+ * Look up a single account by its {@link AccountId}.
107
+ *
108
+ * This method first checks the registry for O(1) lookup.
109
+ * If not found, it falls back to calling {@link getAccounts} which
110
+ * should populate the registry as a side effect.
111
+ *
112
+ * @param accountId - The AccountId to look up.
113
+ * @returns The matching KeyringAccount.
114
+ */
115
+ getAccount(accountId: AccountId): Promise<KeyringAccount>;
116
+ /**
117
+ * Create one or more new accounts managed by this keyring.
118
+ *
119
+ * Implementations are responsible for interpreting the
120
+ * {@link CreateAccountOptions} (for example BIP-44 derivation or
121
+ * private-key import) and returning the resulting {@link KeyringAccount}
122
+ * objects. Implementors should also ensure that the registry is updated so
123
+ * that {@link getAccount} works for newly created accounts.
124
+ */
125
+ abstract createAccounts(options: CreateAccountOptions): Promise<KeyringAccount[]>;
126
+ /**
127
+ * Remove the account associated with the given {@link AccountId} from this
128
+ * keyring.
129
+ *
130
+ * Implementations are expected to translate the ID to an underlying
131
+ * address (typically via the registry) and then invoke the appropriate
132
+ * removal mechanism on the legacy keyring.
133
+ */
134
+ abstract deleteAccount(accountId: AccountId): Promise<void>;
135
+ /**
136
+ * Export the secrets associated with the given account in a format
137
+ * described by {@link ExportAccountOptions}.
138
+ *
139
+ * This method is optional, and concrete adapters should only
140
+ * implement it if the underlying keyring supports exporting
141
+ * accounts.
142
+ */
143
+ exportAccount?(accountId: AccountId, options?: ExportAccountOptions): Promise<ExportedAccount>;
144
+ /**
145
+ * Handle a high-level {@link KeyringRequest} on behalf of this keyring.
146
+ *
147
+ * Concrete adapters are responsible for routing the request's method and
148
+ * parameters to the appropriate legacy keyring APIs (for example signing
149
+ * transactions or decrypting messages) and returning a JSON-serializable
150
+ * result.
151
+ */
152
+ abstract submitRequest(request: KeyringRequest): Promise<Json>;
153
+ }
154
+ //# sourceMappingURL=keyring-wrapper.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyring-wrapper.d.mts","sourceRoot":"","sources":["../../../../src/api/v2/wrapper/keyring-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,gCAAgC;AAClE,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,EAAE,sBAAsB,EAAE,uCAAmC;AACpE,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EAChB,qBAAW;AACZ,OAAO,KAAK,EAAE,cAAc,EAAE,0BAAsB;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,0BAAsB;AACpD,OAAO,KAAK,EAAE,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B,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"}
@@ -0,0 +1,130 @@
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
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _KeyringWrapper_capabilities, _KeyringWrapper_lock;
13
+ import { Mutex } from "async-mutex";
14
+ import { KeyringAccountRegistry } from "./keyring-account-registry.mjs";
15
+ /**
16
+ * Generic adapter that turns an existing {@link Keyring} implementation into a
17
+ * {@link KeyringV2} instance.
18
+ *
19
+ * Consumers are expected to provide concrete mappings between high-level V2
20
+ * operations and the underlying keyring methods (for example BIP-44 account
21
+ * creation, private-key import, and request handling). This class focuses on
22
+ * the common mechanics required by all adapters: state serialization,
23
+ * account-ID/address mapping and basic account management.
24
+ */
25
+ export class KeyringWrapper {
26
+ constructor(options) {
27
+ _KeyringWrapper_capabilities.set(this, void 0);
28
+ /**
29
+ * Mutex to ensure exclusive access to the inner keyring during
30
+ * operations that mutate its state.
31
+ */
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
+ this.inner = options.inner;
42
+ this.type = `${options.type}`;
43
+ __classPrivateFieldSet(this, _KeyringWrapper_capabilities, options.capabilities, "f");
44
+ }
45
+ /**
46
+ * Get the capabilities of this keyring.
47
+ *
48
+ * Subclasses can override this getter to return capabilities dynamically
49
+ * based on runtime state.
50
+ *
51
+ * @returns The keyring's capabilities.
52
+ */
53
+ get capabilities() {
54
+ return __classPrivateFieldGet(this, _KeyringWrapper_capabilities, "f");
55
+ }
56
+ /**
57
+ * Execute an operation with exclusive access to the inner keyring.
58
+ *
59
+ * This method ensures thread-safety for operations that read or mutate
60
+ * the inner keyring state. All operations that modify the keyring
61
+ * (createAccounts, deleteAccount, deserialize) should use this method
62
+ * to prevent race conditions.
63
+ *
64
+ * Within the callback, use `this.inner` to access the inner keyring.
65
+ *
66
+ * @param callback - A function that performs the operation.
67
+ * @returns The result of the callback.
68
+ */
69
+ async withLock(callback) {
70
+ return __classPrivateFieldGet(this, _KeyringWrapper_lock, "f").runExclusive(callback);
71
+ }
72
+ /**
73
+ * Serialize the underlying keyring state to a JSON-serializable object.
74
+ *
75
+ * This simply delegates to the legacy keyring's {@link Keyring.serialize}
76
+ * implementation.
77
+ *
78
+ * @returns The serialized keyring state.
79
+ */
80
+ async serialize() {
81
+ return this.inner.serialize();
82
+ }
83
+ /**
84
+ * Hydrate the underlying keyring from a previously serialized state.
85
+ *
86
+ * This clears the registry, delegates to the legacy keyring's
87
+ * {@link Keyring.deserialize} implementation, and rebuilds the registry
88
+ * by calling {@link getAccounts}.
89
+ *
90
+ * @param state - The serialized keyring state.
91
+ */
92
+ async deserialize(state) {
93
+ await this.withLock(async () => {
94
+ // Clear the registry when deserializing
95
+ this.registry.clear();
96
+ // Deserialize the legacy keyring
97
+ await this.inner.deserialize(state);
98
+ // Rebuild the registry by calling getAccounts().
99
+ // Subclass implementations of getAccounts() should populate the registry
100
+ // as a side effect (see the abstract method's documentation).
101
+ await this.getAccounts();
102
+ });
103
+ }
104
+ /**
105
+ * Look up a single account by its {@link AccountId}.
106
+ *
107
+ * This method first checks the registry for O(1) lookup.
108
+ * If not found, it falls back to calling {@link getAccounts} which
109
+ * should populate the registry as a side effect.
110
+ *
111
+ * @param accountId - The AccountId to look up.
112
+ * @returns The matching KeyringAccount.
113
+ */
114
+ async getAccount(accountId) {
115
+ let cached = this.registry.get(accountId);
116
+ if (cached) {
117
+ return cached;
118
+ }
119
+ // Prime the registry by calling getAccounts
120
+ await this.getAccounts();
121
+ // Try registry again after priming
122
+ cached = this.registry.get(accountId);
123
+ if (!cached) {
124
+ throw new Error(`Account not found for id: ${accountId}`);
125
+ }
126
+ return cached;
127
+ }
128
+ }
129
+ _KeyringWrapper_capabilities = new WeakMap(), _KeyringWrapper_lock = new WeakMap();
130
+ //# sourceMappingURL=keyring-wrapper.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyring-wrapper.mjs","sourceRoot":"","sources":["../../../../src/api/v2/wrapper/keyring-wrapper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAEpC,OAAO,EAAE,sBAAsB,EAAE,uCAAmC;AAgCpE;;;;;;;;;GASG;AACH,MAAM,OAAgB,cAAc;IA2BlC,YAAY,OAA4C;QApB/C,+CAAmC;QAI5C;;;WAGG;QACM,+BAAQ,IAAI,KAAK,EAAE,EAAC;QAE7B;;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AEzB,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 { Keyring, AccountId } from '@metamask/keyring-utils';\nimport type { Json } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport { KeyringAccountRegistry } from './keyring-account-registry';\nimport type {\n CreateAccountOptions,\n ExportAccountOptions,\n ExportedAccount,\n} from '..';\nimport type { KeyringAccount } from '../../account';\nimport type { KeyringRequest } from '../../request';\nimport type { KeyringV2 } from '../keyring';\nimport type { KeyringCapabilities } from '../keyring-capabilities';\nimport type { KeyringType } from '../keyring-type';\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/**\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 =\n new KeyringAccountRegistry<KeyringAccountType>();\n\n constructor(options: KeyringWrapperOptions<InnerKeyring>) {\n this.inner = options.inner;\n this.type = `${options.type}`;\n this.#capabilities = options.capabilities;\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"]}