@metamask/accounts-controller 32.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 +8 -1
- package/dist/utils.cjs +4 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +3 -0
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +3 -0
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +5 -2
- package/dist/utils.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [32.0.1]
|
11
|
+
|
12
|
+
### Fixed
|
13
|
+
|
14
|
+
- Allow extra `options` properties when detecting BIP-44 Snap account ([#6189](https://github.com/MetaMask/core/pull/6189))
|
15
|
+
|
10
16
|
## [32.0.0]
|
11
17
|
|
12
18
|
### Added
|
@@ -572,7 +578,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
572
578
|
|
573
579
|
- Initial release ([#1637](https://github.com/MetaMask/core/pull/1637))
|
574
580
|
|
575
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@32.0.
|
581
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@32.0.1...HEAD
|
582
|
+
[32.0.1]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@32.0.0...@metamask/accounts-controller@32.0.1
|
576
583
|
[32.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@31.0.0...@metamask/accounts-controller@32.0.0
|
577
584
|
[31.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@30.0.0...@metamask/accounts-controller@31.0.0
|
578
585
|
[30.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@29.0.1...@metamask/accounts-controller@30.0.0
|
package/dist/utils.cjs
CHANGED
@@ -157,8 +157,11 @@ exports.getEvmGroupIndexFromAddressIndex = getEvmGroupIndexFromAddressIndex;
|
|
157
157
|
/**
|
158
158
|
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
159
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`.
|
160
163
|
*/
|
161
|
-
exports.HdSnapKeyringAccountOptionsStruct = (0, superstruct_1.
|
164
|
+
exports.HdSnapKeyringAccountOptionsStruct = (0, superstruct_1.type)({
|
162
165
|
entropySource: (0, superstruct_1.string)(),
|
163
166
|
index: (0, superstruct_1.number)(),
|
164
167
|
derivationPath: (0, superstruct_1.string)(),
|
package/dist/utils.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,qEAA4D;AAG5D,
|
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,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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
@@ -70,6 +70,9 @@ export declare function getEvmGroupIndexFromAddressIndex(keyring: KeyringObject,
|
|
70
70
|
/**
|
71
71
|
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
72
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`.
|
73
76
|
*/
|
74
77
|
export declare const HdSnapKeyringAccountOptionsStruct: import("@metamask/superstruct").Struct<{
|
75
78
|
entropySource: string;
|
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,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
|
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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AAsB,CACpC,OAAO,EAAE,eAAe,GACvB,OAAO,IAAI,oBAAoB,CAEjC"}
|
package/dist/utils.d.mts
CHANGED
@@ -70,6 +70,9 @@ export declare function getEvmGroupIndexFromAddressIndex(keyring: KeyringObject,
|
|
70
70
|
/**
|
71
71
|
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
72
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`.
|
73
76
|
*/
|
74
77
|
export declare const HdSnapKeyringAccountOptionsStruct: import("@metamask/superstruct").Struct<{
|
75
78
|
entropySource: string;
|
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,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,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AED;;;;;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,5 +1,5 @@
|
|
1
1
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
2
|
-
import { is, number,
|
2
|
+
import { is, number, string, type } from "@metamask/superstruct";
|
3
3
|
import { hexToBytes } from "@metamask/utils";
|
4
4
|
import { sha256 } from "ethereum-cryptography/sha256";
|
5
5
|
import { v4 as uuid } from "uuid";
|
@@ -145,8 +145,11 @@ export function getEvmGroupIndexFromAddressIndex(keyring, address) {
|
|
145
145
|
/**
|
146
146
|
* HD keyring account for Snap accounts that handles non-EVM HD accounts. (e.g the
|
147
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`.
|
148
151
|
*/
|
149
|
-
export const HdSnapKeyringAccountOptionsStruct =
|
152
|
+
export const HdSnapKeyringAccountOptionsStruct = type({
|
150
153
|
entropySource: string(),
|
151
154
|
index: number(),
|
152
155
|
derivationPath: string(),
|
package/dist/utils.mjs.map
CHANGED
@@ -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,
|
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,SAAS,CAAC;SAClB;QACD,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,YAAY,CAAC,OAAO,CAAC,CAAC;YACzB,OAAO,SAAS,CAAC;SAClB;QACD,KAAK,YAAY,CAAC,EAAE,CAAC,CAAC;YACpB,OAAO,IAAI,CAAC;SACb;QACD,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;YACtB,OAAO,cAAc,CAAC;SACvB;QACD,OAAO,CAAC,CAAC;YACP,MAAM,IAAI,KAAK,CAAC,mBAAmB,WAAW,EAAE,CAAC,CAAC;SACnD;KACF;AACH,CA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AAC,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;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","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"]}
|