@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,50 @@
1
+ import type { ExternalScopeObject, ExternalScopesObject, InternalScopesObject, NormalizedScopeObject, NormalizedScopesObject } from "./types.cjs";
2
+ /**
3
+ * Returns a list of unique items
4
+ *
5
+ * @param list - The list of items to filter
6
+ * @returns A list of unique items
7
+ */
8
+ export declare const getUniqueArrayItems: <Value>(list: Value[]) => Value[];
9
+ /**
10
+ * Normalizes a ScopeString and ExternalScopeObject into a separate
11
+ * InternalScopeString and NormalizedScopeObject for each reference in the `references`
12
+ * value if defined and adds an empty `accounts` array if not defined.
13
+ *
14
+ * @param scopeString - The string representing the scope
15
+ * @param externalScopeObject - The object that defines the scope
16
+ * @returns a map of caipChainId to ScopeObjects
17
+ */
18
+ export declare const normalizeScope: (scopeString: string, externalScopeObject: ExternalScopeObject) => NormalizedScopesObject;
19
+ /**
20
+ * Merges two NormalizedScopeObjects
21
+ *
22
+ * @param scopeObjectA - The first scope object to merge.
23
+ * @param scopeObjectB - The second scope object to merge.
24
+ * @returns The merged scope object.
25
+ */
26
+ export declare const mergeScopeObject: (scopeObjectA: NormalizedScopeObject, scopeObjectB: NormalizedScopeObject) => NormalizedScopeObject;
27
+ /**
28
+ * Merges two NormalizedScopeObjects
29
+ *
30
+ * @param scopeA - The first normalized scope object to merge.
31
+ * @param scopeB - The second normalized scope object to merge.
32
+ * @returns The merged normalized scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
33
+ */
34
+ export declare const mergeNormalizedScopes: (scopeA: NormalizedScopesObject, scopeB: NormalizedScopesObject) => NormalizedScopesObject;
35
+ /**
36
+ * Merges two InternalScopeObjects
37
+ *
38
+ * @param scopeA - The first internal scope object to merge.
39
+ * @param scopeB - The second internal scope object to merge.
40
+ * @returns The merged internal scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
41
+ */
42
+ export declare const mergeInternalScopes: (scopeA: InternalScopesObject, scopeB: InternalScopesObject) => InternalScopesObject;
43
+ /**
44
+ * Normalizes and merges a set of ExternalScopesObjects into a NormalizedScopesObject (i.e. a set of NormalizedScopeObjects where references are flattened).
45
+ *
46
+ * @param scopes - The external scopes to normalize and merge.
47
+ * @returns The normalized and merged scopes.
48
+ */
49
+ export declare const normalizeAndMergeScopes: (scopes: ExternalScopesObject) => NormalizedScopesObject;
50
+ //# sourceMappingURL=transform.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.d.cts","sourceRoot":"","sources":["../../src/scope/transform.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACvB,oBAAgB;AAGjB;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,mCAE/B,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,gBACZ,MAAM,uBACE,mBAAmB,KACvC,sBAwBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,iBACb,qBAAqB,gBACrB,qBAAqB,0BAgCpC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,WACxB,sBAAsB,UACtB,sBAAsB,KAC7B,sBAwBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,WACtB,oBAAoB,UACpB,oBAAoB,KAC3B,oBAmBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,WAC1B,oBAAoB,KAC3B,sBAQF,CAAC"}
@@ -0,0 +1,50 @@
1
+ import type { ExternalScopeObject, ExternalScopesObject, InternalScopesObject, NormalizedScopeObject, NormalizedScopesObject } from "./types.mjs";
2
+ /**
3
+ * Returns a list of unique items
4
+ *
5
+ * @param list - The list of items to filter
6
+ * @returns A list of unique items
7
+ */
8
+ export declare const getUniqueArrayItems: <Value>(list: Value[]) => Value[];
9
+ /**
10
+ * Normalizes a ScopeString and ExternalScopeObject into a separate
11
+ * InternalScopeString and NormalizedScopeObject for each reference in the `references`
12
+ * value if defined and adds an empty `accounts` array if not defined.
13
+ *
14
+ * @param scopeString - The string representing the scope
15
+ * @param externalScopeObject - The object that defines the scope
16
+ * @returns a map of caipChainId to ScopeObjects
17
+ */
18
+ export declare const normalizeScope: (scopeString: string, externalScopeObject: ExternalScopeObject) => NormalizedScopesObject;
19
+ /**
20
+ * Merges two NormalizedScopeObjects
21
+ *
22
+ * @param scopeObjectA - The first scope object to merge.
23
+ * @param scopeObjectB - The second scope object to merge.
24
+ * @returns The merged scope object.
25
+ */
26
+ export declare const mergeScopeObject: (scopeObjectA: NormalizedScopeObject, scopeObjectB: NormalizedScopeObject) => NormalizedScopeObject;
27
+ /**
28
+ * Merges two NormalizedScopeObjects
29
+ *
30
+ * @param scopeA - The first normalized scope object to merge.
31
+ * @param scopeB - The second normalized scope object to merge.
32
+ * @returns The merged normalized scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
33
+ */
34
+ export declare const mergeNormalizedScopes: (scopeA: NormalizedScopesObject, scopeB: NormalizedScopesObject) => NormalizedScopesObject;
35
+ /**
36
+ * Merges two InternalScopeObjects
37
+ *
38
+ * @param scopeA - The first internal scope object to merge.
39
+ * @param scopeB - The second internal scope object to merge.
40
+ * @returns The merged internal scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
41
+ */
42
+ export declare const mergeInternalScopes: (scopeA: InternalScopesObject, scopeB: InternalScopesObject) => InternalScopesObject;
43
+ /**
44
+ * Normalizes and merges a set of ExternalScopesObjects into a NormalizedScopesObject (i.e. a set of NormalizedScopeObjects where references are flattened).
45
+ *
46
+ * @param scopes - The external scopes to normalize and merge.
47
+ * @returns The normalized and merged scopes.
48
+ */
49
+ export declare const normalizeAndMergeScopes: (scopes: ExternalScopesObject) => NormalizedScopesObject;
50
+ //# sourceMappingURL=transform.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.d.mts","sourceRoot":"","sources":["../../src/scope/transform.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACvB,oBAAgB;AAGjB;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,mCAE/B,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,gBACZ,MAAM,uBACE,mBAAmB,KACvC,sBAwBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,iBACb,qBAAqB,gBACrB,qBAAqB,0BAgCpC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,WACxB,sBAAsB,UACtB,sBAAsB,KAC7B,sBAwBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,WACtB,oBAAoB,UACpB,oBAAoB,KAC3B,oBAmBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,WAC1B,oBAAoB,KAC3B,sBAQF,CAAC"}
@@ -0,0 +1,144 @@
1
+ import $lodash from "lodash";
2
+ const { cloneDeep } = $lodash;
3
+ import { parseScopeString } from "./types.mjs";
4
+ /**
5
+ * Returns a list of unique items
6
+ *
7
+ * @param list - The list of items to filter
8
+ * @returns A list of unique items
9
+ */
10
+ export const getUniqueArrayItems = (list) => {
11
+ return Array.from(new Set(list));
12
+ };
13
+ /**
14
+ * Normalizes a ScopeString and ExternalScopeObject into a separate
15
+ * InternalScopeString and NormalizedScopeObject for each reference in the `references`
16
+ * value if defined and adds an empty `accounts` array if not defined.
17
+ *
18
+ * @param scopeString - The string representing the scope
19
+ * @param externalScopeObject - The object that defines the scope
20
+ * @returns a map of caipChainId to ScopeObjects
21
+ */
22
+ export const normalizeScope = (scopeString, externalScopeObject) => {
23
+ const { references, ...scopeObject } = externalScopeObject;
24
+ const { namespace, reference } = parseScopeString(scopeString);
25
+ const normalizedScopeObject = {
26
+ accounts: [],
27
+ ...scopeObject,
28
+ };
29
+ const shouldFlatten = namespace &&
30
+ !reference &&
31
+ references !== undefined &&
32
+ references.length > 0;
33
+ if (shouldFlatten) {
34
+ return Object.fromEntries(references.map((ref) => [
35
+ `${namespace}:${ref}`,
36
+ cloneDeep(normalizedScopeObject),
37
+ ]));
38
+ }
39
+ return { [scopeString]: normalizedScopeObject };
40
+ };
41
+ /**
42
+ * Merges two NormalizedScopeObjects
43
+ *
44
+ * @param scopeObjectA - The first scope object to merge.
45
+ * @param scopeObjectB - The second scope object to merge.
46
+ * @returns The merged scope object.
47
+ */
48
+ export const mergeScopeObject = (scopeObjectA, scopeObjectB) => {
49
+ const mergedScopeObject = {
50
+ methods: getUniqueArrayItems([
51
+ ...scopeObjectA.methods,
52
+ ...scopeObjectB.methods,
53
+ ]),
54
+ notifications: getUniqueArrayItems([
55
+ ...scopeObjectA.notifications,
56
+ ...scopeObjectB.notifications,
57
+ ]),
58
+ accounts: getUniqueArrayItems([
59
+ ...scopeObjectA.accounts,
60
+ ...scopeObjectB.accounts,
61
+ ]),
62
+ };
63
+ if (scopeObjectA.rpcDocuments || scopeObjectB.rpcDocuments) {
64
+ mergedScopeObject.rpcDocuments = getUniqueArrayItems([
65
+ ...(scopeObjectA.rpcDocuments ?? []),
66
+ ...(scopeObjectB.rpcDocuments ?? []),
67
+ ]);
68
+ }
69
+ if (scopeObjectA.rpcEndpoints || scopeObjectB.rpcEndpoints) {
70
+ mergedScopeObject.rpcEndpoints = getUniqueArrayItems([
71
+ ...(scopeObjectA.rpcEndpoints ?? []),
72
+ ...(scopeObjectB.rpcEndpoints ?? []),
73
+ ]);
74
+ }
75
+ return mergedScopeObject;
76
+ };
77
+ /**
78
+ * Merges two NormalizedScopeObjects
79
+ *
80
+ * @param scopeA - The first normalized scope object to merge.
81
+ * @param scopeB - The second normalized scope object to merge.
82
+ * @returns The merged normalized scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
83
+ */
84
+ export const mergeNormalizedScopes = (scopeA, scopeB) => {
85
+ const scope = {};
86
+ Object.entries(scopeA).forEach(([_scopeString, scopeObjectA]) => {
87
+ // Cast needed because index type is returned as `string` by `Object.entries`
88
+ const scopeString = _scopeString;
89
+ const scopeObjectB = scopeB[scopeString];
90
+ scope[scopeString] = scopeObjectB
91
+ ? mergeScopeObject(scopeObjectA, scopeObjectB)
92
+ : scopeObjectA;
93
+ });
94
+ Object.entries(scopeB).forEach(([_scopeString, scopeObjectB]) => {
95
+ // Cast needed because index type is returned as `string` by `Object.entries`
96
+ const scopeString = _scopeString;
97
+ const scopeObjectA = scopeA[scopeString];
98
+ if (!scopeObjectA) {
99
+ scope[scopeString] = scopeObjectB;
100
+ }
101
+ });
102
+ return scope;
103
+ };
104
+ /**
105
+ * Merges two InternalScopeObjects
106
+ *
107
+ * @param scopeA - The first internal scope object to merge.
108
+ * @param scopeB - The second internal scope object to merge.
109
+ * @returns The merged internal scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
110
+ */
111
+ export const mergeInternalScopes = (scopeA, scopeB) => {
112
+ const resultScope = cloneDeep(scopeA);
113
+ Object.entries(scopeB).forEach(([scopeString, rightScopeObject]) => {
114
+ const internalScopeString = scopeString;
115
+ const leftRequiredScopeObject = resultScope[internalScopeString];
116
+ if (!leftRequiredScopeObject) {
117
+ resultScope[internalScopeString] = rightScopeObject;
118
+ }
119
+ else {
120
+ resultScope[internalScopeString] = {
121
+ accounts: getUniqueArrayItems([
122
+ ...leftRequiredScopeObject.accounts,
123
+ ...rightScopeObject.accounts,
124
+ ]),
125
+ };
126
+ }
127
+ });
128
+ return resultScope;
129
+ };
130
+ /**
131
+ * Normalizes and merges a set of ExternalScopesObjects into a NormalizedScopesObject (i.e. a set of NormalizedScopeObjects where references are flattened).
132
+ *
133
+ * @param scopes - The external scopes to normalize and merge.
134
+ * @returns The normalized and merged scopes.
135
+ */
136
+ export const normalizeAndMergeScopes = (scopes) => {
137
+ let mergedScopes = {};
138
+ Object.keys(scopes).forEach((scopeString) => {
139
+ const normalizedScopes = normalizeScope(scopeString, scopes[scopeString]);
140
+ mergedScopes = mergeNormalizedScopes(mergedScopes, normalizedScopes);
141
+ });
142
+ return mergedScopes;
143
+ };
144
+ //# sourceMappingURL=transform.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.mjs","sourceRoot":"","sources":["../../src/scope/transform.ts"],"names":[],"mappings":";;AAUA,OAAO,EAAE,gBAAgB,EAAE,oBAAgB;AAE3C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAQ,IAAa,EAAW,EAAE;IACnE,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,WAAmB,EACnB,mBAAwC,EAChB,EAAE;IAC1B,MAAM,EAAE,UAAU,EAAE,GAAG,WAAW,EAAE,GAAG,mBAAmB,CAAC;IAC3D,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAE/D,MAAM,qBAAqB,GAA0B;QACnD,QAAQ,EAAE,EAAE;QACZ,GAAG,WAAW;KACf,CAAC;IAEF,MAAM,aAAa,GACjB,SAAS;QACT,CAAC,SAAS;QACV,UAAU,KAAK,SAAS;QACxB,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC;IAExB,IAAI,aAAa,EAAE;QACjB,OAAO,MAAM,CAAC,WAAW,C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uBAAuB,EAAE;YAC5B,WAAW,CAAC,mBAAmB,CAAC,GAAG,gBAAgB,CAAC;SACrD;aAAM;YACL,WAAW,CAAC,mBAAmB,CAAC,GAAG;gBACjC,QAAQ,EAAE,mBAAmB,CAAC;oBAC5B,GAAG,uBAAuB,CAAC,QAAQ;oBACnC,GAAG,gBAAgB,CAAC,QAAQ;iBAC7B,CAAC;aACH,CAAC;SACH;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,MAA4B,EACJ,EAAE;IAC1B,IAAI,YAAY,GAA2B,EAAE,CAAC;IAC9C,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,gBAAgB,GAAG,cAAc,CAAC,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;QAC1E,YAAY,GAAG,qBAAqB,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;IACvE,CAAC,CAAC,CAAC;IAEH,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC","sourcesContent":["import type { CaipReference } from '@metamask/utils';\nimport { cloneDeep } from 'lodash';\n\nimport type {\n ExternalScopeObject,\n ExternalScopesObject,\n InternalScopesObject,\n NormalizedScopeObject,\n NormalizedScopesObject,\n} from './types';\nimport { parseScopeString } from './types';\n\n/**\n * Returns a list of unique items\n *\n * @param list - The list of items to filter\n * @returns A list of unique items\n */\nexport const getUniqueArrayItems = <Value>(list: Value[]): Value[] => {\n return Array.from(new Set(list));\n};\n\n/**\n * Normalizes a ScopeString and ExternalScopeObject into a separate\n * InternalScopeString and NormalizedScopeObject for each reference in the `references`\n * value if defined and adds an empty `accounts` array if not defined.\n *\n * @param scopeString - The string representing the scope\n * @param externalScopeObject - The object that defines the scope\n * @returns a map of caipChainId to ScopeObjects\n */\nexport const normalizeScope = (\n scopeString: string,\n externalScopeObject: ExternalScopeObject,\n): NormalizedScopesObject => {\n const { references, ...scopeObject } = externalScopeObject;\n const { namespace, reference } = parseScopeString(scopeString);\n\n const normalizedScopeObject: NormalizedScopeObject = {\n accounts: [],\n ...scopeObject,\n };\n\n const shouldFlatten =\n namespace &&\n !reference &&\n references !== undefined &&\n references.length > 0;\n\n if (shouldFlatten) {\n return Object.fromEntries(\n references.map((ref: CaipReference) => [\n `${namespace}:${ref}`,\n cloneDeep(normalizedScopeObject),\n ]),\n );\n }\n return { [scopeString]: normalizedScopeObject };\n};\n\n/**\n * Merges two NormalizedScopeObjects\n *\n * @param scopeObjectA - The first scope object to merge.\n * @param scopeObjectB - The second scope object to merge.\n * @returns The merged scope object.\n */\nexport const mergeScopeObject = (\n scopeObjectA: NormalizedScopeObject,\n scopeObjectB: NormalizedScopeObject,\n) => {\n const mergedScopeObject: NormalizedScopeObject = {\n methods: getUniqueArrayItems([\n ...scopeObjectA.methods,\n ...scopeObjectB.methods,\n ]),\n notifications: getUniqueArrayItems([\n ...scopeObjectA.notifications,\n ...scopeObjectB.notifications,\n ]),\n accounts: getUniqueArrayItems([\n ...scopeObjectA.accounts,\n ...scopeObjectB.accounts,\n ]),\n };\n\n if (scopeObjectA.rpcDocuments || scopeObjectB.rpcDocuments) {\n mergedScopeObject.rpcDocuments = getUniqueArrayItems([\n ...(scopeObjectA.rpcDocuments ?? []),\n ...(scopeObjectB.rpcDocuments ?? []),\n ]);\n }\n\n if (scopeObjectA.rpcEndpoints || scopeObjectB.rpcEndpoints) {\n mergedScopeObject.rpcEndpoints = getUniqueArrayItems([\n ...(scopeObjectA.rpcEndpoints ?? []),\n ...(scopeObjectB.rpcEndpoints ?? []),\n ]);\n }\n\n return mergedScopeObject;\n};\n\n/**\n * Merges two NormalizedScopeObjects\n *\n * @param scopeA - The first normalized scope object to merge.\n * @param scopeB - The second normalized scope object to merge.\n * @returns The merged normalized scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n */\nexport const mergeNormalizedScopes = (\n scopeA: NormalizedScopesObject,\n scopeB: NormalizedScopesObject,\n): NormalizedScopesObject => {\n const scope: NormalizedScopesObject = {};\n\n Object.entries(scopeA).forEach(([_scopeString, scopeObjectA]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = _scopeString as keyof typeof scopeA;\n const scopeObjectB = scopeB[scopeString];\n\n scope[scopeString] = scopeObjectB\n ? mergeScopeObject(scopeObjectA, scopeObjectB)\n : scopeObjectA;\n });\n\n Object.entries(scopeB).forEach(([_scopeString, scopeObjectB]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = _scopeString as keyof typeof scopeB;\n const scopeObjectA = scopeA[scopeString];\n\n if (!scopeObjectA) {\n scope[scopeString] = scopeObjectB;\n }\n });\n\n return scope;\n};\n\n/**\n * Merges two InternalScopeObjects\n *\n * @param scopeA - The first internal scope object to merge.\n * @param scopeB - The second internal scope object to merge.\n * @returns The merged internal scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n */\nexport const mergeInternalScopes = (\n scopeA: InternalScopesObject,\n scopeB: InternalScopesObject,\n): InternalScopesObject => {\n const resultScope = cloneDeep(scopeA);\n\n Object.entries(scopeB).forEach(([scopeString, rightScopeObject]) => {\n const internalScopeString = scopeString as keyof typeof scopeB;\n const leftRequiredScopeObject = resultScope[internalScopeString];\n if (!leftRequiredScopeObject) {\n resultScope[internalScopeString] = rightScopeObject;\n } else {\n resultScope[internalScopeString] = {\n accounts: getUniqueArrayItems([\n ...leftRequiredScopeObject.accounts,\n ...rightScopeObject.accounts,\n ]),\n };\n }\n });\n\n return resultScope;\n};\n\n/**\n * Normalizes and merges a set of ExternalScopesObjects into a NormalizedScopesObject (i.e. a set of NormalizedScopeObjects where references are flattened).\n *\n * @param scopes - The external scopes to normalize and merge.\n * @returns The normalized and merged scopes.\n */\nexport const normalizeAndMergeScopes = (\n scopes: ExternalScopesObject,\n): NormalizedScopesObject => {\n let mergedScopes: NormalizedScopesObject = {};\n Object.keys(scopes).forEach((scopeString) => {\n const normalizedScopes = normalizeScope(scopeString, scopes[scopeString]);\n mergedScopes = mergeNormalizedScopes(mergedScopes, normalizedScopes);\n });\n\n return mergedScopes;\n};\n"]}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseScopeString = void 0;
4
+ const utils_1 = require("@metamask/utils");
5
+ /**
6
+ * Parses a scope string into a namespace and reference.
7
+ *
8
+ * @param scopeString - The scope string to parse.
9
+ * @returns An object containing the namespace and reference.
10
+ */
11
+ const parseScopeString = (scopeString) => {
12
+ if ((0, utils_1.isCaipNamespace)(scopeString)) {
13
+ return {
14
+ namespace: scopeString,
15
+ };
16
+ }
17
+ if ((0, utils_1.isCaipChainId)(scopeString)) {
18
+ return (0, utils_1.parseCaipChainId)(scopeString);
19
+ }
20
+ return {};
21
+ };
22
+ exports.parseScopeString = parseScopeString;
23
+ //# sourceMappingURL=types.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.cjs","sourceRoot":"","sources":["../../src/scope/types.ts"],"names":[],"mappings":";;;AAAA,2CAIyB;AAuFzB;;;;;GAKG;AACI,MAAM,gBAAgB,GAAG,CAC9B,WAAmB,EAInB,EAAE;IACF,IAAI,IAAA,uBAAe,EAAC,WAAW,CAAC,EAAE;QAChC,OAAO;YACL,SAAS,EAAE,WAAW;SACvB,CAAC;KACH;IACD,IAAI,IAAA,qBAAa,EAAC,WAAW,CAAC,EAAE;QAC9B,OAAO,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;KACtC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC;AAhBW,QAAA,gBAAgB,oBAgB3B","sourcesContent":["import {\n isCaipNamespace,\n isCaipChainId,\n parseCaipChainId,\n} from '@metamask/utils';\nimport type {\n CaipChainId,\n CaipReference,\n CaipAccountId,\n KnownCaipNamespace,\n CaipNamespace,\n Json,\n} from '@metamask/utils';\n\n/**\n * Represents a `scopeString` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).\n */\nexport type ExternalScopeString = CaipChainId | CaipNamespace;\n/**\n * Represents a `scopeObject` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).\n */\nexport type ExternalScopeObject = Omit<NormalizedScopeObject, 'accounts'> & {\n references?: CaipReference[];\n accounts?: CaipAccountId[];\n};\n/**\n * Represents a `scope` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).\n * TODO update the language in CAIP-217 to use \"scope\" instead of \"scopeObject\" for this full record type.\n */\nexport type ExternalScopesObject = Record<\n ExternalScopeString,\n ExternalScopeObject\n>;\n\n/**\n * Represents a `scopeString` as defined in\n * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that\n * CAIP namespaces without a reference (aside from \"wallet\") are disallowed for our internal representations of CAIP-25 session scopes\n */\nexport type InternalScopeString = CaipChainId | KnownCaipNamespace.Wallet;\n\n/**\n * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) defined scopeObject that is stored in a `endowment:caip25` permission.\n * The only property from the original CAIP-25 scopeObject that we use for permissioning is `accounts`.\n */\nexport type InternalScopeObject = {\n accounts: CaipAccountId[];\n};\n\n/**\n * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) scope that is stored in a `endowment:caip25` permission.\n * Accounts arrays are mapped to CAIP-2 chainIds. These are currently the only properties used by the permission system.\n */\nexport type InternalScopesObject = Record<CaipChainId, InternalScopeObject> & {\n [KnownCaipNamespace.Wallet]?: InternalScopeObject;\n};\n\n/**\n * Represents a `scopeObject` as defined in\n * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that\n * we resolve the `references` property into a scopeObject per reference and\n * assign an empty array to the `accounts` property if not already defined\n * to more easily perform support checks for `wallet_createSession` requests.\n * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.\n */\nexport type NormalizedScopeObject = {\n methods: string[];\n notifications: string[];\n accounts: CaipAccountId[];\n rpcDocuments?: string[];\n rpcEndpoints?: string[];\n};\n/**\n * Represents a keyed `scopeObject` as defined in\n * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that\n * we resolve the `references` property into a scopeObject per reference and\n * assign an empty array to the `accounts` property if not already defined\n * to more easily perform support checks for `wallet_createSession` requests.\n * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.\n */\nexport type NormalizedScopesObject = Record<\n CaipChainId,\n NormalizedScopeObject\n> & {\n [KnownCaipNamespace.Wallet]?: NormalizedScopeObject;\n};\n\nexport type ScopedProperties = Record<CaipChainId, Record<string, Json>> & {\n [KnownCaipNamespace.Wallet]?: Record<string, Json>;\n};\n\n/**\n * Parses a scope string into a namespace and reference.\n *\n * @param scopeString - The scope string to parse.\n * @returns An object containing the namespace and reference.\n */\nexport const parseScopeString = (\n scopeString: string,\n): {\n namespace?: string;\n reference?: string;\n} => {\n if (isCaipNamespace(scopeString)) {\n return {\n namespace: scopeString,\n };\n }\n if (isCaipChainId(scopeString)) {\n return parseCaipChainId(scopeString);\n }\n\n return {};\n};\n\n/**\n * CAIP namespaces excluding \"wallet\" currently supported by/known to the wallet.\n */\nexport type NonWalletKnownCaipNamespace = Exclude<\n KnownCaipNamespace,\n KnownCaipNamespace.Wallet\n>;\n"]}
@@ -0,0 +1,81 @@
1
+ import type { CaipChainId, CaipReference, CaipAccountId, KnownCaipNamespace, CaipNamespace, Json } from "@metamask/utils";
2
+ /**
3
+ * Represents a `scopeString` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).
4
+ */
5
+ export type ExternalScopeString = CaipChainId | CaipNamespace;
6
+ /**
7
+ * Represents a `scopeObject` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).
8
+ */
9
+ export type ExternalScopeObject = Omit<NormalizedScopeObject, 'accounts'> & {
10
+ references?: CaipReference[];
11
+ accounts?: CaipAccountId[];
12
+ };
13
+ /**
14
+ * Represents a `scope` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).
15
+ * TODO update the language in CAIP-217 to use "scope" instead of "scopeObject" for this full record type.
16
+ */
17
+ export type ExternalScopesObject = Record<ExternalScopeString, ExternalScopeObject>;
18
+ /**
19
+ * Represents a `scopeString` as defined in
20
+ * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that
21
+ * CAIP namespaces without a reference (aside from "wallet") are disallowed for our internal representations of CAIP-25 session scopes
22
+ */
23
+ export type InternalScopeString = CaipChainId | KnownCaipNamespace.Wallet;
24
+ /**
25
+ * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) defined scopeObject that is stored in a `endowment:caip25` permission.
26
+ * The only property from the original CAIP-25 scopeObject that we use for permissioning is `accounts`.
27
+ */
28
+ export type InternalScopeObject = {
29
+ accounts: CaipAccountId[];
30
+ };
31
+ /**
32
+ * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) scope that is stored in a `endowment:caip25` permission.
33
+ * Accounts arrays are mapped to CAIP-2 chainIds. These are currently the only properties used by the permission system.
34
+ */
35
+ export type InternalScopesObject = Record<CaipChainId, InternalScopeObject> & {
36
+ [KnownCaipNamespace.Wallet]?: InternalScopeObject;
37
+ };
38
+ /**
39
+ * Represents a `scopeObject` as defined in
40
+ * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that
41
+ * we resolve the `references` property into a scopeObject per reference and
42
+ * assign an empty array to the `accounts` property if not already defined
43
+ * to more easily perform support checks for `wallet_createSession` requests.
44
+ * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.
45
+ */
46
+ export type NormalizedScopeObject = {
47
+ methods: string[];
48
+ notifications: string[];
49
+ accounts: CaipAccountId[];
50
+ rpcDocuments?: string[];
51
+ rpcEndpoints?: string[];
52
+ };
53
+ /**
54
+ * Represents a keyed `scopeObject` as defined in
55
+ * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that
56
+ * we resolve the `references` property into a scopeObject per reference and
57
+ * assign an empty array to the `accounts` property if not already defined
58
+ * to more easily perform support checks for `wallet_createSession` requests.
59
+ * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.
60
+ */
61
+ export type NormalizedScopesObject = Record<CaipChainId, NormalizedScopeObject> & {
62
+ [KnownCaipNamespace.Wallet]?: NormalizedScopeObject;
63
+ };
64
+ export type ScopedProperties = Record<CaipChainId, Record<string, Json>> & {
65
+ [KnownCaipNamespace.Wallet]?: Record<string, Json>;
66
+ };
67
+ /**
68
+ * Parses a scope string into a namespace and reference.
69
+ *
70
+ * @param scopeString - The scope string to parse.
71
+ * @returns An object containing the namespace and reference.
72
+ */
73
+ export declare const parseScopeString: (scopeString: string) => {
74
+ namespace?: string;
75
+ reference?: string;
76
+ };
77
+ /**
78
+ * CAIP namespaces excluding "wallet" currently supported by/known to the wallet.
79
+ */
80
+ export type NonWalletKnownCaipNamespace = Exclude<KnownCaipNamespace, KnownCaipNamespace.Wallet>;
81
+ //# sourceMappingURL=types.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../src/scope/types.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,WAAW,EACX,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,aAAa,EACb,IAAI,EACL,wBAAwB;AAEzB;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,WAAW,GAAG,aAAa,CAAC;AAC9D;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,GAAG;IAC1E,UAAU,CAAC,EAAE,aAAa,EAAE,CAAC;IAC7B,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;CAC5B,CAAC;AACF;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,MAAM,CACvC,mBAAmB,EACnB,mBAAmB,CACpB,CAAC;AAEF;;;;GA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0,0 +1,81 @@
1
+ import type { CaipChainId, CaipReference, CaipAccountId, KnownCaipNamespace, CaipNamespace, Json } from "@metamask/utils";
2
+ /**
3
+ * Represents a `scopeString` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).
4
+ */
5
+ export type ExternalScopeString = CaipChainId | CaipNamespace;
6
+ /**
7
+ * Represents a `scopeObject` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).
8
+ */
9
+ export type ExternalScopeObject = Omit<NormalizedScopeObject, 'accounts'> & {
10
+ references?: CaipReference[];
11
+ accounts?: CaipAccountId[];
12
+ };
13
+ /**
14
+ * Represents a `scope` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).
15
+ * TODO update the language in CAIP-217 to use "scope" instead of "scopeObject" for this full record type.
16
+ */
17
+ export type ExternalScopesObject = Record<ExternalScopeString, ExternalScopeObject>;
18
+ /**
19
+ * Represents a `scopeString` as defined in
20
+ * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that
21
+ * CAIP namespaces without a reference (aside from "wallet") are disallowed for our internal representations of CAIP-25 session scopes
22
+ */
23
+ export type InternalScopeString = CaipChainId | KnownCaipNamespace.Wallet;
24
+ /**
25
+ * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) defined scopeObject that is stored in a `endowment:caip25` permission.
26
+ * The only property from the original CAIP-25 scopeObject that we use for permissioning is `accounts`.
27
+ */
28
+ export type InternalScopeObject = {
29
+ accounts: CaipAccountId[];
30
+ };
31
+ /**
32
+ * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) scope that is stored in a `endowment:caip25` permission.
33
+ * Accounts arrays are mapped to CAIP-2 chainIds. These are currently the only properties used by the permission system.
34
+ */
35
+ export type InternalScopesObject = Record<CaipChainId, InternalScopeObject> & {
36
+ [KnownCaipNamespace.Wallet]?: InternalScopeObject;
37
+ };
38
+ /**
39
+ * Represents a `scopeObject` as defined in
40
+ * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that
41
+ * we resolve the `references` property into a scopeObject per reference and
42
+ * assign an empty array to the `accounts` property if not already defined
43
+ * to more easily perform support checks for `wallet_createSession` requests.
44
+ * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.
45
+ */
46
+ export type NormalizedScopeObject = {
47
+ methods: string[];
48
+ notifications: string[];
49
+ accounts: CaipAccountId[];
50
+ rpcDocuments?: string[];
51
+ rpcEndpoints?: string[];
52
+ };
53
+ /**
54
+ * Represents a keyed `scopeObject` as defined in
55
+ * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that
56
+ * we resolve the `references` property into a scopeObject per reference and
57
+ * assign an empty array to the `accounts` property if not already defined
58
+ * to more easily perform support checks for `wallet_createSession` requests.
59
+ * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.
60
+ */
61
+ export type NormalizedScopesObject = Record<CaipChainId, NormalizedScopeObject> & {
62
+ [KnownCaipNamespace.Wallet]?: NormalizedScopeObject;
63
+ };
64
+ export type ScopedProperties = Record<CaipChainId, Record<string, Json>> & {
65
+ [KnownCaipNamespace.Wallet]?: Record<string, Json>;
66
+ };
67
+ /**
68
+ * Parses a scope string into a namespace and reference.
69
+ *
70
+ * @param scopeString - The scope string to parse.
71
+ * @returns An object containing the namespace and reference.
72
+ */
73
+ export declare const parseScopeString: (scopeString: string) => {
74
+ namespace?: string;
75
+ reference?: string;
76
+ };
77
+ /**
78
+ * CAIP namespaces excluding "wallet" currently supported by/known to the wallet.
79
+ */
80
+ export type NonWalletKnownCaipNamespace = Exclude<KnownCaipNamespace, KnownCaipNamespace.Wallet>;
81
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../src/scope/types.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,WAAW,EACX,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,aAAa,EACb,IAAI,EACL,wBAAwB;AAEzB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E,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;CACpD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,gBACd,MAAM,KAClB;IACD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CAYpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG,OAAO,CAC/C,kBAAkB,EAClB,kBAAkB,CAAC,MAAM,CAC1B,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { isCaipNamespace, isCaipChainId, parseCaipChainId } from "@metamask/utils";
2
+ /**
3
+ * Parses a scope string into a namespace and reference.
4
+ *
5
+ * @param scopeString - The scope string to parse.
6
+ * @returns An object containing the namespace and reference.
7
+ */
8
+ export const parseScopeString = (scopeString) => {
9
+ if (isCaipNamespace(scopeString)) {
10
+ return {
11
+ namespace: scopeString,
12
+ };
13
+ }
14
+ if (isCaipChainId(scopeString)) {
15
+ return parseCaipChainId(scopeString);
16
+ }
17
+ return {};
18
+ };
19
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../src/scope/types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,aAAa,EACb,gBAAgB,EACjB,wBAAwB;AAuFzB;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,WAAmB,EAInB,EAAE;IACF,IAAI,eAAe,CAAC,WAAW,CAAC,EAAE;QAChC,OAAO;YACL,SAAS,EAAE,WAAW;SACvB,CAAC;KACH;IACD,IAAI,aAAa,CAAC,WAAW,CAAC,EAAE;QAC9B,OAAO,gBAAgB,CAAC,WAAW,CAAC,CAAC;KACtC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC","sourcesContent":["import {\n isCaipNamespace,\n isCaipChainId,\n parseCaipChainId,\n} from '@metamask/utils';\nimport type {\n CaipChainId,\n CaipReference,\n CaipAccountId,\n KnownCaipNamespace,\n CaipNamespace,\n Json,\n} from '@metamask/utils';\n\n/**\n * Represents a `scopeString` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).\n */\nexport type ExternalScopeString = CaipChainId | CaipNamespace;\n/**\n * Represents a `scopeObject` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).\n */\nexport type ExternalScopeObject = Omit<NormalizedScopeObject, 'accounts'> & {\n references?: CaipReference[];\n accounts?: CaipAccountId[];\n};\n/**\n * Represents a `scope` as defined in [CAIP-217](https://chainagnostic.org/CAIPs/caip-217).\n * TODO update the language in CAIP-217 to use \"scope\" instead of \"scopeObject\" for this full record type.\n */\nexport type ExternalScopesObject = Record<\n ExternalScopeString,\n ExternalScopeObject\n>;\n\n/**\n * Represents a `scopeString` as defined in\n * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that\n * CAIP namespaces without a reference (aside from \"wallet\") are disallowed for our internal representations of CAIP-25 session scopes\n */\nexport type InternalScopeString = CaipChainId | KnownCaipNamespace.Wallet;\n\n/**\n * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) defined scopeObject that is stored in a `endowment:caip25` permission.\n * The only property from the original CAIP-25 scopeObject that we use for permissioning is `accounts`.\n */\nexport type InternalScopeObject = {\n accounts: CaipAccountId[];\n};\n\n/**\n * A trimmed down version of a [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) scope that is stored in a `endowment:caip25` permission.\n * Accounts arrays are mapped to CAIP-2 chainIds. These are currently the only properties used by the permission system.\n */\nexport type InternalScopesObject = Record<CaipChainId, InternalScopeObject> & {\n [KnownCaipNamespace.Wallet]?: InternalScopeObject;\n};\n\n/**\n * Represents a `scopeObject` as defined in\n * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that\n * we resolve the `references` property into a scopeObject per reference and\n * assign an empty array to the `accounts` property if not already defined\n * to more easily perform support checks for `wallet_createSession` requests.\n * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.\n */\nexport type NormalizedScopeObject = {\n methods: string[];\n notifications: string[];\n accounts: CaipAccountId[];\n rpcDocuments?: string[];\n rpcEndpoints?: string[];\n};\n/**\n * Represents a keyed `scopeObject` as defined in\n * [CAIP-217](https://chainagnostic.org/CAIPs/caip-217), with the exception that\n * we resolve the `references` property into a scopeObject per reference and\n * assign an empty array to the `accounts` property if not already defined\n * to more easily perform support checks for `wallet_createSession` requests.\n * Also used as the return type for `wallet_createSession` and `wallet_sessionChanged`.\n */\nexport type NormalizedScopesObject = Record<\n CaipChainId,\n NormalizedScopeObject\n> & {\n [KnownCaipNamespace.Wallet]?: NormalizedScopeObject;\n};\n\nexport type ScopedProperties = Record<CaipChainId, Record<string, Json>> & {\n [KnownCaipNamespace.Wallet]?: Record<string, Json>;\n};\n\n/**\n * Parses a scope string into a namespace and reference.\n *\n * @param scopeString - The scope string to parse.\n * @returns An object containing the namespace and reference.\n */\nexport const parseScopeString = (\n scopeString: string,\n): {\n namespace?: string;\n reference?: string;\n} => {\n if (isCaipNamespace(scopeString)) {\n return {\n namespace: scopeString,\n };\n }\n if (isCaipChainId(scopeString)) {\n return parseCaipChainId(scopeString);\n }\n\n return {};\n};\n\n/**\n * CAIP namespaces excluding \"wallet\" currently supported by/known to the wallet.\n */\nexport type NonWalletKnownCaipNamespace = Exclude<\n KnownCaipNamespace,\n KnownCaipNamespace.Wallet\n>;\n"]}
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getValidScopes = exports.isValidScope = void 0;
4
+ const utils_1 = require("@metamask/utils");
5
+ const types_1 = require("./types.cjs");
6
+ /**
7
+ * Validates a scope object according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
8
+ *
9
+ * @param scopeString - The scope string to validate.
10
+ * @param scopeObject - The scope object to validate.
11
+ * @returns A boolean indicating if the scope object is valid according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
12
+ */
13
+ const isValidScope = (scopeString, scopeObject) => {
14
+ const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
15
+ // Namespace is required
16
+ if (!namespace) {
17
+ return false;
18
+ }
19
+ const { references, methods, notifications, accounts, rpcDocuments, rpcEndpoints, ...extraProperties } = scopeObject;
20
+ // Methods and notifications are required
21
+ if (!methods || !notifications) {
22
+ return false;
23
+ }
24
+ // For namespaces other than 'wallet', either reference or non-empty references array must be present
25
+ if (namespace !== 'wallet' &&
26
+ !reference &&
27
+ (!references || references.length === 0)) {
28
+ return false;
29
+ }
30
+ // If references are present, reference must be absent and all references must be valid
31
+ if (references) {
32
+ if (reference) {
33
+ return false;
34
+ }
35
+ const areReferencesValid = references.every((nestedReference) => (0, utils_1.isCaipReference)(nestedReference));
36
+ if (!areReferencesValid) {
37
+ return false;
38
+ }
39
+ }
40
+ const areMethodsValid = methods.every((method) => typeof method === 'string' && method.trim() !== '');
41
+ if (!areMethodsValid) {
42
+ return false;
43
+ }
44
+ const areNotificationsValid = notifications.every((notification) => typeof notification === 'string' && notification.trim() !== '');
45
+ if (!areNotificationsValid) {
46
+ return false;
47
+ }
48
+ // Ensure no unexpected properties are present in the scope object
49
+ if (Object.keys(extraProperties).length > 0) {
50
+ return false;
51
+ }
52
+ return true;
53
+ };
54
+ exports.isValidScope = isValidScope;
55
+ /**
56
+ * Filters out invalid scopes and returns valid sets of required and optional scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
57
+ *
58
+ * @param requiredScopes - The required scopes to validate.
59
+ * @param optionalScopes - The optional scopes to validate.
60
+ * @returns An object containing valid required scopes and optional scopes.
61
+ */
62
+ const getValidScopes = (requiredScopes, optionalScopes) => {
63
+ const validRequiredScopes = {};
64
+ for (const [scopeString, scopeObject] of Object.entries(requiredScopes || {})) {
65
+ if ((0, exports.isValidScope)(scopeString, scopeObject)) {
66
+ validRequiredScopes[scopeString] = {
67
+ accounts: [],
68
+ ...scopeObject,
69
+ };
70
+ }
71
+ }
72
+ const validOptionalScopes = {};
73
+ for (const [scopeString, scopeObject] of Object.entries(optionalScopes || {})) {
74
+ if ((0, exports.isValidScope)(scopeString, scopeObject)) {
75
+ validOptionalScopes[scopeString] = {
76
+ accounts: [],
77
+ ...scopeObject,
78
+ };
79
+ }
80
+ }
81
+ return {
82
+ validRequiredScopes,
83
+ validOptionalScopes,
84
+ };
85
+ };
86
+ exports.getValidScopes = getValidScopes;
87
+ //# sourceMappingURL=validation.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.cjs","sourceRoot":"","sources":["../../src/scope/validation.ts"],"names":[],"mappings":";;;AAAA,2CAAkD;AAOlD,uCAA2C;AAE3C;;;;;;GAMG;AACI,MAAM,YAAY,GAAG,CAC1B,WAAgC,EAChC,WAAgC,EACvB,EAAE;IACX,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAE/D,wBAAwB;IACxB,IAAI,CAAC,SAAS,EAAE;QACd,OAAO,KAAK,CAAC;KACd;IAED,MAAM,EACJ,UAAU,EACV,OAAO,EACP,aAAa,EACb,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,GAAG,eAAe,EACnB,GAAG,WAAW,CAAC;IAEhB,yCAAyC;IACzC,IAAI,CAAC,OAAO,IAAI,CAAC,aAAa,EAAE;QAC9B,OAAO,KAAK,CAAC;KACd;IAED,qGAAqG;IACrG,IACE,SAAS,KAAK,QAAQ;QACtB,CAAC,SAAS;QACV,CAAC,CAAC,UAAU,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,CAAC,EACxC;QACA,OAAO,KAAK,CAAC;KACd;IAED,uFAAuF;IACvF,IAAI,UAAU,EAAE;QACd,IAAI,SAAS,EAAE;YACb,OAAO,KAAK,CAAC;SACd;QAED,MAAM,kBAAkB,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,eAAe,EAAE,EA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I,MAAM,CAAC,OAAO,CACrD,cAAc,IAAI,EAAE,CACrB,EAAE;QACD,IAAI,IAAA,oBAAY,EAAC,WAAW,EAAE,WAAW,CAAC,EAAE;YAC1C,mBAAmB,CAAC,WAAW,CAAC,GAAG;gBACjC,QAAQ,EAAE,EAAE;gBACZ,GAAG,WAAW;aACf,CAAC;SACH;KACF;IAED,OAAO;QACL,mBAAmB;QACnB,mBAAmB;KACpB,CAAC;AACJ,CAAC,CAAC;AAhCW,QAAA,cAAc,kBAgCzB","sourcesContent":["import { isCaipReference } from '@metamask/utils';\n\nimport type {\n ExternalScopeString,\n ExternalScopeObject,\n ExternalScopesObject,\n} from './types';\nimport { parseScopeString } from './types';\n\n/**\n * Validates a scope object according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.\n *\n * @param scopeString - The scope string to validate.\n * @param scopeObject - The scope object to validate.\n * @returns A boolean indicating if the scope object is valid according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.\n */\nexport const isValidScope = (\n scopeString: ExternalScopeString,\n scopeObject: ExternalScopeObject,\n): boolean => {\n const { namespace, reference } = parseScopeString(scopeString);\n\n // Namespace is required\n if (!namespace) {\n return false;\n }\n\n const {\n references,\n methods,\n notifications,\n accounts,\n rpcDocuments,\n rpcEndpoints,\n ...extraProperties\n } = scopeObject;\n\n // Methods and notifications are required\n if (!methods || !notifications) {\n return false;\n }\n\n // For namespaces other than 'wallet', either reference or non-empty references array must be present\n if (\n namespace !== 'wallet' &&\n !reference &&\n (!references || references.length === 0)\n ) {\n return false;\n }\n\n // If references are present, reference must be absent and all references must be valid\n if (references) {\n if (reference) {\n return false;\n }\n\n const areReferencesValid = references.every((nestedReference) =>\n isCaipReference(nestedReference),\n );\n\n if (!areReferencesValid) {\n return false;\n }\n }\n\n const areMethodsValid = methods.every(\n (method) => typeof method === 'string' && method.trim() !== '',\n );\n\n if (!areMethodsValid) {\n return false;\n }\n\n const areNotificationsValid = notifications.every(\n (notification) =>\n typeof notification === 'string' && notification.trim() !== '',\n );\n\n if (!areNotificationsValid) {\n return false;\n }\n\n // Ensure no unexpected properties are present in the scope object\n if (Object.keys(extraProperties).length > 0) {\n return false;\n }\n\n return true;\n};\n\n/**\n * Filters out invalid scopes and returns valid sets of required and optional scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.\n *\n * @param requiredScopes - The required scopes to validate.\n * @param optionalScopes - The optional scopes to validate.\n * @returns An object containing valid required scopes and optional scopes.\n */\nexport const getValidScopes = (\n requiredScopes?: ExternalScopesObject,\n optionalScopes?: ExternalScopesObject,\n) => {\n const validRequiredScopes: ExternalScopesObject = {};\n for (const [scopeString, scopeObject] of Object.entries(\n requiredScopes || {},\n )) {\n if (isValidScope(scopeString, scopeObject)) {\n validRequiredScopes[scopeString] = {\n accounts: [],\n ...scopeObject,\n };\n }\n }\n\n const validOptionalScopes: ExternalScopesObject = {};\n for (const [scopeString, scopeObject] of Object.entries(\n optionalScopes || {},\n )) {\n if (isValidScope(scopeString, scopeObject)) {\n validOptionalScopes[scopeString] = {\n accounts: [],\n ...scopeObject,\n };\n }\n }\n\n return {\n validRequiredScopes,\n validOptionalScopes,\n };\n};\n"]}
@@ -0,0 +1,21 @@
1
+ import type { ExternalScopeString, ExternalScopeObject, ExternalScopesObject } from "./types.cjs";
2
+ /**
3
+ * Validates a scope object according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
4
+ *
5
+ * @param scopeString - The scope string to validate.
6
+ * @param scopeObject - The scope object to validate.
7
+ * @returns A boolean indicating if the scope object is valid according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
8
+ */
9
+ export declare const isValidScope: (scopeString: ExternalScopeString, scopeObject: ExternalScopeObject) => boolean;
10
+ /**
11
+ * Filters out invalid scopes and returns valid sets of required and optional scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
12
+ *
13
+ * @param requiredScopes - The required scopes to validate.
14
+ * @param optionalScopes - The optional scopes to validate.
15
+ * @returns An object containing valid required scopes and optional scopes.
16
+ */
17
+ export declare const getValidScopes: (requiredScopes?: ExternalScopesObject, optionalScopes?: ExternalScopesObject) => {
18
+ validRequiredScopes: ExternalScopesObject;
19
+ validOptionalScopes: ExternalScopesObject;
20
+ };
21
+ //# sourceMappingURL=validation.d.cts.map