@metamask/snaps-utils 8.1.0 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Changelog
2
+
2
3
  All notable changes to this project will be documented in this file.
3
4
 
4
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
@@ -6,14 +7,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
7
 
7
8
  ## [Unreleased]
8
9
 
10
+ ## [8.2.0]
11
+
12
+ ### Added
13
+
14
+ - Add support for `metamask:` schemed URLs ([#2719](https://github.com/MetaMask/snaps/pull/2719))
15
+ - Add support for JSX in `snap_notify` notifications ([#2706](https://github.com/MetaMask/snaps/pull/2706))
16
+
17
+ ### Fixed
18
+
19
+ - Validate list of emails in `validateLink` function ([#2750](https://github.com/MetaMask/snaps/pull/2750))
20
+
21
+ ## [8.1.1]
22
+
23
+ ### Fixed
24
+
25
+ - Fix invalid types in type declaration in some cases ([#2714](https://github.com/MetaMask/snaps/pull/2714))
26
+
9
27
  ## [8.1.0]
28
+
10
29
  ### Added
30
+
11
31
  - Add Bitcoin Taproot derivation paths ([#2695](https://github.com/MetaMask/snaps/pull/2695))
12
32
 
13
33
  ### Changed
34
+
14
35
  - Improve `validateLink` error handling ([#2702](https://github.com/MetaMask/snaps/pull/2702))
15
36
 
16
37
  ### Fixed
38
+
17
39
  - Fix ESM version of the package ([#2682](https://github.com/MetaMask/snaps/pull/2682))
18
40
  - This fixes the ESM version of the package to be fully compliant with the ESM
19
41
  standard.
@@ -21,25 +43,34 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
21
43
  - Bump other MetaMask dependencies ([#2703](https://github.com/MetaMask/snaps/pull/2703))
22
44
 
23
45
  ## [8.0.1]
46
+
24
47
  ### Changed
48
+
25
49
  - Bump `@metamask/slip44` to 4.0.0 ([#2624](https://github.com/MetaMask/snaps/pull/2624))
26
50
 
27
51
  ## [8.0.0]
52
+
28
53
  ### Changed
54
+
29
55
  - **BREAKING:** Improve manifest validation output ([#2605](https://github.com/MetaMask/snaps/pull/2605), [#2572](https://github.com/MetaMask/snaps/pull/2572))
30
56
  - This is breaking as it removes exports such as `validateNpmSnap`.
31
57
  - Bump `fast-xml-parser` ([#2610](https://github.com/MetaMask/snaps/pull/2610))
32
58
 
33
59
  ## [7.8.1]
60
+
34
61
  ### Changed
62
+
35
63
  - Bump `@metamask/base-controller` from `^6.0.1` to `^6.0.2` ([#2593](https://github.com/metamask/snaps/pull/2593))
36
64
  - Bump `@metamask/permission-controller` from `^10.0.1` to `^11.0.0` ([#2593](https://github.com/metamask/snaps/pull/2593))
37
65
 
38
66
  ## [7.8.0]
67
+
39
68
  ### Added
69
+
40
70
  - Move `serialiseJsx` function from `snaps-jest` to `snaps-utils` ([#2409](https://github.com/metamask/snaps/pull/2409))
41
71
 
42
72
  ### Changed
73
+
43
74
  - Bump `@metamask/base-controller` from `^6.0.0` to `^6.0.1` ([#2445](https://github.com/MetaMask/snaps/pull/2445))
44
75
  - Bump `@metamask/key-tree` from `^9.1.1` to `^9.1.2` ([#2445](https://github.com/MetaMask/snaps/pull/2445))
45
76
  - Bump `@metamask/permission-controller` from `^10.0.0` to `^10.0.1` ([#2445](https://github.com/MetaMask/snaps/pull/2445))
@@ -48,44 +79,59 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
48
79
  - Bump `@metamask/utils` from `^8.3.0` to `^9.1.0` ([#2445](https://github.com/MetaMask/snaps/pull/2445))
49
80
 
50
81
  ### Fixed
82
+
51
83
  - Replace `superstruct` with ESM-compatible `@metamask/superstruct` `^3.1.0` ([#2445](https://github.com/MetaMask/snaps/pull/2445))
52
84
  - This fixes the issue of this package being unusable by any TypeScript project that uses `Node16` or `NodeNext` as its `moduleResolution` option.
53
85
  - Fix `allowedOrigins` bypass caused by unterminated regex ([#2576](https://github.com/metamask/snaps/pull/2576))
54
86
 
55
87
  ## [7.7.0]
88
+
56
89
  ### Added
90
+
57
91
  - Support conditional children in most JSX components ([#2506](https://github.com/MetaMask/snaps/pull/2506))
58
92
  - Support additional components inside forms ([#2497](https://github.com/MetaMask/snaps/pull/2497))
59
93
 
60
94
  ### Fixed
95
+
61
96
  - Improve validation of `endowment:rpc` ([#2512](https://github.com/MetaMask/snaps/pull/2512))
62
97
 
63
98
  ## [7.6.0]
99
+
64
100
  ### Added
101
+
65
102
  - Support nested children in JSX ([#2482](https://github.com/MetaMask/snaps/pull/2482))
66
103
 
67
104
  ### Changed
105
+
68
106
  - Update `onNameLookup` response to include `domainName` ([#2484](https://github.com/MetaMask/snaps/pull/2484))
69
107
  - Bump MetaMask dependencies ([#2460](https://github.com/MetaMask/snaps/pull/2460))
70
108
 
71
109
  ### Fixed
110
+
72
111
  - Fix a problem with converting legacy UIs that used `row()` ([#2486](https://github.com/MetaMask/snaps/pull/2486))
73
112
  - Revert requiring at least one child in JSX components ([#2470](https://github.com/MetaMask/snaps/pull/2470))
74
113
 
75
114
  ## [7.5.0]
115
+
76
116
  ### Added
117
+
77
118
  - Allow wildcards in `allowedOrigins` ([#2458](https://github.com/MetaMask/snaps/pull/2458))
78
119
  - Add `hidden` flag for preinstalled Snaps ([#2463](https://github.com/MetaMask/snaps/pull/2463))
79
120
 
80
121
  ### Fixed
122
+
81
123
  - Require at least 1 child in JSX components ([#2466](https://github.com/MetaMask/snaps/pull/2466))
82
124
 
83
125
  ## [7.4.1]
126
+
84
127
  ### Fixed
128
+
85
129
  - Fix invalid `@metamask/snaps-sdk` imports ([#2452](https://github.com/MetaMask/snaps/pull/2452))
86
130
 
87
131
  ## [7.4.0]
132
+
88
133
  ### Added
134
+
89
135
  - Add support for BIP-32-Ed25519 / CIP-3 key derivation ([#2408](https://github.com/MetaMask/snaps/pull/2408))
90
136
 
91
137
  ## Fixed
@@ -93,47 +139,66 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
93
139
  - Fix build producing invalid JSX types ([#2410](https://github.com/MetaMask/snaps/pull/2410))
94
140
 
95
141
  ## [7.3.0]
142
+
96
143
  ### Added
144
+
97
145
  - Add JSX support for custom UI ([#2258](https://github.com/MetaMask/snaps/pull/2258), [#2383](https://github.com/MetaMask/snaps/pull/2383))
98
146
  - This adds utility functions for working with JSX in Snaps.
99
147
 
100
148
  ### Changed
149
+
101
150
  - Bump `@metamask/base-controller` from `5.0.1` to `5.0.2` ([#2375](https://github.com/MetaMask/snaps/pull/2375))
102
151
 
103
152
  ## [7.2.0]
153
+
104
154
  ### Added
155
+
105
156
  - Add `getJsonSizeUnsafe` ([#2342](https://github.com/MetaMask/snaps/pull/2342))
106
157
 
107
158
  ## [7.1.0]
159
+
108
160
  ### Added
161
+
109
162
  - Add derivation path for Nimiq ([#2309](https://github.com/MetaMask/snaps/pull/2309))
110
163
 
111
164
  ### Fixed
165
+
112
166
  - Disable GitHub flavored Markdown when lexing ([#2317](https://github.com/MetaMask/snaps/pull/2317))
113
167
 
114
168
  ## [7.0.4]
169
+
115
170
  ### Changed
171
+
116
172
  - Bump MetaMask dependencies ([#2270](https://github.com/MetaMask/snaps/pull/2270))
117
173
 
118
174
  ### Fixed
175
+
119
176
  - Allow `maxRequestTime` on `endowment:rpc` ([#2291](https://github.com/MetaMask/snaps/pull/2291))
120
177
 
121
178
  ## [7.0.3]
179
+
122
180
  ### Changed
181
+
123
182
  - Update markdown parsing for better link validation ([#2261](https://github.com/MetaMask/snaps/pull/2261))
124
183
  - Bump `@metamask/snaps-registry` to `^3.0.1` ([#2255](https://github.com/MetaMask/snaps/pull/2255))
125
184
 
126
185
  ## [7.0.2]
186
+
127
187
  ### Fixed
188
+
128
189
  - Remove usage of `Buffer` from browser entrypoint ([#2238](https://github.com/MetaMask/snaps/pull/2238))
129
190
 
130
191
  ## [7.0.1]
192
+
131
193
  ### Fixed
194
+
132
195
  - Fix minor build configuration problems ([#2220](https://github.com/MetaMask/snaps/pull/2220))
133
196
  - Fix regex for HTML comment tokens ([#2222](https://github.com/MetaMask/snaps/pull/2222))
134
197
 
135
198
  ## [7.0.0]
199
+
136
200
  ### Changed
201
+
137
202
  - **BREAKING:** Update ESM build to be fully compliant with the ESM standard ([#2210](https://github.com/MetaMask/snaps/pull/2210))
138
203
  - **BREAKING:** Move Node.js exports to separate export ([#2210](https://github.com/MetaMask/snaps/pull/2210))
139
204
  - The default export is now browser-compatible.
@@ -141,81 +206,110 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
141
206
  - Bump `@metamask/rpc-errors` to `^6.2.1` ([#2209](https://github.com/MetaMask/snaps/pull/2209))
142
207
 
143
208
  ### Removed
209
+
144
210
  - **BREAKING:** Move `file` struct to CLI ([#2207](https://github.com/MetaMask/snaps/pull/2207))
145
211
  - The previously exported `file` struct can now be found in `@metamask/snaps-cli`.
146
212
 
147
213
  ### Fixed
214
+
148
215
  - Add sizing limits for custom UI ([#2199](https://github.com/MetaMask/snaps/pull/2199))
149
216
  - Properly validate links contained in rows ([#2205](https://github.com/MetaMask/snaps/pull/2205))
150
217
 
151
218
  ## [6.1.0]
219
+
152
220
  ### Added
221
+
153
222
  - Add a manifest warning when no icon is found and when icon is not square ([#2185](https://github.com/MetaMask/snaps/pull/2185))
154
223
 
155
224
  ## [6.0.0]
225
+
156
226
  ### Added
227
+
157
228
  - Add support for dynamic user interfaces ([#1465](https://github.com/MetaMask/snaps/pull/1465), [#2126](https://github.com/MetaMask/snaps/pull/2126))
158
229
  - Add support for snap defined execution timeouts ([#2098](https://github.com/MetaMask/snaps/pull/2098))
159
230
 
160
231
  ### Changed
232
+
161
233
  - **BREAKING:** Update name lookup type validation ([#2113](https://github.com/MetaMask/snaps/pull/2113))
162
234
  - The return value and the permission format has changed, see [SIP-12](https://metamask.github.io/SIPs/SIPS/sip-12) for more details.
163
235
  - Speed up eval-worker for improved CLI performance ([#2147](https://github.com/MetaMask/snaps/pull/2147))
164
236
  - Update MetaMask dependencies ([#2132](https://github.com/MetaMask/snaps/pull/2132), [#2142](https://github.com/MetaMask/snaps/pull/2142))
165
237
 
166
238
  ### Fixed
239
+
167
240
  - Fix initial permissions types ([#2111](https://github.com/MetaMask/snaps/pull/2111))
168
241
 
169
242
  ## [5.2.0]
243
+
170
244
  ### Added
245
+
171
246
  - Add support for initial connections ([#2048](https://github.com/MetaMask/snaps/pull/2048))
172
247
  - Add support for preinstalled snaps ([#2008](https://github.com/MetaMask/snaps/pull/2008))
173
248
  - Add support for signature insights ([#2074](https://github.com/MetaMask/snaps/pull/2074))
174
249
 
175
250
  ### Changed
251
+
176
252
  - Bump several MetaMask dependencies ([#2086](https://github.com/MetaMask/snaps/pull/2086), [#2100](https://github.com/MetaMask/snaps/pull/2100))
177
253
 
178
254
  ## [5.1.2]
255
+
179
256
  ### Fixed
257
+
180
258
  - Fix missing `global` during snap evaluation ([#2072](https://github.com/MetaMask/snaps/pull/2072))
181
259
 
182
260
  ## [5.1.1]
261
+
183
262
  ### Changed
263
+
184
264
  - Fix missing export ([#2045](https://github.com/MetaMask/snaps/pull/2045))
185
265
  - Bump `@metamask/permission-controller` from `6.0.0` to `7.0.0` ([#2064](https://github.com/MetaMask/snaps/pull/2064))
186
266
 
187
267
  ### Removed
268
+
188
269
  - Remove support for object-like syntax for cronjobs ([#2057](https://github.com/MetaMask/snaps/pull/2057))
189
270
  - Since this never worked in the first place we aren't marking it as breaking.
190
271
 
191
272
  ## [5.1.0]
273
+
192
274
  ### Added
275
+
193
276
  - Add `getSnapDerivationPathName` and `getSlip44ProtocolName` to be shared across clients ([#2033](https://github.com/MetaMask/snaps/pull/2033))
194
277
 
195
278
  ### Changed
279
+
196
280
  - Bump `snaps-registry` ([#2020](https://hub.com/MetaMask/snaps/pull/2020))
197
281
 
198
282
  ## [5.0.1]
283
+
199
284
  ### Changed
285
+
200
286
  - Improve base64 encoding/decoding speeds ([#1985](https://github.com/MetaMask/snaps/pull/1985))
201
287
  - Bump several MetaMask dependencies ([#1989](https://github.com/MetaMask/snaps/pull/1989), [#1993](https://github.com/MetaMask/snaps/pull/1993))
202
288
 
203
289
  ## [5.0.0]
290
+
204
291
  ### Changed
292
+
205
293
  - Bump several MetaMask dependencies ([#1964](https://github.com/MetaMask/snaps/pull/1964))
206
294
 
207
295
  ### Removed
296
+
208
297
  - **BREAKING:** Move `enumValue`, `literal` and `union` to `snaps-sdk` ([#1968](https://github.com/MetaMask/snaps/pull/1968))
209
298
 
210
299
  ### Fixed
300
+
211
301
  - Fix issues generating checksum with binary auxiliary files ([#1975](https://github.com/MetaMask/snaps/pull/1975))
212
302
 
213
303
  ## [4.0.1]
304
+
214
305
  ### Fixed
306
+
215
307
  - Change `validateTextLinks` to only get URL in markdown links ([#1914](https://github.com/MetaMask/snaps/pull/1914))
216
308
 
217
309
  ## [4.0.0]
310
+
218
311
  ### Changed
312
+
219
313
  - Use `SubtleCrypto` for checksum calculation if available ([#1953](https://github.com/MetaMask/snaps/pull/1953))
220
314
  - This reduces the time of the checksum calculation by up to 95% in some
221
315
  environments.
@@ -226,6 +320,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
226
320
  - `@metamask/snaps-ui` packages, and is much more lightweight.
227
321
 
228
322
  ### Removed
323
+
229
324
  - **BREAKING**: Remove `ValidatedSnapId` and `SnapId` types ([#1930](https://github.com/MetaMask/snaps/pull/1930))
230
325
  - `ValidatedSnapId` was moved to the `@metamask/snaps-sdk` package as
231
326
  `SnapId`.
@@ -239,79 +334,110 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
239
334
  type from the `@metamask/snaps-sdk` package.
240
335
 
241
336
  ### Fixed
337
+
242
338
  - Include localization files in checksum calculations ([#1956](https://github.com/MetaMask/snaps/pull/1956))
243
339
 
244
340
  ## [3.3.0]
341
+
245
342
  ### Added
343
+
246
344
  - Add manifest localization functionality ([#1889](https://github.com/MetaMask/snaps/pull/1889))
247
345
  - Add `OnHomePage` export ([#1896](https://github.com/MetaMask/snaps/pull/1896))
248
346
 
249
347
  ## [3.2.0]
348
+
250
349
  ### Added
350
+
251
351
  - Add support for links in custom UI and notifications ([#1814](https://github.com/MetaMask/snaps/pull/1814))
252
352
 
253
353
  ## [3.1.0]
354
+
254
355
  ### Added
356
+
255
357
  - Add static file API ([#1836](https://github.com/MetaMask/snaps/pull/1836), [#1858](https://github.com/MetaMask/snaps/pull/1858))
256
358
 
257
359
  ### Changed
360
+
258
361
  - Update multiple MetaMask dependencies ([#1841](https://github.com/MetaMask/snaps/pull/1841))
259
362
  - Bump Babel packages from `^7.20.12` to `^7.23.2` ([#1862](https://github.com/MetaMask/snaps/pull/1862))
260
363
 
261
364
  ## [3.0.0]
365
+
262
366
  ### Added
367
+
263
368
  - Add keyring export and endowment ([#1787](https://github.com/MetaMask/snaps/pull/1787))
264
369
  - Add optional `allowedOrigins` field to `endowment:rpc` ([#1822](https://github.com/MetaMask/snaps/pull/1822))
265
370
  - This can be used to only accept certain origins in your Snap.
266
371
 
267
372
  ### Changed
373
+
268
374
  - **BREAKING:** Bump minimum Node.js version to `^18.16.0` ([#1741](https://github.com/MetaMask/snaps/pull/1741))
269
375
 
270
376
  ## [2.0.1]
377
+
271
378
  ### Changed
379
+
272
380
  - Remove deprecated `endowment:long-running` ([#1751](https://github.com/MetaMask/snaps/pull/1751))
273
381
 
274
382
  ## [2.0.0]
383
+
275
384
  ### Changed
385
+
276
386
  - Initial stable release from main branch ([#1757](https://github.com/MetaMask/snaps/pull/1757))
277
387
 
278
388
  ## [0.38.4-flask.1]
389
+
279
390
  ### Added
391
+
280
392
  - Add `onNameLookup` export ([#1394](https://github.com/MetaMask/snaps/pull/1394), [#1759](https://github.com/MetaMask/snaps/pull/1759))
281
393
 
282
394
  ### Changed
395
+
283
396
  - Bump `metamask/utils` and `metamask/snaps-registry` ([#1738](https://github.com/MetaMask/snaps/pull/1738))
284
397
 
285
398
  ## [0.38.3-flask.1]
399
+
286
400
  ### Changed
401
+
287
402
  - Bump `@metamask/post-message-stream` from 6.1.2 to 7.0.0 ([#1707](https://github.com/MetaMask/snaps/pull/1707), [#1724](https://github.com/MetaMask/snaps/pull/1724))
288
403
  - Bump `@metamask/utils` and `@metamask/snaps-registry` ([#1694](https://github.com/MetaMask/snaps/pull/1694))
289
404
 
290
405
  ## [0.38.2-flask.1]
406
+
291
407
  ### Fixed
408
+
292
409
  - Remove unused dependencies ([#1680](https://github.com/MetaMask/snaps/pull/1680))
293
410
 
294
411
  ## [0.38.1-flask.1]
412
+
295
413
  ### Changed
414
+
296
415
  - Update transaction insights response and add severity level enum ([#1653](https://github.com/MetaMask/snaps/pull/1653))
297
416
  - Snaps are now able to specify a `severity` for alongside their insights.
298
417
  - See [SIP-11](https://metamask.github.io/SIPs/SIPS/sip-11) for more information.
299
418
 
300
419
  ## [0.38.0-flask.1]
420
+
301
421
  ### Added
422
+
302
423
  - Add `onInstall` and `onUpdate` lifecycle hooks ([#1643](https://github.com/MetaMask/snaps/pull/1643))
303
424
 
304
425
  ### Changed
426
+
305
427
  - Move source code and snap state back to controller state ([#1634](https://github.com/MetaMask/snaps/pull/1634))
306
428
  - Bump `semver` to `^7.5.4` ([#1631](https://github.com/MetaMask/snaps/pull/1631))
307
429
 
308
430
  ## [0.37.2-flask.1]
431
+
309
432
  ### Changed
433
+
310
434
  - Release package independently ([#1600](https://github.com/MetaMask/snaps/pull/1600))
311
435
  - The version of the package no longer needs to match the version of all other
312
436
  MetaMask Snaps packages.
313
437
 
314
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@8.1.0...HEAD
438
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@8.2.0...HEAD
439
+ [8.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@8.1.1...@metamask/snaps-utils@8.2.0
440
+ [8.1.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@8.1.0...@metamask/snaps-utils@8.1.1
315
441
  [8.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@8.0.1...@metamask/snaps-utils@8.1.0
316
442
  [8.0.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@8.0.0...@metamask/snaps-utils@8.0.1
317
443
  [8.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.8.1...@metamask/snaps-utils@8.0.0
package/dist/errors.cjs CHANGED
@@ -1,16 +1,4 @@
1
1
  "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _WrappedSnapError_error, _WrappedSnapError_message, _WrappedSnapError_stack;
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
3
  exports.unwrapError = exports.isWrappedSnapError = exports.isSerializedSnapError = exports.isSnapError = exports.WrappedSnapError = exports.SNAP_ERROR_WRAPPER_MESSAGE = exports.SNAP_ERROR_WRAPPER_CODE = void 0;
16
4
  const rpc_errors_1 = require("@metamask/rpc-errors");
@@ -19,6 +7,9 @@ const utils_1 = require("@metamask/utils");
19
7
  exports.SNAP_ERROR_WRAPPER_CODE = -31001;
20
8
  exports.SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
21
9
  class WrappedSnapError extends Error {
10
+ #error;
11
+ #message;
12
+ #stack;
22
13
  /**
23
14
  * Create a new `WrappedSnapError`.
24
15
  *
@@ -27,12 +18,9 @@ class WrappedSnapError extends Error {
27
18
  constructor(error) {
28
19
  const message = (0, snaps_sdk_1.getErrorMessage)(error);
29
20
  super(message);
30
- _WrappedSnapError_error.set(this, void 0);
31
- _WrappedSnapError_message.set(this, void 0);
32
- _WrappedSnapError_stack.set(this, void 0);
33
- __classPrivateFieldSet(this, _WrappedSnapError_error, error, "f");
34
- __classPrivateFieldSet(this, _WrappedSnapError_message, message, "f");
35
- __classPrivateFieldSet(this, _WrappedSnapError_stack, (0, snaps_sdk_1.getErrorStack)(error), "f");
21
+ this.#error = error;
22
+ this.#message = message;
23
+ this.#stack = (0, snaps_sdk_1.getErrorStack)(error);
36
24
  }
37
25
  /**
38
26
  * The error name.
@@ -48,7 +36,7 @@ class WrappedSnapError extends Error {
48
36
  * @returns The error message.
49
37
  */
50
38
  get message() {
51
- return __classPrivateFieldGet(this, _WrappedSnapError_message, "f");
39
+ return this.#message;
52
40
  }
53
41
  /**
54
42
  * The error stack.
@@ -56,7 +44,7 @@ class WrappedSnapError extends Error {
56
44
  * @returns The error stack.
57
45
  */
58
46
  get stack() {
59
- return __classPrivateFieldGet(this, _WrappedSnapError_stack, "f");
47
+ return this.#stack;
60
48
  }
61
49
  /**
62
50
  * Convert the error to a JSON object.
@@ -64,9 +52,9 @@ class WrappedSnapError extends Error {
64
52
  * @returns The JSON object.
65
53
  */
66
54
  toJSON() {
67
- const cause = isSnapError(__classPrivateFieldGet(this, _WrappedSnapError_error, "f"))
68
- ? __classPrivateFieldGet(this, _WrappedSnapError_error, "f").serialize()
69
- : (0, rpc_errors_1.serializeCause)(__classPrivateFieldGet(this, _WrappedSnapError_error, "f"));
55
+ const cause = isSnapError(this.#error)
56
+ ? this.#error.serialize()
57
+ : (0, rpc_errors_1.serializeCause)(this.#error);
70
58
  return {
71
59
  code: exports.SNAP_ERROR_WRAPPER_CODE,
72
60
  message: exports.SNAP_ERROR_WRAPPER_MESSAGE,
@@ -86,7 +74,6 @@ class WrappedSnapError extends Error {
86
74
  }
87
75
  }
88
76
  exports.WrappedSnapError = WrappedSnapError;
89
- _WrappedSnapError_error = new WeakMap(), _WrappedSnapError_message = new WeakMap(), _WrappedSnapError_stack = new WeakMap();
90
77
  /**
91
78
  * Check if an object is a `SnapError`.
92
79
  *
@@ -1 +1 @@
1
- {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAI8B;AAG9B,mDAK6B;AAE7B,2CAA2D;AAE9C,QAAA,uBAAuB,GAAG,CAAC,KAAK,CAAC;AACjC,QAAA,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAa,gBAAiB,SAAQ,KAAK;IAOzC;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAbR,0CAAgB;QAEhB,4CAAiB;QAEjB,0CAAgB;QAWvB,uBAAA,IAAI,2BAAU,KAAK,MAAA,CAAC;QACpB,uBAAA,IAAI,6BAAY,OAAO,MAAA,CAAC;QACxB,uBAAA,IAAI,2BAAU,IAAA,yBAAa,EAAC,KAAK,CAAC,MAAA,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,kBAAkB,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,iCAAS,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,uBAAA,IAAI,+BAAO,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,MAAM,KAAK,GAAG,WAAW,CAAC,uBAAA,IAAI,+BAAO,CAAC;YACpC,CAAC,CAAC,uBAAA,IAAI,+BAAO,CAAC,SAAS,EAAE;YACzB,CAAC,CAAC,IAAA,2BAAc,EAAC,uBAAA,IAAI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,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,EACtB,IAAA,yBAAa,EAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAnDD,kCAmDC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped 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 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 * 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"]}
1
+ {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,qDAI8B;AAG9B,mDAK6B;AAE7B,2CAA2D;AAE9C,QAAA,uBAAuB,GAAG,CAAC,KAAK,CAAC;AACjC,QAAA,0B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@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped 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 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 * 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"]}
package/dist/errors.mjs CHANGED
@@ -1,21 +1,12 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _WrappedSnapError_error, _WrappedSnapError_message, _WrappedSnapError_stack;
13
1
  import { errorCodes, JsonRpcError as RpcError, serializeCause } from "@metamask/rpc-errors";
14
2
  import { getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from "@metamask/snaps-sdk";
15
3
  import { isObject, isJsonRpcError } from "@metamask/utils";
16
4
  export const SNAP_ERROR_WRAPPER_CODE = -31001;
17
5
  export const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
18
6
  export class WrappedSnapError extends Error {
7
+ #error;
8
+ #message;
9
+ #stack;
19
10
  /**
20
11
  * Create a new `WrappedSnapError`.
21
12
  *
@@ -24,12 +15,9 @@ export class WrappedSnapError extends Error {
24
15
  constructor(error) {
25
16
  const message = getErrorMessage(error);
26
17
  super(message);
27
- _WrappedSnapError_error.set(this, void 0);
28
- _WrappedSnapError_message.set(this, void 0);
29
- _WrappedSnapError_stack.set(this, void 0);
30
- __classPrivateFieldSet(this, _WrappedSnapError_error, error, "f");
31
- __classPrivateFieldSet(this, _WrappedSnapError_message, message, "f");
32
- __classPrivateFieldSet(this, _WrappedSnapError_stack, getErrorStack(error), "f");
18
+ this.#error = error;
19
+ this.#message = message;
20
+ this.#stack = getErrorStack(error);
33
21
  }
34
22
  /**
35
23
  * The error name.
@@ -45,7 +33,7 @@ export class WrappedSnapError extends Error {
45
33
  * @returns The error message.
46
34
  */
47
35
  get message() {
48
- return __classPrivateFieldGet(this, _WrappedSnapError_message, "f");
36
+ return this.#message;
49
37
  }
50
38
  /**
51
39
  * The error stack.
@@ -53,7 +41,7 @@ export class WrappedSnapError extends Error {
53
41
  * @returns The error stack.
54
42
  */
55
43
  get stack() {
56
- return __classPrivateFieldGet(this, _WrappedSnapError_stack, "f");
44
+ return this.#stack;
57
45
  }
58
46
  /**
59
47
  * Convert the error to a JSON object.
@@ -61,9 +49,9 @@ export class WrappedSnapError extends Error {
61
49
  * @returns The JSON object.
62
50
  */
63
51
  toJSON() {
64
- const cause = isSnapError(__classPrivateFieldGet(this, _WrappedSnapError_error, "f"))
65
- ? __classPrivateFieldGet(this, _WrappedSnapError_error, "f").serialize()
66
- : serializeCause(__classPrivateFieldGet(this, _WrappedSnapError_error, "f"));
52
+ const cause = isSnapError(this.#error)
53
+ ? this.#error.serialize()
54
+ : serializeCause(this.#error);
67
55
  return {
68
56
  code: SNAP_ERROR_WRAPPER_CODE,
69
57
  message: SNAP_ERROR_WRAPPER_MESSAGE,
@@ -82,7 +70,6 @@ export class WrappedSnapError extends Error {
82
70
  return this.toJSON();
83
71
  }
84
72
  }
85
- _WrappedSnapError_error = new WeakMap(), _WrappedSnapError_message = new WeakMap(), _WrappedSnapError_stack = new WeakMap();
86
73
  /**
87
74
  * Check if an object is a `SnapError`.
88
75
  *