@metamask/snaps-utils 3.0.0 → 3.1.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 (56) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/dist/cjs/auxiliary-files.js +40 -0
  3. package/dist/cjs/auxiliary-files.js.map +1 -0
  4. package/dist/cjs/errors.js +353 -3
  5. package/dist/cjs/errors.js.map +1 -1
  6. package/dist/cjs/index.browser.js +1 -0
  7. package/dist/cjs/index.browser.js.map +1 -1
  8. package/dist/cjs/index.executionenv.js +1 -0
  9. package/dist/cjs/index.executionenv.js.map +1 -1
  10. package/dist/cjs/index.js +1 -0
  11. package/dist/cjs/index.js.map +1 -1
  12. package/dist/cjs/manifest/manifest.js +25 -5
  13. package/dist/cjs/manifest/manifest.js.map +1 -1
  14. package/dist/cjs/manifest/validation.js +6 -1
  15. package/dist/cjs/manifest/validation.js.map +1 -1
  16. package/dist/cjs/npm.js +5 -3
  17. package/dist/cjs/npm.js.map +1 -1
  18. package/dist/cjs/snaps.js +3 -2
  19. package/dist/cjs/snaps.js.map +1 -1
  20. package/dist/cjs/types.js.map +1 -1
  21. package/dist/cjs/validation.js.map +1 -1
  22. package/dist/cjs/virtual-file/VirtualFile.js +6 -0
  23. package/dist/cjs/virtual-file/VirtualFile.js.map +1 -1
  24. package/dist/esm/auxiliary-files.js +28 -0
  25. package/dist/esm/auxiliary-files.js.map +1 -0
  26. package/dist/esm/errors.js +350 -1
  27. package/dist/esm/errors.js.map +1 -1
  28. package/dist/esm/index.browser.js +1 -0
  29. package/dist/esm/index.browser.js.map +1 -1
  30. package/dist/esm/index.executionenv.js +1 -0
  31. package/dist/esm/index.executionenv.js.map +1 -1
  32. package/dist/esm/index.js +1 -0
  33. package/dist/esm/index.js.map +1 -1
  34. package/dist/esm/manifest/manifest.js +30 -5
  35. package/dist/esm/manifest/manifest.js.map +1 -1
  36. package/dist/esm/manifest/validation.js +3 -1
  37. package/dist/esm/manifest/validation.js.map +1 -1
  38. package/dist/esm/npm.js +5 -3
  39. package/dist/esm/npm.js.map +1 -1
  40. package/dist/esm/snaps.js +3 -2
  41. package/dist/esm/snaps.js.map +1 -1
  42. package/dist/esm/types.js.map +1 -1
  43. package/dist/esm/validation.js.map +1 -1
  44. package/dist/esm/virtual-file/VirtualFile.js +7 -1
  45. package/dist/esm/virtual-file/VirtualFile.js.map +1 -1
  46. package/dist/types/auxiliary-files.d.ts +13 -0
  47. package/dist/types/errors.d.ts +182 -0
  48. package/dist/types/index.browser.d.ts +1 -0
  49. package/dist/types/index.d.ts +1 -0
  50. package/dist/types/index.executionenv.d.ts +1 -0
  51. package/dist/types/manifest/manifest.d.ts +11 -1
  52. package/dist/types/manifest/validation.d.ts +4 -0
  53. package/dist/types/snaps.d.ts +13 -6
  54. package/dist/types/types.d.ts +6 -0
  55. package/dist/types/validation.d.ts +2 -2
  56. package/package.json +17 -18
package/CHANGELOG.md CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.1.0]
10
+ ### Added
11
+ - Add static file API ([#1836](https://github.com/MetaMask/snaps/pull/1836), [#1858](https://github.com/MetaMask/snaps/pull/1858))
12
+
13
+ ### Changed
14
+ - Update multiple MetaMask dependencies ([#1841](https://github.com/MetaMask/snaps/pull/1841))
15
+ - Bump Babel packages from `^7.20.12` to `^7.23.2` ([#1862](https://github.com/MetaMask/snaps/pull/1862))
16
+
9
17
  ## [3.0.0]
10
18
  ### Added
11
19
  - Add keyring export and endowment ([#1787](https://github.com/MetaMask/snaps/pull/1787))
@@ -59,7 +67,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
59
67
  - The version of the package no longer needs to match the version of all other
60
68
  MetaMask Snaps packages.
61
69
 
62
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@3.0.0...HEAD
70
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@3.1.0...HEAD
71
+ [3.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@3.0.0...@metamask/snaps-utils@3.1.0
63
72
  [3.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@2.0.1...@metamask/snaps-utils@3.0.0
64
73
  [2.0.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@2.0.0...@metamask/snaps-utils@2.0.1
65
74
  [2.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@0.38.4-flask.1...@metamask/snaps-utils@2.0.0
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ AuxiliaryFileEncoding: function() {
13
+ return AuxiliaryFileEncoding;
14
+ },
15
+ encodeAuxiliaryFile: function() {
16
+ return encodeAuxiliaryFile;
17
+ }
18
+ });
19
+ const _utils = require("@metamask/utils");
20
+ const _base = require("@scure/base");
21
+ var AuxiliaryFileEncoding;
22
+ (function(AuxiliaryFileEncoding) {
23
+ AuxiliaryFileEncoding["Base64"] = 'base64';
24
+ AuxiliaryFileEncoding["Hex"] = 'hex';
25
+ AuxiliaryFileEncoding["Utf8"] = 'utf8';
26
+ })(AuxiliaryFileEncoding || (AuxiliaryFileEncoding = {}));
27
+ function encodeAuxiliaryFile(value, encoding) {
28
+ // Input is assumed to be the stored file in base64.
29
+ if (encoding === AuxiliaryFileEncoding.Base64) {
30
+ return value;
31
+ }
32
+ // TODO: Use @metamask/utils for this
33
+ const decoded = _base.base64.decode(value);
34
+ if (encoding === AuxiliaryFileEncoding.Utf8) {
35
+ return (0, _utils.bytesToString)(decoded);
36
+ }
37
+ return (0, _utils.bytesToHex)(decoded);
38
+ }
39
+
40
+ //# sourceMappingURL=auxiliary-files.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/auxiliary-files.ts"],"sourcesContent":["import { bytesToHex, bytesToString } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nexport enum AuxiliaryFileEncoding {\n Base64 = 'base64',\n Hex = 'hex',\n Utf8 = 'utf8',\n}\n\n/**\n * Re-encodes an auxiliary file if needed depending on the requested file encoding.\n *\n * @param value - The base64 value stored for the auxiliary file.\n * @param encoding - The chosen encoding.\n * @returns The file encoded in the requested encoding.\n */\nexport function encodeAuxiliaryFile(\n value: string,\n encoding: AuxiliaryFileEncoding,\n) {\n // Input is assumed to be the stored file in base64.\n if (encoding === AuxiliaryFileEncoding.Base64) {\n return value;\n }\n\n // TODO: Use @metamask/utils for this\n const decoded = base64.decode(value);\n if (encoding === AuxiliaryFileEncoding.Utf8) {\n return bytesToString(decoded);\n }\n\n return bytesToHex(decoded);\n}\n"],"names":["encodeAuxiliaryFile","AuxiliaryFileEncoding","Base64","Hex","Utf8","value","encoding","decoded","base64","decode","bytesToString","bytesToHex"],"mappings":";;;;;;;;;;;;;;IAgBgBA,mBAAmB;eAAnBA;;;uBAhB0B;sBACnB;IAEhB;UAAKC,qBAAqB;IAArBA,sBACVC,YAAS;IADCD,sBAEVE,SAAM;IAFIF,sBAGVG,UAAO;GAHGH,0BAAAA;AAaL,SAASD,oBACdK,KAAa,EACbC,QAA+B;IAE/B,oDAAoD;IACpD,IAAIA,aAAaL,sBAAsBC,MAAM,EAAE;QAC7C,OAAOG;IACT;IAEA,qCAAqC;IACrC,MAAME,UAAUC,YAAM,CAACC,MAAM,CAACJ;IAC9B,IAAIC,aAAaL,sBAAsBG,IAAI,EAAE;QAC3C,OAAOM,IAAAA,oBAAa,EAACH;IACvB;IAEA,OAAOI,IAAAA,iBAAU,EAACJ;AACpB"}
@@ -2,18 +2,368 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- Object.defineProperty(exports, "getErrorMessage", {
6
- enumerable: true,
7
- get: function() {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ getErrorMessage: function() {
8
13
  return getErrorMessage;
14
+ },
15
+ getErrorStack: function() {
16
+ return getErrorStack;
17
+ },
18
+ getErrorCode: function() {
19
+ return getErrorCode;
20
+ },
21
+ getErrorData: function() {
22
+ return getErrorData;
23
+ },
24
+ SNAP_ERROR_WRAPPER_CODE: function() {
25
+ return SNAP_ERROR_WRAPPER_CODE;
26
+ },
27
+ SNAP_ERROR_WRAPPER_MESSAGE: function() {
28
+ return SNAP_ERROR_WRAPPER_MESSAGE;
29
+ },
30
+ SNAP_ERROR_CODE: function() {
31
+ return SNAP_ERROR_CODE;
32
+ },
33
+ SNAP_ERROR_MESSAGE: function() {
34
+ return SNAP_ERROR_MESSAGE;
35
+ },
36
+ WrappedSnapError: function() {
37
+ return WrappedSnapError;
38
+ },
39
+ SnapError: function() {
40
+ return SnapError;
41
+ },
42
+ isSnapError: function() {
43
+ return isSnapError;
44
+ },
45
+ isSerializedSnapError: function() {
46
+ return isSerializedSnapError;
47
+ },
48
+ isWrappedSnapError: function() {
49
+ return isWrappedSnapError;
50
+ },
51
+ unwrapError: function() {
52
+ return unwrapError;
9
53
  }
10
54
  });
55
+ const _rpcerrors = require("@metamask/rpc-errors");
11
56
  const _utils = require("@metamask/utils");
57
+ function _check_private_redeclaration(obj, privateCollection) {
58
+ if (privateCollection.has(obj)) {
59
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
60
+ }
61
+ }
62
+ function _class_apply_descriptor_get(receiver, descriptor) {
63
+ if (descriptor.get) {
64
+ return descriptor.get.call(receiver);
65
+ }
66
+ return descriptor.value;
67
+ }
68
+ function _class_apply_descriptor_set(receiver, descriptor, value) {
69
+ if (descriptor.set) {
70
+ descriptor.set.call(receiver, value);
71
+ } else {
72
+ if (!descriptor.writable) {
73
+ throw new TypeError("attempted to set read only private field");
74
+ }
75
+ descriptor.value = value;
76
+ }
77
+ }
78
+ function _class_extract_field_descriptor(receiver, privateMap, action) {
79
+ if (!privateMap.has(receiver)) {
80
+ throw new TypeError("attempted to " + action + " private field on non-instance");
81
+ }
82
+ return privateMap.get(receiver);
83
+ }
84
+ function _class_private_field_get(receiver, privateMap) {
85
+ var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
86
+ return _class_apply_descriptor_get(receiver, descriptor);
87
+ }
88
+ function _class_private_field_init(obj, privateMap, value) {
89
+ _check_private_redeclaration(obj, privateMap);
90
+ privateMap.set(obj, value);
91
+ }
92
+ function _class_private_field_set(receiver, privateMap, value) {
93
+ var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
94
+ _class_apply_descriptor_set(receiver, descriptor, value);
95
+ return value;
96
+ }
12
97
  function getErrorMessage(error) {
13
98
  if ((0, _utils.isObject)(error) && (0, _utils.hasProperty)(error, 'message') && typeof error.message === 'string') {
14
99
  return error.message;
15
100
  }
16
101
  return String(error);
17
102
  }
103
+ function getErrorStack(error) {
104
+ if ((0, _utils.isObject)(error) && (0, _utils.hasProperty)(error, 'stack') && typeof error.stack === 'string') {
105
+ return error.stack;
106
+ }
107
+ return undefined;
108
+ }
109
+ function getErrorCode(error) {
110
+ if ((0, _utils.isObject)(error) && (0, _utils.hasProperty)(error, 'code') && typeof error.code === 'number' && Number.isInteger(error.code)) {
111
+ return error.code;
112
+ }
113
+ return _rpcerrors.errorCodes.rpc.internal;
114
+ }
115
+ function getErrorData(error) {
116
+ if ((0, _utils.isObject)(error) && (0, _utils.hasProperty)(error, 'data') && typeof error.data === 'object' && error.data !== null && (0, _utils.isValidJson)(error.data) && !Array.isArray(error.data)) {
117
+ return error.data;
118
+ }
119
+ return {};
120
+ }
121
+ const SNAP_ERROR_WRAPPER_CODE = -31001;
122
+ const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
123
+ const SNAP_ERROR_CODE = -31002;
124
+ const SNAP_ERROR_MESSAGE = 'Snap Error';
125
+ var _error = /*#__PURE__*/ new WeakMap(), _message = /*#__PURE__*/ new WeakMap(), _stack = /*#__PURE__*/ new WeakMap();
126
+ class WrappedSnapError extends Error {
127
+ /**
128
+ * The error name.
129
+ *
130
+ * @returns The error name.
131
+ */ get name() {
132
+ return 'WrappedSnapError';
133
+ }
134
+ /**
135
+ * The error message.
136
+ *
137
+ * @returns The error message.
138
+ */ get message() {
139
+ return _class_private_field_get(this, _message);
140
+ }
141
+ /**
142
+ * The error stack.
143
+ *
144
+ * @returns The error stack.
145
+ */ get stack() {
146
+ return _class_private_field_get(this, _stack);
147
+ }
148
+ /**
149
+ * Convert the error to a JSON object.
150
+ *
151
+ * @returns The JSON object.
152
+ */ toJSON() {
153
+ const cause = isSnapError(_class_private_field_get(this, _error)) ? _class_private_field_get(this, _error).serialize() : (0, _rpcerrors.serializeCause)(_class_private_field_get(this, _error));
154
+ return {
155
+ code: SNAP_ERROR_WRAPPER_CODE,
156
+ message: SNAP_ERROR_WRAPPER_MESSAGE,
157
+ data: {
158
+ cause
159
+ }
160
+ };
161
+ }
162
+ /**
163
+ * Serialize the error to a JSON object. This is called by
164
+ * `@metamask/rpc-errors` when serializing the error.
165
+ *
166
+ * @returns The JSON object.
167
+ */ serialize() {
168
+ return this.toJSON();
169
+ }
170
+ /**
171
+ * Create a new `WrappedSnapError`.
172
+ *
173
+ * @param error - The error to create the `WrappedSnapError` from.
174
+ */ constructor(error){
175
+ const message = getErrorMessage(error);
176
+ super(message);
177
+ _class_private_field_init(this, _error, {
178
+ writable: true,
179
+ value: void 0
180
+ });
181
+ _class_private_field_init(this, _message, {
182
+ writable: true,
183
+ value: void 0
184
+ });
185
+ _class_private_field_init(this, _stack, {
186
+ writable: true,
187
+ value: void 0
188
+ });
189
+ _class_private_field_set(this, _error, error);
190
+ _class_private_field_set(this, _message, message);
191
+ _class_private_field_set(this, _stack, getErrorStack(error));
192
+ }
193
+ }
194
+ var _code = /*#__PURE__*/ new WeakMap(), _message1 = /*#__PURE__*/ new WeakMap(), _data = /*#__PURE__*/ new WeakMap(), _stack1 = /*#__PURE__*/ new WeakMap();
195
+ class SnapError extends Error {
196
+ /**
197
+ * The error name.
198
+ *
199
+ * @returns The error name.
200
+ */ get name() {
201
+ return 'SnapError';
202
+ }
203
+ /**
204
+ * The error code.
205
+ *
206
+ * @returns The error code.
207
+ */ get code() {
208
+ return _class_private_field_get(this, _code);
209
+ }
210
+ /**
211
+ * The error message.
212
+ *
213
+ * @returns The error message.
214
+ */ get message() {
215
+ return _class_private_field_get(this, _message1);
216
+ }
217
+ /**
218
+ * Additional data for the error.
219
+ *
220
+ * @returns Additional data for the error.
221
+ */ get data() {
222
+ return _class_private_field_get(this, _data);
223
+ }
224
+ /**
225
+ * The error stack.
226
+ *
227
+ * @returns The error stack.
228
+ */ get stack() {
229
+ return _class_private_field_get(this, _stack1);
230
+ }
231
+ /**
232
+ * Convert the error to a JSON object.
233
+ *
234
+ * @returns The JSON object.
235
+ */ toJSON() {
236
+ return {
237
+ code: SNAP_ERROR_CODE,
238
+ message: SNAP_ERROR_MESSAGE,
239
+ data: {
240
+ cause: {
241
+ code: this.code,
242
+ message: this.message,
243
+ stack: this.stack,
244
+ data: this.data
245
+ }
246
+ }
247
+ };
248
+ }
249
+ /**
250
+ * Serialize the error to a JSON object. This is called by
251
+ * `@metamask/rpc-errors` when serializing the error.
252
+ *
253
+ * @returns The JSON object.
254
+ */ serialize() {
255
+ return this.toJSON();
256
+ }
257
+ /**
258
+ * Create a new `SnapError`.
259
+ *
260
+ * @param error - The error to create the `SnapError` from. If this is a
261
+ * `string`, it will be used as the error message. If this is an `Error`, its
262
+ * `message` property will be used as the error message. If this is a
263
+ * `JsonRpcError`, its `message` property will be used as the error message
264
+ * and its `code` property will be used as the error code. Otherwise, the
265
+ * error will be converted to a string and used as the error message.
266
+ * @param data - Additional data to include in the error. This will be merged
267
+ * with the error data, if any.
268
+ */ constructor(error, data = {}){
269
+ const message = getErrorMessage(error);
270
+ super(message);
271
+ _class_private_field_init(this, _code, {
272
+ writable: true,
273
+ value: void 0
274
+ });
275
+ _class_private_field_init(this, _message1, {
276
+ writable: true,
277
+ value: void 0
278
+ });
279
+ _class_private_field_init(this, _data, {
280
+ writable: true,
281
+ value: void 0
282
+ });
283
+ _class_private_field_init(this, _stack1, {
284
+ writable: true,
285
+ value: void 0
286
+ });
287
+ _class_private_field_set(this, _message1, message);
288
+ _class_private_field_set(this, _code, getErrorCode(error));
289
+ _class_private_field_set(this, _data, {
290
+ ...getErrorData(error),
291
+ ...data
292
+ });
293
+ _class_private_field_set(this, _stack1, super.stack);
294
+ }
295
+ }
296
+ function isSnapError(error) {
297
+ if ((0, _utils.isObject)(error) && 'serialize' in error && typeof error.serialize === 'function') {
298
+ const serialized = error.serialize();
299
+ return (0, _utils.isJsonRpcError)(serialized) && isSerializedSnapError(serialized);
300
+ }
301
+ return false;
302
+ }
303
+ function isSerializedSnapError(error) {
304
+ return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;
305
+ }
306
+ function isWrappedSnapError(error) {
307
+ return (0, _utils.isJsonRpcError)(error) && error.code === SNAP_ERROR_WRAPPER_CODE && error.message === SNAP_ERROR_WRAPPER_MESSAGE;
308
+ }
309
+ /**
310
+ * Get a JSON-RPC error with the given code, message, stack, and data.
311
+ *
312
+ * @param code - The error code.
313
+ * @param message - The error message.
314
+ * @param stack - The error stack.
315
+ * @param data - Additional data for the error.
316
+ * @returns The JSON-RPC error.
317
+ */ function getJsonRpcError(code, message, stack, data) {
318
+ const error = new _rpcerrors.JsonRpcError(code, message, data);
319
+ error.stack = stack;
320
+ return error;
321
+ }
322
+ function unwrapError(error) {
323
+ // This logic is a bit complicated, but it's necessary to handle all the
324
+ // different types of errors that can be thrown by a Snap.
325
+ // If the error is a wrapped Snap error, unwrap it.
326
+ if (isWrappedSnapError(error)) {
327
+ // The wrapped error can be a JSON-RPC error, or an unknown error. If it's
328
+ // a JSON-RPC error, we can unwrap it further.
329
+ if ((0, _utils.isJsonRpcError)(error.data.cause)) {
330
+ // If the JSON-RPC error is a wrapped Snap error, unwrap it further.
331
+ if (isSerializedSnapError(error.data.cause)) {
332
+ const { code, message, stack, data } = error.data.cause.data.cause;
333
+ return [
334
+ getJsonRpcError(code, message, stack, data),
335
+ true
336
+ ];
337
+ }
338
+ // Otherwise, we use the original JSON-RPC error.
339
+ const { code, message, stack, data } = error.data.cause;
340
+ return [
341
+ getJsonRpcError(code, message, stack, data),
342
+ false
343
+ ];
344
+ }
345
+ // Otherwise, we throw an internal error with the wrapped error as the
346
+ // message.
347
+ return [
348
+ getJsonRpcError(_rpcerrors.errorCodes.rpc.internal, getErrorMessage(error.data.cause), getErrorStack(error.data.cause)),
349
+ false
350
+ ];
351
+ }
352
+ // The error can be a non-wrapped JSON-RPC error, in which case we can just
353
+ // re-throw it with the same code, message, and data.
354
+ if ((0, _utils.isJsonRpcError)(error)) {
355
+ const { code, message, stack, data } = error;
356
+ return [
357
+ getJsonRpcError(code, message, stack, data),
358
+ false
359
+ ];
360
+ }
361
+ // If the error is not a wrapped error, we don't know how to handle it, so we
362
+ // throw an internal error with the error as the message.
363
+ return [
364
+ getJsonRpcError(_rpcerrors.errorCodes.rpc.internal, getErrorMessage(error), getErrorStack(error)),
365
+ false
366
+ ];
367
+ }
18
368
 
19
369
  //# sourceMappingURL=errors.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import { hasProperty, isObject } from '@metamask/utils';\n\n/**\n * Get the error message from an unknown error type.\n *\n * - If the error is an object with a `message` property, return the message.\n * - Otherwise, return the error converted to a string.\n *\n * @param error - The error to get the message from.\n * @returns The error message.\n */\nexport function getErrorMessage(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'message') &&\n typeof error.message === 'string'\n ) {\n return error.message;\n }\n\n return String(error);\n}\n"],"names":["getErrorMessage","error","isObject","hasProperty","message","String"],"mappings":";;;;+BAWgBA;;;eAAAA;;;uBAXsB;AAW/B,SAASA,gBAAgBC,KAAc;IAC5C,IACEC,IAAAA,eAAQ,EAACD,UACTE,IAAAA,kBAAW,EAACF,OAAO,cACnB,OAAOA,MAAMG,OAAO,KAAK,UACzB;QACA,OAAOH,MAAMG,OAAO;IACtB;IAEA,OAAOC,OAAOJ;AAChB"}
1
+ {"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport {\n hasProperty,\n isJsonRpcError,\n isObject,\n isValidJson,\n} from '@metamask/utils';\n\n/**\n * Get the error message from an unknown error type.\n *\n * - If the error is an object with a `message` property, return the message.\n * - Otherwise, return the error converted to a string.\n *\n * @param error - The error to get the message from.\n * @returns The error message.\n */\nexport function getErrorMessage(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'message') &&\n typeof error.message === 'string'\n ) {\n return error.message;\n }\n\n return String(error);\n}\n\n/**\n * Get the error stack from an unknown error type.\n *\n * @param error - The error to get the stack from.\n * @returns The error stack, or undefined if the error does not have a valid\n * stack.\n */\nexport function getErrorStack(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'stack') &&\n typeof error.stack === 'string'\n ) {\n return error.stack;\n }\n\n return undefined;\n}\n\n/**\n * Get the error code from an unknown error type.\n *\n * @param error - The error to get the code from.\n * @returns The error code, or `-32603` if the error does not have a valid code.\n */\nexport function getErrorCode(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'code') &&\n typeof error.code === 'number' &&\n Number.isInteger(error.code)\n ) {\n return error.code;\n }\n\n return errorCodes.rpc.internal;\n}\n\n/**\n * Get the error data from an unknown error type.\n *\n * @param error - The error to get the data from.\n * @returns The error data, or an empty object if the error does not have valid\n * data.\n */\nexport function getErrorData(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'data') &&\n typeof error.data === 'object' &&\n error.data !== null &&\n isValidJson(error.data) &&\n !Array.isArray(error.data)\n ) {\n return error.data;\n }\n\n return {};\n}\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport const SNAP_ERROR_CODE = -31002;\nexport const SNAP_ERROR_MESSAGE = 'Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport type SerializedSnapError = {\n code: typeof SNAP_ERROR_CODE;\n message: typeof SNAP_ERROR_MESSAGE;\n data: {\n cause: JsonRpcError & {\n data: Record<string, Json>;\n };\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error);\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * A generic error which can be thrown by a Snap, without it causing the Snap to\n * crash.\n */\nexport class SnapError extends Error {\n readonly #code: number;\n\n readonly #message: string;\n\n readonly #data: Record<string, Json>;\n\n readonly #stack?: string;\n\n /**\n * Create a new `SnapError`.\n *\n * @param error - The error to create the `SnapError` from. If this is a\n * `string`, it will be used as the error message. If this is an `Error`, its\n * `message` property will be used as the error message. If this is a\n * `JsonRpcError`, its `message` property will be used as the error message\n * and its `code` property will be used as the error code. Otherwise, the\n * error will be converted to a string and used as the error message.\n * @param data - Additional data to include in the error. This will be merged\n * with the error data, if any.\n */\n constructor(\n error: string | Error | JsonRpcError,\n data: Record<string, Json> = {},\n ) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#message = message;\n this.#code = getErrorCode(error);\n this.#data = { ...getErrorData(error), ...data };\n this.#stack = super.stack;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'SnapError';\n }\n\n /**\n * The error code.\n *\n * @returns The error code.\n */\n get code() {\n return this.#code;\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * Additional data for the error.\n *\n * @returns Additional data for the error.\n */\n get data() {\n return this.#data;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapError {\n return {\n code: SNAP_ERROR_CODE,\n message: SNAP_ERROR_MESSAGE,\n data: {\n cause: {\n code: this.code,\n message: this.message,\n stack: this.stack,\n data: this.data,\n },\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"],"names":["getErrorMessage","getErrorStack","getErrorCode","getErrorData","SNAP_ERROR_WRAPPER_CODE","SNAP_ERROR_WRAPPER_MESSAGE","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","WrappedSnapError","SnapError","isSnapError","isSerializedSnapError","isWrappedSnapError","unwrapError","error","isObject","hasProperty","message","String","stack","undefined","code","Number","isInteger","errorCodes","rpc","internal","data","isValidJson","Array","isArray","Error","name","toJSON","cause","serialize","serializeCause","constructor","serialized","isJsonRpcError","getJsonRpcError","RpcError"],"mappings":";;;;;;;;;;;IAuBgBA,eAAe;eAAfA;;IAmBAC,aAAa;eAAbA;;IAkBAC,YAAY;eAAZA;;IAoBAC,YAAY;eAAZA;;IAeHC,uBAAuB;eAAvBA;;IACAC,0BAA0B;eAA1BA;;IAEAC,eAAe;eAAfA;;IACAC,kBAAkB;eAAlBA;;IAoBAC,gBAAgB;eAAhBA;;IAkFAC,SAAS;eAATA;;IAoHGC,WAAW;eAAXA;;IAmBAC,qBAAqB;eAArBA;;IAYAC,kBAAkB;eAAlBA;;IAwCAC,WAAW;eAAX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C,IAAIyB,IAAAA,qBAAc,EAACzB,MAAMa,IAAI,CAACO,KAAK,GAAG;YACpC,oEAAoE;YACpE,IAAIvB,sBAAsBG,MAAMa,IAAI,CAACO,KAAK,GAAG;gBAC3C,MAAM,EAAEb,IAAI,EAAEJ,OAAO,EAAEE,KAAK,EAAEQ,IAAI,EAAE,GAAGb,MAAMa,IAAI,CAACO,KAAK,CAACP,IAAI,CAACO,KAAK;gBAClE,OAAO;oBAACM,gBAAgBnB,MAAMJ,SAASE,OAAOQ;oBAAO;iBAAK;YAC5D;YAEA,iDAAiD;YACjD,MAAM,EAAEN,IAAI,EAAEJ,OAAO,EAAEE,KAAK,EAAEQ,IAAI,EAAE,GAAGb,MAAMa,IAAI,CAACO,KAAK;YACvD,OAAO;gBAACM,gBAAgBnB,MAAMJ,SAASE,OAAOQ;gBAAO;aAAM;QAC7D;QAEA,sEAAsE;QACtE,WAAW;QACX,OAAO;YACLa,gBACEhB,qBAAU,CAACC,GAAG,CAACC,QAAQ,EACvB1B,gBAAgBc,MAAMa,IAAI,CAACO,KAAK,GAChCjC,cAAca,MAAMa,IAAI,CAACO,KAAK;YAEhC;SACD;IACH;IAEA,2EAA2E;IAC3E,qDAAqD;IACrD,IAAIK,IAAAA,qBAAc,EAACzB,QAAQ;QACzB,MAAM,EAAEO,IAAI,EAAEJ,OAAO,EAAEE,KAAK,EAAEQ,IAAI,EAAE,GAAGb;QACvC,OAAO;YAAC0B,gBAAgBnB,MAAMJ,SAASE,OAAOQ;YAAO;SAAM;IAC7D;IAEA,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACLa,gBACEhB,qBAAU,CAACC,GAAG,CAACC,QAAQ,EACvB1B,gBAAgBc,QAChBb,cAAca;QAEhB;KACD;AACH"}
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
5
  _export_star(require("./array"), exports);
6
+ _export_star(require("./auxiliary-files"), exports);
6
7
  _export_star(require("./caveats"), exports);
7
8
  _export_star(require("./checksum"), exports);
8
9
  _export_star(require("./cronjob"), exports);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.browser.ts"],"sourcesContent":["export * from './array';\nexport * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './errors';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './path';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
1
+ {"version":3,"sources":["../../src/index.browser.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './errors';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './path';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ _export_star(require("./errors"), exports);
6
7
  _export_star(require("./handlers"), exports);
7
8
  _export_star(require("./logging"), exports);
8
9
  _export_star(require("./namespace"), exports);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.executionenv.ts"],"sourcesContent":["// Special entrypoint for execution environments for bundle sizing reasons\nexport * from './handlers';\nexport * from './logging';\nexport * from './namespace';\nexport * from './types';\n"],"names":[],"mappings":"AAAA,0EAA0E;;;;;qBAC5D;qBACA;qBACA;qBACA"}
1
+ {"version":3,"sources":["../../src/index.executionenv.ts"],"sourcesContent":["// Special entrypoint for execution environments for bundle sizing reasons\nexport * from './errors';\nexport * from './handlers';\nexport * from './logging';\nexport * from './namespace';\nexport * from './types';\n"],"names":[],"mappings":"AAAA,0EAA0E;;;;;qBAC5D;qBACA;qBACA;qBACA;qBACA"}
package/dist/cjs/index.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
5
  _export_star(require("./array"), exports);
6
+ _export_star(require("./auxiliary-files"), exports);
6
7
  _export_star(require("./caveats"), exports);
7
8
  _export_star(require("./cronjob"), exports);
8
9
  _export_star(require("./checksum"), exports);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './array';\nexport * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './eval';\nexport * from './errors';\nexport * from './fs';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './eval';\nexport * from './errors';\nexport * from './fs';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA;qBACA"}
@@ -21,6 +21,9 @@ _export(exports, {
21
21
  getSnapIcon: function() {
22
22
  return getSnapIcon;
23
23
  },
24
+ getSnapAuxiliaryFiles: function() {
25
+ return getSnapAuxiliaryFiles;
26
+ },
24
27
  getWritableManifest: function() {
25
28
  return getWritableManifest;
26
29
  },
@@ -33,6 +36,7 @@ const _fastdeepequal = /*#__PURE__*/ _interop_require_default(require("fast-deep
33
36
  const _fs = require("fs");
34
37
  const _path = /*#__PURE__*/ _interop_require_default(require("path"));
35
38
  const _deepclone = require("../deep-clone");
39
+ const _errors = require("../errors");
36
40
  const _fs1 = require("../fs");
37
41
  const _npm = require("../npm");
38
42
  const _snaps = require("../snaps");
@@ -65,7 +69,8 @@ async function checkManifest(basePath, writeManifest = true, sourceCode, writeFi
65
69
  manifest: manifestFile,
66
70
  packageJson: packageFile,
67
71
  sourceCode: await getSnapSourceCode(basePath, unvalidatedManifest, sourceCode),
68
- svgIcon: await getSnapIcon(basePath, unvalidatedManifest)
72
+ svgIcon: await getSnapIcon(basePath, unvalidatedManifest),
73
+ auxiliaryFiles: await getSnapAuxiliaryFiles(basePath, unvalidatedManifest) ?? []
69
74
  };
70
75
  let manifest;
71
76
  try {
@@ -181,7 +186,7 @@ async function getSnapSourceCode(basePath, manifest, sourceCode) {
181
186
  const virtualFile = await (0, _virtualfile.readVirtualFile)(_path.default.join(basePath, sourceFilePath), 'utf8');
182
187
  return virtualFile;
183
188
  } catch (error) {
184
- throw new Error(`Failed to read snap bundle file: ${error.message}`);
189
+ throw new Error(`Failed to read snap bundle file: ${(0, _errors.getErrorMessage)(error)}`);
185
190
  }
186
191
  }
187
192
  async function getSnapIcon(basePath, manifest) {
@@ -196,7 +201,21 @@ async function getSnapIcon(basePath, manifest) {
196
201
  const virtualFile = await (0, _virtualfile.readVirtualFile)(_path.default.join(basePath, iconPath), 'utf8');
197
202
  return virtualFile;
198
203
  } catch (error) {
199
- throw new Error(`Failed to read snap icon file: ${error.message}`);
204
+ throw new Error(`Failed to read snap icon file: ${(0, _errors.getErrorMessage)(error)}`);
205
+ }
206
+ }
207
+ async function getSnapAuxiliaryFiles(basePath, manifest) {
208
+ if (!(0, _utils.isPlainObject)(manifest)) {
209
+ return undefined;
210
+ }
211
+ const filePaths = manifest.source?.files;
212
+ if (!filePaths) {
213
+ return undefined;
214
+ }
215
+ try {
216
+ return await Promise.all(filePaths.map(async (filePath)=>(0, _virtualfile.readVirtualFile)(_path.default.join(basePath, filePath), 'utf8')));
217
+ } catch (error) {
218
+ throw new Error(`Failed to read snap files: ${(0, _errors.getErrorMessage)(error)}`);
200
219
  }
201
220
  }
202
221
  function getWritableManifest(manifest) {
@@ -211,7 +230,7 @@ function getWritableManifest(manifest) {
211
230
  }), {});
212
231
  return writableManifest;
213
232
  }
214
- function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon }) {
233
+ function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, auxiliaryFiles }) {
215
234
  const packageJsonName = packageJson.result.name;
216
235
  const packageJsonVersion = packageJson.result.version;
217
236
  const packageJsonRepository = packageJson.result.repository;
@@ -232,7 +251,8 @@ function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon })
232
251
  (0, _snaps.validateSnapShasum)({
233
252
  manifest,
234
253
  sourceCode,
235
- svgIcon
254
+ svgIcon,
255
+ auxiliaryFiles
236
256
  }, `"${_types.NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
237
257
  }
238
258