@metamask-previews/chain-agnostic-permission 0.1.0-preview-b69c669

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +20 -0
  3. package/README.md +15 -0
  4. package/dist/adapters/caip-permission-adapter-eth-accounts.cjs +103 -0
  5. package/dist/adapters/caip-permission-adapter-eth-accounts.cjs.map +1 -0
  6. package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts +20 -0
  7. package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts.map +1 -0
  8. package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts +20 -0
  9. package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts.map +1 -0
  10. package/dist/adapters/caip-permission-adapter-eth-accounts.mjs +98 -0
  11. package/dist/adapters/caip-permission-adapter-eth-accounts.mjs.map +1 -0
  12. package/dist/adapters/caip-permission-adapter-permittedChains.cjs +115 -0
  13. package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -0
  14. package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +27 -0
  15. package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -0
  16. package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +27 -0
  17. package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -0
  18. package/dist/adapters/caip-permission-adapter-permittedChains.mjs +109 -0
  19. package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -0
  20. package/dist/adapters/caip-permission-adapter-session-scopes.cjs +85 -0
  21. package/dist/adapters/caip-permission-adapter-session-scopes.cjs.map +1 -0
  22. package/dist/adapters/caip-permission-adapter-session-scopes.d.cts +23 -0
  23. package/dist/adapters/caip-permission-adapter-session-scopes.d.cts.map +1 -0
  24. package/dist/adapters/caip-permission-adapter-session-scopes.d.mts +23 -0
  25. package/dist/adapters/caip-permission-adapter-session-scopes.d.mts.map +1 -0
  26. package/dist/adapters/caip-permission-adapter-session-scopes.mjs +80 -0
  27. package/dist/adapters/caip-permission-adapter-session-scopes.mjs.map +1 -0
  28. package/dist/caip25Permission.cjs +285 -0
  29. package/dist/caip25Permission.cjs.map +1 -0
  30. package/dist/caip25Permission.d.cts +131 -0
  31. package/dist/caip25Permission.d.cts.map +1 -0
  32. package/dist/caip25Permission.d.mts +131 -0
  33. package/dist/caip25Permission.d.mts.map +1 -0
  34. package/dist/caip25Permission.mjs +280 -0
  35. package/dist/caip25Permission.mjs.map +1 -0
  36. package/dist/index.cjs +43 -0
  37. package/dist/index.cjs.map +1 -0
  38. package/dist/index.d.cts +14 -0
  39. package/dist/index.d.cts.map +1 -0
  40. package/dist/index.d.mts +14 -0
  41. package/dist/index.d.mts.map +1 -0
  42. package/dist/index.mjs +11 -0
  43. package/dist/index.mjs.map +1 -0
  44. package/dist/scope/assert.cjs +170 -0
  45. package/dist/scope/assert.cjs.map +1 -0
  46. package/dist/scope/assert.d.cts +50 -0
  47. package/dist/scope/assert.d.cts.map +1 -0
  48. package/dist/scope/assert.d.mts +50 -0
  49. package/dist/scope/assert.d.mts.map +1 -0
  50. package/dist/scope/assert.mjs +162 -0
  51. package/dist/scope/assert.mjs.map +1 -0
  52. package/dist/scope/authorization.cjs +51 -0
  53. package/dist/scope/authorization.cjs.map +1 -0
  54. package/dist/scope/authorization.d.cts +50 -0
  55. package/dist/scope/authorization.d.cts.map +1 -0
  56. package/dist/scope/authorization.d.mts +50 -0
  57. package/dist/scope/authorization.d.mts.map +1 -0
  58. package/dist/scope/authorization.mjs +46 -0
  59. package/dist/scope/authorization.mjs.map +1 -0
  60. package/dist/scope/constants.cjs +83 -0
  61. package/dist/scope/constants.cjs.map +1 -0
  62. package/dist/scope/constants.d.cts +32 -0
  63. package/dist/scope/constants.d.cts.map +1 -0
  64. package/dist/scope/constants.d.mts +32 -0
  65. package/dist/scope/constants.d.mts.map +1 -0
  66. package/dist/scope/constants.mjs +84 -0
  67. package/dist/scope/constants.mjs.map +1 -0
  68. package/dist/scope/errors.cjs +47 -0
  69. package/dist/scope/errors.cjs.map +1 -0
  70. package/dist/scope/errors.d.cts +44 -0
  71. package/dist/scope/errors.d.cts.map +1 -0
  72. package/dist/scope/errors.d.mts +44 -0
  73. package/dist/scope/errors.d.mts.map +1 -0
  74. package/dist/scope/errors.mjs +44 -0
  75. package/dist/scope/errors.mjs.map +1 -0
  76. package/dist/scope/filter.cjs +76 -0
  77. package/dist/scope/filter.cjs.map +1 -0
  78. package/dist/scope/filter.d.cts +35 -0
  79. package/dist/scope/filter.d.cts.map +1 -0
  80. package/dist/scope/filter.d.mts +35 -0
  81. package/dist/scope/filter.d.mts.map +1 -0
  82. package/dist/scope/filter.mjs +71 -0
  83. package/dist/scope/filter.mjs.map +1 -0
  84. package/dist/scope/supported.cjs +116 -0
  85. package/dist/scope/supported.cjs.map +1 -0
  86. package/dist/scope/supported.d.cts +52 -0
  87. package/dist/scope/supported.d.cts.map +1 -0
  88. package/dist/scope/supported.d.mts +52 -0
  89. package/dist/scope/supported.d.mts.map +1 -0
  90. package/dist/scope/supported.mjs +109 -0
  91. package/dist/scope/supported.mjs.map +1 -0
  92. package/dist/scope/transform.cjs +152 -0
  93. package/dist/scope/transform.cjs.map +1 -0
  94. package/dist/scope/transform.d.cts +50 -0
  95. package/dist/scope/transform.d.cts.map +1 -0
  96. package/dist/scope/transform.d.mts +50 -0
  97. package/dist/scope/transform.d.mts.map +1 -0
  98. package/dist/scope/transform.mjs +144 -0
  99. package/dist/scope/transform.mjs.map +1 -0
  100. package/dist/scope/types.cjs +23 -0
  101. package/dist/scope/types.cjs.map +1 -0
  102. package/dist/scope/types.d.cts +81 -0
  103. package/dist/scope/types.d.cts.map +1 -0
  104. package/dist/scope/types.d.mts +81 -0
  105. package/dist/scope/types.d.mts.map +1 -0
  106. package/dist/scope/types.mjs +19 -0
  107. package/dist/scope/types.mjs.map +1 -0
  108. package/dist/scope/validation.cjs +87 -0
  109. package/dist/scope/validation.cjs.map +1 -0
  110. package/dist/scope/validation.d.cts +21 -0
  111. package/dist/scope/validation.d.cts.map +1 -0
  112. package/dist/scope/validation.d.mts +21 -0
  113. package/dist/scope/validation.d.mts.map +1 -0
  114. package/dist/scope/validation.mjs +82 -0
  115. package/dist/scope/validation.mjs.map +1 -0
  116. package/package.json +75 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0]
11
+
12
+ ### Added
13
+
14
+ - Initial release
15
+
16
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.1.0...HEAD
17
+ [0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/chain-agnostic-permission@0.1.0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MetaMask
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # `@metamask/chain-agnostic-permission`
2
+
3
+ Defines an endowment type permission designed to persist the account and chain components of a [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request. This package also includes adapters and utility functions for interfacing with this permission.
4
+
5
+ ## Installation
6
+
7
+ `yarn add @metamask/chain-agnostic-permission`
8
+
9
+ or
10
+
11
+ `npm install @metamask/chain-agnostic-permission`
12
+
13
+ ## Contributing
14
+
15
+ This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setEthAccounts = exports.getEthAccounts = void 0;
4
+ const utils_1 = require("@metamask/utils");
5
+ const constants_1 = require("../scope/constants.cjs");
6
+ const transform_1 = require("../scope/transform.cjs");
7
+ const types_1 = require("../scope/types.cjs");
8
+ /**
9
+ * Checks if a scope string is either an EIP155 or wallet namespaced scope string.
10
+ *
11
+ * @param scopeString - The scope string to check.
12
+ * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.
13
+ */
14
+ const isEip155ScopeString = (scopeString) => {
15
+ const { namespace } = (0, types_1.parseScopeString)(scopeString);
16
+ return (namespace === utils_1.KnownCaipNamespace.Eip155 ||
17
+ // We are trying to discern the type of `scopeString`.
18
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
19
+ scopeString === constants_1.KnownWalletScopeString.Eip155);
20
+ };
21
+ /**
22
+ * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.
23
+ *
24
+ * @param scopes - The internal scopes from which to get the Ethereum accounts.
25
+ * @returns An array of Ethereum accounts.
26
+ */
27
+ const getEthAccountsFromScopes = (scopes) => {
28
+ const ethAccounts = [];
29
+ Object.entries(scopes).forEach(([_, { accounts }]) => {
30
+ accounts?.forEach((account) => {
31
+ const { address, chainId } = (0, utils_1.parseCaipAccountId)(account);
32
+ if (isEip155ScopeString(chainId)) {
33
+ // This address should always be a valid Hex string because
34
+ // it's an EIP155/Ethereum account
35
+ (0, utils_1.assertIsStrictHexString)(address);
36
+ ethAccounts.push(address);
37
+ }
38
+ });
39
+ });
40
+ return ethAccounts;
41
+ };
42
+ /**
43
+ * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
44
+ *
45
+ * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.
46
+ * @returns An array of Ethereum accounts.
47
+ */
48
+ const getEthAccounts = (caip25CaveatValue) => {
49
+ const { requiredScopes, optionalScopes } = caip25CaveatValue;
50
+ const ethAccounts = [
51
+ ...getEthAccountsFromScopes(requiredScopes),
52
+ ...getEthAccountsFromScopes(optionalScopes),
53
+ ];
54
+ return (0, transform_1.getUniqueArrayItems)(ethAccounts);
55
+ };
56
+ exports.getEthAccounts = getEthAccounts;
57
+ /**
58
+ * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.
59
+ *
60
+ * @param scopesObject - The scopes object to set the Ethereum accounts for.
61
+ * @param accounts - The Ethereum accounts to set.
62
+ * @returns The updated scopes object with the Ethereum accounts set.
63
+ */
64
+ const setEthAccountsForScopesObject = (scopesObject, accounts) => {
65
+ const updatedScopesObject = {};
66
+ Object.entries(scopesObject).forEach(([key, scopeObject]) => {
67
+ // Cast needed because index type is returned as `string` by `Object.entries`
68
+ const scopeString = key;
69
+ const isWalletNamespace = scopeString === utils_1.KnownCaipNamespace.Wallet;
70
+ const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
71
+ if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {
72
+ updatedScopesObject[scopeString] = scopeObject;
73
+ return;
74
+ }
75
+ let caipAccounts = [];
76
+ if (namespace && reference) {
77
+ caipAccounts = accounts.map((account) => `${namespace}:${reference}:${account}`);
78
+ }
79
+ updatedScopesObject[scopeString] = {
80
+ ...scopeObject,
81
+ accounts: caipAccounts,
82
+ };
83
+ });
84
+ return updatedScopesObject;
85
+ };
86
+ /**
87
+ * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.
88
+ * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because
89
+ * we do not provide UI/UX flows for selecting different accounts across different chains.
90
+ *
91
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.
92
+ * @param accounts - The Ethereum accounts to set.
93
+ * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
94
+ */
95
+ const setEthAccounts = (caip25CaveatValue, accounts) => {
96
+ return {
97
+ ...caip25CaveatValue,
98
+ requiredScopes: setEthAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
99
+ optionalScopes: setEthAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
100
+ };
101
+ };
102
+ exports.setEthAccounts = setEthAccounts;
103
+ //# sourceMappingURL=caip-permission-adapter-eth-accounts.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-eth-accounts.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-accounts.ts"],"names":[],"mappings":";;;AAAA,2CAMyB;AAGzB,sDAA4D;AAC5D,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,0BAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,kCAAsB,CAAC,MAAM,CAC9C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,MAA4B,EAAE,EAAE;IAChE,MAAM,WAAW,GAAU,EAAE,CAAC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AAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,CAAC,mBAAmB,CAAC,WAAW,CAAC,IAAI,CAAC,iBAAiB,EAAE;YAC3D,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;YAC/C,OAAO;SACR;QAED,IAAI,YAAY,GAAoB,EAAE,CAAC;QACvC,IAAI,SAAS,IAAI,SAAS,EAAE;YAC1B,YAAY,GAAG,QAAQ,CAAC,GAAG,CACzB,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,SAAS,IAAI,SAAS,IAAI,OAAO,EAAE,CACpD,CAAC;SACH;QAED,mBAAmB,CAAC,WAAW,CAAC,GAAG;YACjC,GAAG,WAAW;YACd,QAAQ,EAAE,YAAY;SACvB,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACI,MAAM,cAAc,GAAG,CAC5B,iBAAoC,EACpC,QAAe,EACI,EAAE;IACrB,OAAO;QACL,GAAG,iBAAiB;QACpB,cAAc,EAAE,6BAA6B,CAC3C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,6BAA6B,CAC3C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;KACF,CAAC;AACJ,CAAC,CAAC;AAfW,QAAA,cAAc,kBAezB","sourcesContent":["import {\n assertIsStrictHexString,\n type CaipAccountId,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n"]}
@@ -0,0 +1,20 @@
1
+ import { type Hex } from "@metamask/utils";
2
+ import type { Caip25CaveatValue } from "../caip25Permission.cjs";
3
+ /**
4
+ * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
5
+ *
6
+ * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.
7
+ * @returns An array of Ethereum accounts.
8
+ */
9
+ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>) => Hex[];
10
+ /**
11
+ * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.
12
+ * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because
13
+ * we do not provide UI/UX flows for selecting different accounts across different chains.
14
+ *
15
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.
16
+ * @param accounts - The Ethereum accounts to set.
17
+ * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
18
+ */
19
+ export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
20
+ //# sourceMappingURL=caip-permission-adapter-eth-accounts.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-eth-accounts.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAgD7D;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { type Hex } from "@metamask/utils";
2
+ import type { Caip25CaveatValue } from "../caip25Permission.mjs";
3
+ /**
4
+ * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
5
+ *
6
+ * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.
7
+ * @returns An array of Ethereum accounts.
8
+ */
9
+ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>) => Hex[];
10
+ /**
11
+ * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.
12
+ * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because
13
+ * we do not provide UI/UX flows for selecting different accounts across different chains.
14
+ *
15
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.
16
+ * @param accounts - The Ethereum accounts to set.
17
+ * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
18
+ */
19
+ export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
20
+ //# sourceMappingURL=caip-permission-adapter-eth-accounts.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-eth-accounts.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAgD7D;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC"}
@@ -0,0 +1,98 @@
1
+ import { assertIsStrictHexString, KnownCaipNamespace, parseCaipAccountId } from "@metamask/utils";
2
+ import { KnownWalletScopeString } from "../scope/constants.mjs";
3
+ import { getUniqueArrayItems } from "../scope/transform.mjs";
4
+ import { parseScopeString } from "../scope/types.mjs";
5
+ /**
6
+ * Checks if a scope string is either an EIP155 or wallet namespaced scope string.
7
+ *
8
+ * @param scopeString - The scope string to check.
9
+ * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.
10
+ */
11
+ const isEip155ScopeString = (scopeString) => {
12
+ const { namespace } = parseScopeString(scopeString);
13
+ return (namespace === KnownCaipNamespace.Eip155 ||
14
+ // We are trying to discern the type of `scopeString`.
15
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
16
+ scopeString === KnownWalletScopeString.Eip155);
17
+ };
18
+ /**
19
+ * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.
20
+ *
21
+ * @param scopes - The internal scopes from which to get the Ethereum accounts.
22
+ * @returns An array of Ethereum accounts.
23
+ */
24
+ const getEthAccountsFromScopes = (scopes) => {
25
+ const ethAccounts = [];
26
+ Object.entries(scopes).forEach(([_, { accounts }]) => {
27
+ accounts?.forEach((account) => {
28
+ const { address, chainId } = parseCaipAccountId(account);
29
+ if (isEip155ScopeString(chainId)) {
30
+ // This address should always be a valid Hex string because
31
+ // it's an EIP155/Ethereum account
32
+ assertIsStrictHexString(address);
33
+ ethAccounts.push(address);
34
+ }
35
+ });
36
+ });
37
+ return ethAccounts;
38
+ };
39
+ /**
40
+ * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
41
+ *
42
+ * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.
43
+ * @returns An array of Ethereum accounts.
44
+ */
45
+ export const getEthAccounts = (caip25CaveatValue) => {
46
+ const { requiredScopes, optionalScopes } = caip25CaveatValue;
47
+ const ethAccounts = [
48
+ ...getEthAccountsFromScopes(requiredScopes),
49
+ ...getEthAccountsFromScopes(optionalScopes),
50
+ ];
51
+ return getUniqueArrayItems(ethAccounts);
52
+ };
53
+ /**
54
+ * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.
55
+ *
56
+ * @param scopesObject - The scopes object to set the Ethereum accounts for.
57
+ * @param accounts - The Ethereum accounts to set.
58
+ * @returns The updated scopes object with the Ethereum accounts set.
59
+ */
60
+ const setEthAccountsForScopesObject = (scopesObject, accounts) => {
61
+ const updatedScopesObject = {};
62
+ Object.entries(scopesObject).forEach(([key, scopeObject]) => {
63
+ // Cast needed because index type is returned as `string` by `Object.entries`
64
+ const scopeString = key;
65
+ const isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;
66
+ const { namespace, reference } = parseScopeString(scopeString);
67
+ if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {
68
+ updatedScopesObject[scopeString] = scopeObject;
69
+ return;
70
+ }
71
+ let caipAccounts = [];
72
+ if (namespace && reference) {
73
+ caipAccounts = accounts.map((account) => `${namespace}:${reference}:${account}`);
74
+ }
75
+ updatedScopesObject[scopeString] = {
76
+ ...scopeObject,
77
+ accounts: caipAccounts,
78
+ };
79
+ });
80
+ return updatedScopesObject;
81
+ };
82
+ /**
83
+ * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.
84
+ * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because
85
+ * we do not provide UI/UX flows for selecting different accounts across different chains.
86
+ *
87
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.
88
+ * @param accounts - The Ethereum accounts to set.
89
+ * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
90
+ */
91
+ export const setEthAccounts = (caip25CaveatValue, accounts) => {
92
+ return {
93
+ ...caip25CaveatValue,
94
+ requiredScopes: setEthAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
95
+ optionalScopes: setEthAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
96
+ };
97
+ };
98
+ //# sourceMappingURL=caip-permission-adapter-eth-accounts.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-eth-accounts.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EAGvB,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AAGzB,OAAO,EAAE,sBAAsB,EAAE,+BAA2B;AAC5D,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAElD;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL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AAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,WAAW,CAAC,EAAE,EAAE;QAC1D,6EAA6E;QAC7E,MAAM,WAAW,GAAG,GAAgC,CAAC;QACrD,MAAM,iBAAiB,GAAG,WAAW,KAAK,kBAAkB,CAAC,MAAM,CAAC;QACpE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,CAAC,mBAAmB,CAAC,WAAW,CAAC,IAAI,CAAC,iBAAiB,EAAE;YAC3D,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;YAC/C,OAAO;SACR;QAED,IAAI,YAAY,GAAoB,EAAE,CAAC;QACvC,IAAI,SAAS,IAAI,SAAS,EAAE;YAC1B,YAAY,GAAG,QAAQ,CAAC,GAAG,CACzB,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,SAAS,IAAI,SAAS,IAAI,OAAO,EAAE,CACpD,CAAC;SACH;QAED,mBAAmB,CAAC,WAAW,CAAC,GAAG;YACjC,GAAG,WAAW;YACd,QAAQ,EAAE,YAAY;SACvB,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,iBAAoC,EACpC,QAAe,EACI,EAAE;IACrB,OAAO;QACL,GAAG,iBAAiB;QACpB,cAAc,EAAE,6BAA6B,CAC3C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,6BAA6B,CAC3C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;KACF,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {\n assertIsStrictHexString,\n type CaipAccountId,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n"]}
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setPermittedEthChainIds = exports.addPermittedEthChainId = exports.getPermittedEthChainIds = void 0;
4
+ const controller_utils_1 = require("@metamask/controller-utils");
5
+ const utils_1 = require("@metamask/utils");
6
+ const transform_1 = require("../scope/transform.cjs");
7
+ const types_1 = require("../scope/types.cjs");
8
+ /**
9
+ * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.
10
+ *
11
+ * @param scopes - The internal scopes from which to get the Ethereum chainIDs.
12
+ * @returns An array of Ethereum chainIDs.
13
+ */
14
+ const getPermittedEthChainIdsFromScopes = (scopes) => {
15
+ const ethChainIds = [];
16
+ Object.keys(scopes).forEach((scopeString) => {
17
+ const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
18
+ if (namespace === utils_1.KnownCaipNamespace.Eip155 && reference) {
19
+ ethChainIds.push((0, controller_utils_1.toHex)(reference));
20
+ }
21
+ });
22
+ return ethChainIds;
23
+ };
24
+ /**
25
+ * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.
26
+ *
27
+ * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.
28
+ * @returns An array of Ethereum chainIDs.
29
+ */
30
+ const getPermittedEthChainIds = (caip25CaveatValue) => {
31
+ const { requiredScopes, optionalScopes } = caip25CaveatValue;
32
+ const ethChainIds = [
33
+ ...getPermittedEthChainIdsFromScopes(requiredScopes),
34
+ ...getPermittedEthChainIdsFromScopes(optionalScopes),
35
+ ];
36
+ return (0, transform_1.getUniqueArrayItems)(ethChainIds);
37
+ };
38
+ exports.getPermittedEthChainIds = getPermittedEthChainIds;
39
+ /**
40
+ * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present
41
+ * in either the pre-existing required or optional scopes.
42
+ *
43
+ * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.
44
+ * @param chainId - The Ethereum chainID to add.
45
+ * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.
46
+ */
47
+ const addPermittedEthChainId = (caip25CaveatValue, chainId) => {
48
+ const scopeString = `eip155:${(0, utils_1.hexToBigInt)(chainId).toString(10)}`;
49
+ if (Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||
50
+ Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)) {
51
+ return caip25CaveatValue;
52
+ }
53
+ return {
54
+ ...caip25CaveatValue,
55
+ optionalScopes: {
56
+ ...caip25CaveatValue.optionalScopes,
57
+ [scopeString]: {
58
+ accounts: [],
59
+ },
60
+ },
61
+ };
62
+ };
63
+ exports.addPermittedEthChainId = addPermittedEthChainId;
64
+ /**
65
+ * Filters the scopes object to only include:
66
+ * - Scopes without references (e.g. "wallet:")
67
+ * - EIP155 scopes for the given chainIDs
68
+ * - Non EIP155 scopes (e.g. "bip122:" or any other non ethereum namespaces)
69
+ *
70
+ * @param scopesObject - The scopes object to filter.
71
+ * @param chainIds - The chainIDs to filter EIP155 scopes by.
72
+ * @returns The filtered scopes object.
73
+ */
74
+ const filterEthScopesObjectByChainId = (scopesObject, chainIds) => {
75
+ const updatedScopesObject = {};
76
+ Object.entries(scopesObject).forEach(([key, scopeObject]) => {
77
+ // Cast needed because index type is returned as `string` by `Object.entries`
78
+ const scopeString = key;
79
+ const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
80
+ if (!reference) {
81
+ updatedScopesObject[scopeString] = scopeObject;
82
+ return;
83
+ }
84
+ if (namespace === utils_1.KnownCaipNamespace.Eip155) {
85
+ const chainId = (0, controller_utils_1.toHex)(reference);
86
+ if (chainIds.includes(chainId)) {
87
+ updatedScopesObject[scopeString] = scopeObject;
88
+ }
89
+ }
90
+ else {
91
+ updatedScopesObject[scopeString] = scopeObject;
92
+ }
93
+ });
94
+ return updatedScopesObject;
95
+ };
96
+ /**
97
+ * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.
98
+ *
99
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.
100
+ * @param chainIds - The Ethereum chainIDs to set as permitted.
101
+ * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.
102
+ */
103
+ const setPermittedEthChainIds = (caip25CaveatValue, chainIds) => {
104
+ let updatedCaveatValue = {
105
+ ...caip25CaveatValue,
106
+ requiredScopes: filterEthScopesObjectByChainId(caip25CaveatValue.requiredScopes, chainIds),
107
+ optionalScopes: filterEthScopesObjectByChainId(caip25CaveatValue.optionalScopes, chainIds),
108
+ };
109
+ chainIds.forEach((chainId) => {
110
+ updatedCaveatValue = (0, exports.addPermittedEthChainId)(updatedCaveatValue, chainId);
111
+ });
112
+ return updatedCaveatValue;
113
+ };
114
+ exports.setPermittedEthChainIds = setPermittedEthChainIds;
115
+ //# sourceMappingURL=caip-permission-adapter-permittedChains.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-permittedChains.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAEnD,2CAAkE;AAGlE,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,SAAS,KAAK,0BAAkB,CAAC,MAAM,IAAI,SAAS,EAAE;YACxD,WAAW,CAAC,IAAI,CAAC,IAAA,wBAAK,EAAC,SAAS,CAAC,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,iBAGC,EACD,EAAE;IACF,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,GAAG,iBAAiB,CAAC;IAE7D,MAAM,WAAW,GAAU;QACzB,GAAG,iCAAiC,CAAC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O;SACR;QACD,IAAI,SAAS,KAAK,0BAAkB,CAAC,MAAM,EAAE;YAC3C,MAAM,OAAO,GAAG,IAAA,wBAAK,EAAC,SAAS,CAAC,CAAC;YACjC,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;gBAC9B,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;aAChD;SACF;aAAM;YACL,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;SAChD;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC;AAEF;;;;;;GAMG;AACI,MAAM,uBAAuB,GAAG,CACrC,iBAAoC,EACpC,QAAe,EACI,EAAE;IACrB,IAAI,kBAAkB,GAAsB;QAC1C,GAAG,iBAAiB;QACpB,cAAc,EAAE,8BAA8B,CAC5C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,8BAA8B,CAC5C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;KACF,CAAC;IAEF,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,kBAAkB,GAAG,IAAA,8BAAsB,EAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;IAC3E,CAAC,CAAC,CAAC;IAEH,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC;AArBW,QAAA,uBAAuB,2BAqBlC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\nimport { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nconst getPermittedEthChainIdsFromScopes = (scopes: InternalScopesObject) => {\n const ethChainIds: Hex[] = [];\n\n Object.keys(scopes).forEach((scopeString) => {\n const { namespace, reference } = parseScopeString(scopeString);\n if (namespace === KnownCaipNamespace.Eip155 && reference) {\n ethChainIds.push(toHex(reference));\n }\n });\n\n return ethChainIds;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nexport const getPermittedEthChainIds = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n) => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethChainIds: Hex[] = [\n ...getPermittedEthChainIdsFromScopes(requiredScopes),\n ...getPermittedEthChainIdsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethChainIds);\n};\n\n/**\n * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.\n * @param chainId - The Ethereum chainID to add.\n * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.\n */\nexport const addPermittedEthChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: Hex,\n): Caip25CaveatValue => {\n const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [scopeString]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - EIP155 scopes for the given chainIDs\n * - Non EIP155 scopes (e.g. \"bip122:\" or any other non ethereum namespaces)\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The chainIDs to filter EIP155 scopes by.\n * @returns The filtered scopes object.\n */\nconst filterEthScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: Hex[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!reference) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n if (namespace === KnownCaipNamespace.Eip155) {\n const chainId = toHex(reference);\n if (chainIds.includes(chainId)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n } else {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.\n * @param chainIds - The Ethereum chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.\n */\nexport const setPermittedEthChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: Hex[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\n};\n"]}
@@ -0,0 +1,27 @@
1
+ import type { Hex } from "@metamask/utils";
2
+ import type { Caip25CaveatValue } from "../caip25Permission.cjs";
3
+ /**
4
+ * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.
5
+ *
6
+ * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.
7
+ * @returns An array of Ethereum chainIDs.
8
+ */
9
+ export declare const getPermittedEthChainIds: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>) => `0x${string}`[];
10
+ /**
11
+ * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present
12
+ * in either the pre-existing required or optional scopes.
13
+ *
14
+ * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.
15
+ * @param chainId - The Ethereum chainID to add.
16
+ * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.
17
+ */
18
+ export declare const addPermittedEthChainId: (caip25CaveatValue: Caip25CaveatValue, chainId: Hex) => Caip25CaveatValue;
19
+ /**
20
+ * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.
21
+ *
22
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.
23
+ * @param chainIds - The Ethereum chainIDs to set as permitted.
24
+ * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.
25
+ */
26
+ export declare const setPermittedEthChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: Hex[]) => Caip25CaveatValue;
27
+ //# sourceMappingURL=caip-permission-adapter-permittedChains.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-permittedChains.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAwB7D;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,sBACf,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,oBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,sBACd,iBAAiB,WAC3B,GAAG,KACX,iBAkBF,CAAC;AAuCF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,sBACf,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAkBF,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { Hex } from "@metamask/utils";
2
+ import type { Caip25CaveatValue } from "../caip25Permission.mjs";
3
+ /**
4
+ * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.
5
+ *
6
+ * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.
7
+ * @returns An array of Ethereum chainIDs.
8
+ */
9
+ export declare const getPermittedEthChainIds: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>) => `0x${string}`[];
10
+ /**
11
+ * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present
12
+ * in either the pre-existing required or optional scopes.
13
+ *
14
+ * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.
15
+ * @param chainId - The Ethereum chainID to add.
16
+ * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.
17
+ */
18
+ export declare const addPermittedEthChainId: (caip25CaveatValue: Caip25CaveatValue, chainId: Hex) => Caip25CaveatValue;
19
+ /**
20
+ * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.
21
+ *
22
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.
23
+ * @param chainIds - The Ethereum chainIDs to set as permitted.
24
+ * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.
25
+ */
26
+ export declare const setPermittedEthChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: Hex[]) => Caip25CaveatValue;
27
+ //# sourceMappingURL=caip-permission-adapter-permittedChains.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-permittedChains.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAwB7D;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,sBACf,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,oBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,sBACd,iBAAiB,WAC3B,GAAG,KACX,iBAkBF,CAAC;AAuCF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,sBACf,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAkBF,CAAC"}