Skip to content

Commit

Permalink
Handle origins and use proper method for address resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 15, 2024
1 parent 6cf774a commit 503050a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ describe('MultichainRoutingController', () => {
rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
// TODO: Use proper handler
if (handler === HandlerType.OnProtocolRequest) {
if (handler === HandlerType.OnKeyringRequest) {
return null;
}
throw new Error('Unmocked request');
Expand Down Expand Up @@ -97,8 +96,7 @@ describe('MultichainRoutingController', () => {
rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
// TODO: Use proper handler
if (handler === HandlerType.OnProtocolRequest) {
if (handler === HandlerType.OnKeyringRequest) {
return { address: SOLANA_CONNECTED_ACCOUNTS[0] };
}
throw new Error('Unmocked request');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,20 @@ export class MultichainRoutingController extends BaseController<
request: JsonRpcRequest,
) {
try {
// TODO: Decide if we should call this using another abstraction.
const result = (await this.messagingSystem.call(
'SnapController:handleRequest',
{
snapId,
origin: 'metamask',
request: {
method: '',
method: 'keyring_resolveAccountAddress',
params: {
scope,
request,
},
},
handler: HandlerType.OnProtocolRequest, // TODO: Export and request format
handler: HandlerType.OnKeyringRequest,
},
)) as { address: CaipAccountId } | null;
const address = result?.address;
Expand Down Expand Up @@ -233,6 +234,7 @@ export class MultichainRoutingController extends BaseController<

async handleRequest({
connectedAddresses,
origin,
scope,
request,
}: {
Expand Down Expand Up @@ -272,10 +274,13 @@ export class MultichainRoutingController extends BaseController<
if (protocolSnap) {
return this.messagingSystem.call('SnapController:handleRequest', {
snapId: protocolSnap.snapId,
origin: 'metamask', // TODO: Determine origin of these requests?
origin: 'metamask',
request: {
method: '',
params: {
// We are overriding the origin here, so that the Snap gets the proper origin
// while the permissions check is skipped due to the requesting origin being metamask.
origin,
request,
scope,
},
Expand Down
15 changes: 14 additions & 1 deletion packages/snaps-execution-environments/src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
assertIsOnSignatureRequestArguments,
assertIsOnNameLookupRequestArguments,
assertIsOnUserInputRequestArguments,
assertIsOnProtocolRequestArguments,
} from './validation';

export type CommandMethodsMapping = {
Expand Down Expand Up @@ -74,9 +75,21 @@ export function getHandlerArguments(
address,
};
}

case HandlerType.OnProtocolRequest: {
assertIsOnProtocolRequestArguments(request.params);

// For this specific handler we extract the origin from the parameters.
const {
origin: nestedOrigin,
request: nestedRequest,
scope,
} = request.params;
return { origin: nestedOrigin, request: nestedRequest, scope };
}

case HandlerType.OnRpcRequest:
case HandlerType.OnKeyringRequest:
case HandlerType.OnProtocolRequest: // TODO: Decide on origin
return { origin, request };

case HandlerType.OnCronjob:
Expand Down
30 changes: 30 additions & 0 deletions packages/snaps-execution-environments/src/common/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
assertStruct,
JsonRpcIdStruct,
JsonRpcParamsStruct,
JsonRpcRequestStruct,
JsonRpcSuccessStruct,
JsonRpcVersionStruct,
JsonStruct,
Expand Down Expand Up @@ -243,6 +244,35 @@ export function assertIsOnUserInputRequestArguments(
);
}

export const OnProtocolRequestArgumentsStruct = object({
origin: string(),
scope: ChainIdStruct,
request: JsonRpcRequestStruct,
});

export type OnProtocolRequestArguments = Infer<
typeof OnProtocolRequestArgumentsStruct
>;

/**
* Asserts that the given value is a valid {@link OnProtocolRequestArguments}
* object.
*
* @param value - The value to validate.
* @throws If the value is not a valid {@link OnProtocolRequestArguments}
* object.
*/
export function assertIsOnProtocolRequestArguments(
value: unknown,
): asserts value is OnProtocolRequestArguments {
assertStruct(
value,
OnProtocolRequestArgumentsStruct,
'Invalid request params',
rpcErrors.invalidParams,
);
}

const OkResponseStruct = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
Expand Down

0 comments on commit 503050a

Please sign in to comment.