@metamask/snaps-controllers 3.1.1 → 3.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 +9 -1
- package/dist/cjs/snaps/SnapController.js +22 -2
- package/dist/cjs/snaps/SnapController.js.map +1 -1
- package/dist/esm/snaps/SnapController.js +24 -4
- package/dist/esm/snaps/SnapController.js.map +1 -1
- package/dist/types/snaps/SnapController.d.ts +2 -1
- package/package.json +5 -3
|
@@ -65,8 +65,9 @@ import { BaseControllerV2 as BaseController } from '@metamask/base-controller';
|
|
|
65
65
|
import { SubjectType } from '@metamask/permission-controller';
|
|
66
66
|
import { rpcErrors } from '@metamask/rpc-errors';
|
|
67
67
|
import { WALLET_SNAP_PERMISSION_KEY } from '@metamask/snaps-rpc-methods';
|
|
68
|
-
import {
|
|
69
|
-
import {
|
|
68
|
+
import { assertUILinksAreSafe } from '@metamask/snaps-ui';
|
|
69
|
+
import { assertIsSnapManifest, assertIsValidSnapId, AuxiliaryFileEncoding, DEFAULT_ENDOWMENTS, DEFAULT_REQUESTED_SNAP_VERSION, encodeAuxiliaryFile, getErrorMessage, HandlerType, isOriginAllowed, logError, normalizeRelative, OnTransactionResponseStruct, resolveVersionRange, SnapCaveatType, SnapStatus, SnapStatusEvents, validateFetchedSnap, unwrapError } from '@metamask/snaps-utils';
|
|
70
|
+
import { assert, assertIsJsonRpcRequest, assertStruct, Duration, gtRange, gtVersion, hasProperty, inMilliseconds, isNonEmptyArray, isValidSemVerRange, satisfiesVersionRange, timeSince } from '@metamask/utils';
|
|
70
71
|
import { createMachine, interpret } from '@xstate/fsm';
|
|
71
72
|
import { nanoid } from 'nanoid';
|
|
72
73
|
import { forceStrict, validateMachine } from '../fsm';
|
|
@@ -166,7 +167,7 @@ _initializeStateMachine = /*#__PURE__*/ new WeakSet(), /**
|
|
|
166
167
|
*
|
|
167
168
|
* @param snapId - The id of the Snap whose message handler to get.
|
|
168
169
|
* @returns The RPC handler for the given snap.
|
|
169
|
-
*/ _getRpcRequestHandler = /*#__PURE__*/ new WeakSet(), _executeWithTimeout = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestStart = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestFinish = /*#__PURE__*/ new WeakSet(), /**
|
|
170
|
+
*/ _getRpcRequestHandler = /*#__PURE__*/ new WeakSet(), _assertSnapRpcRequestResult = /*#__PURE__*/ new WeakSet(), _executeWithTimeout = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestStart = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestFinish = /*#__PURE__*/ new WeakSet(), /**
|
|
170
171
|
* Retrieves the rollback snapshot of a snap.
|
|
171
172
|
*
|
|
172
173
|
* @param snapId - The snap id.
|
|
@@ -1000,6 +1001,12 @@ _initializeStateMachine = /*#__PURE__*/ new WeakSet(), /**
|
|
|
1000
1001
|
_class_private_method_init(this, _validateSnapPermissions);
|
|
1001
1002
|
_class_private_method_init(this, _getRpcRequestHandler);
|
|
1002
1003
|
/**
|
|
1004
|
+
* Asserts that the returned result of a Snap RPC call is the expected shape.
|
|
1005
|
+
*
|
|
1006
|
+
* @param handlerType - The handler type of the RPC Request.
|
|
1007
|
+
* @param result - The result of the RPC request.
|
|
1008
|
+
*/ _class_private_method_init(this, _assertSnapRpcRequestResult);
|
|
1009
|
+
/**
|
|
1003
1010
|
* Awaits the specified promise and rejects if the promise doesn't resolve
|
|
1004
1011
|
* before the timeout.
|
|
1005
1012
|
*
|
|
@@ -1540,7 +1547,7 @@ function getRpcRequestHandler(snapId) {
|
|
|
1540
1547
|
// This will either get the result or reject due to the timeout.
|
|
1541
1548
|
try {
|
|
1542
1549
|
const result = await _class_private_method_get(this, _executeWithTimeout, executeWithTimeout).call(this, handleRpcRequestPromise, timer);
|
|
1543
|
-
_class_private_method_get(this,
|
|
1550
|
+
await _class_private_method_get(this, _assertSnapRpcRequestResult, assertSnapRpcRequestResult).call(this, handlerType, result);
|
|
1544
1551
|
return result;
|
|
1545
1552
|
} catch (error) {
|
|
1546
1553
|
const [jsonRpcError, handled] = unwrapError(error);
|
|
@@ -1548,11 +1555,24 @@ function getRpcRequestHandler(snapId) {
|
|
|
1548
1555
|
await this.stopSnap(snapId, SnapStatusEvents.Crash);
|
|
1549
1556
|
}
|
|
1550
1557
|
throw jsonRpcError;
|
|
1558
|
+
} finally{
|
|
1559
|
+
_class_private_method_get(this, _recordSnapRpcRequestFinish, recordSnapRpcRequestFinish).call(this, snapId, request.id);
|
|
1551
1560
|
}
|
|
1552
1561
|
};
|
|
1553
1562
|
runtime.rpcHandler = rpcHandler;
|
|
1554
1563
|
return rpcHandler;
|
|
1555
1564
|
}
|
|
1565
|
+
async function assertSnapRpcRequestResult(handlerType, result) {
|
|
1566
|
+
switch(handlerType){
|
|
1567
|
+
case HandlerType.OnTransaction:
|
|
1568
|
+
assertStruct(result, OnTransactionResponseStruct);
|
|
1569
|
+
await this.messagingSystem.call('PhishingController:maybeUpdateState');
|
|
1570
|
+
await assertUILinksAreSafe(result.content, (url)=>this.messagingSystem.call('PhishingController:testOrigin', url).result);
|
|
1571
|
+
break;
|
|
1572
|
+
default:
|
|
1573
|
+
break;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1556
1576
|
async function executeWithTimeout(promise, timer) {
|
|
1557
1577
|
const result = await withTimeout(promise, timer ?? this.maxRequestTime);
|
|
1558
1578
|
if (result === hasTimedOut) {
|