Skip to content

Commit 503050a

Browse files
Handle origins and use proper method for address resolution
1 parent 6cf774a commit 503050a

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ describe('MultichainRoutingController', () => {
4040
rootMessenger.registerActionHandler(
4141
'SnapController:handleRequest',
4242
async ({ handler }) => {
43-
// TODO: Use proper handler
44-
if (handler === HandlerType.OnProtocolRequest) {
43+
if (handler === HandlerType.OnKeyringRequest) {
4544
return null;
4645
}
4746
throw new Error('Unmocked request');
@@ -97,8 +96,7 @@ describe('MultichainRoutingController', () => {
9796
rootMessenger.registerActionHandler(
9897
'SnapController:handleRequest',
9998
async ({ handler }) => {
100-
// TODO: Use proper handler
101-
if (handler === HandlerType.OnProtocolRequest) {
99+
if (handler === HandlerType.OnKeyringRequest) {
102100
return { address: SOLANA_CONNECTED_ACCOUNTS[0] };
103101
}
104102
throw new Error('Unmocked request');

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,20 @@ export class MultichainRoutingController extends BaseController<
133133
request: JsonRpcRequest,
134134
) {
135135
try {
136+
// TODO: Decide if we should call this using another abstraction.
136137
const result = (await this.messagingSystem.call(
137138
'SnapController:handleRequest',
138139
{
139140
snapId,
140141
origin: 'metamask',
141142
request: {
142-
method: '',
143+
method: 'keyring_resolveAccountAddress',
143144
params: {
144145
scope,
145146
request,
146147
},
147148
},
148-
handler: HandlerType.OnProtocolRequest, // TODO: Export and request format
149+
handler: HandlerType.OnKeyringRequest,
149150
},
150151
)) as { address: CaipAccountId } | null;
151152
const address = result?.address;
@@ -233,6 +234,7 @@ export class MultichainRoutingController extends BaseController<
233234

234235
async handleRequest({
235236
connectedAddresses,
237+
origin,
236238
scope,
237239
request,
238240
}: {
@@ -272,10 +274,13 @@ export class MultichainRoutingController extends BaseController<
272274
if (protocolSnap) {
273275
return this.messagingSystem.call('SnapController:handleRequest', {
274276
snapId: protocolSnap.snapId,
275-
origin: 'metamask', // TODO: Determine origin of these requests?
277+
origin: 'metamask',
276278
request: {
277279
method: '',
278280
params: {
281+
// We are overriding the origin here, so that the Snap gets the proper origin
282+
// while the permissions check is skipped due to the requesting origin being metamask.
283+
origin,
279284
request,
280285
scope,
281286
},

packages/snaps-execution-environments/src/common/commands.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
assertIsOnSignatureRequestArguments,
1616
assertIsOnNameLookupRequestArguments,
1717
assertIsOnUserInputRequestArguments,
18+
assertIsOnProtocolRequestArguments,
1819
} from './validation';
1920

2021
export type CommandMethodsMapping = {
@@ -74,9 +75,21 @@ export function getHandlerArguments(
7475
address,
7576
};
7677
}
78+
79+
case HandlerType.OnProtocolRequest: {
80+
assertIsOnProtocolRequestArguments(request.params);
81+
82+
// For this specific handler we extract the origin from the parameters.
83+
const {
84+
origin: nestedOrigin,
85+
request: nestedRequest,
86+
scope,
87+
} = request.params;
88+
return { origin: nestedOrigin, request: nestedRequest, scope };
89+
}
90+
7791
case HandlerType.OnRpcRequest:
7892
case HandlerType.OnKeyringRequest:
79-
case HandlerType.OnProtocolRequest: // TODO: Decide on origin
8093
return { origin, request };
8194

8295
case HandlerType.OnCronjob:

packages/snaps-execution-environments/src/common/validation.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
assertStruct,
2626
JsonRpcIdStruct,
2727
JsonRpcParamsStruct,
28+
JsonRpcRequestStruct,
2829
JsonRpcSuccessStruct,
2930
JsonRpcVersionStruct,
3031
JsonStruct,
@@ -243,6 +244,35 @@ export function assertIsOnUserInputRequestArguments(
243244
);
244245
}
245246

247+
export const OnProtocolRequestArgumentsStruct = object({
248+
origin: string(),
249+
scope: ChainIdStruct,
250+
request: JsonRpcRequestStruct,
251+
});
252+
253+
export type OnProtocolRequestArguments = Infer<
254+
typeof OnProtocolRequestArgumentsStruct
255+
>;
256+
257+
/**
258+
* Asserts that the given value is a valid {@link OnProtocolRequestArguments}
259+
* object.
260+
*
261+
* @param value - The value to validate.
262+
* @throws If the value is not a valid {@link OnProtocolRequestArguments}
263+
* object.
264+
*/
265+
export function assertIsOnProtocolRequestArguments(
266+
value: unknown,
267+
): asserts value is OnProtocolRequestArguments {
268+
assertStruct(
269+
value,
270+
OnProtocolRequestArgumentsStruct,
271+
'Invalid request params',
272+
rpcErrors.invalidParams,
273+
);
274+
}
275+
246276
const OkResponseStruct = object({
247277
id: JsonRpcIdStruct,
248278
jsonrpc: JsonRpcVersionStruct,

0 commit comments

Comments
 (0)