@metamask/accounts-controller 31.0.0 → 32.0.1
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 +26 -1
- package/dist/AccountsController.cjs +125 -134
- package/dist/AccountsController.cjs.map +1 -1
- package/dist/AccountsController.d.cts.map +1 -1
- package/dist/AccountsController.d.mts.map +1 -1
- package/dist/AccountsController.mjs +128 -136
- package/dist/AccountsController.mjs.map +1 -1
- package/dist/tests/mocks.cjs +12 -2
- package/dist/tests/mocks.cjs.map +1 -1
- package/dist/tests/mocks.d.cts +13 -3
- package/dist/tests/mocks.d.cts.map +1 -1
- package/dist/tests/mocks.d.mts +13 -3
- package/dist/tests/mocks.d.mts.map +1 -1
- package/dist/tests/mocks.mjs +13 -3
- package/dist/tests/mocks.mjs.map +1 -1
- package/dist/utils.cjs +83 -5
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +59 -4
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +59 -4
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +77 -3
- package/dist/utils.mjs.map +1 -1
- package/package.json +15 -12
package/dist/utils.cjs
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.isHdSnapKeyringAccount = exports.HdSnapKeyringAccountOptionsStruct = exports.getEvmGroupIndexFromAddressIndex = exports.getEvmDerivationPathForIndex = exports.isHdKeyringType = exports.isSimpleKeyringType = exports.isSnapKeyringType = exports.isNormalKeyringType = exports.getUUIDFromAddressOfNormalAccount = exports.getUUIDOptionsFromAddressOfNormalAccount = exports.keyringTypeToName = void 0;
|
4
4
|
const keyring_controller_1 = require("@metamask/keyring-controller");
|
5
|
+
const superstruct_1 = require("@metamask/superstruct");
|
5
6
|
const utils_1 = require("@metamask/utils");
|
6
7
|
const sha256_1 = require("ethereum-cryptography/sha256");
|
7
8
|
const uuid_1 = require("uuid");
|
@@ -78,6 +79,26 @@ function isNormalKeyringType(keyringType) {
|
|
78
79
|
return keyringType !== keyring_controller_1.KeyringTypes.snap;
|
79
80
|
}
|
80
81
|
exports.isNormalKeyringType = isNormalKeyringType;
|
82
|
+
/**
|
83
|
+
* Check if a keyring type is a Snap keyring.
|
84
|
+
*
|
85
|
+
* @param keyringType - The account's keyring type.
|
86
|
+
* @returns True if the keyring type is considered a Snap keyring, false otherwise.
|
87
|
+
*/
|
88
|
+
function isSnapKeyringType(keyringType) {
|
89
|
+
return keyringType === keyring_controller_1.KeyringTypes.snap;
|
90
|
+
}
|
91
|
+
exports.isSnapKeyringType = isSnapKeyringType;
|
92
|
+
/**
|
93
|
+
* Check if a keyring type is a simple keyring.
|
94
|
+
*
|
95
|
+
* @param keyringType - The account's keyring type.
|
96
|
+
* @returns True if the keyring type is considered a simple keyring, false otherwise.
|
97
|
+
*/
|
98
|
+
function isSimpleKeyringType(keyringType) {
|
99
|
+
return keyringType === keyring_controller_1.KeyringTypes.simple;
|
100
|
+
}
|
101
|
+
exports.isSimpleKeyringType = isSimpleKeyringType;
|
81
102
|
/**
|
82
103
|
* Check if a keyring is a HD keyring.
|
83
104
|
*
|
@@ -89,13 +110,70 @@ function isHdKeyringType(keyringType) {
|
|
89
110
|
}
|
90
111
|
exports.isHdKeyringType = isHdKeyringType;
|
91
112
|
/**
|
92
|
-
* Get the derivation path for the index of an account within a HD keyring.
|
113
|
+
* Get the derivation path for the index of an account within a EVM HD keyring.
|
93
114
|
*
|
94
115
|
* @param index - The account index.
|
95
116
|
* @returns The derivation path.
|
96
117
|
*/
|
97
|
-
function
|
98
|
-
|
118
|
+
function getEvmDerivationPathForIndex(index) {
|
119
|
+
const purpose = '44';
|
120
|
+
const coinType = '60'; // Ethereum.
|
121
|
+
return `m/${purpose}'/${coinType}'/0'/0/${index}`;
|
122
|
+
}
|
123
|
+
exports.getEvmDerivationPathForIndex = getEvmDerivationPathForIndex;
|
124
|
+
/**
|
125
|
+
* Get the group index from a keyring object (EVM HD keyring only) and an address.
|
126
|
+
*
|
127
|
+
* @param keyring - The keyring object.
|
128
|
+
* @param address - The address to match.
|
129
|
+
* @returns The group index for that address, undefined if not able to match the address.
|
130
|
+
*/
|
131
|
+
function getEvmGroupIndexFromAddressIndex(keyring, address) {
|
132
|
+
// TODO: Remove this function once EVM HD keyrings start using the new unified
|
133
|
+
// keyring API.
|
134
|
+
// NOTE: We mostly put that logic in a separate function so we can easily add coverage
|
135
|
+
// for (supposedly) unreachable code path.
|
136
|
+
if (!isHdKeyringType(keyring.type)) {
|
137
|
+
// We cannot extract the group index from non-HD keyrings.
|
138
|
+
return undefined;
|
139
|
+
}
|
140
|
+
// We need to find the account index from its HD keyring. We assume those
|
141
|
+
// accounts are ordered, thus we can use their index to compute their
|
142
|
+
// derivation path and group index.
|
143
|
+
const groupIndex = keyring.accounts.findIndex(
|
144
|
+
// NOTE: This is ok to use `toLowerCase` here, since we're only dealing
|
145
|
+
// with EVM addresses.
|
146
|
+
(accountAddress) => accountAddress.toLowerCase() === address.toLowerCase());
|
147
|
+
// If for some reason, we cannot find this address, then the caller made a mistake
|
148
|
+
// and it did not use the proper keyring object. For now, we do not fail and just
|
149
|
+
// consider this account as "simple account".
|
150
|
+
if (groupIndex === -1) {
|
151
|
+
console.warn(`! Unable to get group index for HD account: "${address}"`);
|
152
|
+
return undefined;
|
153
|
+
}
|
154
|
+
return groupIndex;
|
155
|
+
}
|
156
|
+
exports.getEvmGroupIndexFromAddressIndex = getEvmGroupIndexFromAddressIndex;
|
157
|
+
/**
|
158
|
+
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
159
|
+
* Solana Snap).
|
160
|
+
*
|
161
|
+
* NOTE: We use `superstruct.type` here `superstruct.object` since it allows
|
162
|
+
* extra-properties than a Snap might add in its `options`.
|
163
|
+
*/
|
164
|
+
exports.HdSnapKeyringAccountOptionsStruct = (0, superstruct_1.type)({
|
165
|
+
entropySource: (0, superstruct_1.string)(),
|
166
|
+
index: (0, superstruct_1.number)(),
|
167
|
+
derivationPath: (0, superstruct_1.string)(),
|
168
|
+
});
|
169
|
+
/**
|
170
|
+
* Check if an account is an HD Snap keyring account.
|
171
|
+
*
|
172
|
+
* @param account - Snap keyring account.
|
173
|
+
* @returns True if valid, false otherwise.
|
174
|
+
*/
|
175
|
+
function isHdSnapKeyringAccount(account) {
|
176
|
+
return (0, superstruct_1.is)(account.options, exports.HdSnapKeyringAccountOptionsStruct);
|
99
177
|
}
|
100
|
-
exports.
|
178
|
+
exports.isHdSnapKeyringAccount = isHdSnapKeyringAccount;
|
101
179
|
//# sourceMappingURL=utils.cjs.map
|
package/dist/utils.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,qEAA4D;AAG5D,uDAAiE;AACjE,2CAA6C;AAC7C,yDAAsD;AAEtD,+BAAkC;AAElC;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,WAAmB;IACnD,QAAQ,WAAW,EAAE;QACnB,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,SAAS,CAAC;SAClB;QACD,KAAK,iCAAY,CAAC,EAAE,CAAC,CAAC;YACpB,OAAO,SAAS,CAAC;SAClB;QACD,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,iCAAY,CAAC,OAAO,CAAC,CAAC;YACzB,OAAO,SAAS,CAAC;SAClB;QACD,KAAK,iCAAY,CAAC,E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AAC;IACrB,MAAM,QAAQ,GAAG,IAAI,CAAC,CAAC,YAAY;IACnC,OAAO,KAAK,OAAO,KAAK,QAAQ,UAAU,KAAK,EAAE,CAAC;AACpD,CAAC;AAJD,oEAIC;AAED;;;;;;GAMG;AACH,SAAgB,gCAAgC,CAC9C,OAAsB,EACtB,OAAe;IAEf,8EAA8E;IAC9E,eAAe;IAEf,sFAAsF;IACtF,0CAA0C;IAE1C,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QAClC,0DAA0D;QAC1D,OAAO,SAAS,CAAC;KAClB;IAED,yEAAyE;IACzE,qEAAqE;IACrE,mCAAmC;IACnC,MAAM,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC3C,uEAAuE;IACvE,sBAAsB;IACtB,CAAC,cAAc,EAAE,EAAE,CAAC,cAAc,CAAC,WAAW,EAAE,KAAK,OAAO,CAAC,WAAW,EAAE,CAC3E,CAAC;IAEF,kFAAkF;IAClF,iFAAiF;IACjF,6CAA6C;IAC7C,IAAI,UAAU,KAAK,CAAC,CAAC,EAAE;QACrB,OAAO,CAAC,IAAI,CAAC,gDAAgD,OAAO,GAAG,CAAC,CAAC;QACzE,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAjCD,4EAiCC;AAED;;;;;;GAMG;AACU,QAAA,iCAAiC,GAAG,IAAA,kBAAI,EAAC;IACpD,aAAa,EAAE,IAAA,oBAAM,GAAE;IACvB,KAAK,EAAE,IAAA,oBAAM,GAAE;IACf,cAAc,EAAE,IAAA,oBAAM,GAAE;CACzB,CAAC,CAAC;AAYH;;;;;GAKG;AACH,SAAgB,sBAAsB,CACpC,OAAwB;IAExB,OAAO,IAAA,gBAAE,EAAC,OAAO,CAAC,OAAO,EAAE,yCAAiC,CAAC,CAAC;AAChE,CAAC;AAJD,wDAIC","sourcesContent":["import type { KeyringObject } from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Infer } from '@metamask/superstruct';\nimport { is, number, string, type } from '@metamask/superstruct';\nimport { hexToBytes } from '@metamask/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport type { V4Options } from 'uuid';\nimport { v4 as uuid } from 'uuid';\n\n/**\n * Returns the name of the keyring type.\n *\n * @param keyringType - The type of the keyring.\n * @returns The name of the keyring type.\n */\nexport function keyringTypeToName(keyringType: string): string {\n switch (keyringType) {\n case KeyringTypes.simple: {\n return 'Account';\n }\n case KeyringTypes.hd: {\n return 'Account';\n }\n case KeyringTypes.trezor: {\n return 'Trezor';\n }\n case KeyringTypes.oneKey: {\n return 'OneKey';\n }\n case KeyringTypes.ledger: {\n return 'Ledger';\n }\n case KeyringTypes.lattice: {\n return 'Lattice';\n }\n case KeyringTypes.qr: {\n return 'QR';\n }\n case KeyringTypes.snap: {\n return 'Snap Account';\n }\n default: {\n throw new Error(`Unknown keyring ${keyringType}`);\n }\n }\n}\n\n/**\n * Generates a UUID v4 options from a given Ethereum address.\n *\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The UUID v4 options.\n */\nexport function getUUIDOptionsFromAddressOfNormalAccount(\n address: string,\n): V4Options {\n const v4options = {\n random: sha256(hexToBytes(address)).slice(0, 16),\n };\n\n return v4options;\n}\n\n/**\n * Generates a UUID from a given Ethereum address.\n *\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The generated UUID.\n */\nexport function getUUIDFromAddressOfNormalAccount(address: string): string {\n return uuid(getUUIDOptionsFromAddressOfNormalAccount(address));\n}\n\n/**\n * Check if a keyring type is considered a \"normal\" keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is considered a \"normal\" keyring, false otherwise.\n */\nexport function isNormalKeyringType(\n keyringType: KeyringTypes | string,\n): boolean {\n // Right now, we only have to \"exclude\" Snap accounts, but this might need to be\n // adapted later on if we have new kind of keyrings!\n return keyringType !== (KeyringTypes.snap as string);\n}\n\n/**\n * Check if a keyring type is a Snap keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is considered a Snap keyring, false otherwise.\n */\nexport function isSnapKeyringType(keyringType: KeyringTypes | string): boolean {\n return keyringType === (KeyringTypes.snap as string);\n}\n\n/**\n * Check if a keyring type is a simple keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is considered a simple keyring, false otherwise.\n */\nexport function isSimpleKeyringType(\n keyringType: KeyringTypes | string,\n): boolean {\n return keyringType === (KeyringTypes.simple as string);\n}\n\n/**\n * Check if a keyring is a HD keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring is a HD keyring, false otherwise.\n */\nexport function isHdKeyringType(keyringType: KeyringTypes | string): boolean {\n return keyringType === (KeyringTypes.hd as string);\n}\n\n/**\n * Get the derivation path for the index of an account within a EVM HD keyring.\n *\n * @param index - The account index.\n * @returns The derivation path.\n */\nexport function getEvmDerivationPathForIndex(index: number): string {\n const purpose = '44';\n const coinType = '60'; // Ethereum.\n return `m/${purpose}'/${coinType}'/0'/0/${index}`;\n}\n\n/**\n * Get the group index from a keyring object (EVM HD keyring only) and an address.\n *\n * @param keyring - The keyring object.\n * @param address - The address to match.\n * @returns The group index for that address, undefined if not able to match the address.\n */\nexport function getEvmGroupIndexFromAddressIndex(\n keyring: KeyringObject,\n address: string,\n): number | undefined {\n // TODO: Remove this function once EVM HD keyrings start using the new unified\n // keyring API.\n\n // NOTE: We mostly put that logic in a separate function so we can easily add coverage\n // for (supposedly) unreachable code path.\n\n if (!isHdKeyringType(keyring.type)) {\n // We cannot extract the group index from non-HD keyrings.\n return undefined;\n }\n\n // We need to find the account index from its HD keyring. We assume those\n // accounts are ordered, thus we can use their index to compute their\n // derivation path and group index.\n const groupIndex = keyring.accounts.findIndex(\n // NOTE: This is ok to use `toLowerCase` here, since we're only dealing\n // with EVM addresses.\n (accountAddress) => accountAddress.toLowerCase() === address.toLowerCase(),\n );\n\n // If for some reason, we cannot find this address, then the caller made a mistake\n // and it did not use the proper keyring object. For now, we do not fail and just\n // consider this account as \"simple account\".\n if (groupIndex === -1) {\n console.warn(`! Unable to get group index for HD account: \"${address}\"`);\n return undefined;\n }\n\n return groupIndex;\n}\n\n/**\n * HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the\n * Solana Snap).\n *\n * NOTE: We use `superstruct.type` here `superstruct.object` since it allows\n * extra-properties than a Snap might add in its `options`.\n */\nexport const HdSnapKeyringAccountOptionsStruct = type({\n entropySource: string(),\n index: number(),\n derivationPath: string(),\n});\nexport type HdSnapKeyringAccountOptions = Infer<\n typeof HdSnapKeyringAccountOptionsStruct\n>;\n\n/**\n * HD keyring account for Snap accounts that handles non-EVM HD accounts.\n */\nexport type HdSnapKeyringAccount = InternalAccount & {\n options: InternalAccount['options'] & HdSnapKeyringAccountOptions;\n};\n\n/**\n * Check if an account is an HD Snap keyring account.\n *\n * @param account - Snap keyring account.\n * @returns True if valid, false otherwise.\n */\nexport function isHdSnapKeyringAccount(\n account: InternalAccount,\n): account is HdSnapKeyringAccount {\n return is(account.options, HdSnapKeyringAccountOptionsStruct);\n}\n"]}
|
package/dist/utils.d.cts
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
import type { KeyringObject } from "@metamask/keyring-controller";
|
1
2
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
3
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
4
|
+
import type { Infer } from "@metamask/superstruct";
|
2
5
|
import type { V4Options } from "uuid";
|
3
6
|
/**
|
4
7
|
* Returns the name of the keyring type.
|
@@ -27,19 +30,71 @@ export declare function getUUIDFromAddressOfNormalAccount(address: string): stri
|
|
27
30
|
* @param keyringType - The account's keyring type.
|
28
31
|
* @returns True if the keyring type is considered a "normal" keyring, false otherwise.
|
29
32
|
*/
|
30
|
-
export declare function isNormalKeyringType(keyringType: KeyringTypes): boolean;
|
33
|
+
export declare function isNormalKeyringType(keyringType: KeyringTypes | string): boolean;
|
34
|
+
/**
|
35
|
+
* Check if a keyring type is a Snap keyring.
|
36
|
+
*
|
37
|
+
* @param keyringType - The account's keyring type.
|
38
|
+
* @returns True if the keyring type is considered a Snap keyring, false otherwise.
|
39
|
+
*/
|
40
|
+
export declare function isSnapKeyringType(keyringType: KeyringTypes | string): boolean;
|
41
|
+
/**
|
42
|
+
* Check if a keyring type is a simple keyring.
|
43
|
+
*
|
44
|
+
* @param keyringType - The account's keyring type.
|
45
|
+
* @returns True if the keyring type is considered a simple keyring, false otherwise.
|
46
|
+
*/
|
47
|
+
export declare function isSimpleKeyringType(keyringType: KeyringTypes | string): boolean;
|
31
48
|
/**
|
32
49
|
* Check if a keyring is a HD keyring.
|
33
50
|
*
|
34
51
|
* @param keyringType - The account's keyring type.
|
35
52
|
* @returns True if the keyring is a HD keyring, false otherwise.
|
36
53
|
*/
|
37
|
-
export declare function isHdKeyringType(keyringType: KeyringTypes): boolean;
|
54
|
+
export declare function isHdKeyringType(keyringType: KeyringTypes | string): boolean;
|
38
55
|
/**
|
39
|
-
* Get the derivation path for the index of an account within a HD keyring.
|
56
|
+
* Get the derivation path for the index of an account within a EVM HD keyring.
|
40
57
|
*
|
41
58
|
* @param index - The account index.
|
42
59
|
* @returns The derivation path.
|
43
60
|
*/
|
44
|
-
export declare function
|
61
|
+
export declare function getEvmDerivationPathForIndex(index: number): string;
|
62
|
+
/**
|
63
|
+
* Get the group index from a keyring object (EVM HD keyring only) and an address.
|
64
|
+
*
|
65
|
+
* @param keyring - The keyring object.
|
66
|
+
* @param address - The address to match.
|
67
|
+
* @returns The group index for that address, undefined if not able to match the address.
|
68
|
+
*/
|
69
|
+
export declare function getEvmGroupIndexFromAddressIndex(keyring: KeyringObject, address: string): number | undefined;
|
70
|
+
/**
|
71
|
+
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
72
|
+
* Solana Snap).
|
73
|
+
*
|
74
|
+
* NOTE: We use `superstruct.type` here `superstruct.object` since it allows
|
75
|
+
* extra-properties than a Snap might add in its `options`.
|
76
|
+
*/
|
77
|
+
export declare const HdSnapKeyringAccountOptionsStruct: import("@metamask/superstruct").Struct<{
|
78
|
+
entropySource: string;
|
79
|
+
index: number;
|
80
|
+
derivationPath: string;
|
81
|
+
}, {
|
82
|
+
entropySource: import("@metamask/superstruct").Struct<string, null>;
|
83
|
+
index: import("@metamask/superstruct").Struct<number, null>;
|
84
|
+
derivationPath: import("@metamask/superstruct").Struct<string, null>;
|
85
|
+
}>;
|
86
|
+
export type HdSnapKeyringAccountOptions = Infer<typeof HdSnapKeyringAccountOptionsStruct>;
|
87
|
+
/**
|
88
|
+
* HD keyring account for Snap accounts that handles non-EVM HD accounts.
|
89
|
+
*/
|
90
|
+
export type HdSnapKeyringAccount = InternalAccount & {
|
91
|
+
options: InternalAccount['options'] & HdSnapKeyringAccountOptions;
|
92
|
+
};
|
93
|
+
/**
|
94
|
+
* Check if an account is an HD Snap keyring account.
|
95
|
+
*
|
96
|
+
* @param account - Snap keyring account.
|
97
|
+
* @returns True if valid, false otherwise.
|
98
|
+
*/
|
99
|
+
export declare function isHdSnapKeyringAccount(account: InternalAccount): account is HdSnapKeyringAccount;
|
45
100
|
//# sourceMappingURL=utils.d.cts.map
|
package/dist/utils.d.cts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,qCAAqC;
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,qCAAqC;AAClE,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAInD,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa;AAGtC;;;;;GAKG;AACH,wBAAgB,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2
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
3
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
4
|
+
import type { Infer } from "@metamask/superstruct";
|
2
5
|
import type { V4Options } from "uuid";
|
3
6
|
/**
|
4
7
|
* Returns the name of the keyring type.
|
@@ -27,19 +30,71 @@ export declare function getUUIDFromAddressOfNormalAccount(address: string): stri
|
|
27
30
|
* @param keyringType - The account's keyring type.
|
28
31
|
* @returns True if the keyring type is considered a "normal" keyring, false otherwise.
|
29
32
|
*/
|
30
|
-
export declare function isNormalKeyringType(keyringType: KeyringTypes): boolean;
|
33
|
+
export declare function isNormalKeyringType(keyringType: KeyringTypes | string): boolean;
|
34
|
+
/**
|
35
|
+
* Check if a keyring type is a Snap keyring.
|
36
|
+
*
|
37
|
+
* @param keyringType - The account's keyring type.
|
38
|
+
* @returns True if the keyring type is considered a Snap keyring, false otherwise.
|
39
|
+
*/
|
40
|
+
export declare function isSnapKeyringType(keyringType: KeyringTypes | string): boolean;
|
41
|
+
/**
|
42
|
+
* Check if a keyring type is a simple keyring.
|
43
|
+
*
|
44
|
+
* @param keyringType - The account's keyring type.
|
45
|
+
* @returns True if the keyring type is considered a simple keyring, false otherwise.
|
46
|
+
*/
|
47
|
+
export declare function isSimpleKeyringType(keyringType: KeyringTypes | string): boolean;
|
31
48
|
/**
|
32
49
|
* Check if a keyring is a HD keyring.
|
33
50
|
*
|
34
51
|
* @param keyringType - The account's keyring type.
|
35
52
|
* @returns True if the keyring is a HD keyring, false otherwise.
|
36
53
|
*/
|
37
|
-
export declare function isHdKeyringType(keyringType: KeyringTypes): boolean;
|
54
|
+
export declare function isHdKeyringType(keyringType: KeyringTypes | string): boolean;
|
38
55
|
/**
|
39
|
-
* Get the derivation path for the index of an account within a HD keyring.
|
56
|
+
* Get the derivation path for the index of an account within a EVM HD keyring.
|
40
57
|
*
|
41
58
|
* @param index - The account index.
|
42
59
|
* @returns The derivation path.
|
43
60
|
*/
|
44
|
-
export declare function
|
61
|
+
export declare function getEvmDerivationPathForIndex(index: number): string;
|
62
|
+
/**
|
63
|
+
* Get the group index from a keyring object (EVM HD keyring only) and an address.
|
64
|
+
*
|
65
|
+
* @param keyring - The keyring object.
|
66
|
+
* @param address - The address to match.
|
67
|
+
* @returns The group index for that address, undefined if not able to match the address.
|
68
|
+
*/
|
69
|
+
export declare function getEvmGroupIndexFromAddressIndex(keyring: KeyringObject, address: string): number | undefined;
|
70
|
+
/**
|
71
|
+
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
72
|
+
* Solana Snap).
|
73
|
+
*
|
74
|
+
* NOTE: We use `superstruct.type` here `superstruct.object` since it allows
|
75
|
+
* extra-properties than a Snap might add in its `options`.
|
76
|
+
*/
|
77
|
+
export declare const HdSnapKeyringAccountOptionsStruct: import("@metamask/superstruct").Struct<{
|
78
|
+
entropySource: string;
|
79
|
+
index: number;
|
80
|
+
derivationPath: string;
|
81
|
+
}, {
|
82
|
+
entropySource: import("@metamask/superstruct").Struct<string, null>;
|
83
|
+
index: import("@metamask/superstruct").Struct<number, null>;
|
84
|
+
derivationPath: import("@metamask/superstruct").Struct<string, null>;
|
85
|
+
}>;
|
86
|
+
export type HdSnapKeyringAccountOptions = Infer<typeof HdSnapKeyringAccountOptionsStruct>;
|
87
|
+
/**
|
88
|
+
* HD keyring account for Snap accounts that handles non-EVM HD accounts.
|
89
|
+
*/
|
90
|
+
export type HdSnapKeyringAccount = InternalAccount & {
|
91
|
+
options: InternalAccount['options'] & HdSnapKeyringAccountOptions;
|
92
|
+
};
|
93
|
+
/**
|
94
|
+
* Check if an account is an HD Snap keyring account.
|
95
|
+
*
|
96
|
+
* @param account - Snap keyring account.
|
97
|
+
* @returns True if valid, false otherwise.
|
98
|
+
*/
|
99
|
+
export declare function isHdSnapKeyringAccount(account: InternalAccount): account is HdSnapKeyringAccount;
|
45
100
|
//# sourceMappingURL=utils.d.mts.map
|
package/dist/utils.d.mts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,qCAAqC;
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,qCAAqC;AAClE,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAInD,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa;AAGtC;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CA8B7D;AAED;;;;;GAKG;AACH,wBAAgB,wCAAwC,CACtD,OAAO,EAAE,MAAM,GACd,SAAS,CAMX;AAED;;;;;GAKG;AACH,wBAAgB,iCAAiC,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,WAAW,EAAE,YAAY,GAAG,MAAM,GACjC,OAAO,CAIT;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,YAAY,GAAG,MAAM,GAAG,OAAO,CAE7E;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,WAAW,EAAE,YAAY,GAAG,MAAM,GACjC,OAAO,CAET;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,YAAY,GAAG,MAAM,GAAG,OAAO,CAE3E;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIlE;AAED;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,aAAa,EACtB,OAAO,EAAE,MAAM,GACd,MAAM,GAAG,SAAS,CA8BpB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iCAAiC;;;;;;;;EAI5C,CAAC;AACH,MAAM,MAAM,2BAA2B,GAAG,KAAK,CAC7C,OAAO,iCAAiC,CACzC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,eAAe,GAAG;IACnD,OAAO,EAAE,eAAe,CAAC,SAAS,CAAC,GAAG,2BAA2B,CAAC;CACnE,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,eAAe,GACvB,OAAO,IAAI,oBAAoB,CAEjC"}
|
package/dist/utils.mjs
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
2
|
+
import { is, number, string, type } from "@metamask/superstruct";
|
2
3
|
import { hexToBytes } from "@metamask/utils";
|
3
4
|
import { sha256 } from "ethereum-cryptography/sha256";
|
4
5
|
import { v4 as uuid } from "uuid";
|
@@ -71,6 +72,24 @@ export function isNormalKeyringType(keyringType) {
|
|
71
72
|
// adapted later on if we have new kind of keyrings!
|
72
73
|
return keyringType !== KeyringTypes.snap;
|
73
74
|
}
|
75
|
+
/**
|
76
|
+
* Check if a keyring type is a Snap keyring.
|
77
|
+
*
|
78
|
+
* @param keyringType - The account's keyring type.
|
79
|
+
* @returns True if the keyring type is considered a Snap keyring, false otherwise.
|
80
|
+
*/
|
81
|
+
export function isSnapKeyringType(keyringType) {
|
82
|
+
return keyringType === KeyringTypes.snap;
|
83
|
+
}
|
84
|
+
/**
|
85
|
+
* Check if a keyring type is a simple keyring.
|
86
|
+
*
|
87
|
+
* @param keyringType - The account's keyring type.
|
88
|
+
* @returns True if the keyring type is considered a simple keyring, false otherwise.
|
89
|
+
*/
|
90
|
+
export function isSimpleKeyringType(keyringType) {
|
91
|
+
return keyringType === KeyringTypes.simple;
|
92
|
+
}
|
74
93
|
/**
|
75
94
|
* Check if a keyring is a HD keyring.
|
76
95
|
*
|
@@ -81,12 +100,67 @@ export function isHdKeyringType(keyringType) {
|
|
81
100
|
return keyringType === KeyringTypes.hd;
|
82
101
|
}
|
83
102
|
/**
|
84
|
-
* Get the derivation path for the index of an account within a HD keyring.
|
103
|
+
* Get the derivation path for the index of an account within a EVM HD keyring.
|
85
104
|
*
|
86
105
|
* @param index - The account index.
|
87
106
|
* @returns The derivation path.
|
88
107
|
*/
|
89
|
-
export function
|
90
|
-
|
108
|
+
export function getEvmDerivationPathForIndex(index) {
|
109
|
+
const purpose = '44';
|
110
|
+
const coinType = '60'; // Ethereum.
|
111
|
+
return `m/${purpose}'/${coinType}'/0'/0/${index}`;
|
112
|
+
}
|
113
|
+
/**
|
114
|
+
* Get the group index from a keyring object (EVM HD keyring only) and an address.
|
115
|
+
*
|
116
|
+
* @param keyring - The keyring object.
|
117
|
+
* @param address - The address to match.
|
118
|
+
* @returns The group index for that address, undefined if not able to match the address.
|
119
|
+
*/
|
120
|
+
export function getEvmGroupIndexFromAddressIndex(keyring, address) {
|
121
|
+
// TODO: Remove this function once EVM HD keyrings start using the new unified
|
122
|
+
// keyring API.
|
123
|
+
// NOTE: We mostly put that logic in a separate function so we can easily add coverage
|
124
|
+
// for (supposedly) unreachable code path.
|
125
|
+
if (!isHdKeyringType(keyring.type)) {
|
126
|
+
// We cannot extract the group index from non-HD keyrings.
|
127
|
+
return undefined;
|
128
|
+
}
|
129
|
+
// We need to find the account index from its HD keyring. We assume those
|
130
|
+
// accounts are ordered, thus we can use their index to compute their
|
131
|
+
// derivation path and group index.
|
132
|
+
const groupIndex = keyring.accounts.findIndex(
|
133
|
+
// NOTE: This is ok to use `toLowerCase` here, since we're only dealing
|
134
|
+
// with EVM addresses.
|
135
|
+
(accountAddress) => accountAddress.toLowerCase() === address.toLowerCase());
|
136
|
+
// If for some reason, we cannot find this address, then the caller made a mistake
|
137
|
+
// and it did not use the proper keyring object. For now, we do not fail and just
|
138
|
+
// consider this account as "simple account".
|
139
|
+
if (groupIndex === -1) {
|
140
|
+
console.warn(`! Unable to get group index for HD account: "${address}"`);
|
141
|
+
return undefined;
|
142
|
+
}
|
143
|
+
return groupIndex;
|
144
|
+
}
|
145
|
+
/**
|
146
|
+
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
147
|
+
* Solana Snap).
|
148
|
+
*
|
149
|
+
* NOTE: We use `superstruct.type` here `superstruct.object` since it allows
|
150
|
+
* extra-properties than a Snap might add in its `options`.
|
151
|
+
*/
|
152
|
+
export const HdSnapKeyringAccountOptionsStruct = type({
|
153
|
+
entropySource: string(),
|
154
|
+
index: number(),
|
155
|
+
derivationPath: string(),
|
156
|
+
});
|
157
|
+
/**
|
158
|
+
* Check if an account is an HD Snap keyring account.
|
159
|
+
*
|
160
|
+
* @param account - Snap keyring account.
|
161
|
+
* @returns True if valid, false otherwise.
|
162
|
+
*/
|
163
|
+
export function isHdSnapKeyringAccount(account) {
|
164
|
+
return is(account.options, HdSnapKeyringAccountOptionsStruct);
|
91
165
|
}
|
92
166
|
//# sourceMappingURL=utils.mjs.map
|
package/dist/utils.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAG5D,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,8BAA8B;AACjE,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAC7C,OAAO,EAAE,MAAM,EAAE,qCAAqC;AAEtD,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,aAAa;AAElC;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,WAAmB;IACnD,QAAQ,WAAW,EAAE;QACnB,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,SAAS,CAAC;SAClB;QACD,KAAK,YAAY,CAAC,EAAE,CAAC,CAAC;YACpB,OAAO,S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;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,OAAwB;IAExB,OAAO,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,iCAAiC,CAAC,CAAC;AAChE,CAAC","sourcesContent":["import type { KeyringObject } from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Infer } from '@metamask/superstruct';\nimport { is, number, string, type } from '@metamask/superstruct';\nimport { hexToBytes } from '@metamask/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport type { V4Options } from 'uuid';\nimport { v4 as uuid } from 'uuid';\n\n/**\n * Returns the name of the keyring type.\n *\n * @param keyringType - The type of the keyring.\n * @returns The name of the keyring type.\n */\nexport function keyringTypeToName(keyringType: string): string {\n switch (keyringType) {\n case KeyringTypes.simple: {\n return 'Account';\n }\n case KeyringTypes.hd: {\n return 'Account';\n }\n case KeyringTypes.trezor: {\n return 'Trezor';\n }\n case KeyringTypes.oneKey: {\n return 'OneKey';\n }\n case KeyringTypes.ledger: {\n return 'Ledger';\n }\n case KeyringTypes.lattice: {\n return 'Lattice';\n }\n case KeyringTypes.qr: {\n return 'QR';\n }\n case KeyringTypes.snap: {\n return 'Snap Account';\n }\n default: {\n throw new Error(`Unknown keyring ${keyringType}`);\n }\n }\n}\n\n/**\n * Generates a UUID v4 options from a given Ethereum address.\n *\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The UUID v4 options.\n */\nexport function getUUIDOptionsFromAddressOfNormalAccount(\n address: string,\n): V4Options {\n const v4options = {\n random: sha256(hexToBytes(address)).slice(0, 16),\n };\n\n return v4options;\n}\n\n/**\n * Generates a UUID from a given Ethereum address.\n *\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The generated UUID.\n */\nexport function getUUIDFromAddressOfNormalAccount(address: string): string {\n return uuid(getUUIDOptionsFromAddressOfNormalAccount(address));\n}\n\n/**\n * Check if a keyring type is considered a \"normal\" keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is considered a \"normal\" keyring, false otherwise.\n */\nexport function isNormalKeyringType(\n keyringType: KeyringTypes | string,\n): boolean {\n // Right now, we only have to \"exclude\" Snap accounts, but this might need to be\n // adapted later on if we have new kind of keyrings!\n return keyringType !== (KeyringTypes.snap as string);\n}\n\n/**\n * Check if a keyring type is a Snap keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is considered a Snap keyring, false otherwise.\n */\nexport function isSnapKeyringType(keyringType: KeyringTypes | string): boolean {\n return keyringType === (KeyringTypes.snap as string);\n}\n\n/**\n * Check if a keyring type is a simple keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is considered a simple keyring, false otherwise.\n */\nexport function isSimpleKeyringType(\n keyringType: KeyringTypes | string,\n): boolean {\n return keyringType === (KeyringTypes.simple as string);\n}\n\n/**\n * Check if a keyring is a HD keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring is a HD keyring, false otherwise.\n */\nexport function isHdKeyringType(keyringType: KeyringTypes | string): boolean {\n return keyringType === (KeyringTypes.hd as string);\n}\n\n/**\n * Get the derivation path for the index of an account within a EVM HD keyring.\n *\n * @param index - The account index.\n * @returns The derivation path.\n */\nexport function getEvmDerivationPathForIndex(index: number): string {\n const purpose = '44';\n const coinType = '60'; // Ethereum.\n return `m/${purpose}'/${coinType}'/0'/0/${index}`;\n}\n\n/**\n * Get the group index from a keyring object (EVM HD keyring only) and an address.\n *\n * @param keyring - The keyring object.\n * @param address - The address to match.\n * @returns The group index for that address, undefined if not able to match the address.\n */\nexport function getEvmGroupIndexFromAddressIndex(\n keyring: KeyringObject,\n address: string,\n): number | undefined {\n // TODO: Remove this function once EVM HD keyrings start using the new unified\n // keyring API.\n\n // NOTE: We mostly put that logic in a separate function so we can easily add coverage\n // for (supposedly) unreachable code path.\n\n if (!isHdKeyringType(keyring.type)) {\n // We cannot extract the group index from non-HD keyrings.\n return undefined;\n }\n\n // We need to find the account index from its HD keyring. We assume those\n // accounts are ordered, thus we can use their index to compute their\n // derivation path and group index.\n const groupIndex = keyring.accounts.findIndex(\n // NOTE: This is ok to use `toLowerCase` here, since we're only dealing\n // with EVM addresses.\n (accountAddress) => accountAddress.toLowerCase() === address.toLowerCase(),\n );\n\n // If for some reason, we cannot find this address, then the caller made a mistake\n // and it did not use the proper keyring object. For now, we do not fail and just\n // consider this account as \"simple account\".\n if (groupIndex === -1) {\n console.warn(`! Unable to get group index for HD account: \"${address}\"`);\n return undefined;\n }\n\n return groupIndex;\n}\n\n/**\n * HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the\n * Solana Snap).\n *\n * NOTE: We use `superstruct.type` here `superstruct.object` since it allows\n * extra-properties than a Snap might add in its `options`.\n */\nexport const HdSnapKeyringAccountOptionsStruct = type({\n entropySource: string(),\n index: number(),\n derivationPath: string(),\n});\nexport type HdSnapKeyringAccountOptions = Infer<\n typeof HdSnapKeyringAccountOptionsStruct\n>;\n\n/**\n * HD keyring account for Snap accounts that handles non-EVM HD accounts.\n */\nexport type HdSnapKeyringAccount = InternalAccount & {\n options: InternalAccount['options'] & HdSnapKeyringAccountOptions;\n};\n\n/**\n * Check if an account is an HD Snap keyring account.\n *\n * @param account - Snap keyring account.\n * @returns True if valid, false otherwise.\n */\nexport function isHdSnapKeyringAccount(\n account: InternalAccount,\n): account is HdSnapKeyringAccount {\n return is(account.options, HdSnapKeyringAccountOptionsStruct);\n}\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@metamask/accounts-controller",
|
3
|
-
"version": "
|
3
|
+
"version": "32.0.1",
|
4
4
|
"description": "Manages internal accounts",
|
5
5
|
"keywords": [
|
6
6
|
"MetaMask",
|
@@ -49,24 +49,27 @@
|
|
49
49
|
"dependencies": {
|
50
50
|
"@ethereumjs/util": "^9.1.0",
|
51
51
|
"@metamask/base-controller": "^8.0.1",
|
52
|
-
"@metamask/eth-snap-keyring": "^
|
53
|
-
"@metamask/keyring-api": "^
|
54
|
-
"@metamask/keyring-internal-api": "^
|
55
|
-
"@metamask/keyring-utils": "^3.
|
56
|
-
"@metamask/snaps-sdk": "^
|
57
|
-
"@metamask/snaps-utils": "^
|
58
|
-
"@metamask/
|
52
|
+
"@metamask/eth-snap-keyring": "^14.0.0",
|
53
|
+
"@metamask/keyring-api": "^19.0.0",
|
54
|
+
"@metamask/keyring-internal-api": "^7.0.0",
|
55
|
+
"@metamask/keyring-utils": "^3.1.0",
|
56
|
+
"@metamask/snaps-sdk": "^9.0.0",
|
57
|
+
"@metamask/snaps-utils": "^11.0.0",
|
58
|
+
"@metamask/superstruct": "^3.1.0",
|
59
|
+
"@metamask/utils": "^11.4.2",
|
59
60
|
"deepmerge": "^4.2.2",
|
60
61
|
"ethereum-cryptography": "^2.1.2",
|
61
62
|
"immer": "^9.0.6",
|
63
|
+
"lodash": "^4.17.21",
|
62
64
|
"uuid": "^8.3.2"
|
63
65
|
},
|
64
66
|
"devDependencies": {
|
65
67
|
"@metamask/auto-changelog": "^3.4.4",
|
66
|
-
"@metamask/
|
67
|
-
"@metamask/
|
68
|
+
"@metamask/controller-utils": "^11.11.0",
|
69
|
+
"@metamask/keyring-controller": "^22.1.0",
|
70
|
+
"@metamask/network-controller": "^24.0.1",
|
68
71
|
"@metamask/providers": "^22.1.0",
|
69
|
-
"@metamask/snaps-controllers": "^
|
72
|
+
"@metamask/snaps-controllers": "^14.0.1",
|
70
73
|
"@types/jest": "^27.4.1",
|
71
74
|
"@types/readable-stream": "^2.3.0",
|
72
75
|
"jest": "^27.5.1",
|
@@ -80,7 +83,7 @@
|
|
80
83
|
"@metamask/keyring-controller": "^22.0.0",
|
81
84
|
"@metamask/network-controller": "^24.0.0",
|
82
85
|
"@metamask/providers": "^22.0.0",
|
83
|
-
"@metamask/snaps-controllers": "^
|
86
|
+
"@metamask/snaps-controllers": "^14.0.0",
|
84
87
|
"webextension-polyfill": "^0.10.0 || ^0.11.0 || ^0.12.0"
|
85
88
|
},
|
86
89
|
"engines": {
|