Skip to content

Commit 8f1f519

Browse files
Mrtenzdavid0xd
andauthored
chore: Bump Snaps packages (#15232)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This bumps Snaps packages to the latest versions. The main changes are: - Support for `snap_trackEvent`. - The execution environment is now built with `@lavamoat/webpack`, resulting in a smaller bundle. - Several small performance optimisations and fixes. ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Co-authored-by: David Drazic <[email protected]>
1 parent 3a09663 commit 8f1f519

File tree

11 files changed

+187
-91
lines changed

11 files changed

+187
-91
lines changed

app/__mocks__/htmlMock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
//@ts-expect-error TS issue: Cannot find module '../../node_modules/@metamask/...
22

3-
export { default as html } from '../../node_modules/@metamask/snaps-execution-environments/dist/browserify/webview/index.html';
3+
export { default as html } from '../../node_modules/@metamask/snaps-execution-environments/dist/webpack/webview/index.html';

app/core/Engine/Engine.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ export class Engine {
14381438
(state: NetworkState) => {
14391439
if (
14401440
state.networksMetadata[state.selectedNetworkClientId].status ===
1441-
NetworkStatus.Available &&
1441+
NetworkStatus.Available &&
14421442
getGlobalChainId(networkController) !== currentChainId
14431443
) {
14441444
// We should add a state or event emitter saying the provider changed
@@ -1611,7 +1611,7 @@ export class Engine {
16111611
const chainIdHex = toHexadecimal(chainId);
16121612
const tokens =
16131613
TokensController.state.allTokens?.[chainIdHex]?.[
1614-
selectedInternalAccount.address
1614+
selectedInternalAccount.address
16151615
] || [];
16161616
const { marketData } = TokenRatesController.state;
16171617
const tokenExchangeRates = marketData?.[toHexadecimal(chainId)];
@@ -1624,7 +1624,7 @@ export class Engine {
16241624
const decimalsToShow = (currentCurrency === 'usd' && 2) || undefined;
16251625
if (
16261626
accountsByChainId?.[toHexadecimal(chainId)]?.[
1627-
selectedInternalAccountFormattedAddress
1627+
selectedInternalAccountFormattedAddress
16281628
]
16291629
) {
16301630
const balanceHex =
@@ -1665,7 +1665,7 @@ export class Engine {
16651665

16661666
const tokenBalances =
16671667
allTokenBalances?.[selectedInternalAccount.address as Hex]?.[
1668-
chainId
1668+
chainId
16691669
] ?? {};
16701670
tokens.forEach(
16711671
(item: { address: string; balance?: string; decimals: number }) => {
@@ -1676,9 +1676,9 @@ export class Engine {
16761676
item.balance ||
16771677
(item.address in tokenBalances
16781678
? renderFromTokenMinimalUnit(
1679-
tokenBalances[item.address as Hex],
1680-
item.decimals,
1681-
)
1679+
tokenBalances[item.address as Hex],
1680+
item.decimals,
1681+
)
16821682
: undefined);
16831683
const tokenBalanceFiat = balanceToFiatNumber(
16841684
// TODO: Fix this by handling or eliminating the undefined case
@@ -1834,7 +1834,7 @@ export class Engine {
18341834
// Remove all permissions.
18351835
PermissionController?.clearState?.();
18361836
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
1837-
SnapController.clearState();
1837+
await SnapController.clearState();
18381838
///: END:ONLY_INCLUDE_IF
18391839

18401840
// Clear selected network

app/core/Engine/controllers/snaps/snap-controller-init.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { buildControllerInitRequestMock } from '../../utils/test-utils';
1111
import { ExtendedControllerMessenger } from '../../../ExtendedControllerMessenger';
1212
import { KeyringControllerGetKeyringsByTypeAction } from '@metamask/keyring-controller';
1313
import { store } from '../../../../store';
14+
import { MetaMetrics } from '../../../Analytics';
1415

1516
jest.mock('@metamask/snaps-controllers');
1617

@@ -68,6 +69,7 @@ describe('SnapControllerInit', () => {
6869
getMnemonicSeed: expect.any(Function),
6970
maxIdleTime: expect.any(Number),
7071
preinstalledSnaps: expect.any(Array),
72+
trackEvent: expect.any(Function),
7173
});
7274
});
7375

@@ -138,4 +140,33 @@ describe('SnapControllerInit', () => {
138140
});
139141
});
140142
});
143+
144+
describe('trackEvent', () => {
145+
it('calls the MetaMetrics `trackEvent` function', () => {
146+
snapControllerInit(getInitRequestMock());
147+
148+
const controllerMock = jest.mocked(SnapController);
149+
const trackEvent = controllerMock.mock.calls[0][0].trackEvent;
150+
151+
const instance = MetaMetrics.getInstance();
152+
const spy = jest.spyOn(instance, 'trackEvent');
153+
154+
trackEvent({
155+
event: 'test-event',
156+
category: 'test-category',
157+
properties: {
158+
testProperty: 'test-value',
159+
},
160+
});
161+
162+
expect(spy).toHaveBeenCalledWith(
163+
expect.objectContaining({
164+
name: 'test-event',
165+
properties: {
166+
testProperty: 'test-value',
167+
},
168+
}),
169+
);
170+
});
171+
});
141172
});

app/core/Engine/controllers/snaps/snap-controller-init.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { selectBasicFunctionalityEnabled } from '../../../../selectors/settings'
2121
import { store } from '../../../../store';
2222
import PREINSTALLED_SNAPS from '../../../../lib/snaps/preinstalled-snaps';
2323
import { Caip25EndowmentPermissionName } from '@metamask/chain-agnostic-permission';
24+
import { MetaMetrics } from '../../../Analytics';
25+
import { MetricsEventBuilder } from '../../../Analytics/MetricsEventBuilder';
2426

2527
/**
2628
* Initialize the Snap controller.
@@ -119,6 +121,16 @@ export const snapControllerInit: ControllerInitFunction<
119121
clientCryptography: {
120122
pbkdf2Sha512: pbkdf2,
121123
},
124+
trackEvent: (params: {
125+
event: string;
126+
properties?: Record<string, unknown>;
127+
}) =>
128+
MetaMetrics.getInstance().trackEvent(
129+
MetricsEventBuilder.createEventBuilder({
130+
category: params.event,
131+
properties: params.properties,
132+
}).build(),
133+
),
122134
});
123135

124136
return {

app/core/Permissions/specifications.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export const unrestrictedMethods = Object.freeze([
172172
'snap_resolveInterface',
173173
'snap_setState',
174174
'snap_scheduleBackgroundEvent',
175+
'snap_trackEvent',
175176
'snap_cancelBackgroundEvent',
176177
'snap_getBackgroundEvents',
177178
'snap_experimentalProviderRequest',

app/core/Snaps/SnapsMethodMiddleware.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import {
2626
SnapInterfaceControllerUpdateInterfaceStateAction,
2727
} from '../Engine/controllers/snaps';
2828
import { KeyringTypes } from '@metamask/keyring-controller';
29+
import { MetaMetrics } from '../../../app/core/Analytics';
30+
import { MetricsEventBuilder } from '../Analytics/MetricsEventBuilder';
31+
import { Json } from '@metamask/utils';
2932

3033
export function getSnapIdFromRequest(
3134
request: Record<string, unknown>,
@@ -121,6 +124,19 @@ const snapMethodMiddlewareBuilder = (
121124
controllerMessenger,
122125
SnapControllerGetSnapAction,
123126
),
127+
trackEvent: (eventPayload: {
128+
event: string;
129+
properties: Record<string, Json>;
130+
sensitiveProperties: Record<string, Json>;
131+
}) => {
132+
MetaMetrics.getInstance().trackEvent(
133+
MetricsEventBuilder.createEventBuilder({
134+
category: eventPayload.event,
135+
properties: eventPayload.properties,
136+
sensitiveProperties: eventPayload.sensitiveProperties,
137+
}).build(),
138+
);
139+
},
124140
updateInterfaceState: controllerMessenger.call.bind(
125141
controllerMessenger,
126142
SnapInterfaceControllerUpdateInterfaceStateAction,

app/lib/snaps/SnapsExecutionWebView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { WebViewInterface } from '@metamask/snaps-controllers/react-native';
77
import { WebViewError } from '@metamask/react-native-webview/lib/WebViewTypes';
88
import { PostMessageEvent } from '@metamask/post-message-stream';
99
// @ts-expect-error Types are currently broken for this.
10-
import WebViewHTML from '@metamask/snaps-execution-environments/dist/browserify/webview/index.html';
10+
import WebViewHTML from '@metamask/snaps-execution-environments/dist/webpack/webview/index.html';
1111
import { EmptyObject } from '@metamask/snaps-sdk';
1212

1313
const styles = createStyles();

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
"@metamask/notification-services-controller": "^7.0.0",
204204
"@metamask/permission-controller": "^11.0.6",
205205
"@metamask/phishing-controller": "^12.4.1",
206-
"@metamask/post-message-stream": "^9.0.0",
206+
"@metamask/post-message-stream": "^10.0.0",
207207
"@metamask/ppom-validator": "0.36.0",
208208
"@metamask/preferences-controller": "^17.0.0",
209209
"@metamask/profile-sync-controller": "^13.0.0",
@@ -220,11 +220,11 @@
220220
"@metamask/signature-controller": "^28.0.0",
221221
"@metamask/slip44": "^4.1.0",
222222
"@metamask/smart-transactions-controller": "^16.3.1",
223-
"@metamask/snaps-controllers": "^11.2.3",
224-
"@metamask/snaps-execution-environments": "^7.2.2",
225-
"@metamask/snaps-rpc-methods": "^12.1.0",
226-
"@metamask/snaps-sdk": "^6.22.1",
227-
"@metamask/snaps-utils": "^9.2.1",
223+
"@metamask/snaps-controllers": "^12.0.0",
224+
"@metamask/snaps-execution-environments": "^8.0.1",
225+
"@metamask/snaps-rpc-methods": "^12.2.0",
226+
"@metamask/snaps-sdk": "^6.23.0",
227+
"@metamask/snaps-utils": "^9.2.2",
228228
"@metamask/solana-wallet-snap": "^1.27.0",
229229
"@metamask/stake-sdk": "^1.0.0",
230230
"@metamask/swappable-obj-proxy": "^2.1.0",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
diff --git a/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.cjs b/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.cjs
2+
index 0aaba03..e6eb9ea 100644
3+
--- a/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.cjs
4+
+++ b/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.cjs
5+
@@ -1,4 +1,11 @@
6+
"use strict";
7+
+/**
8+
+ * ============================== PATCH INFORMATION ==============================
9+
+ * This patch was added for Snaps controller integration. The MessageEvent is not
10+
+ * available in react native so we can simply return undefined here and handle the
11+
+ * origin and source elsewhere.
12+
+ * ===============================================================================
13+
+ */
14+
var __rest = (this && this.__rest) || function (s, e) {
15+
var t = {};
16+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
17+
@@ -17,11 +24,13 @@ const utils_1 = require("@metamask/utils");
18+
const BasePostMessageStream_1 = require("../BasePostMessageStream.cjs");
19+
const utils_2 = require("../utils.cjs");
20+
/* istanbul ignore next */
21+
-const getSource = (_a = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'source')) === null || _a === void 0 ? void 0 : _a.get;
22+
-(0, utils_1.assert)(getSource, 'MessageEvent.prototype.source getter is not defined.');
23+
+// const getSource = (_a = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'source')) === null || _a === void 0 ? void 0 : _a.get;
24+
+// (0, utils_1.assert)(getSource, 'MessageEvent.prototype.source getter is not defined.');
25+
+const getSource = () => undefined;
26+
/* istanbul ignore next */
27+
-const getOrigin = (_b = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'origin')) === null || _b === void 0 ? void 0 : _b.get;
28+
-(0, utils_1.assert)(getOrigin, 'MessageEvent.prototype.origin getter is not defined.');
29+
+// const getOrigin = (_b = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'origin')) === null || _b === void 0 ? void 0 : _b.get;
30+
+// (0, utils_1.assert)(getOrigin, 'MessageEvent.prototype.origin getter is not defined.');
31+
+const getOrigin = () => undefined;
32+
/**
33+
* A {@link Window.postMessage} stream.
34+
*/
35+
diff --git a/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.mjs b/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.mjs
36+
index db60ec4..ff6d786 100644
37+
--- a/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.mjs
38+
+++ b/node_modules/@metamask/post-message-stream/dist/window/WindowPostMessageStream.mjs
39+
@@ -1,3 +1,10 @@
40+
+/**
41+
+ * ============================== PATCH INFORMATION ==============================
42+
+ * This patch was added for Snaps controller integration. The MessageEvent is not
43+
+ * available in react native so we can simply return undefined here and handle the
44+
+ * origin and source elsewhere.
45+
+ * ===============================================================================
46+
+ */
47+
var __rest = (this && this.__rest) || function (s, e) {
48+
var t = {};
49+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
50+
@@ -14,11 +21,13 @@ import { assert } from "@metamask/utils";
51+
import { BasePostMessageStream } from "../BasePostMessageStream.mjs";
52+
import { isValidStreamMessage } from "../utils.mjs";
53+
/* istanbul ignore next */
54+
-const getSource = (_a = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'source')) === null || _a === void 0 ? void 0 : _a.get;
55+
-assert(getSource, 'MessageEvent.prototype.source getter is not defined.');
56+
+// const getSource = (_a = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'source')) === null || _a === void 0 ? void 0 : _a.get;
57+
+// assert(getSource, 'MessageEvent.prototype.source getter is not defined.');
58+
+const getSource = () => undefined;
59+
/* istanbul ignore next */
60+
-const getOrigin = (_b = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'origin')) === null || _b === void 0 ? void 0 : _b.get;
61+
-assert(getOrigin, 'MessageEvent.prototype.origin getter is not defined.');
62+
+// const getOrigin = (_b = Object.getOwnPropertyDescriptor(MessageEvent.prototype, 'origin')) === null || _b === void 0 ? void 0 : _b.get;
63+
+// assert(getOrigin, 'MessageEvent.prototype.origin getter is not defined.');
64+
+const getOrigin = () => undefined;
65+
/**
66+
* A {@link Window.postMessage} stream.
67+
*/

patches/@metamask+post-message-stream+9.0.0.patch

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)