@metamask/accounts-controller 37.1.1 → 38.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.constructAccountIdByAddress = exports.isHdSnapKeyringAccount = exports.HdSnapKeyringAccountOptionsStruct = exports.getEvmGroupIndexFromAddressIndex = exports.getEvmDerivationPathForIndex = exports.isHdKeyringType = exports.isSimpleKeyringType = exports.isSnapKeyringType = exports.isNormalKeyringType = exports.getUUIDFromAddressOfNormalAccount = exports.getUUIDOptionsFromAddressOfNormalAccount = exports.keyringTypeToName = void 0;
3
+ exports.constructAccountIdByAddress = exports.isHdSnapKeyringAccount = exports.HdSnapKeyringAccountOptionsStruct = exports.getEvmGroupIndexFromAddressIndex = exports.getEvmDerivationPathForIndex = exports.isMoneyKeyringType = exports.isHdKeyringType = exports.isSimpleKeyringType = exports.isSnapKeyringV2Type = exports.isSnapKeyringType = exports.isNormalKeyringType = exports.getUUIDFromAddressOfNormalAccount = exports.getUUIDOptionsFromAddressOfNormalAccount = exports.keyringTypeToName = void 0;
4
+ const v2_1 = require("@metamask/keyring-api/v2");
4
5
  const keyring_controller_1 = require("@metamask/keyring-controller");
5
6
  const superstruct_1 = require("@metamask/superstruct");
6
7
  const utils_1 = require("@metamask/utils");
@@ -35,9 +36,17 @@ function keyringTypeToName(keyringType) {
35
36
  case keyring_controller_1.KeyringTypes.qr: {
36
37
  return 'QR';
37
38
  }
39
+ case v2_1.KeyringType.Snap:
38
40
  case keyring_controller_1.KeyringTypes.snap: {
39
41
  return 'Snap Account';
40
42
  }
43
+ case keyring_controller_1.KeyringTypes.money: {
44
+ // NOTE: We don't use Money keyring/accounts within this controller. However, since this
45
+ // function only use the keyring type to return a name, we still support it here in case
46
+ // clients need it.
47
+ // FIXME: This should probably live in the `KeyringController` package instead.
48
+ return 'Money';
49
+ }
41
50
  default: {
42
51
  throw new Error(`Unknown keyring ${keyringType}`);
43
52
  }
@@ -74,9 +83,9 @@ exports.getUUIDFromAddressOfNormalAccount = getUUIDFromAddressOfNormalAccount;
74
83
  * @returns True if the keyring type is considered a "normal" keyring, false otherwise.
75
84
  */
76
85
  function isNormalKeyringType(keyringType) {
77
- // Right now, we only have to "exclude" Snap accounts, but this might need to be
78
- // adapted later on if we have new kind of keyrings!
79
- return keyringType !== keyring_controller_1.KeyringTypes.snap;
86
+ return (!isSnapKeyringType(keyringType) &&
87
+ !isSnapKeyringV2Type(keyringType) &&
88
+ !isMoneyKeyringType(keyringType));
80
89
  }
81
90
  exports.isNormalKeyringType = isNormalKeyringType;
82
91
  /**
@@ -89,6 +98,16 @@ function isSnapKeyringType(keyringType) {
89
98
  return keyringType === keyring_controller_1.KeyringTypes.snap;
90
99
  }
91
100
  exports.isSnapKeyringType = isSnapKeyringType;
101
+ /**
102
+ * Check if a keyring type is a Snap keyring.
103
+ *
104
+ * @param keyringType - The account's keyring type.
105
+ * @returns True if the keyring type is considered a Snap keyring, false otherwise.
106
+ */
107
+ function isSnapKeyringV2Type(keyringType) {
108
+ return keyringType === v2_1.KeyringType.Snap;
109
+ }
110
+ exports.isSnapKeyringV2Type = isSnapKeyringV2Type;
92
111
  /**
93
112
  * Check if a keyring type is a simple keyring.
94
113
  *
@@ -109,6 +128,16 @@ function isHdKeyringType(keyringType) {
109
128
  return keyringType === keyring_controller_1.KeyringTypes.hd;
110
129
  }
111
130
  exports.isHdKeyringType = isHdKeyringType;
131
+ /**
132
+ * Check if a keyring type is a Money keyring.
133
+ *
134
+ * @param keyringType - The account's keyring type.
135
+ * @returns True if the keyring type is a Money keyring, false otherwise.
136
+ */
137
+ function isMoneyKeyringType(keyringType) {
138
+ return keyringType === keyring_controller_1.KeyringTypes.money;
139
+ }
140
+ exports.isMoneyKeyringType = isMoneyKeyringType;
112
141
  /**
113
142
  * Get the derivation path for the index of an account within a EVM HD keyring.
114
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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,CAAC;QACtB,OAAO,CAAC,IAAI,CAAC,gDAAgD,OAAO,GAAG,CAAC,CAAC;QACzE,OAAO,SAAS,CAAC;IACnB,CAAC;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;AAED,SAAgB,2BAA2B,CACzC,WAA+C;IAE/C,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC5C,OAAO,QAAQ,CAAC,MAAM,CAA4B,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE;QACjE,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,EAAE,CAAC;QAClC,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC;AARD,kEAQC","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\nimport type { AccountId } from './AccountsController';\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\nexport function constructAccountIdByAddress(\n accountsMap: Record<AccountId, InternalAccount>,\n): Record<string, AccountId> {\n const accounts = Object.values(accountsMap);\n return accounts.reduce<Record<string, AccountId>>((acc, account) => {\n acc[account.address] = account.id;\n return acc;\n }, {});\n}\n"]}
1
+ {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,iDAAuD;AAEvD,qEAA4D;AAG5D,uDAAiE;AACjE,2CAA6C;AAC7C,yDAAsD;AAEtD,+BAAkC;AAIlC;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,WAAmB;IACnD,QAAQ,WAAW,EAAE,CAAC;QACpB,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC,CAAC;YACzB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,KAAK,iCAAY,CAAC,EAAE,CAAC,CAAC,CAAC;YACrB,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ent":["import { KeyringType } from '@metamask/keyring-api/v2';\nimport 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\nimport type { AccountId } from './AccountsController';\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 KeyringType.Snap:\n case KeyringTypes.snap: {\n return 'Snap Account';\n }\n case KeyringTypes.money: {\n // NOTE: We don't use Money keyring/accounts within this controller. However, since this\n // function only use the keyring type to return a name, we still support it here in case\n // clients need it.\n // FIXME: This should probably live in the `KeyringController` package instead.\n return 'Money';\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 return (\n !isSnapKeyringType(keyringType) &&\n !isSnapKeyringV2Type(keyringType) &&\n !isMoneyKeyringType(keyringType)\n );\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 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 isSnapKeyringV2Type(\n keyringType: KeyringTypes | KeyringType | string,\n): boolean {\n return keyringType === (KeyringType.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 * Check if a keyring type is a Money keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is a Money keyring, false otherwise.\n */\nexport function isMoneyKeyringType(\n keyringType: KeyringTypes | string,\n): boolean {\n return keyringType === (KeyringTypes.money 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\nexport function constructAccountIdByAddress(\n accountsMap: Record<AccountId, InternalAccount>,\n): Record<string, AccountId> {\n const accounts = Object.values(accountsMap);\n return accounts.reduce<Record<string, AccountId>>((acc, account) => {\n acc[account.address] = account.id;\n return acc;\n }, {});\n}\n"]}
package/dist/utils.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ import { KeyringType } from "@metamask/keyring-api/v2";
1
2
  import type { KeyringObject } from "@metamask/keyring-controller";
2
3
  import { KeyringTypes } from "@metamask/keyring-controller";
3
4
  import type { InternalAccount } from "@metamask/keyring-internal-api";
@@ -39,6 +40,13 @@ export declare function isNormalKeyringType(keyringType: KeyringTypes | string):
39
40
  * @returns True if the keyring type is considered a Snap keyring, false otherwise.
40
41
  */
41
42
  export declare function isSnapKeyringType(keyringType: KeyringTypes | string): boolean;
43
+ /**
44
+ * Check if a keyring type is a Snap keyring.
45
+ *
46
+ * @param keyringType - The account's keyring type.
47
+ * @returns True if the keyring type is considered a Snap keyring, false otherwise.
48
+ */
49
+ export declare function isSnapKeyringV2Type(keyringType: KeyringTypes | KeyringType | string): boolean;
42
50
  /**
43
51
  * Check if a keyring type is a simple keyring.
44
52
  *
@@ -53,6 +61,13 @@ export declare function isSimpleKeyringType(keyringType: KeyringTypes | string):
53
61
  * @returns True if the keyring is a HD keyring, false otherwise.
54
62
  */
55
63
  export declare function isHdKeyringType(keyringType: KeyringTypes | string): boolean;
64
+ /**
65
+ * Check if a keyring type is a Money keyring.
66
+ *
67
+ * @param keyringType - The account's keyring type.
68
+ * @returns True if the keyring type is a Money keyring, false otherwise.
69
+ */
70
+ export declare function isMoneyKeyringType(keyringType: KeyringTypes | string): boolean;
56
71
  /**
57
72
  * Get the derivation path for the index of an account within a EVM HD keyring.
58
73
  *
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.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AAO,CAET;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,kBAAkB,CAChC,WAAW,EAAE,YAAY,GAAG,MAAM,GACjC,OAAO,CAET;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;AAED,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,MAAM,CAAC,SAAS,EAAE,eAAe,CAAC,GAC9C,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAM3B"}
package/dist/utils.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { KeyringType } from "@metamask/keyring-api/v2";
1
2
  import type { KeyringObject } from "@metamask/keyring-controller";
2
3
  import { KeyringTypes } from "@metamask/keyring-controller";
3
4
  import type { InternalAccount } from "@metamask/keyring-internal-api";
@@ -39,6 +40,13 @@ export declare function isNormalKeyringType(keyringType: KeyringTypes | string):
39
40
  * @returns True if the keyring type is considered a Snap keyring, false otherwise.
40
41
  */
41
42
  export declare function isSnapKeyringType(keyringType: KeyringTypes | string): boolean;
43
+ /**
44
+ * Check if a keyring type is a Snap keyring.
45
+ *
46
+ * @param keyringType - The account's keyring type.
47
+ * @returns True if the keyring type is considered a Snap keyring, false otherwise.
48
+ */
49
+ export declare function isSnapKeyringV2Type(keyringType: KeyringTypes | KeyringType | string): boolean;
42
50
  /**
43
51
  * Check if a keyring type is a simple keyring.
44
52
  *
@@ -53,6 +61,13 @@ export declare function isSimpleKeyringType(keyringType: KeyringTypes | string):
53
61
  * @returns True if the keyring is a HD keyring, false otherwise.
54
62
  */
55
63
  export declare function isHdKeyringType(keyringType: KeyringTypes | string): boolean;
64
+ /**
65
+ * Check if a keyring type is a Money keyring.
66
+ *
67
+ * @param keyringType - The account's keyring type.
68
+ * @returns True if the keyring type is a Money keyring, false otherwise.
69
+ */
70
+ export declare function isMoneyKeyringType(keyringType: KeyringTypes | string): boolean;
56
71
  /**
57
72
  * Get the derivation path for the index of an account within a EVM HD keyring.
58
73
  *
@@ -1 +1 @@
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,OAAO,KAAK,EAAE,SAAS,EAAE,iCAA6B;AAEtD;;;;;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,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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;AAED,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,MAAM,CAAC,SAAS,EAAE,eAAe,CAAC,GAC9C,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAM3B"}
package/dist/utils.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { KeyringType } from "@metamask/keyring-api/v2";
1
2
  import { KeyringTypes } from "@metamask/keyring-controller";
2
3
  import { is, number, string, type } from "@metamask/superstruct";
3
4
  import { hexToBytes } from "@metamask/utils";
@@ -32,9 +33,17 @@ export function keyringTypeToName(keyringType) {
32
33
  case KeyringTypes.qr: {
33
34
  return 'QR';
34
35
  }
36
+ case KeyringType.Snap:
35
37
  case KeyringTypes.snap: {
36
38
  return 'Snap Account';
37
39
  }
40
+ case KeyringTypes.money: {
41
+ // NOTE: We don't use Money keyring/accounts within this controller. However, since this
42
+ // function only use the keyring type to return a name, we still support it here in case
43
+ // clients need it.
44
+ // FIXME: This should probably live in the `KeyringController` package instead.
45
+ return 'Money';
46
+ }
38
47
  default: {
39
48
  throw new Error(`Unknown keyring ${keyringType}`);
40
49
  }
@@ -68,9 +77,9 @@ export function getUUIDFromAddressOfNormalAccount(address) {
68
77
  * @returns True if the keyring type is considered a "normal" keyring, false otherwise.
69
78
  */
70
79
  export function isNormalKeyringType(keyringType) {
71
- // Right now, we only have to "exclude" Snap accounts, but this might need to be
72
- // adapted later on if we have new kind of keyrings!
73
- return keyringType !== KeyringTypes.snap;
80
+ return (!isSnapKeyringType(keyringType) &&
81
+ !isSnapKeyringV2Type(keyringType) &&
82
+ !isMoneyKeyringType(keyringType));
74
83
  }
75
84
  /**
76
85
  * Check if a keyring type is a Snap keyring.
@@ -81,6 +90,15 @@ export function isNormalKeyringType(keyringType) {
81
90
  export function isSnapKeyringType(keyringType) {
82
91
  return keyringType === KeyringTypes.snap;
83
92
  }
93
+ /**
94
+ * Check if a keyring type is a Snap keyring.
95
+ *
96
+ * @param keyringType - The account's keyring type.
97
+ * @returns True if the keyring type is considered a Snap keyring, false otherwise.
98
+ */
99
+ export function isSnapKeyringV2Type(keyringType) {
100
+ return keyringType === KeyringType.Snap;
101
+ }
84
102
  /**
85
103
  * Check if a keyring type is a simple keyring.
86
104
  *
@@ -99,6 +117,15 @@ export function isSimpleKeyringType(keyringType) {
99
117
  export function isHdKeyringType(keyringType) {
100
118
  return keyringType === KeyringTypes.hd;
101
119
  }
120
+ /**
121
+ * Check if a keyring type is a Money keyring.
122
+ *
123
+ * @param keyringType - The account's keyring type.
124
+ * @returns True if the keyring type is a Money keyring, false otherwise.
125
+ */
126
+ export function isMoneyKeyringType(keyringType) {
127
+ return keyringType === KeyringTypes.money;
128
+ }
102
129
  /**
103
130
  * Get the derivation path for the index of an account within a EVM HD keyring.
104
131
  *
@@ -1 +1 @@
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;AAIlC;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,WAAmB;IACnD,QAAQ,WAAW,EAAE,CAAC;QACpB,KAAK,YAAY,CAAC,MAAM,CAAC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;IACnB,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,IAAI,CAAC;IACpD,aAAa,EAAE,MAAM,EAAE;IACvB,KAAK,EAAE,MAAM,EAAE;IACf,cAAc,EAAE,MAAM,EAAE;CACzB,CAAC,CAAC;AAYH;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,OAAwB;IAExB,OAAO,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,iCAAiC,CAAC,CAAC;AAChE,CAAC;AAED,MAAM,UAAU,2BAA2B,CACzC,WAA+C;IAE/C,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC5C,OAAO,QAAQ,CAAC,MAAM,CAA4B,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE;QACjE,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,EAAE,CAAC;QAClC,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,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\nimport type { AccountId } from './AccountsController';\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\nexport function constructAccountIdByAddress(\n accountsMap: Record<AccountId, InternalAccount>,\n): Record<string, AccountId> {\n const accounts = Object.values(accountsMap);\n return accounts.reduce<Record<string, AccountId>>((acc, account) => {\n acc[account.address] = account.id;\n return acc;\n }, {});\n}\n"]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iCAAiC;AAEvD,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;AAIlC;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,WAAmB;IACnD,QAAQ,WAAW,EAAE,CAAC;QACpB,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;YACzB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,KAAK,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC;YACrB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;YACzB,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;YACzB,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;Y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,UAAU,KAAK,CAAC,CAAC,EAAE,CAAC;QACtB,OAAO,CAAC,IAAI,CAAC,gDAAgD,OAAO,GAAG,CAAC,CAAC;QACzE,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,IAAI,CAAC;IACpD,aAAa,EAAE,MAAM,EAAE;IACvB,KAAK,EAAE,MAAM,EAAE;IACf,cAAc,EAAE,MAAM,EAAE;CACzB,CAAC,CAAC;AAYH;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,OAAwB;IAExB,OAAO,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,iCAAiC,CAAC,CAAC;AAChE,CAAC;AAED,MAAM,UAAU,2BAA2B,CACzC,WAA+C;IAE/C,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC5C,OAAO,QAAQ,CAAC,MAAM,CAA4B,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE;QACjE,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,EAAE,CAAC;QAClC,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC","sourcesContent":["import { KeyringType } from '@metamask/keyring-api/v2';\nimport 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\nimport type { AccountId } from './AccountsController';\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 KeyringType.Snap:\n case KeyringTypes.snap: {\n return 'Snap Account';\n }\n case KeyringTypes.money: {\n // NOTE: We don't use Money keyring/accounts within this controller. However, since this\n // function only use the keyring type to return a name, we still support it here in case\n // clients need it.\n // FIXME: This should probably live in the `KeyringController` package instead.\n return 'Money';\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 return (\n !isSnapKeyringType(keyringType) &&\n !isSnapKeyringV2Type(keyringType) &&\n !isMoneyKeyringType(keyringType)\n );\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 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 isSnapKeyringV2Type(\n keyringType: KeyringTypes | KeyringType | string,\n): boolean {\n return keyringType === (KeyringType.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 * Check if a keyring type is a Money keyring.\n *\n * @param keyringType - The account's keyring type.\n * @returns True if the keyring type is a Money keyring, false otherwise.\n */\nexport function isMoneyKeyringType(\n keyringType: KeyringTypes | string,\n): boolean {\n return keyringType === (KeyringTypes.money 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\nexport function constructAccountIdByAddress(\n accountsMap: Record<AccountId, InternalAccount>,\n): Record<string, AccountId> {\n const accounts = Object.values(accountsMap);\n return accounts.reduce<Record<string, AccountId>>((acc, account) => {\n acc[account.address] = account.id;\n return acc;\n }, {});\n}\n"]}
package/package.json CHANGED
@@ -1,21 +1,26 @@
1
1
  {
2
2
  "name": "@metamask/accounts-controller",
3
- "version": "37.1.1",
3
+ "version": "38.0.0",
4
4
  "description": "Manages internal accounts",
5
5
  "keywords": [
6
- "MetaMask",
7
- "Ethereum"
6
+ "Ethereum",
7
+ "MetaMask"
8
8
  ],
9
9
  "homepage": "https://github.com/MetaMask/core/tree/main/packages/accounts-controller#readme",
10
10
  "bugs": {
11
11
  "url": "https://github.com/MetaMask/core/issues"
12
12
  },
13
+ "license": "MIT",
13
14
  "repository": {
14
15
  "type": "git",
15
16
  "url": "https://github.com/MetaMask/core.git"
16
17
  },
17
- "license": "MIT",
18
+ "files": [
19
+ "dist/"
20
+ ],
18
21
  "sideEffects": false,
22
+ "main": "./dist/index.cjs",
23
+ "types": "./dist/index.d.cts",
19
24
  "exports": {
20
25
  ".": {
21
26
  "import": {
@@ -29,18 +34,18 @@
29
34
  },
30
35
  "./package.json": "./package.json"
31
36
  },
32
- "main": "./dist/index.cjs",
33
- "types": "./dist/index.d.cts",
34
- "files": [
35
- "dist/"
36
- ],
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org/"
40
+ },
37
41
  "scripts": {
38
42
  "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
39
43
  "build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
40
44
  "build:docs": "typedoc",
41
45
  "changelog:update": "../../scripts/update-changelog.sh @metamask/accounts-controller",
42
46
  "changelog:validate": "../../scripts/validate-changelog.sh @metamask/accounts-controller",
43
- "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts",
47
+ "messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --check",
48
+ "messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --generate",
44
49
  "since-latest-release": "../../scripts/since-latest-release.sh",
45
50
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
46
51
  "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
@@ -49,17 +54,14 @@
49
54
  },
50
55
  "dependencies": {
51
56
  "@ethereumjs/util": "^9.1.0",
52
- "@metamask/base-controller": "^9.0.1",
53
- "@metamask/eth-snap-keyring": "^19.0.0",
54
- "@metamask/keyring-api": "^21.6.0",
55
- "@metamask/keyring-controller": "^25.1.1",
56
- "@metamask/keyring-internal-api": "^10.0.0",
57
+ "@metamask/base-controller": "^9.1.0",
58
+ "@metamask/eth-snap-keyring": "^22.0.1",
59
+ "@metamask/keyring-api": "^23.1.0",
60
+ "@metamask/keyring-controller": "^25.4.0",
61
+ "@metamask/keyring-internal-api": "^11.0.1",
57
62
  "@metamask/keyring-utils": "^3.1.0",
58
- "@metamask/messenger": "^1.0.0",
59
- "@metamask/network-controller": "^30.0.1",
60
- "@metamask/snaps-controllers": "^19.0.0",
61
- "@metamask/snaps-sdk": "^11.0.0",
62
- "@metamask/snaps-utils": "^12.1.2",
63
+ "@metamask/messenger": "^1.2.0",
64
+ "@metamask/network-controller": "^30.1.0",
63
65
  "@metamask/superstruct": "^3.1.0",
64
66
  "@metamask/utils": "^11.9.0",
65
67
  "deepmerge": "^4.2.2",
@@ -69,8 +71,8 @@
69
71
  "uuid": "^8.3.2"
70
72
  },
71
73
  "devDependencies": {
72
- "@metamask/auto-changelog": "^3.4.4",
73
- "@metamask/controller-utils": "^11.19.0",
74
+ "@metamask/auto-changelog": "^6.1.0",
75
+ "@metamask/controller-utils": "^11.20.0",
74
76
  "@metamask/providers": "^22.1.0",
75
77
  "@ts-bridge/cli": "^0.6.4",
76
78
  "@types/jest": "^29.5.14",
@@ -89,9 +91,5 @@
89
91
  },
90
92
  "engines": {
91
93
  "node": "^18.18 || >=20"
92
- },
93
- "publishConfig": {
94
- "access": "public",
95
- "registry": "https://registry.npmjs.org/"
96
94
  }
97
95
  }