@metamask/snaps-utils 8.10.0 → 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.
Files changed (40) hide show
  1. package/CHANGELOG.md +33 -1
  2. package/dist/index.cjs +0 -1
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +0 -1
  5. package/dist/index.d.cts.map +1 -1
  6. package/dist/index.d.mts +0 -1
  7. package/dist/index.d.mts.map +1 -1
  8. package/dist/index.executionenv.cjs +0 -1
  9. package/dist/index.executionenv.cjs.map +1 -1
  10. package/dist/index.executionenv.d.cts +0 -1
  11. package/dist/index.executionenv.d.cts.map +1 -1
  12. package/dist/index.executionenv.d.mts +0 -1
  13. package/dist/index.executionenv.d.mts.map +1 -1
  14. package/dist/index.executionenv.mjs +0 -1
  15. package/dist/index.executionenv.mjs.map +1 -1
  16. package/dist/index.mjs +0 -1
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/manifest/validation.cjs +1 -2
  19. package/dist/manifest/validation.cjs.map +1 -1
  20. package/dist/manifest/validation.d.cts.map +1 -1
  21. package/dist/manifest/validation.d.mts.map +1 -1
  22. package/dist/manifest/validation.mjs +1 -2
  23. package/dist/manifest/validation.mjs.map +1 -1
  24. package/dist/snaps.cjs +1 -12
  25. package/dist/snaps.cjs.map +1 -1
  26. package/dist/snaps.d.cts +0 -7
  27. package/dist/snaps.d.cts.map +1 -1
  28. package/dist/snaps.d.mts +0 -7
  29. package/dist/snaps.d.mts.map +1 -1
  30. package/dist/snaps.mjs +0 -10
  31. package/dist/snaps.mjs.map +1 -1
  32. package/package.json +5 -5
  33. package/dist/namespace.cjs +0 -136
  34. package/dist/namespace.cjs.map +0 -1
  35. package/dist/namespace.d.cts +0 -130
  36. package/dist/namespace.d.cts.map +0 -1
  37. package/dist/namespace.d.mts +0 -130
  38. package/dist/namespace.d.mts.map +0 -1
  39. package/dist/namespace.mjs +0 -126
  40. package/dist/namespace.mjs.map +0 -1
@@ -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
@@ -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;KAClC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,SAAoB;IAKjD,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC/C,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IACzC,CAAC;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;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C,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"]}