@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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"post-process.js","sourceRoot":"","sources":["../src/post-process.ts"],"names":[],"mappings":";;;AAAA,wCAQsB;AACtB,sCAAgF;AAgDhF,IAAY,kBAEX;AAFD,WAAY,kBAAkB;IAC5B,+HAAyG,CAAA;AAC3G,CAAC,EAFW,kBAAkB,GAAlB,0BAAkB,KAAlB,0BAAkB,QAE7B;AAED,sEAAsE;AACtE,uEAAuE;AACvE,wEAAwE;AACxE,MAAM,WAAW,GAAG,sCAAsC,CAAC;AAE3D,4EAA4E;AAC5E,cAAc;AACd,MAAM,sBAAsB,GAAG,IAAA,uBA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GAAG;YACnB,QAAQ,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC;SAC/B,CAAC;KACH;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,iCAAiC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;KACnE;AACH,CAAC;AAjQD,8CAiQC","sourcesContent":["import {\n binaryExpression,\n Expression,\n Identifier,\n stringLiteral,\n TemplateElement,\n templateElement,\n templateLiteral,\n} from '@babel/types';\nimport { transformSync, Node, Visitor, template, PluginObj } from '@babel/core';\n\n/**\n * Source map declaration taken from `@babel/core`. Babel doesn't export the\n * type for this, so it's copied from the source code instead here.\n */\nexport type SourceMap = {\n version: number;\n sources: string[];\n names: string[];\n sourceRoot?: string | undefined;\n sourcesContent?: string[] | undefined;\n mappings: string;\n file: string;\n};\n\n/**\n * The post process options.\n *\n * @property stripComments - Whether to strip comments. Defaults to `true`.\n * @property sourceMap - Whether to generate a source map for the modified code.\n * See also `inputSourceMap`.\n * @property inputSourceMap - The source map for the input code. When provided,\n * the source map will be used to generate a source map for the modified code.\n * This ensures that the source map is correct for the modified code, and still\n * points to the original source. If not provided, a new source map will be\n * generated instead.\n */\nexport type PostProcessOptions = {\n stripComments?: boolean;\n sourceMap?: boolean | 'inline';\n inputSourceMap?: SourceMap;\n};\n\n/**\n * The post processed bundle output.\n *\n * @property code - The modified code.\n * @property sourceMap - The source map for the modified code, if the source map\n * option was enabled.\n * @property warnings - Any warnings that occurred during the post-processing.\n */\nexport type PostProcessedBundle = {\n code: string;\n sourceMap?: SourceMap | null;\n warnings: PostProcessWarning[];\n};\n\nexport enum PostProcessWarning {\n UnsafeMathRandom = '`Math.random` was detected in the bundle. This is not a secure source of randomness.',\n}\n\n// The RegEx below consists of multiple groups joined by a boolean OR.\n// Each part consists of two groups which capture a part of each string\n// which needs to be split up, e.g., `<!--` is split into `<!` and `--`.\nconst TOKEN_REGEX = /(<!)(--)|(--)(>)|(import)(\\(.*?\\))/gu;\n\n// An empty template element, i.e., a part of a template literal without any\n// value (\"\").\nconst EMPTY_TEMPLATE_ELEMENT = templateElement({ raw: '', cooked: '' });\n\nconst evalWrapper = template.statement(`\n (1, REF)(ARGS)\n`);\n\nconst objectEvalWrapper = template.statement(`\n (1, OBJECT.REF)\n`);\n\nconst regeneratorRuntimeWrapper = template.statement(`\n var regeneratorRuntime;\n`);\n\n/**\n * Breaks up tokens that would otherwise result in SES errors. The tokens are\n * broken up in a non-destructive way where possible. Currently works with:\n * - HTML comment tags `<!--` and `-->`, broken up into `<!`, `--`, and `--`,\n * `>`.\n * - `import(n)` statements, broken up into `import`, `(n)`.\n *\n * @param value - The string value to break up.\n * @returns The string split into an array, in a way that it can be joined\n * together to form the same string, but with the tokens separated into single\n * array elements.\n */\nfunction breakTokens(value: string): string[] {\n const tokens = value.split(TOKEN_REGEX);\n return (\n tokens\n // TODO: The `split` above results in some values being `undefined`.\n // There may be a better solution to avoid having to filter those out.\n .filter((token) => token !== '' && token !== undefined)\n );\n}\n\n/**\n * Breaks up tokens that would otherwise result in SES errors. The tokens are\n * broken up in a non-destructive way where possible. Currently works with:\n * - HTML comment tags `<!--` and `-->`, broken up into `<!`, `--`, and `--`,\n * `>`.\n * - `import(n)` statements, broken up into `import`, `(n)`.\n *\n * @param value - The string value to break up.\n * @returns The string split into a tuple consisting of the new template\n * elements and string literal expressions.\n */\nfunction breakTokensTemplateLiteral(\n value: string,\n): [TemplateElement[], Expression[]] {\n // @ts-expect-error `matchAll` is not available in ES2017, but this code\n // should only be used in environments where the function is supported.\n const matches: RegExpMatchArray[] = Array.from(value.matchAll(TOKEN_REGEX));\n\n if (matches.length > 0) {\n const output = matches.reduce<[TemplateElement[], Expression[]]>(\n ([elements, expressions], rawMatch, index, values) => {\n const [, first, last] = rawMatch.filter((raw) => raw !== undefined);\n\n // Slice the text in front of the match, which does not need to be\n // broken up.\n const prefix = value.slice(\n index === 0\n ? 0\n : (values[index - 1].index as number) + values[index - 1][0].length,\n rawMatch.index,\n );\n\n return [\n [\n ...elements,\n templateElement({\n raw: getRawTemplateValue(prefix),\n cooked: prefix,\n }),\n EMPTY_TEMPLATE_ELEMENT,\n ],\n [...expressions, stringLiteral(first), stringLiteral(last)],\n ];\n },\n [[], []],\n );\n\n // Add the text after the last match to the output.\n const lastMatch = matches[matches.length - 1];\n const suffix = value.slice(\n (lastMatch.index as number) + lastMatch[0].length,\n );\n\n return [\n [\n ...output[0],\n templateElement({ raw: getRawTemplateValue(suffix), cooked: suffix }),\n ],\n output[1],\n ];\n }\n\n // If there are no matches, simply return the original value.\n return [\n [templateElement({ raw: getRawTemplateValue(value), cooked: value })],\n [],\n ];\n}\n\n/**\n * Get a raw template literal value from a cooked value. This adds a backslash\n * before every '`', '\\' and '${' characters.\n *\n * @see https://github.com/babel/babel/issues/9242#issuecomment-532529613\n * @param value - The cooked string to get the raw string for.\n * @returns The value as raw value.\n */\nfunction getRawTemplateValue(value: string) {\n return value.replace(/\\\\|`|\\$\\{/gu, '\\\\$&');\n}\n\n/**\n * Post process code with AST such that it can be evaluated in SES.\n *\n * Currently:\n * - Makes all direct calls to eval indirect.\n * - Handles certain Babel-related edge cases.\n * - Removes the `Buffer` provided by Browserify.\n * - Optionally removes comments.\n * - Breaks up tokens that would otherwise result in SES errors, such as HTML\n * comment tags `<!--` and `-->` and `import(n)` statements.\n *\n * @param code - The code to post process.\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to `true`.\n * @param options.sourceMap - Whether to generate a source map for the modified\n * code. See also `inputSourceMap`.\n * @param options.inputSourceMap - The source map for the input code. When\n * provided, the source map will be used to generate a source map for the\n * modified code. This ensures that the source map is correct for the modified\n * code, and still points to the original source. If not provided, a new source\n * map will be generated instead.\n * @returns An object containing the modified code, and source map, or null if\n * the provided code is null.\n */\nexport function postProcessBundle(\n code: string,\n {\n stripComments = true,\n sourceMap: sourceMaps,\n inputSourceMap,\n }: Partial<PostProcessOptions> = {},\n): PostProcessedBundle {\n const warnings = new Set<PostProcessWarning>();\n\n const pre: PluginObj['pre'] = ({ ast }) => {\n ast.comments?.forEach((comment) => {\n // Break up tokens that could be parsed as HTML comment terminators. The\n // regular expressions below are written strangely so as to avoid the\n // appearance of such tokens in our source code. For reference:\n // https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md\n comment.value = comment.value\n .replace(new RegExp(`<!${'--'}`, 'gu'), '< !--')\n .replace(new RegExp(`${'--'}>`, 'gu'), '-- >')\n .replace(/import(\\(.*\\))/gu, 'import\\\\$1');\n });\n };\n\n const visitor: Visitor<Node> = {\n FunctionExpression(path) {\n const { node } = path;\n\n // Browserify provides the `Buffer` global as an argument to modules that\n // use it, but this does not work in SES. Since we pass in `Buffer` as an\n // endowment, we can simply remove the argument.\n //\n // Note that this only removes `Buffer` from a wrapped function\n // expression, e.g., `(function (Buffer) { ... })`. Regular functions\n // are not affected.\n //\n // TODO: Since we're working on the AST level, we could check the scope\n // of the function expression, and possibly prevent false positives?\n if (node.type === 'FunctionExpression' && node.extra?.parenthesized) {\n node.params = node.params.filter(\n (param) => !(param.type === 'Identifier' && param.name === 'Buffer'),\n );\n }\n },\n\n CallExpression(path) {\n const { node } = path;\n\n // Replace `eval(foo)` with `(1, eval)(foo)`.\n if (node.callee.type === 'Identifier' && node.callee.name === 'eval') {\n path.replaceWith(\n evalWrapper({\n REF: node.callee,\n ARGS: node.arguments,\n }),\n );\n }\n\n // Detect the use of `Math.random()` and add a warning.\n if (\n node.callee.type === 'MemberExpression' &&\n node.callee.object.type === 'Identifier' &&\n node.callee.object.name === 'Math' &&\n node.callee.property.type === 'Identifier' &&\n node.callee.property.name === 'random'\n ) {\n warnings.add(PostProcessWarning.UnsafeMathRandom);\n }\n },\n\n MemberExpression(path) {\n const { node } = path;\n\n // Replace `object.eval(foo)` with `(1, object.eval)(foo)`.\n if (\n node.property.type === 'Identifier' &&\n node.property.name === 'eval' &&\n // If the expression is already wrapped we can ignore it\n path.parent.type !== 'SequenceExpression'\n ) {\n path.replaceWith(\n objectEvalWrapper({\n OBJECT: node.object,\n REF: node.property,\n }),\n );\n }\n },\n\n Identifier(path) {\n const { node } = path;\n\n // Insert `regeneratorRuntime` global if it's used in the code.\n if (node.name === 'regeneratorRuntime') {\n const program = path.findParent(\n (parent) => parent.node.type === 'Program',\n );\n\n // We know that `program` is a Program node here, but this keeps\n // TypeScript happy.\n if (program?.node.type === 'Program') {\n const body = program.node.body[0];\n\n // This stops it from inserting `regeneratorRuntime` multiple times.\n if (\n body.type === 'VariableDeclaration' &&\n (body.declarations[0].id as Identifier).name ===\n 'regeneratorRuntime'\n ) {\n return;\n }\n\n program?.node.body.unshift(regeneratorRuntimeWrapper());\n }\n }\n },\n\n TemplateLiteral(path) {\n const { node } = path;\n\n // This checks if the template literal was visited before. Without this,\n // it would cause an infinite loop resulting in a stack overflow. We can't\n // skip the path here, because we need to visit the children of the node.\n if (path.getData('visited')) {\n return;\n }\n\n // Break up tokens that could be parsed as HTML comment terminators, or\n // `import()` statements.\n // For reference:\n // - https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md\n // - https://github.com/MetaMask/snaps-monorepo/issues/505\n const [replacementQuasis, replacementExpressions] = node.quasis.reduce<\n [TemplateElement[], Expression[]]\n >(\n ([elements, expressions], quasi, index) => {\n // Note: Template literals have two variants, \"cooked\" and \"raw\". Here\n // we use the cooked version.\n // https://exploringjs.com/impatient-js/ch_template-literals.html#template-strings-cooked-vs-raw\n const tokens = breakTokensTemplateLiteral(\n quasi.value.cooked as string,\n );\n\n // Only update the node if something changed.\n if (tokens[0].length <= 1) {\n return [\n [...elements, quasi as TemplateElement],\n [...expressions, node.expressions[index] as Expression],\n ];\n }\n\n return [\n [...elements, ...(tokens[0] as TemplateElement[])],\n [\n ...expressions,\n ...tokens[1],\n node.expressions[index] as Expression,\n ],\n ];\n },\n [[], []],\n );\n\n path.replaceWith(\n templateLiteral(\n replacementQuasis,\n replacementExpressions.filter((e) => e !== undefined),\n ) as Node,\n );\n\n path.setData('visited', true);\n },\n\n StringLiteral(path) {\n const { node } = path;\n\n // Break up tokens that could be parsed as HTML comment terminators, or\n // `import()` statements.\n // For reference:\n // - https://github.com/endojs/endo/blob/70cc86eb400655e922413b99c38818d7b2e79da0/packages/ses/error-codes/SES_HTML_COMMENT_REJECTED.md\n // - https://github.com/MetaMask/snaps-monorepo/issues/505\n const tokens = breakTokens(node.value);\n\n // Only update the node if the string literal was broken up.\n if (tokens.length <= 1) {\n return;\n }\n\n const replacement = tokens\n .slice(1)\n .reduce<Expression>(\n (acc, value) => binaryExpression('+', acc, stringLiteral(value)),\n stringLiteral(tokens[0]),\n );\n\n path.replaceWith(replacement as Node);\n path.skip();\n },\n\n BinaryExpression(path) {\n const source = path.getSource();\n\n // Throw an error if HTML comments are used as a binary expression.\n if (source.includes('<!--') || source.includes('-->')) {\n throw new Error(\n 'Using HTML comments (`<!--` and `-->`) as operators is not allowed. The behaviour of ' +\n 'these comments is ambiguous, and differs per browser and environment. If you want ' +\n 'to use them as operators, break them up into separate characters, i.e., `a-- > b` ' +\n 'and `a < ! --b`.',\n );\n }\n },\n };\n\n try {\n const file = transformSync(code, {\n // Prevent Babel from searching for a config file.\n configFile: false,\n\n parserOpts: {\n // Strict mode isn't enabled by default, so we need to enable it here.\n strictMode: true,\n\n // If this is disabled, the AST does not include any comments. This is\n // useful for performance reasons, and we use it for stripping comments.\n attachComment: !stripComments,\n },\n\n // By default, Babel optimises bundles that exceed 500 KB, but that\n // results in characters which look like HTML comments, which breaks SES.\n compact: false,\n\n // This configures Babel to generate a new source map from the existing\n // source map if specified. If `sourceMap` is `true` but an input source\n // map is not provided, a new source map will be generated instead.\n inputSourceMap,\n sourceMaps,\n\n plugins: [\n () => ({\n pre,\n visitor,\n }),\n ],\n });\n\n if (!file?.code) {\n throw new Error('Bundled code is empty.');\n }\n\n return {\n code: file.code,\n sourceMap: file.map,\n warnings: Array.from(warnings),\n };\n } catch (error) {\n throw new Error(`Failed to post process code:\\n${error.message}`);\n }\n}\n"]}
@@ -0,0 +1,165 @@
1
+ import { Json } from '@metamask/utils';
2
+ import { SerializedEthereumRpcError } from 'eth-rpc-errors/dist/classes';
3
+ import { SnapManifest, SnapPermissions } from './manifest/validation';
4
+ import { SnapId, SnapIdPrefixes, SnapValidationFailureReason } from './types';
5
+ export declare const LOCALHOST_HOSTNAMES: Set<string>;
6
+ export declare const SNAP_PREFIX = "wallet_snap_";
7
+ export declare const SNAP_PREFIX_REGEX: RegExp;
8
+ export declare const PROPOSED_NAME_REGEX: RegExp;
9
+ /**
10
+ * wallet_enable / wallet_installSnaps permission typing.
11
+ *
12
+ * @deprecated This type is confusing and not descriptive, people confused it with typing initialPermissions, remove when removing wallet_enable.
13
+ */
14
+ export declare type RequestedSnapPermissions = {
15
+ [permission: string]: Record<string, Json>;
16
+ };
17
+ export declare type BlockedSnapInfo = {
18
+ infoUrl?: string;
19
+ reason?: string;
20
+ };
21
+ export declare enum SnapStatus {
22
+ Installing = "installing",
23
+ Updating = "updating",
24
+ Running = "running",
25
+ Stopped = "stopped",
26
+ Crashed = "crashed"
27
+ }
28
+ export declare enum SnapStatusEvents {
29
+ Start = "START",
30
+ Stop = "STOP",
31
+ Crash = "CRASH",
32
+ Update = "UPDATE"
33
+ }
34
+ export declare type StatusContext = {
35
+ snapId: string;
36
+ };
37
+ export declare type StatusEvents = {
38
+ type: SnapStatusEvents;
39
+ };
40
+ export declare type StatusStates = {
41
+ value: SnapStatus;
42
+ context: StatusContext;
43
+ };
44
+ export declare type Status = StatusStates['value'];
45
+ export declare type VersionHistory = {
46
+ origin: string;
47
+ version: string;
48
+ date: number;
49
+ };
50
+ export declare type PersistedSnap = Snap & {
51
+ /**
52
+ * The source code of the Snap.
53
+ */
54
+ sourceCode: string;
55
+ };
56
+ /**
57
+ * A Snap as it exists in {@link SnapController} state.
58
+ */
59
+ export declare type Snap = {
60
+ /**
61
+ * Whether the Snap is enabled, which determines if it can be started.
62
+ */
63
+ enabled: boolean;
64
+ /**
65
+ * The ID of the Snap.
66
+ */
67
+ id: SnapId;
68
+ /**
69
+ * The initial permissions of the Snap, which will be requested when it is
70
+ * installed.
71
+ */
72
+ initialPermissions: SnapPermissions;
73
+ /**
74
+ * The Snap's manifest file.
75
+ */
76
+ manifest: SnapManifest;
77
+ /**
78
+ * Whether the Snap is blocked.
79
+ */
80
+ blocked: boolean;
81
+ /**
82
+ * Information detailing why the snap is blocked.
83
+ */
84
+ blockInformation?: BlockedSnapInfo;
85
+ /**
86
+ * The name of the permission used to invoke the Snap.
87
+ */
88
+ permissionName: string;
89
+ /**
90
+ * The current status of the Snap, e.g. whether it's running or stopped.
91
+ */
92
+ status: Status;
93
+ /**
94
+ * The version of the Snap.
95
+ */
96
+ version: string;
97
+ /**
98
+ * The version history of the Snap.
99
+ * Can be used to derive when the Snap was installed, when it was updated to a certain version and who requested the change.
100
+ */
101
+ versionHistory: VersionHistory[];
102
+ };
103
+ export declare type TruncatedSnapFields = 'id' | 'initialPermissions' | 'permissionName' | 'version' | 'enabled' | 'blocked';
104
+ /**
105
+ * A {@link Snap} object with the fields that are relevant to an external
106
+ * caller.
107
+ */
108
+ export declare type TruncatedSnap = Pick<Snap, TruncatedSnapFields>;
109
+ export declare type ProcessSnapResult = TruncatedSnap | {
110
+ error: SerializedEthereumRpcError;
111
+ };
112
+ export declare type InstallSnapsResult = Record<SnapId, ProcessSnapResult>;
113
+ /**
114
+ * An error indicating that a Snap validation failure is programmatically
115
+ * fixable during development.
116
+ */
117
+ export declare class ProgrammaticallyFixableSnapError extends Error {
118
+ reason: SnapValidationFailureReason;
119
+ constructor(message: string, reason: SnapValidationFailureReason);
120
+ }
121
+ /**
122
+ * Calculates the Base64-encoded SHA-256 digest of a Snap source code string.
123
+ *
124
+ * @param sourceCode - The UTF-8 string source code of a Snap.
125
+ * @returns The Base64-encoded SHA-256 digest of the source code.
126
+ */
127
+ export declare function getSnapSourceShasum(sourceCode: string): string;
128
+ export declare type ValidatedSnapId = `local:${string}` | `npm:${string}`;
129
+ /**
130
+ * Checks whether the `source.shasum` property of a Snap manifest matches the
131
+ * shasum of a snap source code string.
132
+ *
133
+ * @param manifest - The manifest whose shasum to validate.
134
+ * @param sourceCode - The source code of the snap.
135
+ * @param errorMessage - The error message to throw if validation fails.
136
+ */
137
+ export declare function validateSnapShasum(manifest: SnapManifest, sourceCode: string, errorMessage?: string): void;
138
+ /**
139
+ * Extracts the snap prefix from a snap ID.
140
+ *
141
+ * @param snapId - The snap ID to extract the prefix from.
142
+ * @returns The snap prefix from a snap id, e.g. `npm:`.
143
+ */
144
+ export declare function getSnapPrefix(snapId: string): SnapIdPrefixes;
145
+ /**
146
+ * Computes the permission name of a snap from its snap ID.
147
+ *
148
+ * @param snapId - The snap ID.
149
+ * @returns The permission name corresponding to the given snap ID.
150
+ */
151
+ export declare function getSnapPermissionName(snapId: string): string;
152
+ /**
153
+ * Asserts the provided object is a snapId with a supported prefix.
154
+ *
155
+ * @param snapId - The object to validate.
156
+ * @throws {@link Error}. If the validation fails.
157
+ */
158
+ export declare function validateSnapId(snapId: unknown): asserts snapId is ValidatedSnapId;
159
+ /**
160
+ * Typeguard to ensure a chainId follows the CAIP-2 standard.
161
+ *
162
+ * @param chainId - The chainId being tested.
163
+ * @returns `true` if the value is a valid CAIP chain id, and `false` otherwise.
164
+ */
165
+ export declare function isCaipChainId(chainId: unknown): chainId is string;
package/dist/snaps.js ADDED
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isCaipChainId = exports.validateSnapId = exports.getSnapPermissionName = exports.getSnapPrefix = exports.validateSnapShasum = exports.getSnapSourceShasum = exports.ProgrammaticallyFixableSnapError = exports.SnapStatusEvents = exports.SnapStatus = exports.PROPOSED_NAME_REGEX = exports.SNAP_PREFIX_REGEX = exports.SNAP_PREFIX = exports.LOCALHOST_HOSTNAMES = void 0;
4
+ const sha256_1 = require("@noble/hashes/sha256");
5
+ const base_1 = require("@scure/base");
6
+ const types_1 = require("./types");
7
+ exports.LOCALHOST_HOSTNAMES = new Set(['localhost', '127.0.0.1', '::1']);
8
+ exports.SNAP_PREFIX = 'wallet_snap_';
9
+ exports.SNAP_PREFIX_REGEX = new RegExp(`^${exports.SNAP_PREFIX}`, 'u');
10
+ // This RegEx matches valid npm package names (with some exceptions) and space-
11
+ // separated alphanumerical words, optionally with dashes and underscores.
12
+ // The RegEx consists of two parts. The first part matches space-separated
13
+ // words. It is based on the following Stackoverflow answer:
14
+ // https://stackoverflow.com/a/34974982
15
+ // The second part, after the pipe operator, is the same RegEx used for the
16
+ // `name` field of the official package.json JSON Schema, except that we allow
17
+ // mixed-case letters. It was originally copied from:
18
+ // https://github.com/SchemaStore/schemastore/blob/81a16897c1dabfd98c72242a5fd62eb080ff76d8/src/schemas/json/package.json#L132-L138
19
+ exports.PROPOSED_NAME_REGEX = /^(?:[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;
20
+ var SnapStatus;
21
+ (function (SnapStatus) {
22
+ SnapStatus["Installing"] = "installing";
23
+ SnapStatus["Updating"] = "updating";
24
+ SnapStatus["Running"] = "running";
25
+ SnapStatus["Stopped"] = "stopped";
26
+ SnapStatus["Crashed"] = "crashed";
27
+ })(SnapStatus = exports.SnapStatus || (exports.SnapStatus = {}));
28
+ var SnapStatusEvents;
29
+ (function (SnapStatusEvents) {
30
+ SnapStatusEvents["Start"] = "START";
31
+ SnapStatusEvents["Stop"] = "STOP";
32
+ SnapStatusEvents["Crash"] = "CRASH";
33
+ SnapStatusEvents["Update"] = "UPDATE";
34
+ })(SnapStatusEvents = exports.SnapStatusEvents || (exports.SnapStatusEvents = {}));
35
+ /**
36
+ * An error indicating that a Snap validation failure is programmatically
37
+ * fixable during development.
38
+ */
39
+ class ProgrammaticallyFixableSnapError extends Error {
40
+ constructor(message, reason) {
41
+ super(message);
42
+ this.reason = reason;
43
+ }
44
+ }
45
+ exports.ProgrammaticallyFixableSnapError = ProgrammaticallyFixableSnapError;
46
+ /**
47
+ * Calculates the Base64-encoded SHA-256 digest of a Snap source code string.
48
+ *
49
+ * @param sourceCode - The UTF-8 string source code of a Snap.
50
+ * @returns The Base64-encoded SHA-256 digest of the source code.
51
+ */
52
+ function getSnapSourceShasum(sourceCode) {
53
+ return base_1.base64.encode((0, sha256_1.sha256)(sourceCode));
54
+ }
55
+ exports.getSnapSourceShasum = getSnapSourceShasum;
56
+ /**
57
+ * Checks whether the `source.shasum` property of a Snap manifest matches the
58
+ * shasum of a snap source code string.
59
+ *
60
+ * @param manifest - The manifest whose shasum to validate.
61
+ * @param sourceCode - The source code of the snap.
62
+ * @param errorMessage - The error message to throw if validation fails.
63
+ */
64
+ function validateSnapShasum(manifest, sourceCode, errorMessage = 'Invalid Snap manifest: manifest shasum does not match computed shasum.') {
65
+ if (manifest.source.shasum !== getSnapSourceShasum(sourceCode)) {
66
+ throw new ProgrammaticallyFixableSnapError(errorMessage, types_1.SnapValidationFailureReason.ShasumMismatch);
67
+ }
68
+ }
69
+ exports.validateSnapShasum = validateSnapShasum;
70
+ /**
71
+ * Extracts the snap prefix from a snap ID.
72
+ *
73
+ * @param snapId - The snap ID to extract the prefix from.
74
+ * @returns The snap prefix from a snap id, e.g. `npm:`.
75
+ */
76
+ function getSnapPrefix(snapId) {
77
+ const prefix = Object.values(types_1.SnapIdPrefixes).find((p) => snapId.startsWith(p));
78
+ if (prefix !== undefined) {
79
+ return prefix;
80
+ }
81
+ throw new Error(`Invalid or no prefix found for "${snapId}"`);
82
+ }
83
+ exports.getSnapPrefix = getSnapPrefix;
84
+ /**
85
+ * Computes the permission name of a snap from its snap ID.
86
+ *
87
+ * @param snapId - The snap ID.
88
+ * @returns The permission name corresponding to the given snap ID.
89
+ */
90
+ function getSnapPermissionName(snapId) {
91
+ return exports.SNAP_PREFIX + snapId;
92
+ }
93
+ exports.getSnapPermissionName = getSnapPermissionName;
94
+ /**
95
+ * Asserts the provided object is a snapId with a supported prefix.
96
+ *
97
+ * @param snapId - The object to validate.
98
+ * @throws {@link Error}. If the validation fails.
99
+ */
100
+ function validateSnapId(snapId) {
101
+ if (!snapId || typeof snapId !== 'string') {
102
+ throw new Error(`Invalid snap id. Not a string. Received "${snapId}"`);
103
+ }
104
+ for (const prefix of Object.values(types_1.SnapIdPrefixes)) {
105
+ if (snapId.startsWith(prefix) && snapId.replace(prefix, '').length > 0) {
106
+ return;
107
+ }
108
+ }
109
+ throw new Error(`Invalid snap id. Unknown prefix. Received: "${snapId}"`);
110
+ }
111
+ exports.validateSnapId = validateSnapId;
112
+ /**
113
+ * Typeguard to ensure a chainId follows the CAIP-2 standard.
114
+ *
115
+ * @param chainId - The chainId being tested.
116
+ * @returns `true` if the value is a valid CAIP chain id, and `false` otherwise.
117
+ */
118
+ function isCaipChainId(chainId) {
119
+ return (typeof chainId === 'string' &&
120
+ /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-a-zA-Z0-9]{1,32})$/u.test(chainId));
121
+ }
122
+ exports.isCaipChainId = isCaipChainId;
123
+ //# sourceMappingURL=snaps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snaps.js","sourceRoot":"","sources":["../src/snaps.ts"],"names":[],"mappings":";;;AACA,iDAA8C;AAC9C,sCAAqC;AAGrC,mCAA8E;AAEjE,QAAA,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;AACjE,QAAA,WAAW,GAAG,cAAc,CAAC;AAE7B,QAAA,iBAAiB,GAAG,IAAI,MAAM,CAAC,IAAI,mBAAW,EAAE,EAAE,GAAG,CAAC,CAAC;AAEpE,+EAA+E;AAC/E,0EAA0E;AAC1E,0EAA0E;AAC1E,4DAA4D;AAC5D,uCAAuC;AACvC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EAAE,EAAE,CACtD,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CACrB,CAAC;IACF,IAAI,MAAM,KAAK,SAAS,EAAE;QACxB,OAAO,MAAM,CAAC;KACf;IACD,MAAM,IAAI,KAAK,CAAC,mCAAmC,MAAM,GAAG,CAAC,CAAC;AAChE,CAAC;AARD,sCAQC;AAED;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,MAAc;IAClD,OAAO,mBAAW,GAAG,MAAM,CAAC;AAC9B,CAAC;AAFD,sDAEC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAC5B,MAAe;IAEf,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QACzC,MAAM,IAAI,KAAK,CAAC,4CAA4C,MAAM,GAAG,CAAC,CAAC;KACxE;IAED,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,sBAAc,CAAC,EAAE;QAClD,IAAI,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;YACtE,OAAO;SACR;KACF;IAED,MAAM,IAAI,KAAK,CAAC,+CAA+C,MAAM,GAAG,CAAC,CAAC;AAC5E,CAAC;AAdD,wCAcC;AAED;;;;;GAKG;AACH,SAAgB,aAAa,CAAC,OAAgB;IAC5C,OAAO,CACL,OAAO,OAAO,KAAK,QAAQ;QAC3B,kEAAkE,CAAC,IAAI,CACrE,OAAO,CACR,CACF,CAAC;AACJ,CAAC;AAPD,sCAOC","sourcesContent":["import { Json } from '@metamask/utils';\nimport { sha256 } from '@noble/hashes/sha256';\nimport { base64 } from '@scure/base';\nimport { SerializedEthereumRpcError } from 'eth-rpc-errors/dist/classes';\nimport { SnapManifest, SnapPermissions } from './manifest/validation';\nimport { SnapId, SnapIdPrefixes, SnapValidationFailureReason } from './types';\n\nexport const LOCALHOST_HOSTNAMES = new Set(['localhost', '127.0.0.1', '::1']);\nexport const SNAP_PREFIX = 'wallet_snap_';\n\nexport const SNAP_PREFIX_REGEX = new RegExp(`^${SNAP_PREFIX}`, 'u');\n\n// This RegEx matches valid npm package names (with some exceptions) and space-\n// separated alphanumerical words, optionally with dashes and underscores.\n// The RegEx consists of two parts. The first part matches space-separated\n// words. It is based on the following Stackoverflow answer:\n// https://stackoverflow.com/a/34974982\n// The second part, after the pipe operator, is the same RegEx used for the\n// `name` field of the official package.json JSON Schema, except that we allow\n// mixed-case letters. It was originally copied from:\n// https://github.com/SchemaStore/schemastore/blob/81a16897c1dabfd98c72242a5fd62eb080ff76d8/src/schemas/json/package.json#L132-L138\nexport const PROPOSED_NAME_REGEX =\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/**\n * wallet_enable / wallet_installSnaps permission typing.\n *\n * @deprecated This type is confusing and not descriptive, people confused it with typing initialPermissions, remove when removing wallet_enable.\n */\nexport type RequestedSnapPermissions = {\n [permission: string]: Record<string, Json>;\n};\n\nexport type BlockedSnapInfo = { infoUrl?: string; reason?: string };\n\nexport enum SnapStatus {\n Installing = 'installing',\n Updating = 'updating',\n Running = 'running',\n Stopped = 'stopped',\n Crashed = 'crashed',\n}\n\nexport enum SnapStatusEvents {\n Start = 'START',\n Stop = 'STOP',\n Crash = 'CRASH',\n Update = 'UPDATE',\n}\n\nexport type StatusContext = { snapId: string };\nexport type StatusEvents = { type: SnapStatusEvents };\nexport type StatusStates = {\n value: SnapStatus;\n context: StatusContext;\n};\nexport type Status = StatusStates['value'];\n\nexport type VersionHistory = {\n origin: string;\n version: string;\n // Unix timestamp\n date: number;\n};\n\nexport type PersistedSnap = Snap & {\n /**\n * The source code of the Snap.\n */\n sourceCode: string;\n};\n\n/**\n * A Snap as it exists in {@link SnapController} state.\n */\nexport type Snap = {\n /**\n * Whether the Snap is enabled, which determines if it can be started.\n */\n enabled: boolean;\n\n /**\n * The ID of the Snap.\n */\n id: SnapId;\n\n /**\n * The initial permissions of the Snap, which will be requested when it is\n * installed.\n */\n initialPermissions: SnapPermissions;\n\n /**\n * The Snap's manifest file.\n */\n manifest: SnapManifest;\n\n /**\n * Whether the Snap is blocked.\n */\n blocked: boolean;\n\n /**\n * Information detailing why the snap is blocked.\n */\n blockInformation?: BlockedSnapInfo;\n\n /**\n * The name of the permission used to invoke the Snap.\n */\n permissionName: string;\n\n /**\n * The current status of the Snap, e.g. whether it's running or stopped.\n */\n status: Status;\n\n /**\n * The version of the Snap.\n */\n version: string;\n\n /**\n * The version history of the Snap.\n * Can be used to derive when the Snap was installed, when it was updated to a certain version and who requested the change.\n */\n versionHistory: VersionHistory[];\n};\n\nexport type TruncatedSnapFields =\n | 'id'\n | 'initialPermissions'\n | 'permissionName'\n | 'version'\n | 'enabled'\n | 'blocked';\n\n/**\n * A {@link Snap} object with the fields that are relevant to an external\n * caller.\n */\nexport type TruncatedSnap = Pick<Snap, TruncatedSnapFields>;\n\nexport type ProcessSnapResult =\n | TruncatedSnap\n | { error: SerializedEthereumRpcError };\n\nexport type InstallSnapsResult = Record<SnapId, ProcessSnapResult>;\n\n/**\n * An error indicating that a Snap validation failure is programmatically\n * fixable during development.\n */\nexport class ProgrammaticallyFixableSnapError extends Error {\n reason: SnapValidationFailureReason;\n\n constructor(message: string, reason: SnapValidationFailureReason) {\n super(message);\n this.reason = reason;\n }\n}\n\n/**\n * Calculates the Base64-encoded SHA-256 digest of a Snap source code string.\n *\n * @param sourceCode - The UTF-8 string source code of a Snap.\n * @returns The Base64-encoded SHA-256 digest of the source code.\n */\nexport function getSnapSourceShasum(sourceCode: string): string {\n return base64.encode(sha256(sourceCode));\n}\n\nexport type ValidatedSnapId = `local:${string}` | `npm:${string}`;\n\n/**\n * Checks whether the `source.shasum` property of a Snap manifest matches the\n * shasum of a snap source code string.\n *\n * @param manifest - The manifest whose shasum to validate.\n * @param sourceCode - The source code of the snap.\n * @param errorMessage - The error message to throw if validation fails.\n */\nexport function validateSnapShasum(\n manifest: SnapManifest,\n sourceCode: string,\n errorMessage = 'Invalid Snap manifest: manifest shasum does not match computed shasum.',\n): void {\n if (manifest.source.shasum !== getSnapSourceShasum(sourceCode)) {\n throw new ProgrammaticallyFixableSnapError(\n errorMessage,\n SnapValidationFailureReason.ShasumMismatch,\n );\n }\n}\n\n/**\n * Extracts the snap prefix from a snap ID.\n *\n * @param snapId - The snap ID to extract the prefix from.\n * @returns The snap prefix from a snap id, e.g. `npm:`.\n */\nexport function getSnapPrefix(snapId: string): SnapIdPrefixes {\n const prefix = Object.values(SnapIdPrefixes).find((p) =>\n snapId.startsWith(p),\n );\n if (prefix !== undefined) {\n return prefix;\n }\n throw new Error(`Invalid or no prefix found for \"${snapId}\"`);\n}\n\n/**\n * Computes the permission name of a snap from its snap ID.\n *\n * @param snapId - The snap ID.\n * @returns The permission name corresponding to the given snap ID.\n */\nexport function getSnapPermissionName(snapId: string): string {\n return SNAP_PREFIX + snapId;\n}\n\n/**\n * Asserts the provided object is a snapId with a supported prefix.\n *\n * @param snapId - The object to validate.\n * @throws {@link Error}. If the validation fails.\n */\nexport function validateSnapId(\n snapId: unknown,\n): asserts snapId is ValidatedSnapId {\n if (!snapId || typeof snapId !== 'string') {\n throw new Error(`Invalid snap id. Not a string. Received \"${snapId}\"`);\n }\n\n for (const prefix of Object.values(SnapIdPrefixes)) {\n if (snapId.startsWith(prefix) && snapId.replace(prefix, '').length > 0) {\n return;\n }\n }\n\n throw new Error(`Invalid snap id. Unknown prefix. Received: \"${snapId}\"`);\n}\n\n/**\n * Typeguard to ensure a chainId follows the CAIP-2 standard.\n *\n * @param chainId - The chainId being tested.\n * @returns `true` if the value is a valid CAIP chain id, and `false` otherwise.\n */\nexport function isCaipChainId(chainId: unknown): chainId is string {\n return (\n typeof chainId === 'string' &&\n /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-a-zA-Z0-9]{1,32})$/u.test(\n chainId,\n )\n );\n}\n"]}
@@ -0,0 +1,107 @@
1
+ import { SnapFunctionExports, SnapKeyring as Keyring } from '@metamask/snaps-types';
2
+ import { Json } from '@metamask/utils';
3
+ import { Infer } from 'superstruct';
4
+ import { SnapManifest } from './manifest/validation';
5
+ export declare enum NpmSnapFileNames {
6
+ PackageJson = "package.json",
7
+ Manifest = "snap.manifest.json"
8
+ }
9
+ /**
10
+ * A struct for validating a version string.
11
+ */
12
+ export declare const VersionStruct: import("superstruct").Struct<string, null>;
13
+ export declare const NameStruct: import("superstruct").Struct<string, null>;
14
+ export declare const NpmSnapPackageJsonStruct: import("superstruct").Struct<{
15
+ name: string;
16
+ version: string;
17
+ repository?: {
18
+ type: string;
19
+ url: string;
20
+ } | undefined;
21
+ main?: string | undefined;
22
+ }, {
23
+ version: import("superstruct").Struct<string, null>;
24
+ name: import("superstruct").Struct<string, null>;
25
+ main: import("superstruct").Struct<string | undefined, null>;
26
+ repository: import("superstruct").Struct<{
27
+ type: string;
28
+ url: string;
29
+ } | undefined, {
30
+ type: import("superstruct").Struct<string, null>;
31
+ url: import("superstruct").Struct<string, null>;
32
+ }>;
33
+ }>;
34
+ export declare type NpmSnapPackageJson = Infer<typeof NpmSnapPackageJsonStruct> & Record<string, any>;
35
+ /**
36
+ * Check if the given value is a valid {@link NpmSnapPackageJson} object.
37
+ *
38
+ * @param value - The value to check.
39
+ * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.
40
+ */
41
+ export declare function isNpmSnapPackageJson(value: unknown): value is NpmSnapPackageJson;
42
+ /**
43
+ * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.
44
+ *
45
+ * @param value - The value to check.
46
+ * @throws If the value is not a valid {@link NpmSnapPackageJson} object.
47
+ */
48
+ export declare function assertIsNpmSnapPackageJson(value: unknown): asserts value is NpmSnapPackageJson;
49
+ /**
50
+ * An object for storing parsed but unvalidated Snap file contents.
51
+ */
52
+ export declare type UnvalidatedSnapFiles = {
53
+ manifest?: Json;
54
+ packageJson?: Json;
55
+ sourceCode?: string;
56
+ svgIcon?: string;
57
+ };
58
+ /**
59
+ * An object for storing the contents of Snap files that have passed JSON
60
+ * Schema validation, or are non-empty if they are strings.
61
+ */
62
+ export declare type SnapFiles = {
63
+ manifest: SnapManifest;
64
+ packageJson: NpmSnapPackageJson;
65
+ sourceCode: string;
66
+ svgIcon?: string;
67
+ };
68
+ /**
69
+ * The possible prefixes for snap ids.
70
+ */
71
+ export declare enum SnapIdPrefixes {
72
+ npm = "npm:",
73
+ local = "local:"
74
+ }
75
+ export declare type SnapId = string;
76
+ /**
77
+ * Snap validation failure reason codes that are programmatically fixable
78
+ * if validation occurs during development.
79
+ */
80
+ export declare enum SnapValidationFailureReason {
81
+ NameMismatch = "\"name\" field mismatch",
82
+ VersionMismatch = "\"version\" field mismatch",
83
+ RepositoryMismatch = "\"repository\" field mismatch",
84
+ ShasumMismatch = "\"shasum\" field mismatch"
85
+ }
86
+ export declare enum SNAP_STREAM_NAMES {
87
+ JSON_RPC = "jsonRpc",
88
+ COMMAND = "command"
89
+ }
90
+ export declare enum HandlerType {
91
+ OnRpcRequest = "onRpcRequest",
92
+ OnTransaction = "onTransaction",
93
+ SnapKeyring = "keyring",
94
+ OnCronjob = "onCronjob"
95
+ }
96
+ export declare const SNAP_EXPORT_NAMES: HandlerType[];
97
+ export declare type SnapRpcHookArgs = {
98
+ origin: string;
99
+ handler: HandlerType;
100
+ request: Record<string, unknown>;
101
+ };
102
+ export declare type SnapRpcHook = (options: SnapRpcHookArgs) => Promise<unknown>;
103
+ declare type ObjectParameters<Type extends Record<string, (...args: any[]) => unknown>> = Parameters<Type[keyof Type]>;
104
+ declare type KeyringParameter<Fn> = Fn extends (...args: any[]) => unknown ? Parameters<Fn> : never;
105
+ declare type KeyringParameters = KeyringParameter<Keyring[keyof Keyring]>;
106
+ export declare type SnapExportsParameters = ObjectParameters<SnapFunctionExports> | KeyringParameters;
107
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SNAP_EXPORT_NAMES = exports.HandlerType = exports.SNAP_STREAM_NAMES = exports.SnapValidationFailureReason = exports.SnapIdPrefixes = exports.assertIsNpmSnapPackageJson = exports.isNpmSnapPackageJson = exports.NpmSnapPackageJsonStruct = exports.NameStruct = exports.VersionStruct = exports.NpmSnapFileNames = void 0;
4
+ const utils_1 = require("@metamask/utils");
5
+ const semver_1 = require("semver");
6
+ const superstruct_1 = require("superstruct");
7
+ var NpmSnapFileNames;
8
+ (function (NpmSnapFileNames) {
9
+ NpmSnapFileNames["PackageJson"] = "package.json";
10
+ NpmSnapFileNames["Manifest"] = "snap.manifest.json";
11
+ })(NpmSnapFileNames = exports.NpmSnapFileNames || (exports.NpmSnapFileNames = {}));
12
+ /**
13
+ * A struct for validating a version string.
14
+ */
15
+ exports.VersionStruct = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'Version', (value) => {
16
+ return (0, semver_1.valid)(value) !== null;
17
+ });
18
+ exports.NameStruct = (0, superstruct_1.size)((0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/u), 1, 214);
19
+ // Note we use `type` instead of `object` here, because the latter does not
20
+ // allow unknown keys.
21
+ exports.NpmSnapPackageJsonStruct = (0, superstruct_1.type)({
22
+ version: exports.VersionStruct,
23
+ name: exports.NameStruct,
24
+ main: (0, superstruct_1.optional)((0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity)),
25
+ repository: (0, superstruct_1.optional)((0, superstruct_1.object)({
26
+ type: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
27
+ url: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
28
+ })),
29
+ });
30
+ /**
31
+ * Check if the given value is a valid {@link NpmSnapPackageJson} object.
32
+ *
33
+ * @param value - The value to check.
34
+ * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.
35
+ */
36
+ function isNpmSnapPackageJson(value) {
37
+ return (0, superstruct_1.is)(value, exports.NpmSnapPackageJsonStruct);
38
+ }
39
+ exports.isNpmSnapPackageJson = isNpmSnapPackageJson;
40
+ /**
41
+ * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.
42
+ *
43
+ * @param value - The value to check.
44
+ * @throws If the value is not a valid {@link NpmSnapPackageJson} object.
45
+ */
46
+ function assertIsNpmSnapPackageJson(value) {
47
+ (0, utils_1.assertStruct)(value, exports.NpmSnapPackageJsonStruct, `"${NpmSnapFileNames.PackageJson}" is invalid`);
48
+ }
49
+ exports.assertIsNpmSnapPackageJson = assertIsNpmSnapPackageJson;
50
+ /**
51
+ * The possible prefixes for snap ids.
52
+ */
53
+ var SnapIdPrefixes;
54
+ (function (SnapIdPrefixes) {
55
+ SnapIdPrefixes["npm"] = "npm:";
56
+ SnapIdPrefixes["local"] = "local:";
57
+ })(SnapIdPrefixes = exports.SnapIdPrefixes || (exports.SnapIdPrefixes = {}));
58
+ /**
59
+ * Snap validation failure reason codes that are programmatically fixable
60
+ * if validation occurs during development.
61
+ */
62
+ var SnapValidationFailureReason;
63
+ (function (SnapValidationFailureReason) {
64
+ SnapValidationFailureReason["NameMismatch"] = "\"name\" field mismatch";
65
+ SnapValidationFailureReason["VersionMismatch"] = "\"version\" field mismatch";
66
+ SnapValidationFailureReason["RepositoryMismatch"] = "\"repository\" field mismatch";
67
+ SnapValidationFailureReason["ShasumMismatch"] = "\"shasum\" field mismatch";
68
+ })(SnapValidationFailureReason = exports.SnapValidationFailureReason || (exports.SnapValidationFailureReason = {}));
69
+ var SNAP_STREAM_NAMES;
70
+ (function (SNAP_STREAM_NAMES) {
71
+ SNAP_STREAM_NAMES["JSON_RPC"] = "jsonRpc";
72
+ SNAP_STREAM_NAMES["COMMAND"] = "command";
73
+ })(SNAP_STREAM_NAMES = exports.SNAP_STREAM_NAMES || (exports.SNAP_STREAM_NAMES = {}));
74
+ var HandlerType;
75
+ (function (HandlerType) {
76
+ HandlerType["OnRpcRequest"] = "onRpcRequest";
77
+ HandlerType["OnTransaction"] = "onTransaction";
78
+ HandlerType["SnapKeyring"] = "keyring";
79
+ HandlerType["OnCronjob"] = "onCronjob";
80
+ })(HandlerType = exports.HandlerType || (exports.HandlerType = {}));
81
+ exports.SNAP_EXPORT_NAMES = Object.values(HandlerType);
82
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAIA,2CAAqD;AACrD,mCAA8C;AAC9C,6CAUqB;AAGrB,IAAY,gBAGX;AAHD,WAAY,gBAAgB;IAC1B,gDAA4B,CAAA;IAC5B,mDAA+B,CAAA;AACjC,CAAC,EAHW,gBAAgB,GAAhB,wBAAgB,KAAhB,wBAAgB,QAG3B;AAED;;GAEG;AACU,QAAA,aAAa,GAAG,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,EAAE;IACjE,OAAO,IAAA,cAAW,EAAC,KAAK,CAAC,KAAK,IAAI,CAAC;AACrC,CAAC,CAAC,CAAC;AAEU,QAAA,UAAU,GAAG,IAAA,kBAAI,EAC5B,IAAA,qBAAO,EACL,IAAA,oBAAM,GAAE,EACR,6DAA6D,CAC9D,EACD,CAAC,EACD,GAAG,CACJ,CAAC;AAEF,2EAA2E;AAC3E,sBAAsB;AACT,QAAA,wBAAwB,GAAG,IAAA,kBAAI,EAAC;IAC3C,OAAO,EAAE,qBAAa;IACtB,IAAI,EAAE,kBAAU;IAChB,IAAI,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC3C,UAAU,EAAE,IAAA,sBAAQ,EAClB,IAAA,oBAAM,EAAC;QACL,IAAI,EAAE,IAAA,kBAAI,EAAC,I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ctionExports,\n SnapKeyring as Keyring,\n} from '@metamask/snaps-types';\nimport { assertStruct, Json } from '@metamask/utils';\nimport { valid as validSemver } from 'semver';\nimport {\n Infer,\n is,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n type,\n} from 'superstruct';\nimport { SnapManifest } from './manifest/validation';\n\nexport enum NpmSnapFileNames {\n PackageJson = 'package.json',\n Manifest = 'snap.manifest.json',\n}\n\n/**\n * A struct for validating a version string.\n */\nexport const VersionStruct = refine(string(), 'Version', (value) => {\n return validSemver(value) !== null;\n});\n\nexport const NameStruct = size(\n pattern(\n string(),\n /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/u,\n ),\n 1,\n 214,\n);\n\n// Note we use `type` instead of `object` here, because the latter does not\n// allow unknown keys.\nexport const NpmSnapPackageJsonStruct = type({\n version: VersionStruct,\n name: NameStruct,\n main: optional(size(string(), 1, Infinity)),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n});\n\nexport type NpmSnapPackageJson = Infer<typeof NpmSnapPackageJsonStruct> &\n Record<string, any>;\n\n/**\n * Check if the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.\n */\nexport function isNpmSnapPackageJson(\n value: unknown,\n): value is NpmSnapPackageJson {\n return is(value, NpmSnapPackageJsonStruct);\n}\n\n/**\n * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link NpmSnapPackageJson} object.\n */\nexport function assertIsNpmSnapPackageJson(\n value: unknown,\n): asserts value is NpmSnapPackageJson {\n assertStruct(\n value,\n NpmSnapPackageJsonStruct,\n `\"${NpmSnapFileNames.PackageJson}\" is invalid`,\n );\n}\n\n/**\n * An object for storing parsed but unvalidated Snap file contents.\n */\nexport type UnvalidatedSnapFiles = {\n manifest?: Json;\n packageJson?: Json;\n sourceCode?: string;\n svgIcon?: string;\n};\n\n/**\n * An object for storing the contents of Snap files that have passed JSON\n * Schema validation, or are non-empty if they are strings.\n */\nexport type SnapFiles = {\n manifest: SnapManifest;\n packageJson: NpmSnapPackageJson;\n sourceCode: string;\n svgIcon?: string;\n};\n\n/**\n * The possible prefixes for snap ids.\n */\nexport enum SnapIdPrefixes {\n npm = 'npm:',\n local = 'local:',\n}\n\nexport type SnapId = string;\n\n/**\n * Snap validation failure reason codes that are programmatically fixable\n * if validation occurs during development.\n */\nexport enum SnapValidationFailureReason {\n NameMismatch = '\"name\" field mismatch',\n VersionMismatch = '\"version\" field mismatch',\n RepositoryMismatch = '\"repository\" field mismatch',\n ShasumMismatch = '\"shasum\" field mismatch',\n}\n\nexport enum SNAP_STREAM_NAMES {\n JSON_RPC = 'jsonRpc',\n COMMAND = 'command',\n}\n\nexport enum HandlerType {\n OnRpcRequest = 'onRpcRequest',\n OnTransaction = 'onTransaction',\n SnapKeyring = 'keyring',\n OnCronjob = 'onCronjob',\n}\n\nexport const SNAP_EXPORT_NAMES = Object.values(HandlerType);\n\nexport type SnapRpcHookArgs = {\n origin: string;\n handler: HandlerType;\n request: Record<string, unknown>;\n};\n\n// The snap is the callee\nexport type SnapRpcHook = (options: SnapRpcHookArgs) => Promise<unknown>;\n\ntype ObjectParameters<\n Type extends Record<string, (...args: any[]) => unknown>,\n> = Parameters<Type[keyof Type]>;\n\ntype KeyringParameter<Fn> = Fn extends (...args: any[]) => unknown\n ? Parameters<Fn>\n : never;\n\ntype KeyringParameters = KeyringParameter<Keyring[keyof Keyring]>;\n\nexport type SnapExportsParameters =\n | ObjectParameters<SnapFunctionExports>\n | KeyringParameters;\n"]}
package/dist/url.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Checks whether a URL is valid.
3
+ *
4
+ * @param maybeUrl - The string to check.
5
+ * @returns Whether the specified string is a valid URL.
6
+ */
7
+ export declare function isValidUrl(maybeUrl: string): maybeUrl is string;
package/dist/url.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidUrl = void 0;
4
+ /**
5
+ * Checks whether a URL is valid.
6
+ *
7
+ * @param maybeUrl - The string to check.
8
+ * @returns Whether the specified string is a valid URL.
9
+ */
10
+ function isValidUrl(maybeUrl) {
11
+ try {
12
+ return Boolean(new URL(maybeUrl));
13
+ }
14
+ catch (_error) {
15
+ return false;
16
+ }
17
+ }
18
+ exports.isValidUrl = isValidUrl;
19
+ //# sourceMappingURL=url.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url.js","sourceRoot":"","sources":["../src/url.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,UAAU,CAAC,QAAgB;IACzC,IAAI;QACF,OAAO,OAAO,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;KACnC;IAAC,OAAO,MAAM,EAAE;QACf,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAND,gCAMC","sourcesContent":["/**\n * Checks whether a URL is valid.\n *\n * @param maybeUrl - The string to check.\n * @returns Whether the specified string is a valid URL.\n */\nexport function isValidUrl(maybeUrl: string): maybeUrl is string {\n try {\n return Boolean(new URL(maybeUrl));\n } catch (_error) {\n return false;\n }\n}\n"]}
@@ -0,0 +1,44 @@
1
+ import { Json } from '@metamask/utils';
2
+ export declare const DEFAULT_REQUESTED_SNAP_VERSION = "*";
3
+ /**
4
+ * Checks whether a SemVer version is greater than another.
5
+ *
6
+ * @param version1 - The left-hand version.
7
+ * @param version2 - The right-hand version.
8
+ * @returns `version1 > version2`.
9
+ */
10
+ export declare function gtVersion(version1: string, version2: string): boolean;
11
+ /**
12
+ * Returns whether a SemVer version satisfies a SemVer range.
13
+ *
14
+ * @param version - The SemVer version to check.
15
+ * @param versionRange - The SemVer version range to check against.
16
+ * @returns Whether the version satisfied the version range.
17
+ */
18
+ export declare function satisfiesVersionRange(version: string, versionRange: string): boolean;
19
+ /**
20
+ * Return the highest version in the list that satisfies the range, or `null` if
21
+ * none of them do. For the satisfaction check, pre-release versions will only
22
+ * be checked if no satisfactory non-prerelease version is found first.
23
+ *
24
+ * @param versions - The list of version to check.
25
+ * @param versionRange - The SemVer version range to check against.
26
+ * @returns The highest version in the list that satisfies the range,
27
+ * or `null` if none of them do.
28
+ */
29
+ export declare function getTargetVersion(versions: string[], versionRange: string): string | null;
30
+ /**
31
+ * Parse a version received by some subject attempting to access a snap.
32
+ *
33
+ * @param version - The received version value.
34
+ * @returns `*` if the version is `undefined` or `latest", otherwise returns
35
+ * the specified version.
36
+ */
37
+ export declare function resolveVersion(version?: Json): Json;
38
+ /**
39
+ * Checks whether a SemVer version range is valid.
40
+ *
41
+ * @param versionRange - A potential version range.
42
+ * @returns `true` if the version range is valid, and `false` otherwise.
43
+ */
44
+ export declare function isValidSnapVersionRange(versionRange: unknown): versionRange is string;