@metamask/snaps-utils 0.35.0-flask.1 → 1.0.0-prerelease.1

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 (252) hide show
  1. package/CHANGELOG.md +5 -41
  2. package/dist/{esm/array.js → array.js} +7 -3
  3. package/dist/array.js.map +1 -0
  4. package/dist/{types/caveats.d.ts → caveats.d.ts} +4 -0
  5. package/dist/caveats.js +35 -0
  6. package/dist/caveats.js.map +1 -0
  7. package/dist/checksum.js +42 -0
  8. package/dist/checksum.js.map +1 -0
  9. package/dist/cronjob.js +71 -0
  10. package/dist/cronjob.js.map +1 -0
  11. package/dist/deep-clone.js +9 -0
  12. package/dist/deep-clone.js.map +1 -0
  13. package/dist/{esm/default-endowments.js → default-endowments.js} +6 -3
  14. package/dist/default-endowments.js.map +1 -0
  15. package/dist/entropy.js +8 -0
  16. package/dist/entropy.js.map +1 -0
  17. package/dist/{cjs/eval-worker.js → eval-worker.js} +21 -22
  18. package/dist/eval-worker.js.map +1 -0
  19. package/dist/eval.js +27 -0
  20. package/dist/eval.js.map +1 -0
  21. package/dist/{esm/fs.js → fs.js} +56 -33
  22. package/dist/fs.js.map +1 -0
  23. package/dist/{types/handlers.d.ts → handlers.d.ts} +42 -5
  24. package/dist/handlers.js +3 -0
  25. package/dist/handlers.js.map +1 -0
  26. package/dist/{types/iframe.d.ts → iframe.d.ts} +2 -3
  27. package/dist/{esm/iframe.js → iframe.js} +18 -16
  28. package/dist/iframe.js.map +1 -0
  29. package/dist/iframe.test.browser.js +15 -0
  30. package/dist/iframe.test.browser.js.map +1 -0
  31. package/dist/{types/index.browser.d.ts → index.browser.d.ts} +1 -2
  32. package/dist/index.browser.js +37 -0
  33. package/dist/index.browser.js.map +1 -0
  34. package/dist/{types/index.d.ts → index.d.ts} +1 -2
  35. package/dist/index.executionenv.js +22 -0
  36. package/dist/index.executionenv.js.map +1 -0
  37. package/dist/index.js +42 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/json-rpc.js +46 -0
  40. package/dist/json-rpc.js.map +1 -0
  41. package/dist/json.d.ts +9 -0
  42. package/dist/json.js +18 -0
  43. package/dist/json.js.map +1 -0
  44. package/dist/{esm/logging.js → logging.js} +14 -6
  45. package/dist/logging.js.map +1 -0
  46. package/dist/manifest/index.browser.js +18 -0
  47. package/dist/manifest/index.browser.js.map +1 -0
  48. package/dist/manifest/index.js +19 -0
  49. package/dist/manifest/index.js.map +1 -0
  50. package/dist/{esm/manifest → manifest}/manifest.js +113 -98
  51. package/dist/manifest/manifest.js.map +1 -0
  52. package/dist/{types/manifest → manifest}/validation.d.ts +73 -31
  53. package/dist/manifest/validation.js +141 -0
  54. package/dist/manifest/validation.js.map +1 -0
  55. package/dist/{esm/mock.js → mock.js} +40 -42
  56. package/dist/mock.js.map +1 -0
  57. package/dist/namespace.d.ts +275 -0
  58. package/dist/namespace.js +225 -0
  59. package/dist/namespace.js.map +1 -0
  60. package/dist/notification.d.ts +66 -0
  61. package/dist/notification.js +58 -0
  62. package/dist/notification.js.map +1 -0
  63. package/dist/{types/npm.d.ts → npm.d.ts} +2 -0
  64. package/dist/npm.js +74 -0
  65. package/dist/npm.js.map +1 -0
  66. package/dist/path.js +21 -0
  67. package/dist/path.js.map +1 -0
  68. package/dist/{esm/post-process.js → post-process.js} +99 -110
  69. package/dist/post-process.js.map +1 -0
  70. package/dist/{types/snaps.d.ts → snaps.d.ts} +2 -9
  71. package/dist/snaps.js +202 -0
  72. package/dist/snaps.js.map +1 -0
  73. package/dist/{types/types.d.ts → types.d.ts} +5 -2
  74. package/dist/types.js +103 -0
  75. package/dist/types.js.map +1 -0
  76. package/dist/{esm/versions.js → versions.js} +18 -15
  77. package/dist/versions.js.map +1 -0
  78. package/dist/{esm/virtual-file → virtual-file}/VirtualFile.js +33 -47
  79. package/dist/virtual-file/VirtualFile.js.map +1 -0
  80. package/dist/virtual-file/index.browser.js +18 -0
  81. package/dist/virtual-file/index.browser.js.map +1 -0
  82. package/dist/virtual-file/index.js +19 -0
  83. package/dist/virtual-file/index.js.map +1 -0
  84. package/dist/virtual-file/toVirtualFile.js +30 -0
  85. package/dist/virtual-file/toVirtualFile.js.map +1 -0
  86. package/package.json +32 -61
  87. package/dist/cjs/array.js +0 -23
  88. package/dist/cjs/array.js.map +0 -1
  89. package/dist/cjs/caveats.js +0 -33
  90. package/dist/cjs/caveats.js.map +0 -1
  91. package/dist/cjs/checksum.js +0 -38
  92. package/dist/cjs/checksum.js.map +0 -1
  93. package/dist/cjs/cronjob.js +0 -84
  94. package/dist/cjs/cronjob.js.map +0 -1
  95. package/dist/cjs/deep-clone.js +0 -22
  96. package/dist/cjs/deep-clone.js.map +0 -1
  97. package/dist/cjs/default-endowments.js +0 -49
  98. package/dist/cjs/default-endowments.js.map +0 -1
  99. package/dist/cjs/entropy.js +0 -23
  100. package/dist/cjs/entropy.js.map +0 -1
  101. package/dist/cjs/enum.js +0 -16
  102. package/dist/cjs/enum.js.map +0 -1
  103. package/dist/cjs/eval-worker.js.map +0 -1
  104. package/dist/cjs/eval.js +0 -29
  105. package/dist/cjs/eval.js.map +0 -1
  106. package/dist/cjs/fs.js +0 -126
  107. package/dist/cjs/fs.js.map +0 -1
  108. package/dist/cjs/handlers.js +0 -6
  109. package/dist/cjs/handlers.js.map +0 -1
  110. package/dist/cjs/icon.js +0 -37
  111. package/dist/cjs/icon.js.map +0 -1
  112. package/dist/cjs/iframe.js +0 -59
  113. package/dist/cjs/iframe.js.map +0 -1
  114. package/dist/cjs/index.browser.js +0 -40
  115. package/dist/cjs/index.browser.js.map +0 -1
  116. package/dist/cjs/index.executionenv.js +0 -24
  117. package/dist/cjs/index.executionenv.js.map +0 -1
  118. package/dist/cjs/index.js +0 -45
  119. package/dist/cjs/index.js.map +0 -1
  120. package/dist/cjs/json-rpc.js +0 -46
  121. package/dist/cjs/json-rpc.js.map +0 -1
  122. package/dist/cjs/json.js +0 -16
  123. package/dist/cjs/json.js.map +0 -1
  124. package/dist/cjs/logging.js +0 -40
  125. package/dist/cjs/logging.js.map +0 -1
  126. package/dist/cjs/manifest/index.browser.js +0 -20
  127. package/dist/cjs/manifest/index.browser.js.map +0 -1
  128. package/dist/cjs/manifest/index.js +0 -21
  129. package/dist/cjs/manifest/index.js.map +0 -1
  130. package/dist/cjs/manifest/manifest.js +0 -239
  131. package/dist/cjs/manifest/manifest.js.map +0 -1
  132. package/dist/cjs/manifest/validation.js +0 -183
  133. package/dist/cjs/manifest/validation.js.map +0 -1
  134. package/dist/cjs/mock.js +0 -128
  135. package/dist/cjs/mock.js.map +0 -1
  136. package/dist/cjs/namespace.js +0 -124
  137. package/dist/cjs/namespace.js.map +0 -1
  138. package/dist/cjs/npm.js +0 -81
  139. package/dist/cjs/npm.js.map +0 -1
  140. package/dist/cjs/path.js +0 -21
  141. package/dist/cjs/path.js.map +0 -1
  142. package/dist/cjs/post-process.js +0 -328
  143. package/dist/cjs/post-process.js.map +0 -1
  144. package/dist/cjs/snaps.js +0 -230
  145. package/dist/cjs/snaps.js.map +0 -1
  146. package/dist/cjs/types.js +0 -117
  147. package/dist/cjs/types.js.map +0 -1
  148. package/dist/cjs/validation.js +0 -22
  149. package/dist/cjs/validation.js.map +0 -1
  150. package/dist/cjs/versions.js +0 -47
  151. package/dist/cjs/versions.js.map +0 -1
  152. package/dist/cjs/virtual-file/VirtualFile.js +0 -85
  153. package/dist/cjs/virtual-file/VirtualFile.js.map +0 -1
  154. package/dist/cjs/virtual-file/index.browser.js +0 -20
  155. package/dist/cjs/virtual-file/index.browser.js.map +0 -1
  156. package/dist/cjs/virtual-file/index.js +0 -21
  157. package/dist/cjs/virtual-file/index.js.map +0 -1
  158. package/dist/cjs/virtual-file/toVirtualFile.js +0 -33
  159. package/dist/cjs/virtual-file/toVirtualFile.js.map +0 -1
  160. package/dist/esm/array.js.map +0 -1
  161. package/dist/esm/caveats.js +0 -23
  162. package/dist/esm/caveats.js.map +0 -1
  163. package/dist/esm/checksum.js +0 -36
  164. package/dist/esm/checksum.js.map +0 -1
  165. package/dist/esm/cronjob.js +0 -66
  166. package/dist/esm/cronjob.js.map +0 -1
  167. package/dist/esm/deep-clone.js +0 -7
  168. package/dist/esm/deep-clone.js.map +0 -1
  169. package/dist/esm/default-endowments.js.map +0 -1
  170. package/dist/esm/entropy.js +0 -6
  171. package/dist/esm/entropy.js.map +0 -1
  172. package/dist/esm/enum.js +0 -12
  173. package/dist/esm/enum.js.map +0 -1
  174. package/dist/esm/eval-worker.js +0 -47
  175. package/dist/esm/eval-worker.js.map +0 -1
  176. package/dist/esm/eval.js +0 -25
  177. package/dist/esm/eval.js.map +0 -1
  178. package/dist/esm/fs.js.map +0 -1
  179. package/dist/esm/handlers.js +0 -3
  180. package/dist/esm/handlers.js.map +0 -1
  181. package/dist/esm/icon.js +0 -11
  182. package/dist/esm/icon.js.map +0 -1
  183. package/dist/esm/iframe.js.map +0 -1
  184. package/dist/esm/index.browser.js +0 -23
  185. package/dist/esm/index.browser.js.map +0 -1
  186. package/dist/esm/index.executionenv.js +0 -7
  187. package/dist/esm/index.executionenv.js.map +0 -1
  188. package/dist/esm/index.js +0 -28
  189. package/dist/esm/index.js.map +0 -1
  190. package/dist/esm/json-rpc.js +0 -39
  191. package/dist/esm/json-rpc.js.map +0 -1
  192. package/dist/esm/json.js +0 -17
  193. package/dist/esm/json.js.map +0 -1
  194. package/dist/esm/logging.js.map +0 -1
  195. package/dist/esm/manifest/index.browser.js +0 -3
  196. package/dist/esm/manifest/index.browser.js.map +0 -1
  197. package/dist/esm/manifest/index.js +0 -4
  198. package/dist/esm/manifest/index.js.map +0 -1
  199. package/dist/esm/manifest/manifest.js.map +0 -1
  200. package/dist/esm/manifest/validation.js +0 -152
  201. package/dist/esm/manifest/validation.js.map +0 -1
  202. package/dist/esm/mock.js.map +0 -1
  203. package/dist/esm/namespace.js +0 -110
  204. package/dist/esm/namespace.js.map +0 -1
  205. package/dist/esm/npm.js +0 -70
  206. package/dist/esm/npm.js.map +0 -1
  207. package/dist/esm/path.js +0 -17
  208. package/dist/esm/path.js.map +0 -1
  209. package/dist/esm/post-process.js.map +0 -1
  210. package/dist/esm/snaps.js +0 -215
  211. package/dist/esm/snaps.js.map +0 -1
  212. package/dist/esm/types.js +0 -85
  213. package/dist/esm/types.js.map +0 -1
  214. package/dist/esm/validation.js +0 -17
  215. package/dist/esm/validation.js.map +0 -1
  216. package/dist/esm/versions.js.map +0 -1
  217. package/dist/esm/virtual-file/VirtualFile.js.map +0 -1
  218. package/dist/esm/virtual-file/index.browser.js +0 -3
  219. package/dist/esm/virtual-file/index.browser.js.map +0 -1
  220. package/dist/esm/virtual-file/index.js +0 -4
  221. package/dist/esm/virtual-file/index.js.map +0 -1
  222. package/dist/esm/virtual-file/toVirtualFile.js +0 -26
  223. package/dist/esm/virtual-file/toVirtualFile.js.map +0 -1
  224. package/dist/types/enum.d.ts +0 -30
  225. package/dist/types/icon.d.ts +0 -4
  226. package/dist/types/json.d.ts +0 -13
  227. package/dist/types/namespace.d.ts +0 -124
  228. package/dist/types/validation.d.ts +0 -8
  229. /package/dist/{types/array.d.ts → array.d.ts} +0 -0
  230. /package/dist/{types/checksum.d.ts → checksum.d.ts} +0 -0
  231. /package/dist/{types/cronjob.d.ts → cronjob.d.ts} +0 -0
  232. /package/dist/{types/deep-clone.d.ts → deep-clone.d.ts} +0 -0
  233. /package/dist/{types/default-endowments.d.ts → default-endowments.d.ts} +0 -0
  234. /package/dist/{types/entropy.d.ts → entropy.d.ts} +0 -0
  235. /package/dist/{types/eval-worker.d.ts → eval-worker.d.ts} +0 -0
  236. /package/dist/{types/eval.d.ts → eval.d.ts} +0 -0
  237. /package/dist/{types/fs.d.ts → fs.d.ts} +0 -0
  238. /package/dist/{types/iframe.test.browser.d.ts → iframe.test.browser.d.ts} +0 -0
  239. /package/dist/{types/index.executionenv.d.ts → index.executionenv.d.ts} +0 -0
  240. /package/dist/{types/json-rpc.d.ts → json-rpc.d.ts} +0 -0
  241. /package/dist/{types/logging.d.ts → logging.d.ts} +0 -0
  242. /package/dist/{types/manifest → manifest}/index.browser.d.ts +0 -0
  243. /package/dist/{types/manifest → manifest}/index.d.ts +0 -0
  244. /package/dist/{types/manifest → manifest}/manifest.d.ts +0 -0
  245. /package/dist/{types/mock.d.ts → mock.d.ts} +0 -0
  246. /package/dist/{types/path.d.ts → path.d.ts} +0 -0
  247. /package/dist/{types/post-process.d.ts → post-process.d.ts} +0 -0
  248. /package/dist/{types/versions.d.ts → versions.d.ts} +0 -0
  249. /package/dist/{types/virtual-file → virtual-file}/VirtualFile.d.ts +0 -0
  250. /package/dist/{types/virtual-file → virtual-file}/index.browser.d.ts +0 -0
  251. /package/dist/{types/virtual-file → virtual-file}/index.d.ts +0 -0
  252. /package/dist/{types/virtual-file → virtual-file}/toVirtualFile.d.ts +0 -0
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSnapManifest = exports.assertIsSnapManifest = exports.isSnapManifest = exports.SnapManifestStruct = exports.PermissionsStruct = exports.SnapGetBip32EntropyPermissionsStruct = exports.Bip32EntropyStruct = exports.bip32entropy = exports.Bip32PathStruct = exports.FORBIDDEN_COIN_TYPES = void 0;
4
+ const key_tree_1 = require("@metamask/key-tree");
5
+ const utils_1 = require("@metamask/utils");
6
+ const superstruct_1 = require("superstruct");
7
+ const array_1 = require("../array");
8
+ const cronjob_1 = require("../cronjob");
9
+ const entropy_1 = require("../entropy");
10
+ const json_rpc_1 = require("../json-rpc");
11
+ const namespace_1 = require("../namespace");
12
+ const types_1 = require("../types");
13
+ // BIP-43 purposes that cannot be used for entropy derivation. These are in the
14
+ // string form, ending with `'`.
15
+ const FORBIDDEN_PURPOSES = [
16
+ entropy_1.SIP_6_MAGIC_VALUE,
17
+ entropy_1.STATE_ENCRYPTION_MAGIC_VALUE,
18
+ ];
19
+ exports.FORBIDDEN_COIN_TYPES = [60];
20
+ const FORBIDDEN_PATHS = exports.FORBIDDEN_COIN_TYPES.map((coinType) => [
21
+ 'm',
22
+ "44'",
23
+ `${coinType}'`,
24
+ ]);
25
+ exports.Bip32PathStruct = (0, superstruct_1.refine)((0, superstruct_1.array)((0, superstruct_1.string)()), 'BIP-32 path', (path) => {
26
+ if (path.length === 0) {
27
+ return 'Path must be a non-empty BIP-32 derivation path array';
28
+ }
29
+ if (path[0] !== 'm') {
30
+ return 'Path must start with "m".';
31
+ }
32
+ if (path.length < 3) {
33
+ return 'Paths must have a length of at least three.';
34
+ }
35
+ if (path.slice(1).some((part) => !(0, key_tree_1.isValidBIP32PathSegment)(part))) {
36
+ return 'Path must be a valid BIP-32 derivation path array.';
37
+ }
38
+ if (FORBIDDEN_PURPOSES.includes(path[1])) {
39
+ return `The purpose "${path[1]}" is not allowed for entropy derivation.`;
40
+ }
41
+ if (FORBIDDEN_PATHS.some((forbiddenPath) => (0, array_1.isEqual)(path.slice(0, forbiddenPath.length), forbiddenPath))) {
42
+ return `The path "${path.join('/')}" is not allowed for entropy derivation.`;
43
+ }
44
+ return true;
45
+ });
46
+ const bip32entropy = (struct) => (0, superstruct_1.refine)(struct, 'BIP-32 entropy', (value) => {
47
+ if (value.curve === 'ed25519' &&
48
+ value.path.slice(1).some((part) => !part.endsWith("'"))) {
49
+ return 'Ed25519 does not support unhardened paths.';
50
+ }
51
+ return true;
52
+ });
53
+ exports.bip32entropy = bip32entropy;
54
+ // Used outside @metamask/snap-utils
55
+ exports.Bip32EntropyStruct = (0, exports.bip32entropy)((0, superstruct_1.type)({
56
+ path: exports.Bip32PathStruct,
57
+ curve: (0, superstruct_1.enums)(['ed25519', 'secp256k1']),
58
+ }));
59
+ exports.SnapGetBip32EntropyPermissionsStruct = (0, superstruct_1.size)((0, superstruct_1.array)(exports.Bip32EntropyStruct), 1, Infinity);
60
+ /* eslint-disable @typescript-eslint/naming-convention */
61
+ exports.PermissionsStruct = (0, superstruct_1.type)({
62
+ 'endowment:long-running': (0, superstruct_1.optional)((0, superstruct_1.object)({})),
63
+ 'endowment:network-access': (0, superstruct_1.optional)((0, superstruct_1.object)({})),
64
+ 'endowment:webassembly': (0, superstruct_1.optional)((0, superstruct_1.object)({})),
65
+ 'endowment:transaction-insight': (0, superstruct_1.optional)((0, superstruct_1.object)({
66
+ allowTransactionOrigin: (0, superstruct_1.optional)((0, superstruct_1.boolean)()),
67
+ })),
68
+ 'endowment:cronjob': (0, superstruct_1.optional)((0, superstruct_1.object)({ jobs: cronjob_1.CronjobSpecificationArrayStruct })),
69
+ 'endowment:rpc': (0, superstruct_1.optional)(json_rpc_1.RpcOriginsStruct),
70
+ snap_dialog: (0, superstruct_1.optional)((0, superstruct_1.object)({})),
71
+ // TODO: Remove
72
+ snap_confirm: (0, superstruct_1.optional)((0, superstruct_1.object)({})),
73
+ snap_manageState: (0, superstruct_1.optional)((0, superstruct_1.object)({})),
74
+ snap_notify: (0, superstruct_1.optional)((0, superstruct_1.object)({})),
75
+ snap_getBip32Entropy: (0, superstruct_1.optional)(exports.SnapGetBip32EntropyPermissionsStruct),
76
+ snap_getBip32PublicKey: (0, superstruct_1.optional)(exports.SnapGetBip32EntropyPermissionsStruct),
77
+ snap_getBip44Entropy: (0, superstruct_1.optional)((0, superstruct_1.size)((0, superstruct_1.array)((0, superstruct_1.object)({ coinType: (0, superstruct_1.size)((0, superstruct_1.integer)(), 0, 2 ** 32 - 1) })), 1, Infinity)),
78
+ snap_getEntropy: (0, superstruct_1.optional)((0, superstruct_1.object)({})),
79
+ 'endowment:keyring': (0, superstruct_1.optional)((0, superstruct_1.object)({
80
+ namespaces: namespace_1.NamespacesStruct,
81
+ })),
82
+ });
83
+ exports.SnapManifestStruct = (0, superstruct_1.object)({
84
+ version: utils_1.VersionStruct,
85
+ description: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, 280),
86
+ proposedName: (0, superstruct_1.size)((0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:[A-Za-z0-9-_]+( [A-Za-z0-9-_]+)*)|(?:(?:@[A-Za-z0-9-*~][A-Za-z0-9-*._~]*\/)?[A-Za-z0-9-~][A-Za-z0-9-._~]*)$/u), 1, 214),
87
+ repository: (0, superstruct_1.optional)((0, superstruct_1.object)({
88
+ type: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
89
+ url: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
90
+ })),
91
+ source: (0, superstruct_1.object)({
92
+ shasum: utils_1.ChecksumStruct,
93
+ location: (0, superstruct_1.object)({
94
+ npm: (0, superstruct_1.object)({
95
+ filePath: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
96
+ iconPath: (0, superstruct_1.optional)((0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity)),
97
+ packageName: types_1.NameStruct,
98
+ registry: (0, superstruct_1.union)([
99
+ (0, superstruct_1.literal)('https://registry.npmjs.org'),
100
+ (0, superstruct_1.literal)('https://registry.npmjs.org/'),
101
+ ]),
102
+ }),
103
+ }),
104
+ }),
105
+ initialPermissions: exports.PermissionsStruct,
106
+ manifestVersion: (0, superstruct_1.literal)('0.1'),
107
+ });
108
+ /**
109
+ * Check if the given value is a valid {@link SnapManifest} object.
110
+ *
111
+ * @param value - The value to check.
112
+ * @returns Whether the value is a valid {@link SnapManifest} object.
113
+ */
114
+ function isSnapManifest(value) {
115
+ return (0, superstruct_1.is)(value, exports.SnapManifestStruct);
116
+ }
117
+ exports.isSnapManifest = isSnapManifest;
118
+ /**
119
+ * Assert that the given value is a valid {@link SnapManifest} object.
120
+ *
121
+ * @param value - The value to check.
122
+ * @throws If the value is not a valid {@link SnapManifest} object.
123
+ */
124
+ function assertIsSnapManifest(value) {
125
+ (0, utils_1.assertStruct)(value, exports.SnapManifestStruct, `"${types_1.NpmSnapFileNames.Manifest}" is invalid`);
126
+ }
127
+ exports.assertIsSnapManifest = assertIsSnapManifest;
128
+ /**
129
+ * Creates a {@link SnapManifest} object from JSON.
130
+ *
131
+ *
132
+ * @param value - The value to check.
133
+ * @throws If the value cannot be coerced to a {@link SnapManifest} object.
134
+ * @returns The created {@link SnapManifest} object.
135
+ */
136
+ function createSnapManifest(value) {
137
+ // TODO: Add a utility to prefix these errors similar to assertStruct
138
+ return (0, superstruct_1.create)(value, exports.SnapManifestStruct);
139
+ }
140
+ exports.createSnapManifest = createSnapManifest;
141
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/manifest/validation.ts"],"names":[],"mappings":";;;AAAA,iDAA6D;AAC7D,2CAA8E;AAC9E,6CAkBqB;AAErB,oCAAmC;AACnC,wCAA6D;AAC7D,wCAA6E;AAC7E,0CAA+C;AAC/C,4CAAgD;AAChD,oCAAwD;AAExD,+EAA+E;AAC/E,gCAAgC;AAChC,MAAM,kBAAkB,GAAa;IACnC,2BAAiB;IACjB,sCAA4B;CAC7B,CAAC;AAEW,QAAA,oBAAoB,GAAa,CAAC,EAAE,CAAC,CAAC;AACnD,MAAM,eAAe,GAAe,4BAAoB,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC;IACzE,GAAG;IACH,KAAK;IACL,GAAG,QAAQ,GAAG;CACf,CAAC,CAAC;AAEU,QAAA,eAAe,GAAG,IAAA,oBAAM,EACnC,IAAA,mBAAK,EAAC,IAAA,oBAAM,GAAE,CAAC,EACf,aAAa,EACb,CAAC,IAAc,EAAE,EAAE;IACjB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;QACrB,OAAO,uDAAuD,CAAC;KAChE;IAED,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;QACnB,OAAO,2BAA2B,CAAC;KACpC;IAED,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;QACnB,OAAO,6CAA6C,CAAC;KACtD;IAED,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAA,kCAAuB,EAAC,IAAI,CAAC,CAAC,EAA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AC,6BAA6B,CAAC;iBACvC,CAAC;aACH,CAAC;SACH,CAAC;KACH,CAAC;IACF,kBAAkB,EAAE,yBAAiB;IACrC,eAAe,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;CAChC,CAAC,CAAC;AAIH;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AACvC,CAAC;AAFD,wCAEC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAc;IAEd,IAAA,oBAAY,EACV,KAAK,EACL,0BAAkB,EAClB,IAAI,wBAAgB,CAAC,QAAQ,cAAc,CAC5C,CAAC;AACJ,CAAC;AARD,oDAQC;AAED;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAAC,KAAc;IAC/C,qEAAqE;IACrE,OAAO,IAAA,oBAAM,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AAC3C,CAAC;AAHD,gDAGC","sourcesContent":["import { isValidBIP32PathSegment } from '@metamask/key-tree';\nimport { assertStruct, ChecksumStruct, VersionStruct } from '@metamask/utils';\nimport {\n array,\n boolean,\n create,\n enums,\n Infer,\n integer,\n is,\n literal,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n Struct,\n type,\n union,\n} from 'superstruct';\n\nimport { isEqual } from '../array';\nimport { CronjobSpecificationArrayStruct } from '../cronjob';\nimport { SIP_6_MAGIC_VALUE, STATE_ENCRYPTION_MAGIC_VALUE } from '../entropy';\nimport { RpcOriginsStruct } from '../json-rpc';\nimport { NamespacesStruct } from '../namespace';\nimport { NameStruct, NpmSnapFileNames } from '../types';\n\n// BIP-43 purposes that cannot be used for entropy derivation. These are in the\n// string form, ending with `'`.\nconst FORBIDDEN_PURPOSES: string[] = [\n SIP_6_MAGIC_VALUE,\n STATE_ENCRYPTION_MAGIC_VALUE,\n];\n\nexport const FORBIDDEN_COIN_TYPES: number[] = [60];\nconst FORBIDDEN_PATHS: string[][] = FORBIDDEN_COIN_TYPES.map((coinType) => [\n 'm',\n \"44'\",\n `${coinType}'`,\n]);\n\nexport const Bip32PathStruct = refine(\n array(string()),\n 'BIP-32 path',\n (path: string[]) => {\n if (path.length === 0) {\n return 'Path must be a non-empty BIP-32 derivation path array';\n }\n\n if (path[0] !== 'm') {\n return 'Path must start with \"m\".';\n }\n\n if (path.length < 3) {\n return 'Paths must have a length of at least three.';\n }\n\n if (path.slice(1).some((part) => !isValidBIP32PathSegment(part))) {\n return 'Path must be a valid BIP-32 derivation path array.';\n }\n\n if (FORBIDDEN_PURPOSES.includes(path[1])) {\n return `The purpose \"${path[1]}\" is not allowed for entropy derivation.`;\n }\n\n if (\n FORBIDDEN_PATHS.some((forbiddenPath) =>\n isEqual(path.slice(0, forbiddenPath.length), forbiddenPath),\n )\n ) {\n return `The path \"${path.join(\n '/',\n )}\" is not allowed for entropy derivation.`;\n }\n\n return true;\n },\n);\n\nexport const bip32entropy = <T extends { path: string[]; curve: string }, S>(\n struct: Struct<T, S>,\n) =>\n refine(struct, 'BIP-32 entropy', (value) => {\n if (\n value.curve === 'ed25519' &&\n value.path.slice(1).some((part) => !part.endsWith(\"'\"))\n ) {\n return 'Ed25519 does not support unhardened paths.';\n }\n\n return true;\n });\n\n// Used outside @metamask/snap-utils\nexport const Bip32EntropyStruct = bip32entropy(\n type({\n path: Bip32PathStruct,\n curve: enums(['ed25519', 'secp256k1']),\n }),\n);\n\nexport type Bip32Entropy = Infer<typeof Bip32EntropyStruct>;\n\nexport const SnapGetBip32EntropyPermissionsStruct = size(\n array(Bip32EntropyStruct),\n 1,\n Infinity,\n);\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport const PermissionsStruct = type({\n 'endowment:long-running': optional(object({})),\n 'endowment:network-access': optional(object({})),\n 'endowment:webassembly': optional(object({})),\n 'endowment:transaction-insight': optional(\n object({\n allowTransactionOrigin: optional(boolean()),\n }),\n ),\n 'endowment:cronjob': optional(\n object({ jobs: CronjobSpecificationArrayStruct }),\n ),\n 'endowment:rpc': optional(RpcOriginsStruct),\n snap_dialog: optional(object({})),\n // TODO: Remove\n snap_confirm: optional(object({})),\n snap_manageState: optional(object({})),\n snap_notify: optional(object({})),\n snap_getBip32Entropy: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip32PublicKey: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip44Entropy: optional(\n size(\n array(object({ coinType: size(integer(), 0, 2 ** 32 - 1) })),\n 1,\n Infinity,\n ),\n ),\n snap_getEntropy: optional(object({})),\n 'endowment:keyring': optional(\n object({\n namespaces: NamespacesStruct,\n }),\n ),\n});\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport type SnapPermissions = Infer<typeof PermissionsStruct>;\n\nexport const SnapManifestStruct = object({\n version: VersionStruct,\n description: size(string(), 1, 280),\n proposedName: size(\n pattern(\n string(),\n /^(?:[A-Za-z0-9-_]+( [A-Za-z0-9-_]+)*)|(?:(?:@[A-Za-z0-9-*~][A-Za-z0-9-*._~]*\\/)?[A-Za-z0-9-~][A-Za-z0-9-._~]*)$/u,\n ),\n 1,\n 214,\n ),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n source: object({\n shasum: ChecksumStruct,\n location: object({\n npm: object({\n filePath: size(string(), 1, Infinity),\n iconPath: optional(size(string(), 1, Infinity)),\n packageName: NameStruct,\n registry: union([\n literal('https://registry.npmjs.org'),\n literal('https://registry.npmjs.org/'),\n ]),\n }),\n }),\n }),\n initialPermissions: PermissionsStruct,\n manifestVersion: literal('0.1'),\n});\n\nexport type SnapManifest = Infer<typeof SnapManifestStruct>;\n\n/**\n * Check if the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link SnapManifest} object.\n */\nexport function isSnapManifest(value: unknown): value is SnapManifest {\n return is(value, SnapManifestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link SnapManifest} object.\n */\nexport function assertIsSnapManifest(\n value: unknown,\n): asserts value is SnapManifest {\n assertStruct(\n value,\n SnapManifestStruct,\n `\"${NpmSnapFileNames.Manifest}\" is invalid`,\n );\n}\n\n/**\n * Creates a {@link SnapManifest} object from JSON.\n *\n *\n * @param value - The value to check.\n * @throws If the value cannot be coerced to a {@link SnapManifest} object.\n * @returns The created {@link SnapManifest} object.\n */\nexport function createSnapManifest(value: unknown): SnapManifest {\n // TODO: Add a utility to prefix these errors similar to assertStruct\n return create(value, SnapManifestStruct);\n}\n"]}
@@ -1,35 +1,32 @@
1
- import crypto from 'crypto';
2
- import EventEmitter from 'events';
3
- import { DEFAULT_ENDOWMENTS } from './default-endowments';
4
- const NETWORK_APIS = [
5
- 'fetch',
6
- 'Request',
7
- 'Headers',
8
- 'Response'
9
- ];
10
- export const ALL_APIS = [
11
- ...DEFAULT_ENDOWMENTS,
12
- ...NETWORK_APIS,
13
- 'WebAssembly'
14
- ];
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 crypto_1 = __importDefault(require("crypto"));
8
+ const events_1 = __importDefault(require("events"));
9
+ const default_endowments_1 = require("./default-endowments");
10
+ const NETWORK_APIS = ['fetch'];
11
+ exports.ALL_APIS = [...default_endowments_1.DEFAULT_ENDOWMENTS, ...NETWORK_APIS];
15
12
  /**
16
13
  * Get a mock snap API, that always returns `true` for requests.
17
14
  *
18
15
  * @returns A mocked snap provider.
19
- */ function getMockSnapGlobal() {
16
+ */
17
+ function getMockSnapGlobal() {
20
18
  // eslint-disable-next-line @typescript-eslint/require-await
21
- return {
22
- request: async ()=>true
23
- };
19
+ return { request: async () => true };
24
20
  }
25
21
  /**
26
22
  * Get a mock Ethereum provider, that always returns `true` for requests.
27
23
  *
28
24
  * @returns A mocked ethereum provider.
29
- */ function getMockEthereumProvider() {
30
- const mockProvider = new EventEmitter();
25
+ */
26
+ function getMockEthereumProvider() {
27
+ const mockProvider = new events_1.default();
31
28
  // eslint-disable-next-line @typescript-eslint/require-await
32
- mockProvider.request = async ()=>true;
29
+ mockProvider.request = async () => true;
33
30
  return mockProvider;
34
31
  }
35
32
  /**
@@ -37,22 +34,25 @@ export const ALL_APIS = [
37
34
  *
38
35
  * @param value - The value to check.
39
36
  * @returns `true` if the value is a constructor, or `false` otherwise.
40
- */ export const isConstructor = (value)=>Boolean(typeof value?.prototype?.constructor?.name === 'string');
37
+ */
38
+ const isConstructor = (value) => Boolean(typeof value?.prototype?.constructor?.name === 'string');
39
+ exports.isConstructor = isConstructor;
41
40
  /**
42
41
  * A function that always returns `true`.
43
42
  *
44
43
  * @returns `true`.
45
- */ const mockFunction = ()=>true;
44
+ */
45
+ const mockFunction = () => true;
46
46
  class MockClass {
47
47
  }
48
48
  const handler = {
49
49
  // eslint-disable-next-line @typescript-eslint/naming-convention
50
- construct (Target, args) {
50
+ construct(Target, args) {
51
51
  return new Proxy(new Target(...args), handler);
52
52
  },
53
- get (_target, _prop) {
53
+ get(_target, _prop) {
54
54
  return mockFunction;
55
- }
55
+ },
56
56
  };
57
57
  /**
58
58
  * Generate a mock class for a given value. The value is wrapped in a Proxy, and
@@ -60,14 +60,15 @@ const handler = {
60
60
  *
61
61
  * @param value - The value to mock.
62
62
  * @returns A mock class.
63
- */ const generateMockClass = (value)=>{
63
+ */
64
+ const generateMockClass = (value) => {
64
65
  return new Proxy(value, handler);
65
66
  };
66
67
  // Things not currently auto-mocked because of NodeJS, by adding them here we
67
68
  // have types for them and can use that to generate mocks if needed.
68
69
  const mockWindow = {
69
- crypto,
70
- SubtleCrypto: MockClass
70
+ crypto: crypto_1.default,
71
+ SubtleCrypto: MockClass,
71
72
  };
72
73
  /**
73
74
  * Generate a mock endowment for a certain class or function on the `globalThis`
@@ -76,19 +77,21 @@ const mockWindow = {
76
77
  * @param key - The key to generate the mock endowment for.
77
78
  * @returns A mocked class or function. If the key is part of the default
78
79
  * endowments, the original value is returned.
79
- */ const generateMockEndowment = (key)=>{
80
+ */
81
+ const generateMockEndowment = (key) => {
80
82
  const globalValue = globalThis[key];
81
83
  // Default exposed APIs don't need to be mocked
82
- if (globalValue && DEFAULT_ENDOWMENTS.includes(key)) {
84
+ if (globalValue && default_endowments_1.DEFAULT_ENDOWMENTS.includes(key)) {
83
85
  return globalValue;
84
86
  }
85
87
  // Fall back to mockWindow for certain APIs not exposed in global in Node.JS
86
88
  const globalOrMocked = globalValue ?? mockWindow[key];
87
89
  const type = typeof globalOrMocked;
88
90
  const isFunction = type === 'function';
89
- if (isFunction && isConstructor(globalOrMocked)) {
91
+ if (isFunction && (0, exports.isConstructor)(globalOrMocked)) {
90
92
  return generateMockClass(globalOrMocked);
91
- } else if (isFunction || !globalOrMocked) {
93
+ }
94
+ else if (isFunction || !globalOrMocked) {
92
95
  // Fall back to function mock for now
93
96
  return mockFunction;
94
97
  }
@@ -98,14 +101,9 @@ const mockWindow = {
98
101
  * Generate mock endowments for all the APIs as defined in {@link ALL_APIS}.
99
102
  *
100
103
  * @returns A map of endowments.
101
- */ export const generateMockEndowments = ()=>{
102
- return ALL_APIS.reduce((acc, cur)=>({
103
- ...acc,
104
- [cur]: generateMockEndowment(cur)
105
- }), {
106
- snap: getMockSnapGlobal(),
107
- ethereum: getMockEthereumProvider()
108
- });
104
+ */
105
+ const generateMockEndowments = () => {
106
+ return exports.ALL_APIS.reduce((acc, cur) => ({ ...acc, [cur]: generateMockEndowment(cur) }), { snap: getMockSnapGlobal(), ethereum: getMockEthereumProvider() });
109
107
  };
110
-
108
+ exports.generateMockEndowments = generateMockEndowments;
111
109
  //# sourceMappingURL=mock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock.js","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4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;IAC1C,MAAM,EAAN,gBAAM;IACN,YAAY,EAAE,SAAS;CACxB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,qBAAqB,GAAG,CAAC,GAAW,EAAE,EAAE;IAC5C,MAAM,WAAW,GAAI,UAAkB,CAAC,GAAG,CAAC,CAAC;IAE7C,+CAA+C;IAC/C,IAAI,WAAW,IAAI,uCAAkB,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QACnD,OAAO,WAAW,CAAC;KACpB;IAED,4EAA4E;IAC5E,MAAM,cAAc,GAAG,WAAW,IAAI,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,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,qBAAqB,CAAC,GAAG,CAAC,EAAE,CAAC,EAC7D,EAAE,IAAI,EAAE,iBAAiB,EAAE,EAAE,QAAQ,EAAE,uBAAuB,EAAE,EAAE,CACnE,CAAC;AACJ,CAAC,CAAC;AALW,QAAA,sBAAsB,0BAKjC","sourcesContent":["import crypto from 'crypto';\nimport EventEmitter from 'events';\n\nimport { DEFAULT_ENDOWMENTS } from './default-endowments';\n\nconst NETWORK_APIS = ['fetch'];\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 // eslint-disable-next-line @typescript-eslint/require-await\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 // eslint-disable-next-line @typescript-eslint/require-await\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 // eslint-disable-next-line @typescript-eslint/naming-convention\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 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 { AssertionErrorConstructor } from '@metamask/utils';
2
+ import { Infer } from 'superstruct';
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
+ chains: string[];
98
+ accounts: 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
+ chains: string[];
128
+ accounts: string[];
129
+ methods?: string[] | undefined;
130
+ events?: string[] | undefined;
131
+ }>;
132
+ }, {
133
+ namespaces: import("superstruct").Struct<Record<string, {
134
+ chains: string[];
135
+ accounts: 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;