Skip to content

Commit 33cca72

Browse files
Address PR comments
1 parent 05147ed commit 33cca72

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

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

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ export class MultichainRouter {
139139
);
140140
}
141141

142+
/**
143+
* Attempts to resolve the account address to use for a given request by inspecting the request itself.
144+
*
145+
* The request is sent to to an account Snap via the SnapKeyring that will attempt this resolution.
146+
*
147+
* @param snapId - The ID of the Snap to send the request to.
148+
* @param scope - The CAIP-2 scope for the request.
149+
* @param request - The JSON-RPC request.
150+
* @returns The resolved address if found, otherwise null.
151+
* @throws If the invocation of the SnapKeyring fails.
152+
*/
142153
async #resolveRequestAddress(
143154
snapId: SnapId,
144155
scope: CaipChainId,
@@ -155,20 +166,36 @@ export class MultichainRouter {
155166
}
156167
}
157168

158-
async #getAccountSnap(
169+
/**
170+
* Get the account ID of the account that should service the RPC request via an account Snap.
171+
*
172+
* This function checks whether any accounts exist that can service a given request by
173+
* using a combination of the resolveAccountAddress functionality and the connected accounts.
174+
*
175+
* If an account is expected to service this request but none is found, the function will throw.
176+
*
177+
* @param connectedAddresses - The CAIP-10 addresses connected to the requesting origin.
178+
* @param scope - The CAIP-2 scope for the request.
179+
* @param request - The JSON-RPC request.
180+
* @returns An account ID if found, otherwise null.
181+
* @throws If no account is found, but the accounts exist that could service the request.
182+
*/
183+
async #getSnapAccountId(
159184
connectedAddresses: CaipAccountId[],
160185
scope: CaipChainId,
161186
request: JsonRpcRequest,
162187
) {
163188
const accounts = this.#messenger
164189
.call('AccountsController:listMultichainAccounts', scope)
165190
.filter(
166-
(account: InternalAccount) =>
167-
account.metadata.snap?.enabled &&
191+
(
192+
account: InternalAccount,
193+
): account is InternalAccount & {
194+
metadata: Required<InternalAccount['metadata']>;
195+
} =>
196+
Boolean(account.metadata.snap?.enabled) &&
168197
account.methods.includes(request.method),
169-
) as (InternalAccount & {
170-
metadata: Required<InternalAccount['metadata']>;
171-
})[];
198+
);
172199

173200
// If no accounts can service the request, return null.
174201
if (accounts.length === 0) {
@@ -201,12 +228,18 @@ export class MultichainRouter {
201228
throw rpcErrors.invalidParams();
202229
}
203230

204-
return {
205-
accountId: selectedAccount.id,
206-
snapId: selectedAccount.metadata.snap.id,
207-
};
231+
return selectedAccount.id;
208232
}
209233

234+
/**
235+
* Get all protocol Snaps that can service a given CAIP-2 scope.
236+
*
237+
* Protocol Snaps are deemed fit to service a scope if they are runnable
238+
* and have the proper permissions set for the scope.
239+
*
240+
* @param scope - A CAIP-2 scope.
241+
* @returns A list of all the protocol Snaps available and their RPC methods.
242+
*/
210243
#getProtocolSnaps(scope: CaipChainId) {
211244
const allSnaps = this.#messenger.call('SnapController:getAll');
212245
const filteredSnaps = getRunnableSnaps(allSnaps);
@@ -258,16 +291,16 @@ export class MultichainRouter {
258291
const { method, params } = request;
259292

260293
// If the RPC request can be serviced by an account Snap, route it there.
261-
const accountSnap = await this.#getAccountSnap(
294+
const accountId = await this.#getSnapAccountId(
262295
connectedAddresses,
263296
scope,
264297
request,
265298
);
266299

267-
if (accountSnap) {
300+
if (accountId) {
268301
return this.#withSnapKeyring(async (keyring) =>
269302
keyring.submitRequest({
270-
id: accountSnap.accountId,
303+
id: accountId,
271304
scope,
272305
method,
273306
params: params as JsonRpcParams,

0 commit comments

Comments
 (0)