@metamask/snaps-utils 8.9.1 → 9.0.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.
- package/CHANGELOG.md +41 -1
- package/dist/caveats.cjs +4 -0
- package/dist/caveats.cjs.map +1 -1
- package/dist/caveats.d.cts +5 -1
- package/dist/caveats.d.cts.map +1 -1
- package/dist/caveats.d.mts +5 -1
- package/dist/caveats.d.mts.map +1 -1
- package/dist/caveats.mjs +4 -0
- package/dist/caveats.mjs.map +1 -1
- package/dist/default-endowments.cjs +1 -0
- package/dist/default-endowments.cjs.map +1 -1
- package/dist/default-endowments.d.cts.map +1 -1
- package/dist/default-endowments.d.mts.map +1 -1
- package/dist/default-endowments.mjs +1 -0
- package/dist/default-endowments.mjs.map +1 -1
- package/dist/handler-types.cjs +1 -0
- package/dist/handler-types.cjs.map +1 -1
- package/dist/handler-types.d.cts +2 -1
- package/dist/handler-types.d.cts.map +1 -1
- package/dist/handler-types.d.mts +2 -1
- package/dist/handler-types.d.mts.map +1 -1
- package/dist/handler-types.mjs +1 -0
- package/dist/handler-types.mjs.map +1 -1
- package/dist/handlers.cjs +7 -0
- package/dist/handlers.cjs.map +1 -1
- package/dist/handlers.d.cts +24 -3
- package/dist/handlers.d.cts.map +1 -1
- package/dist/handlers.d.mts +24 -3
- package/dist/handlers.d.mts.map +1 -1
- package/dist/handlers.mjs +7 -0
- package/dist/handlers.mjs.map +1 -1
- package/dist/index.cjs +0 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +0 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.executionenv.cjs +0 -1
- package/dist/index.executionenv.cjs.map +1 -1
- package/dist/index.executionenv.d.cts +0 -1
- package/dist/index.executionenv.d.cts.map +1 -1
- package/dist/index.executionenv.d.mts +0 -1
- package/dist/index.executionenv.d.mts.map +1 -1
- package/dist/index.executionenv.mjs +0 -1
- package/dist/index.executionenv.mjs.map +1 -1
- package/dist/index.mjs +0 -1
- package/dist/index.mjs.map +1 -1
- package/dist/manifest/validation.cjs +4 -3
- package/dist/manifest/validation.cjs.map +1 -1
- package/dist/manifest/validation.d.cts +3 -0
- package/dist/manifest/validation.d.cts.map +1 -1
- package/dist/manifest/validation.d.mts +3 -0
- package/dist/manifest/validation.d.mts.map +1 -1
- package/dist/manifest/validation.mjs +4 -3
- package/dist/manifest/validation.mjs.map +1 -1
- package/dist/snaps.cjs +1 -12
- package/dist/snaps.cjs.map +1 -1
- package/dist/snaps.d.cts +0 -7
- package/dist/snaps.d.cts.map +1 -1
- package/dist/snaps.d.mts +0 -7
- package/dist/snaps.d.mts.map +1 -1
- package/dist/snaps.mjs +0 -10
- package/dist/snaps.mjs.map +1 -1
- package/package.json +5 -5
- package/dist/namespace.cjs +0 -136
- package/dist/namespace.cjs.map +0 -1
- package/dist/namespace.d.cts +0 -130
- package/dist/namespace.d.cts.map +0 -1
- package/dist/namespace.d.mts +0 -130
- package/dist/namespace.d.mts.map +0 -1
- package/dist/namespace.mjs +0 -126
- package/dist/namespace.mjs.map +0 -1
package/dist/namespace.d.mts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import type { AccountId, ChainId } from "@metamask/snaps-sdk";
|
|
2
|
-
import type { Infer } from "@metamask/superstruct";
|
|
3
|
-
import type { InferMatching } from "./structs.mjs";
|
|
4
|
-
export declare const CHAIN_ID_REGEX: RegExp;
|
|
5
|
-
export declare const ACCOUNT_ID_REGEX: RegExp;
|
|
6
|
-
export declare const ACCOUNT_ADDRESS_REGEX: RegExp;
|
|
7
|
-
/**
|
|
8
|
-
* Parse a chain ID string to an object containing the namespace and reference.
|
|
9
|
-
* This validates the chain ID before parsing it.
|
|
10
|
-
*
|
|
11
|
-
* @param chainId - The chain ID to validate and parse.
|
|
12
|
-
* @returns The parsed chain ID.
|
|
13
|
-
*/
|
|
14
|
-
export declare function parseChainId(chainId: ChainId): {
|
|
15
|
-
namespace: NamespaceId;
|
|
16
|
-
reference: string;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Parse an account ID to an object containing the chain, chain ID and address.
|
|
20
|
-
* This validates the account ID before parsing it.
|
|
21
|
-
*
|
|
22
|
-
* @param accountId - The account ID to validate and parse.
|
|
23
|
-
* @returns The parsed account ID.
|
|
24
|
-
*/
|
|
25
|
-
export declare function parseAccountId(accountId: AccountId): {
|
|
26
|
-
chain: {
|
|
27
|
-
namespace: NamespaceId;
|
|
28
|
-
reference: string;
|
|
29
|
-
};
|
|
30
|
-
chainId: ChainId;
|
|
31
|
-
address: string;
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* A helper struct for a string with a minimum length of 1 and a maximum length
|
|
35
|
-
* of 40.
|
|
36
|
-
*/
|
|
37
|
-
export declare const LimitedString: import("@metamask/superstruct").Struct<string, null>;
|
|
38
|
-
export declare const ChainIdStringStruct: import("@metamask/superstruct").Struct<`${string}:${string}`, null>;
|
|
39
|
-
/**
|
|
40
|
-
* A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
|
|
41
|
-
*/
|
|
42
|
-
export declare const ChainIdStruct: import("@metamask/superstruct").Struct<`${string}:${string}`, null>;
|
|
43
|
-
export type Caip2ChainId = InferMatching<typeof ChainIdStruct, ChainId>;
|
|
44
|
-
export declare const AccountIdStruct: import("@metamask/superstruct").Struct<string, null>;
|
|
45
|
-
export declare const AccountIdArrayStruct: import("@metamask/superstruct").Struct<string[], import("@metamask/superstruct").Struct<string, null>>;
|
|
46
|
-
export declare const AccountAddressStruct: import("@metamask/superstruct").Struct<string, null>;
|
|
47
|
-
export type AccountAddress = Infer<typeof AccountAddressStruct>;
|
|
48
|
-
/**
|
|
49
|
-
* A chain descriptor.
|
|
50
|
-
*/
|
|
51
|
-
export declare const ChainStruct: import("@metamask/superstruct").Struct<{
|
|
52
|
-
id: `${string}:${string}`;
|
|
53
|
-
name: string;
|
|
54
|
-
}, {
|
|
55
|
-
id: import("@metamask/superstruct").Struct<`${string}:${string}`, null>;
|
|
56
|
-
name: import("@metamask/superstruct").Struct<string, null>;
|
|
57
|
-
}>;
|
|
58
|
-
export type Chain = Infer<typeof ChainStruct>;
|
|
59
|
-
export declare const NamespaceStruct: import("@metamask/superstruct").Struct<{
|
|
60
|
-
chains: {
|
|
61
|
-
id: `${string}:${string}`;
|
|
62
|
-
name: string;
|
|
63
|
-
}[];
|
|
64
|
-
methods?: string[] | undefined;
|
|
65
|
-
events?: string[] | undefined;
|
|
66
|
-
}, {
|
|
67
|
-
/**
|
|
68
|
-
* A list of supported chains in the namespace.
|
|
69
|
-
*/
|
|
70
|
-
chains: import("@metamask/superstruct").Struct<{
|
|
71
|
-
id: `${string}:${string}`;
|
|
72
|
-
name: string;
|
|
73
|
-
}[], import("@metamask/superstruct").Struct<{
|
|
74
|
-
id: `${string}:${string}`;
|
|
75
|
-
name: string;
|
|
76
|
-
}, {
|
|
77
|
-
id: import("@metamask/superstruct").Struct<`${string}:${string}`, null>;
|
|
78
|
-
name: import("@metamask/superstruct").Struct<string, null>;
|
|
79
|
-
}>>;
|
|
80
|
-
/**
|
|
81
|
-
* A list of supported RPC methods on the namespace, that a DApp can call.
|
|
82
|
-
*/
|
|
83
|
-
methods: import("@metamask/superstruct").Struct<string[] | undefined, import("@metamask/superstruct").Struct<string, null>>;
|
|
84
|
-
/**
|
|
85
|
-
* A list of supported RPC events on the namespace, that a DApp can listen to.
|
|
86
|
-
*/
|
|
87
|
-
events: import("@metamask/superstruct").Struct<string[] | undefined, import("@metamask/superstruct").Struct<string, null>>;
|
|
88
|
-
}>;
|
|
89
|
-
export type Namespace = Infer<typeof NamespaceStruct>;
|
|
90
|
-
/**
|
|
91
|
-
* A CAIP-2 namespace, i.e., the first part of a chain ID.
|
|
92
|
-
*/
|
|
93
|
-
export declare const NamespaceIdStruct: import("@metamask/superstruct").Struct<string, null>;
|
|
94
|
-
export type NamespaceId = Infer<typeof NamespaceIdStruct>;
|
|
95
|
-
/**
|
|
96
|
-
* Check if the given value is a CAIP-2 namespace ID.
|
|
97
|
-
*
|
|
98
|
-
* @param value - The value to check.
|
|
99
|
-
* @returns Whether the value is a CAIP-2 namespace ID.
|
|
100
|
-
*/
|
|
101
|
-
export declare function isNamespaceId(value: unknown): value is NamespaceId;
|
|
102
|
-
/**
|
|
103
|
-
* Check if the given value is a CAIP-2 chain ID.
|
|
104
|
-
*
|
|
105
|
-
* @param value - The value to check.
|
|
106
|
-
* @returns Whether the value is a CAIP-2 chain ID.
|
|
107
|
-
*/
|
|
108
|
-
export declare function isChainId(value: unknown): value is ChainId;
|
|
109
|
-
/**
|
|
110
|
-
* Check if the given value is a CAIP-10 account ID.
|
|
111
|
-
*
|
|
112
|
-
* @param value - The value to check.
|
|
113
|
-
* @returns Whether the value is a CAIP-10 account ID.
|
|
114
|
-
*/
|
|
115
|
-
export declare function isAccountId(value: unknown): value is AccountId;
|
|
116
|
-
/**
|
|
117
|
-
* Check if the given value is an array of CAIP-10 account IDs.
|
|
118
|
-
*
|
|
119
|
-
* @param value - The value to check.
|
|
120
|
-
* @returns Whether the value is an array of CAIP-10 account IDs.
|
|
121
|
-
*/
|
|
122
|
-
export declare function isAccountIdArray(value: unknown): value is AccountId[];
|
|
123
|
-
/**
|
|
124
|
-
* Check if a value is a {@link Namespace}.
|
|
125
|
-
*
|
|
126
|
-
* @param value - The value to validate.
|
|
127
|
-
* @returns True if the value is a valid {@link Namespace}.
|
|
128
|
-
*/
|
|
129
|
-
export declare function isNamespace(value: unknown): value is Namespace;
|
|
130
|
-
//# sourceMappingURL=namespace.d.mts.map
|
package/dist/namespace.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"namespace.d.mts","sourceRoot":"","sources":["../src/namespace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,4BAA4B;AAC9D,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAYnD,OA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;GAEG;AACH,eAAO,MAAM,iBAAiB,sDAAyC,CAAC;AACxE,MAAM,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAE1D;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAElE;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAE1D;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,EAAE,CAErE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D"}
|
package/dist/namespace.mjs
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { array, define, is, object, optional, pattern, size, string } from "@metamask/superstruct";
|
|
2
|
-
export const CHAIN_ID_REGEX = /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-a-zA-Z0-9]{1,32})$/u;
|
|
3
|
-
export const 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;
|
|
4
|
-
export const ACCOUNT_ADDRESS_REGEX = /^(?<accountAddress>[a-zA-Z0-9]{1,64})$/u;
|
|
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 function parseChainId(chainId) {
|
|
13
|
-
const match = CHAIN_ID_REGEX.exec(chainId);
|
|
14
|
-
if (!match?.groups) {
|
|
15
|
-
throw new Error('Invalid chain ID.');
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
namespace: match.groups.namespace,
|
|
19
|
-
reference: match.groups.reference,
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Parse an account ID to an object containing the chain, chain ID and address.
|
|
24
|
-
* This validates the account ID before parsing it.
|
|
25
|
-
*
|
|
26
|
-
* @param accountId - The account ID to validate and parse.
|
|
27
|
-
* @returns The parsed account ID.
|
|
28
|
-
*/
|
|
29
|
-
export function parseAccountId(accountId) {
|
|
30
|
-
const match = ACCOUNT_ID_REGEX.exec(accountId);
|
|
31
|
-
if (!match?.groups) {
|
|
32
|
-
throw new Error('Invalid account ID.');
|
|
33
|
-
}
|
|
34
|
-
return {
|
|
35
|
-
address: match.groups.accountAddress,
|
|
36
|
-
chainId: match.groups.chainId,
|
|
37
|
-
chain: {
|
|
38
|
-
namespace: match.groups.namespace,
|
|
39
|
-
reference: match.groups.reference,
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* A helper struct for a string with a minimum length of 1 and a maximum length
|
|
45
|
-
* of 40.
|
|
46
|
-
*/
|
|
47
|
-
export const LimitedString = size(string(), 1, 40);
|
|
48
|
-
export const ChainIdStringStruct = define('Chain ID', string().validator);
|
|
49
|
-
/**
|
|
50
|
-
* A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
|
|
51
|
-
*/
|
|
52
|
-
export const ChainIdStruct = pattern(ChainIdStringStruct, CHAIN_ID_REGEX);
|
|
53
|
-
export const AccountIdStruct = pattern(string(), ACCOUNT_ID_REGEX);
|
|
54
|
-
export const AccountIdArrayStruct = array(AccountIdStruct);
|
|
55
|
-
export const AccountAddressStruct = pattern(string(), ACCOUNT_ADDRESS_REGEX);
|
|
56
|
-
/**
|
|
57
|
-
* A chain descriptor.
|
|
58
|
-
*/
|
|
59
|
-
export const ChainStruct = object({
|
|
60
|
-
id: ChainIdStruct,
|
|
61
|
-
name: LimitedString,
|
|
62
|
-
});
|
|
63
|
-
export const NamespaceStruct = object({
|
|
64
|
-
/**
|
|
65
|
-
* A list of supported chains in the namespace.
|
|
66
|
-
*/
|
|
67
|
-
chains: array(ChainStruct),
|
|
68
|
-
/**
|
|
69
|
-
* A list of supported RPC methods on the namespace, that a DApp can call.
|
|
70
|
-
*/
|
|
71
|
-
methods: optional(array(LimitedString)),
|
|
72
|
-
/**
|
|
73
|
-
* A list of supported RPC events on the namespace, that a DApp can listen to.
|
|
74
|
-
*/
|
|
75
|
-
events: optional(array(LimitedString)),
|
|
76
|
-
});
|
|
77
|
-
/**
|
|
78
|
-
* A CAIP-2 namespace, i.e., the first part of a chain ID.
|
|
79
|
-
*/
|
|
80
|
-
export const NamespaceIdStruct = pattern(string(), /^[-a-z0-9]{3,8}$/u);
|
|
81
|
-
/**
|
|
82
|
-
* Check if the given value is a CAIP-2 namespace ID.
|
|
83
|
-
*
|
|
84
|
-
* @param value - The value to check.
|
|
85
|
-
* @returns Whether the value is a CAIP-2 namespace ID.
|
|
86
|
-
*/
|
|
87
|
-
export function isNamespaceId(value) {
|
|
88
|
-
return is(value, NamespaceIdStruct);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Check if the given value is a CAIP-2 chain ID.
|
|
92
|
-
*
|
|
93
|
-
* @param value - The value to check.
|
|
94
|
-
* @returns Whether the value is a CAIP-2 chain ID.
|
|
95
|
-
*/
|
|
96
|
-
export function isChainId(value) {
|
|
97
|
-
return is(value, ChainIdStruct);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Check if the given value is a CAIP-10 account ID.
|
|
101
|
-
*
|
|
102
|
-
* @param value - The value to check.
|
|
103
|
-
* @returns Whether the value is a CAIP-10 account ID.
|
|
104
|
-
*/
|
|
105
|
-
export function isAccountId(value) {
|
|
106
|
-
return is(value, AccountIdStruct);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Check if the given value is an array of CAIP-10 account IDs.
|
|
110
|
-
*
|
|
111
|
-
* @param value - The value to check.
|
|
112
|
-
* @returns Whether the value is an array of CAIP-10 account IDs.
|
|
113
|
-
*/
|
|
114
|
-
export function isAccountIdArray(value) {
|
|
115
|
-
return is(value, AccountIdArrayStruct);
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Check if a value is a {@link Namespace}.
|
|
119
|
-
*
|
|
120
|
-
* @param value - The value to validate.
|
|
121
|
-
* @returns True if the value is a valid {@link Namespace}.
|
|
122
|
-
*/
|
|
123
|
-
export function isNamespace(value) {
|
|
124
|
-
return is(value, NamespaceStruct);
|
|
125
|
-
}
|
|
126
|
-
//# sourceMappingURL=namespace.mjs.map
|
package/dist/namespace.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"namespace.mjs","sourceRoot":"","sources":["../src/namespace.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,EACL,MAAM,EACN,EAAE,EACF,MAAM,EACN,QAAQ,EACR,OAAO,EACP,IAAI,EACJ,MAAM,EACP,8BAA8B;AAI/B,MAAM,CAAC,MAAM,cAAc,GACzB,kEAAkE,CAAC;AAErE,MAAM,CAAC,MAAM,gBAAgB,GAC3B,mHAAmH,CAAC;AAEtH,MAAM,CAAC,MAAM,qBAAqB,GAAG,yCAAyC,CAAC;AAE/E;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,OAAgB;IAI3C,MAAM,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3C,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;IACvC,CAAC;IAED,OAAO;QACL,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS;QACjC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS;K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;;OAEG;IACH,MAAM,EAAE,KAAK,CAAC,WAAW,CAAC;IAE1B;;OAEG;IACH,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;IAEvC;;OAEG;IACH,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;CACvC,CAAC,CAAC;AAGH;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,mBAAmB,CAAC,CAAC;AAGxE;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,OAAO,EAAE,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC;AACtC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,OAAO,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;AAClC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC;AACpC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,OAAO,EAAE,CAAC,KAAK,EAAE,oBAAoB,CAAC,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC;AACpC,CAAC","sourcesContent":["import type { AccountId, ChainId } from '@metamask/snaps-sdk';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n array,\n define,\n is,\n object,\n optional,\n pattern,\n size,\n string,\n} from '@metamask/superstruct';\n\nimport type { InferMatching } from './structs';\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\nexport const ACCOUNT_ADDRESS_REGEX = /^(?<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\nexport const ChainIdStringStruct = define<ChainId>(\n 'Chain ID',\n string().validator,\n);\n\n/**\n * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.\n */\nexport const ChainIdStruct = pattern<ChainId, null>(\n ChainIdStringStruct,\n CHAIN_ID_REGEX,\n);\n\nexport type Caip2ChainId = InferMatching<typeof ChainIdStruct, ChainId>;\n\nexport const AccountIdStruct = pattern(string(), ACCOUNT_ID_REGEX);\n\nexport const AccountIdArrayStruct = array(AccountIdStruct);\nexport const AccountAddressStruct = pattern(string(), ACCOUNT_ADDRESS_REGEX);\nexport type AccountAddress = Infer<typeof AccountAddressStruct>;\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\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 * 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 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"]}
|