Skip to content

Commit 7f9f076

Browse files
MrtenzFrederikBolding
authored andcommitted
Fix MultichainRouter types and tests
1 parent 5716067 commit 7f9f076

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

packages/snaps-controllers/src/multichain/MultichainRouter.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '@metamask/snaps-utils/test-utils';
66

77
import { MultichainRouter } from './MultichainRouter';
8+
import { METAMASK_ORIGIN } from '../snaps/constants';
89
import {
910
getRootMultichainRouterMessenger,
1011
getRestrictedMultichainRouterMessenger,
@@ -51,9 +52,12 @@ describe('MultichainRouter', () => {
5152
);
5253

5354
const result = await messenger.call('MultichainRouter:handleRequest', {
55+
origin: METAMASK_ORIGIN,
5456
connectedAddresses: BTC_CONNECTED_ACCOUNTS,
5557
scope: BTC_CAIP2,
5658
request: {
59+
jsonrpc: '2.0',
60+
id: 1,
5761
method: 'sendBitcoin',
5862
params: {
5963
message: 'foo',
@@ -102,9 +106,12 @@ describe('MultichainRouter', () => {
102106
);
103107

104108
const result = await messenger.call('MultichainRouter:handleRequest', {
109+
origin: METAMASK_ORIGIN,
105110
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
106111
scope: SOLANA_CAIP2,
107112
request: {
113+
jsonrpc: '2.0',
114+
id: 1,
108115
method: 'signAndSendTransaction',
109116
params: {
110117
message: 'foo',
@@ -149,10 +156,12 @@ describe('MultichainRouter', () => {
149156
);
150157

151158
const result = await messenger.call('MultichainRouter:handleRequest', {
159+
origin: METAMASK_ORIGIN,
152160
connectedAddresses: [],
153161
scope: SOLANA_CAIP2,
154-
origin: 'metamask',
155162
request: {
163+
jsonrpc: '2.0',
164+
id: 1,
156165
method: 'getVersion',
157166
},
158167
});
@@ -162,7 +171,7 @@ describe('MultichainRouter', () => {
162171
'solana-core': '1.16.7',
163172
});
164173

165-
expect(rootMessenger.call).toHaveBeenNthCalledWith(
174+
expect(messenger.call).toHaveBeenNthCalledWith(
166175
5,
167176
'SnapController:handleRequest',
168177
{
@@ -173,7 +182,7 @@ describe('MultichainRouter', () => {
173182
method: '',
174183
params: {
175184
request: {
176-
id: expect.any(String),
185+
id: 1,
177186
jsonrpc: '2.0',
178187
method: 'getVersion',
179188
},
@@ -206,9 +215,12 @@ describe('MultichainRouter', () => {
206215

207216
await expect(
208217
messenger.call('MultichainRouter:handleRequest', {
218+
origin: METAMASK_ORIGIN,
209219
connectedAddresses: [],
210220
scope: SOLANA_CAIP2,
211221
request: {
222+
jsonrpc: '2.0',
223+
id: 1,
212224
method: 'getVersion',
213225
},
214226
}),
@@ -241,9 +253,12 @@ describe('MultichainRouter', () => {
241253

242254
await expect(
243255
messenger.call('MultichainRouter:handleRequest', {
256+
origin: METAMASK_ORIGIN,
244257
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
245258
scope: SOLANA_CAIP2,
246259
request: {
260+
jsonrpc: '2.0',
261+
id: 1,
247262
method: 'signAndSendTransaction',
248263
params: {
249264
message: 'foo',
@@ -282,9 +297,12 @@ describe('MultichainRouter', () => {
282297

283298
await expect(
284299
messenger.call('MultichainRouter:handleRequest', {
300+
origin: METAMASK_ORIGIN,
285301
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
286302
scope: SOLANA_CAIP2,
287303
request: {
304+
jsonrpc: '2.0',
305+
id: 1,
288306
method: 'signAndSendTransaction',
289307
params: {
290308
message: 'foo',
@@ -322,9 +340,12 @@ describe('MultichainRouter', () => {
322340

323341
await expect(
324342
messenger.call('MultichainRouter:handleRequest', {
343+
origin: METAMASK_ORIGIN,
325344
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
326345
scope: SOLANA_CAIP2,
327346
request: {
347+
jsonrpc: '2.0',
348+
id: 1,
328349
method: 'signAndSendTransaction',
329350
params: {
330351
message: 'foo',

packages/snaps-controllers/src/multichain/MultichainRouter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type SnapKeyring = {
6060
};
6161

6262
// Expecting a bound function that calls KeyringController.withKeyring selecting the Snap keyring
63-
type WithSnapKeyringFunction = <ReturnType>(
63+
export type WithSnapKeyringFunction = <ReturnType>(
6464
operation: ({ keyring }: { keyring: SnapKeyring }) => Promise<ReturnType>,
6565
) => Promise<ReturnType>;
6666

packages/snaps-controllers/src/test-utils/controller.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ export const getRestrictedMultichainRouterMessenger = (
919919
) => {
920920
const controllerMessenger = new Messenger<
921921
'MultichainRouter',
922-
MultichainRouterAllowedActions,
922+
MultichainRouterActions | MultichainRouterAllowedActions,
923923
never,
924924
any
925925
>({ namespace: 'MultichainRouter', parent: messenger });
@@ -934,6 +934,8 @@ export const getRestrictedMultichainRouterMessenger = (
934934
messenger: controllerMessenger,
935935
});
936936

937+
jest.spyOn(controllerMessenger, 'call');
938+
937939
return controllerMessenger;
938940
};
939941

packages/snaps-controllers/src/test-utils/multichain.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { SnapCaveatType } from '@metamask/snaps-utils';
44
import { MOCK_SNAP_ID } from '@metamask/snaps-utils/test-utils';
55
import type { CaipAccountId, CaipChainId, Json } from '@metamask/utils';
66

7+
import type { WithSnapKeyringFunction } from '@metamask/snaps-controllers';
8+
79
export const BTC_CAIP2 =
810
'bip122:000000000019d6689c085ae165831e93' as CaipChainId;
911
export const BTC_CONNECTED_ACCOUNTS = [
@@ -28,6 +30,7 @@ export const MOCK_BTC_ACCOUNTS = [
2830
methods: ['sendBitcoin'],
2931
options: { index: 0, scope: BTC_CAIP2 },
3032
type: 'bip122:p2wpkh',
33+
scopes: [],
3134
},
3235
];
3336

@@ -55,6 +58,7 @@ export const MOCK_SOLANA_ACCOUNTS = [
5558
methods: ['signAndSendTransaction'],
5659
options: { index: 0, scope: SOLANA_CAIP2 },
5760
type: 'solana:data-account',
61+
scopes: [],
5862
},
5963
];
6064

@@ -99,14 +103,14 @@ type MockOperationCallback = ({
99103
keyring,
100104
}: {
101105
keyring: MockSnapKeyring;
102-
}) => Promise<Json>;
106+
}) => Promise<any>;
103107

104108
export const getMockWithSnapKeyring = (
105109
{ submitRequest = jest.fn(), resolveAccountAddress = jest.fn() } = {
106110
submitRequest: jest.fn(),
107111
resolveAccountAddress: jest.fn(),
108112
},
109-
) => {
113+
): WithSnapKeyringFunction => {
110114
return async (callback: MockOperationCallback) =>
111115
callback({
112116
keyring: {

0 commit comments

Comments
 (0)