@metamask/snaps-utils 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +3 -0
  3. package/dist/caveats.d.ts +22 -0
  4. package/dist/caveats.js +27 -0
  5. package/dist/caveats.js.map +1 -0
  6. package/dist/cronjob.d.ts +89 -0
  7. package/dist/cronjob.js +74 -0
  8. package/dist/cronjob.js.map +1 -0
  9. package/dist/deep-clone.d.ts +1 -0
  10. package/dist/deep-clone.js +9 -0
  11. package/dist/deep-clone.js.map +1 -0
  12. package/dist/default-endowments.d.ts +4 -0
  13. package/dist/default-endowments.js +43 -0
  14. package/dist/default-endowments.js.map +1 -0
  15. package/dist/eval-worker.d.ts +1 -0
  16. package/dist/eval-worker.js +32 -0
  17. package/dist/eval-worker.js.map +1 -0
  18. package/dist/eval.d.ts +8 -0
  19. package/dist/eval.js +28 -0
  20. package/dist/eval.js.map +1 -0
  21. package/dist/flatMap.d.ts +22 -0
  22. package/dist/flatMap.js +38 -0
  23. package/dist/flatMap.js.map +1 -0
  24. package/dist/fs.d.ts +69 -0
  25. package/dist/fs.js +144 -0
  26. package/dist/fs.js.map +1 -0
  27. package/dist/index.browser.d.ts +13 -0
  28. package/dist/index.browser.js +30 -0
  29. package/dist/index.browser.js.map +1 -0
  30. package/dist/index.d.ts +19 -0
  31. package/dist/index.js +36 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/json-rpc.d.ts +10 -0
  34. package/dist/json-rpc.js +22 -0
  35. package/dist/json-rpc.js.map +1 -0
  36. package/dist/manifest/index.browser.d.ts +1 -0
  37. package/dist/manifest/index.browser.js +18 -0
  38. package/dist/manifest/index.browser.js.map +1 -0
  39. package/dist/manifest/index.d.ts +2 -0
  40. package/dist/manifest/index.js +19 -0
  41. package/dist/manifest/index.js.map +1 -0
  42. package/dist/manifest/manifest.d.ts +75 -0
  43. package/dist/manifest/manifest.js +237 -0
  44. package/dist/manifest/manifest.js.map +1 -0
  45. package/dist/manifest/validation.d.ts +483 -0
  46. package/dist/manifest/validation.js +142 -0
  47. package/dist/manifest/validation.js.map +1 -0
  48. package/dist/mock.d.ts +14 -0
  49. package/dist/mock.js +107 -0
  50. package/dist/mock.js.map +1 -0
  51. package/dist/namespace.d.ts +275 -0
  52. package/dist/namespace.js +223 -0
  53. package/dist/namespace.js.map +1 -0
  54. package/dist/notification.d.ts +66 -0
  55. package/dist/notification.js +58 -0
  56. package/dist/notification.js.map +1 -0
  57. package/dist/npm.d.ts +20 -0
  58. package/dist/npm.js +73 -0
  59. package/dist/npm.js.map +1 -0
  60. package/dist/object.d.ts +8 -0
  61. package/dist/object.js +15 -0
  62. package/dist/object.js.map +1 -0
  63. package/dist/post-process.d.ts +71 -0
  64. package/dist/post-process.js +321 -0
  65. package/dist/post-process.js.map +1 -0
  66. package/dist/snaps.d.ts +165 -0
  67. package/dist/snaps.js +123 -0
  68. package/dist/snaps.js.map +1 -0
  69. package/dist/types.d.ts +107 -0
  70. package/dist/types.js +82 -0
  71. package/dist/types.js.map +1 -0
  72. package/dist/url.d.ts +7 -0
  73. package/dist/url.js +19 -0
  74. package/dist/url.js.map +1 -0
  75. package/dist/versions.d.ts +44 -0
  76. package/dist/versions.js +77 -0
  77. package/dist/versions.js.map +1 -0
  78. package/package.json +100 -0
package/dist/mock.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateMockEndowments = exports.isConstructor = exports.ALL_APIS = void 0;
7
+ const events_1 = __importDefault(require("events"));
8
+ const crypto_1 = __importDefault(require("crypto"));
9
+ const default_endowments_1 = require("./default-endowments");
10
+ const NETWORK_APIS = ['fetch', 'WebSocket'];
11
+ exports.ALL_APIS = [...default_endowments_1.DEFAULT_ENDOWMENTS, ...NETWORK_APIS];
12
+ /**
13
+ * Get a mock snap API, that always returns `true` for requests.
14
+ *
15
+ * @returns A mocked snap provider.
16
+ */
17
+ function getMockSnapGlobal() {
18
+ return { request: async () => true };
19
+ }
20
+ /**
21
+ * Get a mock Ethereum provider, that always returns `true` for requests.
22
+ *
23
+ * @returns A mocked ethereum provider.
24
+ */
25
+ function getMockEthereumProvider() {
26
+ const mockProvider = new events_1.default();
27
+ mockProvider.request = async () => true;
28
+ return mockProvider;
29
+ }
30
+ /**
31
+ * Check if a value is a constructor.
32
+ *
33
+ * @param value - The value to check.
34
+ * @returns `true` if the value is a constructor, or `false` otherwise.
35
+ */
36
+ const isConstructor = (value) => { var _a, _b; return Boolean(typeof ((_b = (_a = value === null || value === void 0 ? void 0 : value.prototype) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name) === 'string'); };
37
+ exports.isConstructor = isConstructor;
38
+ /**
39
+ * A function that always returns `true`.
40
+ *
41
+ * @returns `true`.
42
+ */
43
+ const mockFunction = () => true;
44
+ class MockClass {
45
+ }
46
+ const handler = {
47
+ construct(Target, args) {
48
+ return new Proxy(new Target(...args), handler);
49
+ },
50
+ get(_target, _prop) {
51
+ return mockFunction;
52
+ },
53
+ };
54
+ /**
55
+ * Generate a mock class for a given value. The value is wrapped in a Proxy, and
56
+ * all methods are replaced with a mock function.
57
+ *
58
+ * @param value - The value to mock.
59
+ * @returns A mock class.
60
+ */
61
+ const generateMockClass = (value) => {
62
+ return new Proxy(value, handler);
63
+ };
64
+ // Things not currently auto-mocked because of NodeJS, by adding them here we
65
+ // have types for them and can use that to generate mocks if needed.
66
+ const mockWindow = {
67
+ WebSocket: MockClass,
68
+ crypto: crypto_1.default,
69
+ SubtleCrypto: MockClass,
70
+ };
71
+ /**
72
+ * Generate a mock endowment for a certain class or function on the `globalThis`
73
+ * object.
74
+ *
75
+ * @param key - The key to generate the mock endowment for.
76
+ * @returns A mocked class or function. If the key is part of the default
77
+ * endowments, the original value is returned.
78
+ */
79
+ const generateMockEndowment = (key) => {
80
+ const globalValue = globalThis[key];
81
+ // Default exposed APIs don't need to be mocked
82
+ if (globalValue && default_endowments_1.DEFAULT_ENDOWMENTS.includes(key)) {
83
+ return globalValue;
84
+ }
85
+ // Fall back to mockWindow for certain APIs not exposed in global in Node.JS
86
+ const globalOrMocked = globalValue !== null && globalValue !== void 0 ? globalValue : mockWindow[key];
87
+ const type = typeof globalOrMocked;
88
+ const isFunction = type === 'function';
89
+ if (isFunction && (0, exports.isConstructor)(globalOrMocked)) {
90
+ return generateMockClass(globalOrMocked);
91
+ }
92
+ else if (isFunction || !globalOrMocked) {
93
+ // Fall back to function mock for now
94
+ return mockFunction;
95
+ }
96
+ return globalOrMocked;
97
+ };
98
+ /**
99
+ * Generate mock endowments for all the APIs as defined in {@link ALL_APIS}.
100
+ *
101
+ * @returns A map of endowments.
102
+ */
103
+ const generateMockEndowments = () => {
104
+ return exports.ALL_APIS.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [cur]: generateMockEndowment(cur) })), { snap: getMockSnapGlobal(), ethereum: getMockEthereumProvider() });
105
+ };
106
+ exports.generateMockEndowments = generateMockEndowments;
107
+ //# sourceMappingURL=mock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock.js","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAkC;AAClC,oDAA4B;AAC5B,6DAA0D;AAE1D,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;AAE/B,QAAA,QAAQ,GAAa,CAAC,GAAG,uCAAkB,EAAE,GAAG,YAAY,CAAC,CAAC;AAU3E;;;;GAIG;AACH,SAAS,iBAAiB;IACxB,OAAO,EAAE,OAAO,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;AACvC,CAAC;AAED;;;;GAIG;AACH,SAAS,uBAAuB;IAC9B,MAAM,YAAY,GAAG,IAAI,gBAAY,EAAmC,CAAC;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E;IAC5E,MAAM,cAAc,GAAG,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,UAAU,CAAC,GAAG,CAAC,CAAC;IAEtD,MAAM,IAAI,GAAG,OAAO,cAAc,CAAC;IACnC,MAAM,UAAU,GAAG,IAAI,KAAK,UAAU,CAAC;IACvC,IAAI,UAAU,IAAI,IAAA,qBAAa,EAAC,cAAc,CAAC,EAAE;QAC/C,OAAO,iBAAiB,CAAC,cAAc,CAAC,CAAC;KAC1C;SAAM,IAAI,UAAU,IAAI,CAAC,cAAc,EAAE;QACxC,qCAAqC;QACrC,OAAO,YAAY,CAAC;KACrB;IACD,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAEF;;;;GAIG;AACI,MAAM,sBAAsB,GAAG,GAAG,EAAE;IACzC,OAAO,gBAAQ,CAAC,MAAM,CACpB,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,iCAAM,GAAG,KAAE,CAAC,GAAG,CAAC,EAAE,qBAAqB,CAAC,GAAG,CAAC,IAAG,EAC7D,EAAE,IAAI,EAAE,iBAAiB,EAAE,EAAE,QAAQ,EAAE,uBAAuB,EAAE,EAAE,CACnE,CAAC;AACJ,CAAC,CAAC;AALW,QAAA,sBAAsB,0BAKjC","sourcesContent":["import EventEmitter from 'events';\nimport crypto from 'crypto';\nimport { DEFAULT_ENDOWMENTS } from './default-endowments';\n\nconst NETWORK_APIS = ['fetch', 'WebSocket'];\n\nexport const ALL_APIS: string[] = [...DEFAULT_ENDOWMENTS, ...NETWORK_APIS];\n\ntype MockSnapGlobal = {\n request: () => Promise<any>;\n};\n\ntype MockEthereumProvider = EventEmitter & {\n request: () => Promise<any>;\n};\n\n/**\n * Get a mock snap API, that always returns `true` for requests.\n *\n * @returns A mocked snap provider.\n */\nfunction getMockSnapGlobal(): MockSnapGlobal {\n return { request: async () => true };\n}\n\n/**\n * Get a mock Ethereum provider, that always returns `true` for requests.\n *\n * @returns A mocked ethereum provider.\n */\nfunction getMockEthereumProvider(): MockEthereumProvider {\n const mockProvider = new EventEmitter() as Partial<MockEthereumProvider>;\n mockProvider.request = async () => true;\n return mockProvider as MockEthereumProvider;\n}\n\n/**\n * Check if a value is a constructor.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nexport const isConstructor = (value: any) =>\n Boolean(typeof value?.prototype?.constructor?.name === 'string');\n\n/**\n * A function that always returns `true`.\n *\n * @returns `true`.\n */\nconst mockFunction = () => true;\nclass MockClass {}\n\nconst handler = {\n construct(Target: any, args: any[]): any {\n return new Proxy(new Target(...args), handler);\n },\n get(_target: any, _prop: any) {\n return mockFunction;\n },\n};\n\n/**\n * Generate a mock class for a given value. The value is wrapped in a Proxy, and\n * all methods are replaced with a mock function.\n *\n * @param value - The value to mock.\n * @returns A mock class.\n */\nconst generateMockClass = (value: any) => {\n return new Proxy(value, handler);\n};\n\n// Things not currently auto-mocked because of NodeJS, by adding them here we\n// have types for them and can use that to generate mocks if needed.\nconst mockWindow: Record<string, unknown> = {\n WebSocket: MockClass,\n crypto,\n SubtleCrypto: MockClass,\n};\n\n/**\n * Generate a mock endowment for a certain class or function on the `globalThis`\n * object.\n *\n * @param key - The key to generate the mock endowment for.\n * @returns A mocked class or function. If the key is part of the default\n * endowments, the original value is returned.\n */\nconst generateMockEndowment = (key: string) => {\n const globalValue = (globalThis as any)[key];\n\n // Default exposed APIs don't need to be mocked\n if (globalValue && DEFAULT_ENDOWMENTS.includes(key)) {\n return globalValue;\n }\n\n // Fall back to mockWindow for certain APIs not exposed in global in Node.JS\n const globalOrMocked = globalValue ?? mockWindow[key];\n\n const type = typeof globalOrMocked;\n const isFunction = type === 'function';\n if (isFunction && isConstructor(globalOrMocked)) {\n return generateMockClass(globalOrMocked);\n } else if (isFunction || !globalOrMocked) {\n // Fall back to function mock for now\n return mockFunction;\n }\n return globalOrMocked;\n};\n\n/**\n * Generate mock endowments for all the APIs as defined in {@link ALL_APIS}.\n *\n * @returns A map of endowments.\n */\nexport const generateMockEndowments = () => {\n return ALL_APIS.reduce<Record<string, any>>(\n (acc, cur) => ({ ...acc, [cur]: generateMockEndowment(cur) }),\n { snap: getMockSnapGlobal(), ethereum: getMockEthereumProvider() },\n );\n};\n"]}
@@ -0,0 +1,275 @@
1
+ import { Infer } from 'superstruct';
2
+ import { AssertionErrorConstructor } from '@metamask/utils';
3
+ export declare const CHAIN_ID_REGEX: RegExp;
4
+ export declare const ACCOUNT_ID_REGEX: RegExp;
5
+ /**
6
+ * Parse a chain ID string to an object containing the namespace and reference.
7
+ * This validates the chain ID before parsing it.
8
+ *
9
+ * @param chainId - The chain ID to validate and parse.
10
+ * @returns The parsed chain ID.
11
+ */
12
+ export declare function parseChainId(chainId: ChainId): {
13
+ namespace: NamespaceId;
14
+ reference: string;
15
+ };
16
+ /**
17
+ * Parse an account ID to an object containing the chain, chain ID and address.
18
+ * This validates the account ID before parsing it.
19
+ *
20
+ * @param accountId - The account ID to validate and parse.
21
+ * @returns The parsed account ID.
22
+ */
23
+ export declare function parseAccountId(accountId: AccountId): {
24
+ chain: {
25
+ namespace: NamespaceId;
26
+ reference: string;
27
+ };
28
+ chainId: ChainId;
29
+ address: string;
30
+ };
31
+ /**
32
+ * A helper struct for a string with a minimum length of 1 and a maximum length
33
+ * of 40.
34
+ */
35
+ export declare const LimitedString: import("superstruct").Struct<string, null>;
36
+ /**
37
+ * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
38
+ */
39
+ export declare const ChainIdStruct: import("superstruct").Struct<string, null>;
40
+ export declare type ChainId = `${string}:${string}`;
41
+ export declare const AccountIdStruct: import("superstruct").Struct<string, null>;
42
+ export declare type AccountId = `${ChainId}:${string}`;
43
+ export declare const AccountIdArrayStruct: import("superstruct").Struct<string[], import("superstruct").Struct<string, null>>;
44
+ /**
45
+ * A chain descriptor.
46
+ */
47
+ export declare const ChainStruct: import("superstruct").Struct<{
48
+ name: string;
49
+ id: string;
50
+ }, {
51
+ id: import("superstruct").Struct<string, null>;
52
+ name: import("superstruct").Struct<string, null>;
53
+ }>;
54
+ export declare type Chain = Infer<typeof ChainStruct>;
55
+ export declare const NamespaceStruct: import("superstruct").Struct<{
56
+ chains: {
57
+ name: string;
58
+ id: string;
59
+ }[];
60
+ methods?: string[] | undefined;
61
+ events?: string[] | undefined;
62
+ }, {
63
+ /**
64
+ * A list of supported chains in the namespace.
65
+ */
66
+ chains: import("superstruct").Struct<{
67
+ name: string;
68
+ id: string;
69
+ }[], import("superstruct").Struct<{
70
+ name: string;
71
+ id: string;
72
+ }, {
73
+ id: import("superstruct").Struct<string, null>;
74
+ name: import("superstruct").Struct<string, null>;
75
+ }>>;
76
+ /**
77
+ * A list of supported RPC methods on the namespace, that a DApp can call.
78
+ */
79
+ methods: import("superstruct").Struct<string[] | undefined, import("superstruct").Struct<string, null>>;
80
+ /**
81
+ * A list of supported RPC events on the namespace, that a DApp can listen to.
82
+ */
83
+ events: import("superstruct").Struct<string[] | undefined, import("superstruct").Struct<string, null>>;
84
+ }>;
85
+ export declare type Namespace = Infer<typeof NamespaceStruct>;
86
+ export declare const RequestNamespaceStruct: import("superstruct").Struct<{
87
+ chains: string[];
88
+ methods?: string[] | undefined;
89
+ events?: string[] | undefined;
90
+ }, {
91
+ chains: import("superstruct").Struct<string[], import("superstruct").Struct<string, null>>;
92
+ methods: import("superstruct").Struct<string[] | undefined, import("superstruct").Struct<string, null>>;
93
+ events: import("superstruct").Struct<string[] | undefined, import("superstruct").Struct<string, null>>;
94
+ }>;
95
+ export declare type RequestNamespace = Infer<typeof RequestNamespaceStruct>;
96
+ export declare const SessionNamespaceStruct: import("superstruct").Struct<{
97
+ accounts: string[];
98
+ chains: string[];
99
+ methods?: string[] | undefined;
100
+ events?: string[] | undefined;
101
+ }, {
102
+ accounts: import("superstruct").Struct<string[], import("superstruct").Struct<string, null>>;
103
+ chains: import("superstruct").Struct<string[], import("superstruct").Struct<string, null>>;
104
+ methods: import("superstruct").Struct<string[] | undefined, import("superstruct").Struct<string, null>>;
105
+ events: import("superstruct").Struct<string[] | undefined, import("superstruct").Struct<string, null>>;
106
+ }>;
107
+ export declare type SessionNamespace = Infer<typeof SessionNamespaceStruct>;
108
+ /**
109
+ * A CAIP-2 namespace, i.e., the first part of a chain ID.
110
+ */
111
+ export declare const NamespaceIdStruct: import("superstruct").Struct<string, null>;
112
+ export declare type NamespaceId = Infer<typeof NamespaceIdStruct>;
113
+ /**
114
+ * An object mapping CAIP-2 namespaces to their values.
115
+ */
116
+ export declare const NamespacesStruct: import("superstruct").Struct<Record<string, {
117
+ chains: {
118
+ name: string;
119
+ id: string;
120
+ }[];
121
+ methods?: string[] | undefined;
122
+ events?: string[] | undefined;
123
+ }>, null>;
124
+ export declare type Namespaces = Infer<typeof NamespacesStruct>;
125
+ export declare const SessionStruct: import("superstruct").Struct<{
126
+ namespaces: Record<string, {
127
+ accounts: string[];
128
+ chains: string[];
129
+ methods?: string[] | undefined;
130
+ events?: string[] | undefined;
131
+ }>;
132
+ }, {
133
+ namespaces: import("superstruct").Struct<Record<string, {
134
+ accounts: string[];
135
+ chains: string[];
136
+ methods?: string[] | undefined;
137
+ events?: string[] | undefined;
138
+ }>, null>;
139
+ }>;
140
+ export declare type Session = Infer<typeof SessionStruct>;
141
+ /**
142
+ * Asserts that the given value is a valid {@link Session}.
143
+ *
144
+ * @param value - The value to assert.
145
+ * @throws If the value is not a valid {@link Session}.
146
+ */
147
+ export declare function assertIsSession(value: unknown): asserts value is Session;
148
+ export declare const ConnectArgumentsStruct: import("superstruct").Struct<{
149
+ requiredNamespaces: Record<string, {
150
+ chains: string[];
151
+ methods?: string[] | undefined;
152
+ events?: string[] | undefined;
153
+ }>;
154
+ }, {
155
+ requiredNamespaces: import("superstruct").Struct<Record<string, {
156
+ chains: string[];
157
+ methods?: string[] | undefined;
158
+ events?: string[] | undefined;
159
+ }>, null>;
160
+ }>;
161
+ export declare type ConnectArguments = Infer<typeof ConnectArgumentsStruct>;
162
+ export declare const RequestArgumentsStruct: import("superstruct").Struct<{
163
+ method: string;
164
+ params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
165
+ id?: string | number | null | undefined;
166
+ jsonrpc?: "2.0" | undefined;
167
+ }, {
168
+ params: import("superstruct").Struct<Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined, null>;
169
+ method: import("superstruct").Struct<string, null>;
170
+ id: import("superstruct").Struct<string | number | null | undefined, unknown>;
171
+ jsonrpc: import("superstruct").Struct<"2.0" | undefined, unknown>;
172
+ }>;
173
+ export declare type RequestArguments = Infer<typeof RequestArgumentsStruct>;
174
+ export declare const MultiChainRequestStruct: import("superstruct").Struct<{
175
+ request: {
176
+ method: string;
177
+ params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
178
+ id?: string | number | null | undefined;
179
+ jsonrpc?: "2.0" | undefined;
180
+ };
181
+ chainId: string;
182
+ }, {
183
+ chainId: import("superstruct").Struct<string, null>;
184
+ request: import("superstruct").Struct<{
185
+ method: string;
186
+ params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
187
+ id?: string | number | null | undefined;
188
+ jsonrpc?: "2.0" | undefined;
189
+ }, {
190
+ params: import("superstruct").Struct<Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined, null>;
191
+ method: import("superstruct").Struct<string, null>;
192
+ id: import("superstruct").Struct<string | number | null | undefined, unknown>;
193
+ jsonrpc: import("superstruct").Struct<"2.0" | undefined, unknown>;
194
+ }>;
195
+ }>;
196
+ export declare type MultiChainRequest = Infer<typeof MultiChainRequestStruct>;
197
+ /**
198
+ * Check if the given value is a CAIP-2 namespace ID.
199
+ *
200
+ * @param value - The value to check.
201
+ * @returns Whether the value is a CAIP-2 namespace ID.
202
+ */
203
+ export declare function isNamespaceId(value: unknown): value is NamespaceId;
204
+ /**
205
+ * Check if the given value is a CAIP-2 chain ID.
206
+ *
207
+ * @param value - The value to check.
208
+ * @returns Whether the value is a CAIP-2 chain ID.
209
+ */
210
+ export declare function isChainId(value: unknown): value is ChainId;
211
+ /**
212
+ * Check if the given value is a CAIP-10 account ID.
213
+ *
214
+ * @param value - The value to check.
215
+ * @returns Whether the value is a CAIP-10 account ID.
216
+ */
217
+ export declare function isAccountId(value: unknown): value is AccountId;
218
+ /**
219
+ * Check if the given value is an array of CAIP-10 account IDs.
220
+ *
221
+ * @param value - The value to check.
222
+ * @returns Whether the value is an array of CAIP-10 account IDs.
223
+ */
224
+ export declare function isAccountIdArray(value: unknown): value is AccountId[];
225
+ /**
226
+ * Check if the given value is a {@link ConnectArguments} object.
227
+ *
228
+ * @param value - The value to check.
229
+ * @returns Whether the value is a valid {@link ConnectArguments} object.
230
+ */
231
+ export declare function isConnectArguments(value: unknown): value is ConnectArguments;
232
+ /**
233
+ * Assert that the given value is a {@link ConnectArguments} object.
234
+ *
235
+ * @param value - The value to check.
236
+ * @throws If the value is not a valid {@link ConnectArguments} object.
237
+ */
238
+ export declare function assertIsConnectArguments(value: unknown): asserts value is ConnectArguments;
239
+ /**
240
+ * Check if the given value is a {@link MultiChainRequest} object.
241
+ *
242
+ * @param value - The value to check.
243
+ * @returns Whether the value is a valid {@link MultiChainRequest} object.
244
+ */
245
+ export declare function isMultiChainRequest(value: unknown): value is MultiChainRequest;
246
+ /**
247
+ * Assert that the given value is a {@link MultiChainRequest} object.
248
+ *
249
+ * @param value - The value to check.
250
+ * @throws If the value is not a valid {@link MultiChainRequest} object.
251
+ */
252
+ export declare function assertIsMultiChainRequest(value: unknown): asserts value is MultiChainRequest;
253
+ /**
254
+ * Check if a value is a {@link Namespace}.
255
+ *
256
+ * @param value - The value to validate.
257
+ * @returns True if the value is a valid {@link Namespace}.
258
+ */
259
+ export declare function isNamespace(value: unknown): value is Namespace;
260
+ /**
261
+ * Check if a value is an object containing {@link Namespace}s.
262
+ *
263
+ * @param value - The value to validate.
264
+ * @returns True if the value is a valid object containing namespaces.
265
+ */
266
+ export declare function isNamespacesObject(value: unknown): value is Namespaces;
267
+ /**
268
+ * Assert that the given value is a {@link Namespaces} object.
269
+ *
270
+ * @param value - The value to check.
271
+ * @param ErrorWrapper - The error wrapper to use. Defaults to
272
+ * {@link AssertionError}.
273
+ * @throws If the value is not a valid {@link Namespaces} object.
274
+ */
275
+ export declare function assertIsNamespacesObject(value: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts value is Namespaces;
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertIsNamespacesObject = exports.isNamespacesObject = exports.isNamespace = exports.assertIsMultiChainRequest = exports.isMultiChainRequest = exports.assertIsConnectArguments = exports.isConnectArguments = exports.isAccountIdArray = exports.isAccountId = exports.isChainId = exports.isNamespaceId = exports.MultiChainRequestStruct = exports.RequestArgumentsStruct = exports.ConnectArgumentsStruct = exports.assertIsSession = exports.SessionStruct = exports.NamespacesStruct = exports.NamespaceIdStruct = exports.SessionNamespaceStruct = exports.RequestNamespaceStruct = exports.NamespaceStruct = exports.ChainStruct = exports.AccountIdArrayStruct = exports.AccountIdStruct = exports.ChainIdStruct = exports.LimitedString = exports.parseAccountId = exports.parseChainId = exports.ACCOUNT_ID_REGEX = exports.CHAIN_ID_REGEX = void 0;
4
+ const superstruct_1 = require("superstruct");
5
+ const utils_1 = require("@metamask/utils");
6
+ exports.CHAIN_ID_REGEX = /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-a-zA-Z0-9]{1,32})$/u;
7
+ exports.ACCOUNT_ID_REGEX = /^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-a-zA-Z0-9]{1,32})):(?<accountAddress>[a-zA-Z0-9]{1,64})$/u;
8
+ /**
9
+ * Parse a chain ID string to an object containing the namespace and reference.
10
+ * This validates the chain ID before parsing it.
11
+ *
12
+ * @param chainId - The chain ID to validate and parse.
13
+ * @returns The parsed chain ID.
14
+ */
15
+ function parseChainId(chainId) {
16
+ const match = exports.CHAIN_ID_REGEX.exec(chainId);
17
+ if (!(match === null || match === void 0 ? void 0 : match.groups)) {
18
+ throw new Error('Invalid chain ID.');
19
+ }
20
+ return {
21
+ namespace: match.groups.namespace,
22
+ reference: match.groups.reference,
23
+ };
24
+ }
25
+ exports.parseChainId = parseChainId;
26
+ /**
27
+ * Parse an account ID to an object containing the chain, chain ID and address.
28
+ * This validates the account ID before parsing it.
29
+ *
30
+ * @param accountId - The account ID to validate and parse.
31
+ * @returns The parsed account ID.
32
+ */
33
+ function parseAccountId(accountId) {
34
+ const match = exports.ACCOUNT_ID_REGEX.exec(accountId);
35
+ if (!(match === null || match === void 0 ? void 0 : match.groups)) {
36
+ throw new Error('Invalid account ID.');
37
+ }
38
+ return {
39
+ address: match.groups.accountAddress,
40
+ chainId: match.groups.chainId,
41
+ chain: {
42
+ namespace: match.groups.namespace,
43
+ reference: match.groups.reference,
44
+ },
45
+ };
46
+ }
47
+ exports.parseAccountId = parseAccountId;
48
+ /**
49
+ * A helper struct for a string with a minimum length of 1 and a maximum length
50
+ * of 40.
51
+ */
52
+ exports.LimitedString = (0, superstruct_1.size)((0, superstruct_1.string)(), 1, 40);
53
+ /**
54
+ * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
55
+ */
56
+ exports.ChainIdStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), exports.CHAIN_ID_REGEX);
57
+ exports.AccountIdStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), exports.ACCOUNT_ID_REGEX);
58
+ exports.AccountIdArrayStruct = (0, superstruct_1.array)(exports.AccountIdStruct);
59
+ /**
60
+ * A chain descriptor.
61
+ */
62
+ exports.ChainStruct = (0, superstruct_1.object)({
63
+ id: exports.ChainIdStruct,
64
+ name: exports.LimitedString,
65
+ });
66
+ exports.NamespaceStruct = (0, superstruct_1.object)({
67
+ /**
68
+ * A list of supported chains in the namespace.
69
+ */
70
+ chains: (0, superstruct_1.array)(exports.ChainStruct),
71
+ /**
72
+ * A list of supported RPC methods on the namespace, that a DApp can call.
73
+ */
74
+ methods: (0, superstruct_1.optional)((0, superstruct_1.array)(exports.LimitedString)),
75
+ /**
76
+ * A list of supported RPC events on the namespace, that a DApp can listen to.
77
+ */
78
+ events: (0, superstruct_1.optional)((0, superstruct_1.array)(exports.LimitedString)),
79
+ });
80
+ exports.RequestNamespaceStruct = (0, superstruct_1.assign)((0, superstruct_1.omit)(exports.NamespaceStruct, ['chains']), (0, superstruct_1.object)({ chains: (0, superstruct_1.array)(exports.ChainIdStruct) }));
81
+ exports.SessionNamespaceStruct = (0, superstruct_1.assign)(exports.RequestNamespaceStruct, (0, superstruct_1.object)({ accounts: (0, superstruct_1.array)(exports.AccountIdStruct) }));
82
+ /**
83
+ * A CAIP-2 namespace, i.e., the first part of a chain ID.
84
+ */
85
+ exports.NamespaceIdStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^[-a-z0-9]{3,8}$/u);
86
+ /**
87
+ * An object mapping CAIP-2 namespaces to their values.
88
+ */
89
+ exports.NamespacesStruct = (0, superstruct_1.record)(exports.NamespaceIdStruct, exports.NamespaceStruct);
90
+ exports.SessionStruct = (0, superstruct_1.object)({
91
+ namespaces: (0, superstruct_1.record)(exports.NamespaceIdStruct, exports.SessionNamespaceStruct),
92
+ });
93
+ /**
94
+ * Asserts that the given value is a valid {@link Session}.
95
+ *
96
+ * @param value - The value to assert.
97
+ * @throws If the value is not a valid {@link Session}.
98
+ */
99
+ function assertIsSession(value) {
100
+ (0, utils_1.assertStruct)(value, exports.SessionStruct, 'Invalid session');
101
+ }
102
+ exports.assertIsSession = assertIsSession;
103
+ exports.ConnectArgumentsStruct = (0, superstruct_1.object)({
104
+ requiredNamespaces: (0, superstruct_1.record)(exports.NamespaceIdStruct, exports.RequestNamespaceStruct),
105
+ });
106
+ exports.RequestArgumentsStruct = (0, superstruct_1.assign)((0, superstruct_1.partial)((0, superstruct_1.pick)(utils_1.JsonRpcRequestStruct, ['id', 'jsonrpc'])), (0, superstruct_1.omit)(utils_1.JsonRpcRequestStruct, ['id', 'jsonrpc']));
107
+ exports.MultiChainRequestStruct = (0, superstruct_1.object)({
108
+ chainId: exports.ChainIdStruct,
109
+ request: exports.RequestArgumentsStruct,
110
+ });
111
+ /**
112
+ * Check if the given value is a CAIP-2 namespace ID.
113
+ *
114
+ * @param value - The value to check.
115
+ * @returns Whether the value is a CAIP-2 namespace ID.
116
+ */
117
+ function isNamespaceId(value) {
118
+ return (0, superstruct_1.is)(value, exports.NamespaceIdStruct);
119
+ }
120
+ exports.isNamespaceId = isNamespaceId;
121
+ /**
122
+ * Check if the given value is a CAIP-2 chain ID.
123
+ *
124
+ * @param value - The value to check.
125
+ * @returns Whether the value is a CAIP-2 chain ID.
126
+ */
127
+ function isChainId(value) {
128
+ return (0, superstruct_1.is)(value, exports.ChainIdStruct);
129
+ }
130
+ exports.isChainId = isChainId;
131
+ /**
132
+ * Check if the given value is a CAIP-10 account ID.
133
+ *
134
+ * @param value - The value to check.
135
+ * @returns Whether the value is a CAIP-10 account ID.
136
+ */
137
+ function isAccountId(value) {
138
+ return (0, superstruct_1.is)(value, exports.AccountIdStruct);
139
+ }
140
+ exports.isAccountId = isAccountId;
141
+ /**
142
+ * Check if the given value is an array of CAIP-10 account IDs.
143
+ *
144
+ * @param value - The value to check.
145
+ * @returns Whether the value is an array of CAIP-10 account IDs.
146
+ */
147
+ function isAccountIdArray(value) {
148
+ return (0, superstruct_1.is)(value, exports.AccountIdArrayStruct);
149
+ }
150
+ exports.isAccountIdArray = isAccountIdArray;
151
+ /**
152
+ * Check if the given value is a {@link ConnectArguments} object.
153
+ *
154
+ * @param value - The value to check.
155
+ * @returns Whether the value is a valid {@link ConnectArguments} object.
156
+ */
157
+ function isConnectArguments(value) {
158
+ return (0, superstruct_1.is)(value, exports.ConnectArgumentsStruct);
159
+ }
160
+ exports.isConnectArguments = isConnectArguments;
161
+ /**
162
+ * Assert that the given value is a {@link ConnectArguments} object.
163
+ *
164
+ * @param value - The value to check.
165
+ * @throws If the value is not a valid {@link ConnectArguments} object.
166
+ */
167
+ function assertIsConnectArguments(value) {
168
+ (0, utils_1.assertStruct)(value, exports.ConnectArgumentsStruct, 'Invalid connect arguments');
169
+ }
170
+ exports.assertIsConnectArguments = assertIsConnectArguments;
171
+ /**
172
+ * Check if the given value is a {@link MultiChainRequest} object.
173
+ *
174
+ * @param value - The value to check.
175
+ * @returns Whether the value is a valid {@link MultiChainRequest} object.
176
+ */
177
+ function isMultiChainRequest(value) {
178
+ return (0, superstruct_1.is)(value, exports.MultiChainRequestStruct);
179
+ }
180
+ exports.isMultiChainRequest = isMultiChainRequest;
181
+ /**
182
+ * Assert that the given value is a {@link MultiChainRequest} object.
183
+ *
184
+ * @param value - The value to check.
185
+ * @throws If the value is not a valid {@link MultiChainRequest} object.
186
+ */
187
+ function assertIsMultiChainRequest(value) {
188
+ (0, utils_1.assertStruct)(value, exports.MultiChainRequestStruct, 'Invalid request arguments');
189
+ }
190
+ exports.assertIsMultiChainRequest = assertIsMultiChainRequest;
191
+ /**
192
+ * Check if a value is a {@link Namespace}.
193
+ *
194
+ * @param value - The value to validate.
195
+ * @returns True if the value is a valid {@link Namespace}.
196
+ */
197
+ function isNamespace(value) {
198
+ return (0, superstruct_1.is)(value, exports.NamespaceStruct);
199
+ }
200
+ exports.isNamespace = isNamespace;
201
+ /**
202
+ * Check if a value is an object containing {@link Namespace}s.
203
+ *
204
+ * @param value - The value to validate.
205
+ * @returns True if the value is a valid object containing namespaces.
206
+ */
207
+ function isNamespacesObject(value) {
208
+ return (0, superstruct_1.is)(value, exports.NamespacesStruct);
209
+ }
210
+ exports.isNamespacesObject = isNamespacesObject;
211
+ /**
212
+ * Assert that the given value is a {@link Namespaces} object.
213
+ *
214
+ * @param value - The value to check.
215
+ * @param ErrorWrapper - The error wrapper to use. Defaults to
216
+ * {@link AssertionError}.
217
+ * @throws If the value is not a valid {@link Namespaces} object.
218
+ */
219
+ function assertIsNamespacesObject(value, ErrorWrapper) {
220
+ (0, utils_1.assertStruct)(value, exports.NamespacesStruct, 'Invalid namespaces object', ErrorWrapper);
221
+ }
222
+ exports.assertIsNamespacesObject = assertIsNamespacesObject;
223
+ //# sourceMappingURL=namespace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"namespace.js","sourceRoot":"","sources":["../src/namespace.ts"],"names":[],"mappings":";;;AAAA,6CAcqB;AACrB,2CAIyB;AAEZ,QAAA,cAAc,GACzB,kEAAkE,CAAC;AAExD,QAAA,gBAAgB,GAC3B,mHAAmH,CAAC;AAEtH;;;;;;GAMG;AACH,SAAgB,YAAY,CAAC,OAAgB;IAI3C,MAAM,KAAK,GAAG,sBAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3C,IAAI,CAAC,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,MAAM,CAAA,EAAE;QAClB,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;KACtC;IAED,OAAO;QACL,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS;QACjC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS;KAClC,CAAC;AACJ,CAAC;AAbD,oCAaC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,SAAoB;IAKjD,MAAM,KAAK,GAAG,wBAAgB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC/C,IAAI,CAAC,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,MAAM,CAAA,EAAE;QAClB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;KACxC;IAED,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,cAAc;QACpC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,OAAkB;QACxC,KAAK,EAAE;YACL,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS;YACjC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS;SAClC;KACF,CAAC;AACJ,CAAC;AAlBD,wCAkBC;AAED;;;GAGG;AACU,QAAA,aAAa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n string,\n omit,\n assign,\n partial,\n pick,\n} from 'superstruct';\nimport {\n JsonRpcRequestStruct,\n AssertionErrorConstructor,\n assertStruct,\n} from '@metamask/utils';\n\nexport const CHAIN_ID_REGEX =\n /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-a-zA-Z0-9]{1,32})$/u;\n\nexport const ACCOUNT_ID_REGEX =\n /^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-a-zA-Z0-9]{1,32})):(?<accountAddress>[a-zA-Z0-9]{1,64})$/u;\n\n/**\n * Parse a chain ID string to an object containing the namespace and reference.\n * This validates the chain ID before parsing it.\n *\n * @param chainId - The chain ID to validate and parse.\n * @returns The parsed chain ID.\n */\nexport function parseChainId(chainId: ChainId): {\n namespace: NamespaceId;\n reference: string;\n} {\n const match = CHAIN_ID_REGEX.exec(chainId);\n if (!match?.groups) {\n throw new Error('Invalid chain ID.');\n }\n\n return {\n namespace: match.groups.namespace,\n reference: match.groups.reference,\n };\n}\n\n/**\n * Parse an account ID to an object containing the chain, chain ID and address.\n * This validates the account ID before parsing it.\n *\n * @param accountId - The account ID to validate and parse.\n * @returns The parsed account ID.\n */\nexport function parseAccountId(accountId: AccountId): {\n chain: { namespace: NamespaceId; reference: string };\n chainId: ChainId;\n address: string;\n} {\n const match = ACCOUNT_ID_REGEX.exec(accountId);\n if (!match?.groups) {\n throw new Error('Invalid account ID.');\n }\n\n return {\n address: match.groups.accountAddress,\n chainId: match.groups.chainId as ChainId,\n chain: {\n namespace: match.groups.namespace,\n reference: match.groups.reference,\n },\n };\n}\n\n/**\n * A helper struct for a string with a minimum length of 1 and a maximum length\n * of 40.\n */\nexport const LimitedString = size(string(), 1, 40);\n\n/**\n * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.\n */\nexport const ChainIdStruct = pattern(string(), CHAIN_ID_REGEX);\nexport type ChainId = `${string}:${string}`;\n\nexport const AccountIdStruct = pattern(string(), ACCOUNT_ID_REGEX);\nexport type AccountId = `${ChainId}:${string}`;\n\nexport const AccountIdArrayStruct = array(AccountIdStruct);\n\n/**\n * A chain descriptor.\n */\nexport const ChainStruct = object({\n id: ChainIdStruct,\n name: LimitedString,\n});\nexport type Chain = Infer<typeof ChainStruct>;\n\nexport const NamespaceStruct = object({\n /**\n * A list of supported chains in the namespace.\n */\n chains: array(ChainStruct),\n\n /**\n * A list of supported RPC methods on the namespace, that a DApp can call.\n */\n methods: optional(array(LimitedString)),\n\n /**\n * A list of supported RPC events on the namespace, that a DApp can listen to.\n */\n events: optional(array(LimitedString)),\n});\nexport type Namespace = Infer<typeof NamespaceStruct>;\n\nexport const RequestNamespaceStruct = assign(\n omit(NamespaceStruct, ['chains']),\n object({ chains: array(ChainIdStruct) }),\n);\nexport type RequestNamespace = Infer<typeof RequestNamespaceStruct>;\n\nexport const SessionNamespaceStruct = assign(\n RequestNamespaceStruct,\n object({ accounts: array(AccountIdStruct) }),\n);\nexport type SessionNamespace = Infer<typeof SessionNamespaceStruct>;\n\n/**\n * A CAIP-2 namespace, i.e., the first part of a chain ID.\n */\nexport const NamespaceIdStruct = pattern(string(), /^[-a-z0-9]{3,8}$/u);\nexport type NamespaceId = Infer<typeof NamespaceIdStruct>;\n\n/**\n * An object mapping CAIP-2 namespaces to their values.\n */\nexport const NamespacesStruct = record(NamespaceIdStruct, NamespaceStruct);\nexport type Namespaces = Infer<typeof NamespacesStruct>;\n\nexport const SessionStruct = object({\n namespaces: record(NamespaceIdStruct, SessionNamespaceStruct),\n});\nexport type Session = Infer<typeof SessionStruct>;\n\n/**\n * Asserts that the given value is a valid {@link Session}.\n *\n * @param value - The value to assert.\n * @throws If the value is not a valid {@link Session}.\n */\nexport function assertIsSession(value: unknown): asserts value is Session {\n assertStruct(value, SessionStruct, 'Invalid session');\n}\n\nexport const ConnectArgumentsStruct = object({\n requiredNamespaces: record(NamespaceIdStruct, RequestNamespaceStruct),\n});\nexport type ConnectArguments = Infer<typeof ConnectArgumentsStruct>;\n\nexport const RequestArgumentsStruct = assign(\n partial(pick(JsonRpcRequestStruct, ['id', 'jsonrpc'])),\n omit(JsonRpcRequestStruct, ['id', 'jsonrpc']),\n);\nexport type RequestArguments = Infer<typeof RequestArgumentsStruct>;\n\nexport const MultiChainRequestStruct = object({\n chainId: ChainIdStruct,\n request: RequestArgumentsStruct,\n});\nexport type MultiChainRequest = Infer<typeof MultiChainRequestStruct>;\n\n/**\n * Check if the given value is a CAIP-2 namespace ID.\n *\n * @param value - The value to check.\n * @returns Whether the value is a CAIP-2 namespace ID.\n */\nexport function isNamespaceId(value: unknown): value is NamespaceId {\n return is(value, NamespaceIdStruct);\n}\n\n/**\n * Check if the given value is a CAIP-2 chain ID.\n *\n * @param value - The value to check.\n * @returns Whether the value is a CAIP-2 chain ID.\n */\nexport function isChainId(value: unknown): value is ChainId {\n return is(value, ChainIdStruct);\n}\n\n/**\n * Check if the given value is a CAIP-10 account ID.\n *\n * @param value - The value to check.\n * @returns Whether the value is a CAIP-10 account ID.\n */\nexport function isAccountId(value: unknown): value is AccountId {\n return is(value, AccountIdStruct);\n}\n\n/**\n * Check if the given value is an array of CAIP-10 account IDs.\n *\n * @param value - The value to check.\n * @returns Whether the value is an array of CAIP-10 account IDs.\n */\nexport function isAccountIdArray(value: unknown): value is AccountId[] {\n return is(value, AccountIdArrayStruct);\n}\n\n/**\n * Check if the given value is a {@link ConnectArguments} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link ConnectArguments} object.\n */\nexport function isConnectArguments(value: unknown): value is ConnectArguments {\n return is(value, ConnectArgumentsStruct);\n}\n\n/**\n * Assert that the given value is a {@link ConnectArguments} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link ConnectArguments} object.\n */\nexport function assertIsConnectArguments(\n value: unknown,\n): asserts value is ConnectArguments {\n assertStruct(value, ConnectArgumentsStruct, 'Invalid connect arguments');\n}\n\n/**\n * Check if the given value is a {@link MultiChainRequest} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link MultiChainRequest} object.\n */\nexport function isMultiChainRequest(\n value: unknown,\n): value is MultiChainRequest {\n return is(value, MultiChainRequestStruct);\n}\n\n/**\n * Assert that the given value is a {@link MultiChainRequest} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link MultiChainRequest} object.\n */\nexport function assertIsMultiChainRequest(\n value: unknown,\n): asserts value is MultiChainRequest {\n assertStruct(value, MultiChainRequestStruct, 'Invalid request arguments');\n}\n\n/**\n * Check if a value is a {@link Namespace}.\n *\n * @param value - The value to validate.\n * @returns True if the value is a valid {@link Namespace}.\n */\nexport function isNamespace(value: unknown): value is Namespace {\n return is(value, NamespaceStruct);\n}\n\n/**\n * Check if a value is an object containing {@link Namespace}s.\n *\n * @param value - The value to validate.\n * @returns True if the value is a valid object containing namespaces.\n */\nexport function isNamespacesObject(value: unknown): value is Namespaces {\n return is(value, NamespacesStruct);\n}\n\n/**\n * Assert that the given value is a {@link Namespaces} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error wrapper to use. Defaults to\n * {@link AssertionError}.\n * @throws If the value is not a valid {@link Namespaces} object.\n */\nexport function assertIsNamespacesObject(\n value: unknown,\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is Namespaces {\n assertStruct(\n value,\n NamespacesStruct,\n 'Invalid namespaces object',\n ErrorWrapper,\n );\n}\n"]}