@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
@@ -0,0 +1,109 @@
1
+ import { toHex } from "@metamask/controller-utils";
2
+ import { hexToBigInt, KnownCaipNamespace } from "@metamask/utils";
3
+ import { getUniqueArrayItems } from "../scope/transform.mjs";
4
+ import { parseScopeString } from "../scope/types.mjs";
5
+ /**
6
+ * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.
7
+ *
8
+ * @param scopes - The internal scopes from which to get the Ethereum chainIDs.
9
+ * @returns An array of Ethereum chainIDs.
10
+ */
11
+ const getPermittedEthChainIdsFromScopes = (scopes) => {
12
+ const ethChainIds = [];
13
+ Object.keys(scopes).forEach((scopeString) => {
14
+ const { namespace, reference } = parseScopeString(scopeString);
15
+ if (namespace === KnownCaipNamespace.Eip155 && reference) {
16
+ ethChainIds.push(toHex(reference));
17
+ }
18
+ });
19
+ return ethChainIds;
20
+ };
21
+ /**
22
+ * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.
23
+ *
24
+ * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.
25
+ * @returns An array of Ethereum chainIDs.
26
+ */
27
+ export const getPermittedEthChainIds = (caip25CaveatValue) => {
28
+ const { requiredScopes, optionalScopes } = caip25CaveatValue;
29
+ const ethChainIds = [
30
+ ...getPermittedEthChainIdsFromScopes(requiredScopes),
31
+ ...getPermittedEthChainIdsFromScopes(optionalScopes),
32
+ ];
33
+ return getUniqueArrayItems(ethChainIds);
34
+ };
35
+ /**
36
+ * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present
37
+ * in either the pre-existing required or optional scopes.
38
+ *
39
+ * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.
40
+ * @param chainId - The Ethereum chainID to add.
41
+ * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.
42
+ */
43
+ export const addPermittedEthChainId = (caip25CaveatValue, chainId) => {
44
+ const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;
45
+ if (Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||
46
+ Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)) {
47
+ return caip25CaveatValue;
48
+ }
49
+ return {
50
+ ...caip25CaveatValue,
51
+ optionalScopes: {
52
+ ...caip25CaveatValue.optionalScopes,
53
+ [scopeString]: {
54
+ accounts: [],
55
+ },
56
+ },
57
+ };
58
+ };
59
+ /**
60
+ * Filters the scopes object to only include:
61
+ * - Scopes without references (e.g. "wallet:")
62
+ * - EIP155 scopes for the given chainIDs
63
+ * - Non EIP155 scopes (e.g. "bip122:" or any other non ethereum namespaces)
64
+ *
65
+ * @param scopesObject - The scopes object to filter.
66
+ * @param chainIds - The chainIDs to filter EIP155 scopes by.
67
+ * @returns The filtered scopes object.
68
+ */
69
+ const filterEthScopesObjectByChainId = (scopesObject, chainIds) => {
70
+ const updatedScopesObject = {};
71
+ Object.entries(scopesObject).forEach(([key, scopeObject]) => {
72
+ // Cast needed because index type is returned as `string` by `Object.entries`
73
+ const scopeString = key;
74
+ const { namespace, reference } = parseScopeString(scopeString);
75
+ if (!reference) {
76
+ updatedScopesObject[scopeString] = scopeObject;
77
+ return;
78
+ }
79
+ if (namespace === KnownCaipNamespace.Eip155) {
80
+ const chainId = toHex(reference);
81
+ if (chainIds.includes(chainId)) {
82
+ updatedScopesObject[scopeString] = scopeObject;
83
+ }
84
+ }
85
+ else {
86
+ updatedScopesObject[scopeString] = scopeObject;
87
+ }
88
+ });
89
+ return updatedScopesObject;
90
+ };
91
+ /**
92
+ * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.
93
+ *
94
+ * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.
95
+ * @param chainIds - The Ethereum chainIDs to set as permitted.
96
+ * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.
97
+ */
98
+ export const setPermittedEthChainIds = (caip25CaveatValue, chainIds) => {
99
+ let updatedCaveatValue = {
100
+ ...caip25CaveatValue,
101
+ requiredScopes: filterEthScopesObjectByChainId(caip25CaveatValue.requiredScopes, chainIds),
102
+ optionalScopes: filterEthScopesObjectByChainId(caip25CaveatValue.optionalScopes, chainIds),
103
+ };
104
+ chainIds.forEach((chainId) => {
105
+ updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);
106
+ });
107
+ return updatedCaveatValue;
108
+ };
109
+ //# sourceMappingURL=caip-permission-adapter-permittedChains.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-permittedChains.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AAEnD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,wBAAwB;AAGlE,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;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,gBAAgB,CAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,SAAS,KAAK,kBAAkB,CAAC,MAAM,IAAI,SAAS,EAAE;YACxD,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;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B,CAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,CAAC,SAAS,EAAE;YACd,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;YAC/C,OAAO;SACR;QACD,IAAI,SAAS,KAAK,kBAAkB,CAAC,MAAM,EAAE;YAC3C,MAAM,OAAO,GAAG,KAAK,CAAC,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;AACH,MAAM,CAAC,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,sBAAsB,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;IAC3E,CAAC,CAAC,CAAC;IAEH,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC","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,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSessionScopes = exports.getInternalScopesObject = 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
+ * Converts an NormalizedScopesObject to a InternalScopesObject.
10
+ *
11
+ * @param normalizedScopesObject - The NormalizedScopesObject to convert.
12
+ * @returns An InternalScopesObject.
13
+ */
14
+ const getInternalScopesObject = (normalizedScopesObject) => {
15
+ const internalScopes = {};
16
+ Object.entries(normalizedScopesObject).forEach(([_scopeString, { accounts }]) => {
17
+ const scopeString = _scopeString;
18
+ internalScopes[scopeString] = {
19
+ accounts,
20
+ };
21
+ });
22
+ return internalScopes;
23
+ };
24
+ exports.getInternalScopesObject = getInternalScopesObject;
25
+ /**
26
+ * Converts an InternalScopesObject to a NormalizedScopesObject.
27
+ *
28
+ * @param internalScopesObject - The InternalScopesObject to convert.
29
+ * @param hooks - An object containing the following properties:
30
+ * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
31
+ * @returns A NormalizedScopesObject.
32
+ */
33
+ const getNormalizedScopesObject = (internalScopesObject, { getNonEvmSupportedMethods, }) => {
34
+ const normalizedScopes = {};
35
+ Object.entries(internalScopesObject).forEach(([_scopeString, { accounts }]) => {
36
+ const scopeString = _scopeString;
37
+ const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
38
+ let methods = [];
39
+ let notifications = [];
40
+ if (scopeString === utils_1.KnownCaipNamespace.Wallet ||
41
+ namespace === utils_1.KnownCaipNamespace.Wallet) {
42
+ if (reference === utils_1.KnownCaipNamespace.Eip155) {
43
+ methods = constants_1.KnownWalletNamespaceRpcMethods[reference];
44
+ }
45
+ else if ((0, utils_1.isCaipChainId)(scopeString)) {
46
+ methods = getNonEvmSupportedMethods(scopeString);
47
+ }
48
+ else {
49
+ methods = constants_1.KnownWalletRpcMethods;
50
+ }
51
+ }
52
+ else if (namespace === utils_1.KnownCaipNamespace.Eip155) {
53
+ methods = constants_1.KnownRpcMethods[namespace];
54
+ notifications = constants_1.KnownNotifications[namespace];
55
+ }
56
+ else {
57
+ methods = getNonEvmSupportedMethods(scopeString);
58
+ notifications = [];
59
+ }
60
+ normalizedScopes[scopeString] = {
61
+ methods,
62
+ notifications,
63
+ accounts,
64
+ };
65
+ });
66
+ return normalizedScopes;
67
+ };
68
+ /**
69
+ * Takes the scopes from an endowment:caip25 permission caveat value,
70
+ * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.
71
+ *
72
+ * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.
73
+ * @param hooks - An object containing the following properties:
74
+ * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
75
+ * @returns A NormalizedScopesObject.
76
+ */
77
+ const getSessionScopes = (caip25CaveatValue, { getNonEvmSupportedMethods, }) => {
78
+ return (0, transform_1.mergeNormalizedScopes)(getNormalizedScopesObject(caip25CaveatValue.requiredScopes, {
79
+ getNonEvmSupportedMethods,
80
+ }), getNormalizedScopesObject(caip25CaveatValue.optionalScopes, {
81
+ getNonEvmSupportedMethods,
82
+ }));
83
+ };
84
+ exports.getSessionScopes = getSessionScopes;
85
+ //# sourceMappingURL=caip-permission-adapter-session-scopes.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-session-scopes.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-session-scopes.ts"],"names":[],"mappings":";;;AAAA,2CAIyB;AAGzB,sDAK4B;AAC5B,sDAA2D;AAK3D,8CAAkD;AAElD;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,cAAc,GAAyB,EAAE,CAAC;IAEhD,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,OAAO,CAC5C,CAAC,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QAC/B,MAAM,WAAW,GA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AC;SACpB;QAED,gBAAgB,CAAC,WAAW,CAAC,GAAG;YAC9B,OAAO;YACP,aAAa;YACb,QAAQ;SACT,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,OAAO,gBAAgB,CAAC;AAC1B,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACI,MAAM,gBAAgB,GAAG,CAC9B,iBAGC,EACD,EACE,yBAAyB,GAG1B,EACD,EAAE;IACF,OAAO,IAAA,iCAAqB,EAC1B,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,EACF,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,gBAAgB,oBAmB3B","sourcesContent":["import {\n type CaipChainId,\n isCaipChainId,\n KnownCaipNamespace,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport {\n KnownNotifications,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownWalletRpcMethods,\n} from '../scope/constants';\nimport { mergeNormalizedScopes } from '../scope/transform';\nimport type {\n InternalScopesObject,\n NormalizedScopesObject,\n} from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Converts an NormalizedScopesObject to a InternalScopesObject.\n *\n * @param normalizedScopesObject - The NormalizedScopesObject to convert.\n * @returns An InternalScopesObject.\n */\nexport const getInternalScopesObject = (\n normalizedScopesObject: NormalizedScopesObject,\n) => {\n const internalScopes: InternalScopesObject = {};\n\n Object.entries(normalizedScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof normalizedScopesObject;\n\n internalScopes[scopeString] = {\n accounts,\n };\n },\n );\n\n return internalScopes;\n};\n\n/**\n * Converts an InternalScopesObject to a NormalizedScopesObject.\n *\n * @param internalScopesObject - The InternalScopesObject to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nconst getNormalizedScopesObject = (\n internalScopesObject: InternalScopesObject,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const normalizedScopes: NormalizedScopesObject = {};\n\n Object.entries(internalScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof internalScopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n let methods: string[] = [];\n let notifications: string[] = [];\n\n if (\n scopeString === KnownCaipNamespace.Wallet ||\n namespace === KnownCaipNamespace.Wallet\n ) {\n if (reference === KnownCaipNamespace.Eip155) {\n methods = KnownWalletNamespaceRpcMethods[reference];\n } else if (isCaipChainId(scopeString)) {\n methods = getNonEvmSupportedMethods(scopeString);\n } else {\n methods = KnownWalletRpcMethods;\n }\n } else if (namespace === KnownCaipNamespace.Eip155) {\n methods = KnownRpcMethods[namespace];\n notifications = KnownNotifications[namespace];\n } else {\n methods = getNonEvmSupportedMethods(scopeString);\n notifications = [];\n }\n\n normalizedScopes[scopeString] = {\n methods,\n notifications,\n accounts,\n };\n },\n );\n\n return normalizedScopes;\n};\n\n/**\n * Takes the scopes from an endowment:caip25 permission caveat value,\n * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nexport const getSessionScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n return mergeNormalizedScopes(\n getNormalizedScopesObject(caip25CaveatValue.requiredScopes, {\n getNonEvmSupportedMethods,\n }),\n getNormalizedScopesObject(caip25CaveatValue.optionalScopes, {\n getNonEvmSupportedMethods,\n }),\n );\n};\n"]}
@@ -0,0 +1,23 @@
1
+ import { type CaipChainId } from "@metamask/utils";
2
+ import type { Caip25CaveatValue } from "../caip25Permission.cjs";
3
+ import type { InternalScopesObject, NormalizedScopesObject } from "../scope/types.cjs";
4
+ /**
5
+ * Converts an NormalizedScopesObject to a InternalScopesObject.
6
+ *
7
+ * @param normalizedScopesObject - The NormalizedScopesObject to convert.
8
+ * @returns An InternalScopesObject.
9
+ */
10
+ export declare const getInternalScopesObject: (normalizedScopesObject: NormalizedScopesObject) => InternalScopesObject;
11
+ /**
12
+ * Takes the scopes from an endowment:caip25 permission caveat value,
13
+ * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.
14
+ *
15
+ * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.
16
+ * @param hooks - An object containing the following properties:
17
+ * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
18
+ * @returns A NormalizedScopesObject.
19
+ */
20
+ export declare const getSessionScopes: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>, { getNonEvmSupportedMethods, }: {
21
+ getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
22
+ }) => NormalizedScopesObject;
23
+ //# sourceMappingURL=caip-permission-adapter-session-scopes.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-session-scopes.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-session-scopes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAGjB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAQ7D,OAAO,KAAK,EACV,oBAAoB,EACpB,sBAAsB,EACvB,2BAAuB;AAGxB;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,2BACV,sBAAsB,yBAe/C,CAAC;AAyDF;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,sBACR,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC;uCAIoC,WAAW,KAAK,MAAM,EAAE;4BAW9D,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { type CaipChainId } from "@metamask/utils";
2
+ import type { Caip25CaveatValue } from "../caip25Permission.mjs";
3
+ import type { InternalScopesObject, NormalizedScopesObject } from "../scope/types.mjs";
4
+ /**
5
+ * Converts an NormalizedScopesObject to a InternalScopesObject.
6
+ *
7
+ * @param normalizedScopesObject - The NormalizedScopesObject to convert.
8
+ * @returns An InternalScopesObject.
9
+ */
10
+ export declare const getInternalScopesObject: (normalizedScopesObject: NormalizedScopesObject) => InternalScopesObject;
11
+ /**
12
+ * Takes the scopes from an endowment:caip25 permission caveat value,
13
+ * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.
14
+ *
15
+ * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.
16
+ * @param hooks - An object containing the following properties:
17
+ * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
18
+ * @returns A NormalizedScopesObject.
19
+ */
20
+ export declare const getSessionScopes: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>, { getNonEvmSupportedMethods, }: {
21
+ getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
22
+ }) => NormalizedScopesObject;
23
+ //# sourceMappingURL=caip-permission-adapter-session-scopes.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-session-scopes.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-session-scopes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAGjB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAQ7D,OAAO,KAAK,EACV,oBAAoB,EACpB,sBAAsB,EACvB,2BAAuB;AAGxB;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,2BACV,sBAAsB,yBAe/C,CAAC;AAyDF;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,sBACR,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC;uCAIoC,WAAW,KAAK,MAAM,EAAE;4BAW9D,CAAC"}
@@ -0,0 +1,80 @@
1
+ import { isCaipChainId, KnownCaipNamespace } from "@metamask/utils";
2
+ import { KnownNotifications, KnownRpcMethods, KnownWalletNamespaceRpcMethods, KnownWalletRpcMethods } from "../scope/constants.mjs";
3
+ import { mergeNormalizedScopes } from "../scope/transform.mjs";
4
+ import { parseScopeString } from "../scope/types.mjs";
5
+ /**
6
+ * Converts an NormalizedScopesObject to a InternalScopesObject.
7
+ *
8
+ * @param normalizedScopesObject - The NormalizedScopesObject to convert.
9
+ * @returns An InternalScopesObject.
10
+ */
11
+ export const getInternalScopesObject = (normalizedScopesObject) => {
12
+ const internalScopes = {};
13
+ Object.entries(normalizedScopesObject).forEach(([_scopeString, { accounts }]) => {
14
+ const scopeString = _scopeString;
15
+ internalScopes[scopeString] = {
16
+ accounts,
17
+ };
18
+ });
19
+ return internalScopes;
20
+ };
21
+ /**
22
+ * Converts an InternalScopesObject to a NormalizedScopesObject.
23
+ *
24
+ * @param internalScopesObject - The InternalScopesObject to convert.
25
+ * @param hooks - An object containing the following properties:
26
+ * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
27
+ * @returns A NormalizedScopesObject.
28
+ */
29
+ const getNormalizedScopesObject = (internalScopesObject, { getNonEvmSupportedMethods, }) => {
30
+ const normalizedScopes = {};
31
+ Object.entries(internalScopesObject).forEach(([_scopeString, { accounts }]) => {
32
+ const scopeString = _scopeString;
33
+ const { namespace, reference } = parseScopeString(scopeString);
34
+ let methods = [];
35
+ let notifications = [];
36
+ if (scopeString === KnownCaipNamespace.Wallet ||
37
+ namespace === KnownCaipNamespace.Wallet) {
38
+ if (reference === KnownCaipNamespace.Eip155) {
39
+ methods = KnownWalletNamespaceRpcMethods[reference];
40
+ }
41
+ else if (isCaipChainId(scopeString)) {
42
+ methods = getNonEvmSupportedMethods(scopeString);
43
+ }
44
+ else {
45
+ methods = KnownWalletRpcMethods;
46
+ }
47
+ }
48
+ else if (namespace === KnownCaipNamespace.Eip155) {
49
+ methods = KnownRpcMethods[namespace];
50
+ notifications = KnownNotifications[namespace];
51
+ }
52
+ else {
53
+ methods = getNonEvmSupportedMethods(scopeString);
54
+ notifications = [];
55
+ }
56
+ normalizedScopes[scopeString] = {
57
+ methods,
58
+ notifications,
59
+ accounts,
60
+ };
61
+ });
62
+ return normalizedScopes;
63
+ };
64
+ /**
65
+ * Takes the scopes from an endowment:caip25 permission caveat value,
66
+ * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.
67
+ *
68
+ * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.
69
+ * @param hooks - An object containing the following properties:
70
+ * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
71
+ * @returns A NormalizedScopesObject.
72
+ */
73
+ export const getSessionScopes = (caip25CaveatValue, { getNonEvmSupportedMethods, }) => {
74
+ return mergeNormalizedScopes(getNormalizedScopesObject(caip25CaveatValue.requiredScopes, {
75
+ getNonEvmSupportedMethods,
76
+ }), getNormalizedScopesObject(caip25CaveatValue.optionalScopes, {
77
+ getNonEvmSupportedMethods,
78
+ }));
79
+ };
80
+ //# sourceMappingURL=caip-permission-adapter-session-scopes.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-adapter-session-scopes.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-session-scopes.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,aAAa,EACb,kBAAkB,EACnB,wBAAwB;AAGzB,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,8BAA8B,EAC9B,qBAAqB,EACtB,+BAA2B;AAC5B,OAAO,EAAE,qBAAqB,EAAE,+BAA2B;AAK3D,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAElD;;;;;GAKG;AAC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,MAAM,EAAE;YAClD,OAAO,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;YACrC,aAAa,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;SAC/C;aAAM;YACL,OAAO,GAAG,yBAAyB,CAAC,WAAW,CAAC,CAAC;YACjD,aAAa,GAAG,EAAE,CAAC;SACpB;QAED,gBAAgB,CAAC,WAAW,CAAC,GAAG;YAC9B,OAAO;YACP,aAAa;YACb,QAAQ;SACT,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,OAAO,gBAAgB,CAAC;AAC1B,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,iBAGC,EACD,EACE,yBAAyB,GAG1B,EACD,EAAE;IACF,OAAO,qBAAqB,CAC1B,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,EACF,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,CACH,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {\n type CaipChainId,\n isCaipChainId,\n KnownCaipNamespace,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport {\n KnownNotifications,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownWalletRpcMethods,\n} from '../scope/constants';\nimport { mergeNormalizedScopes } from '../scope/transform';\nimport type {\n InternalScopesObject,\n NormalizedScopesObject,\n} from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Converts an NormalizedScopesObject to a InternalScopesObject.\n *\n * @param normalizedScopesObject - The NormalizedScopesObject to convert.\n * @returns An InternalScopesObject.\n */\nexport const getInternalScopesObject = (\n normalizedScopesObject: NormalizedScopesObject,\n) => {\n const internalScopes: InternalScopesObject = {};\n\n Object.entries(normalizedScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof normalizedScopesObject;\n\n internalScopes[scopeString] = {\n accounts,\n };\n },\n );\n\n return internalScopes;\n};\n\n/**\n * Converts an InternalScopesObject to a NormalizedScopesObject.\n *\n * @param internalScopesObject - The InternalScopesObject to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nconst getNormalizedScopesObject = (\n internalScopesObject: InternalScopesObject,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const normalizedScopes: NormalizedScopesObject = {};\n\n Object.entries(internalScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof internalScopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n let methods: string[] = [];\n let notifications: string[] = [];\n\n if (\n scopeString === KnownCaipNamespace.Wallet ||\n namespace === KnownCaipNamespace.Wallet\n ) {\n if (reference === KnownCaipNamespace.Eip155) {\n methods = KnownWalletNamespaceRpcMethods[reference];\n } else if (isCaipChainId(scopeString)) {\n methods = getNonEvmSupportedMethods(scopeString);\n } else {\n methods = KnownWalletRpcMethods;\n }\n } else if (namespace === KnownCaipNamespace.Eip155) {\n methods = KnownRpcMethods[namespace];\n notifications = KnownNotifications[namespace];\n } else {\n methods = getNonEvmSupportedMethods(scopeString);\n notifications = [];\n }\n\n normalizedScopes[scopeString] = {\n methods,\n notifications,\n accounts,\n };\n },\n );\n\n return normalizedScopes;\n};\n\n/**\n * Takes the scopes from an endowment:caip25 permission caveat value,\n * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nexport const getSessionScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n return mergeNormalizedScopes(\n getNormalizedScopesObject(caip25CaveatValue.requiredScopes, {\n getNonEvmSupportedMethods,\n }),\n getNormalizedScopesObject(caip25CaveatValue.optionalScopes, {\n getNonEvmSupportedMethods,\n }),\n );\n};\n"]}