Skip to content

Commit c5f0d57

Browse files
Chain ID -> Scope
1 parent e0b844a commit c5f0d57

File tree

8 files changed

+41
-41
lines changed

8 files changed

+41
-41
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('MultichainRoutingController', () => {
5151
'MultichainRoutingController:handleRequest',
5252
{
5353
connectedAddresses: BTC_CONNECTED_ACCOUNTS,
54-
chainId: BTC_CAIP2,
54+
scope: BTC_CAIP2,
5555
request: {
5656
method: 'btc_sendmany',
5757
params: {
@@ -104,7 +104,7 @@ describe('MultichainRoutingController', () => {
104104
'MultichainRoutingController:handleRequest',
105105
{
106106
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
107-
chainId: SOLANA_CAIP2,
107+
scope: SOLANA_CAIP2,
108108
request: {
109109
method: 'signAndSendTransaction',
110110
params: {
@@ -154,7 +154,7 @@ describe('MultichainRoutingController', () => {
154154
'MultichainRoutingController:handleRequest',
155155
{
156156
connectedAddresses: [],
157-
chainId: SOLANA_CAIP2,
157+
scope: SOLANA_CAIP2,
158158
request: {
159159
method: 'getVersion',
160160
},
@@ -190,7 +190,7 @@ describe('MultichainRoutingController', () => {
190190
await expect(
191191
messenger.call('MultichainRoutingController:handleRequest', {
192192
connectedAddresses: [],
193-
chainId: SOLANA_CAIP2,
193+
scope: SOLANA_CAIP2,
194194
request: {
195195
method: 'getVersion',
196196
},

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { BaseController } from '@metamask/base-controller';
77
import type { GetPermissions } from '@metamask/permission-controller';
88
import { rpcErrors } from '@metamask/rpc-errors';
99
import {
10-
getProtocolCaveatChains,
10+
getProtocolCaveatScopes,
1111
SnapEndowments,
1212
} from '@metamask/snaps-rpc-methods';
1313
import type {
@@ -136,7 +136,7 @@ export class MultichainRoutingController extends BaseController<
136136

137137
async #resolveRequestAddress(
138138
snapId: SnapId,
139-
chainId: CaipChainId,
139+
scope: CaipChainId,
140140
request: JsonRpcRequest,
141141
) {
142142
try {
@@ -148,7 +148,7 @@ export class MultichainRoutingController extends BaseController<
148148
request: {
149149
method: '',
150150
params: {
151-
chainId,
151+
scope,
152152
request,
153153
},
154154
},
@@ -164,11 +164,11 @@ export class MultichainRoutingController extends BaseController<
164164

165165
async #getAccountSnap(
166166
connectedAddresses: CaipAccountId[],
167-
chainId: CaipChainId,
167+
scope: CaipChainId,
168168
request: JsonRpcRequest,
169169
) {
170170
const accounts = this.messagingSystem
171-
.call('AccountsController:listMultichainAccounts', chainId)
171+
.call('AccountsController:listMultichainAccounts', scope)
172172
.filter(
173173
(account: InternalAccount) =>
174174
account.metadata.snap?.enabled &&
@@ -187,7 +187,7 @@ export class MultichainRoutingController extends BaseController<
187187
// Attempt to resolve the address that should be used for signing.
188188
const address = await this.#resolveRequestAddress(
189189
resolutionSnapId,
190-
chainId,
190+
scope,
191191
request,
192192
);
193193

@@ -214,7 +214,7 @@ export class MultichainRoutingController extends BaseController<
214214
};
215215
}
216216

217-
#getProtocolSnaps(chainId: CaipChainId) {
217+
#getProtocolSnaps(scope: CaipChainId) {
218218
const allSnaps = this.messagingSystem.call('SnapController:getAll');
219219
const filteredSnaps = getRunnableSnaps(allSnaps);
220220

@@ -225,11 +225,11 @@ export class MultichainRoutingController extends BaseController<
225225
);
226226
if (permissions && hasProperty(permissions, SnapEndowments.Protocol)) {
227227
const permission = permissions[SnapEndowments.Protocol];
228-
const chains = getProtocolCaveatChains(permission);
229-
if (chains && hasProperty(chains, chainId)) {
228+
const scopes = getProtocolCaveatScopes(permission);
229+
if (scopes && hasProperty(scopes, scope)) {
230230
accumulator.push({
231231
snapId: snap.id,
232-
methods: chains[chainId].methods,
232+
methods: scopes[scope].methods,
233233
});
234234
}
235235
}
@@ -240,12 +240,12 @@ export class MultichainRoutingController extends BaseController<
240240

241241
async handleRequest({
242242
connectedAddresses,
243-
chainId,
243+
scope,
244244
request,
245245
}: {
246246
connectedAddresses: CaipAccountId[];
247247
origin: string;
248-
chainId: CaipChainId;
248+
scope: CaipChainId;
249249
request: JsonRpcRequest;
250250
}): Promise<unknown> {
251251
// TODO: Determine if the request is already validated here?
@@ -254,7 +254,7 @@ export class MultichainRoutingController extends BaseController<
254254
// If the RPC request can be serviced by an account Snap, route it there.
255255
const accountSnap = await this.#getAccountSnap(
256256
connectedAddresses,
257-
chainId,
257+
scope,
258258
request,
259259
);
260260
if (accountSnap) {
@@ -263,13 +263,13 @@ export class MultichainRoutingController extends BaseController<
263263
address: accountSnap.address,
264264
method,
265265
params,
266-
chainId,
266+
chainId: scope,
267267
});
268268
}
269269

270270
// If the RPC request cannot be serviced by an account Snap,
271271
// but has a protocol Snap available, route it there.
272-
const protocolSnaps = this.#getProtocolSnaps(chainId);
272+
const protocolSnaps = this.#getProtocolSnaps(scope);
273273
const protocolSnap = protocolSnaps.find((snap) =>
274274
snap.methods.includes(method),
275275
);
@@ -281,7 +281,7 @@ export class MultichainRoutingController extends BaseController<
281281
method: '',
282282
params: {
283283
request,
284-
chainId,
284+
scope,
285285
},
286286
},
287287
handler: HandlerType.OnProtocolRequest,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const MOCK_SOLANA_SNAP_PERMISSIONS: Record<
7777
[SnapEndowments.Protocol]: {
7878
caveats: [
7979
{
80-
type: SnapCaveatType.ProtocolSnapChains,
80+
type: SnapCaveatType.ProtocolSnapScopes,
8181
value: { [SOLANA_CAIP2]: { methods: ['getVersion'] } },
8282
},
8383
],

packages/snaps-rpc-methods/src/endowments/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,4 @@ export { getChainIdsCaveat, getLookupMatchersCaveat } from './name-lookup';
128128
export { getKeyringCaveatOrigins } from './keyring';
129129
export { getMaxRequestTimeCaveat } from './caveats';
130130
export { getCronjobCaveatJobs } from './cronjob';
131-
export { getProtocolCaveatChains } from './protocol';
131+
export { getProtocolCaveatScopes } from './protocol';

packages/snaps-rpc-methods/src/endowments/protocol.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
} from '@metamask/permission-controller';
1111
import { PermissionType, SubjectType } from '@metamask/permission-controller';
1212
import { rpcErrors } from '@metamask/rpc-errors';
13-
import { ProtocolChainsStruct, SnapCaveatType } from '@metamask/snaps-utils';
13+
import { ProtocolScopesStruct, SnapCaveatType } from '@metamask/snaps-utils';
1414
import type { Infer } from '@metamask/superstruct';
1515
import type { Json, NonEmptyArray } from '@metamask/utils';
1616
import {
@@ -52,7 +52,7 @@ const specificationBuilder: PermissionSpecificationBuilder<
5252
allowedCaveats: [SnapCaveatType.ChainIds, SnapCaveatType.MaxRequestTime],
5353
endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,
5454
validator: createGenericPermissionValidator([
55-
{ type: SnapCaveatType.ProtocolSnapChains },
55+
{ type: SnapCaveatType.ProtocolSnapScopes },
5656
{ type: SnapCaveatType.MaxRequestTime, optional: true },
5757
]),
5858
subjectTypes: [SubjectType.Snap],
@@ -83,29 +83,29 @@ export function getProtocolCaveatMapper(
8383

8484
if (value.chains) {
8585
caveats.push({
86-
type: SnapCaveatType.ProtocolSnapChains,
86+
type: SnapCaveatType.ProtocolSnapScopes,
8787
value: value.chains,
8888
});
8989
}
9090

9191
return { caveats: caveats as NonEmptyArray<CaveatConstraint> };
9292
}
9393

94-
export type ProtocolChains = Infer<typeof ProtocolChainsStruct>;
94+
export type ProtocolScopes = Infer<typeof ProtocolScopesStruct>;
9595

9696
/**
97-
* Getter function to get the {@link ProtocolSnapChains} caveat value from a
97+
* Getter function to get the {@link ProtocolSnapScopes} caveat value from a
9898
* permission.
9999
*
100100
* @param permission - The permission to get the caveat value from.
101101
* @returns The caveat value.
102102
*/
103-
export function getProtocolCaveatChains(
103+
export function getProtocolCaveatScopes(
104104
permission?: PermissionConstraint,
105-
): ProtocolChains | null {
105+
): ProtocolScopes | null {
106106
const caveat = permission?.caveats?.find(
107-
(permCaveat) => permCaveat.type === SnapCaveatType.ProtocolSnapChains,
108-
) as Caveat<string, ProtocolChains> | undefined;
107+
(permCaveat) => permCaveat.type === SnapCaveatType.ProtocolSnapScopes,
108+
) as Caveat<string, ProtocolScopes> | undefined;
109109

110110
return caveat ? caveat.value : null;
111111
}
@@ -126,18 +126,18 @@ function validateCaveat(caveat: Caveat<string, any>): void {
126126
const { value } = caveat;
127127
assertStruct(
128128
value,
129-
ProtocolChainsStruct,
129+
ProtocolScopesStruct,
130130
'Invalid chains specified',
131131
rpcErrors.invalidParams,
132132
);
133133
}
134134

135135
export const protocolCaveatSpecifications: Record<
136-
SnapCaveatType.ProtocolSnapChains,
136+
SnapCaveatType.ProtocolSnapScopes,
137137
CaveatSpecificationConstraint
138138
> = {
139-
[SnapCaveatType.ProtocolSnapChains]: Object.freeze({
140-
type: SnapCaveatType.ProtocolSnapChains,
139+
[SnapCaveatType.ProtocolSnapScopes]: Object.freeze({
140+
type: SnapCaveatType.ProtocolSnapScopes,
141141
validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),
142142
}),
143143
};

packages/snaps-sdk/src/types/handlers/protocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
* @param args - The request arguments.
1515
* @param args.origin - The origin of the request. This can be the ID of another
1616
* Snap, or the URL of a website.
17-
* @param args.chainId - The chain ID of the request.
17+
* @param args.scope - The scope of the request.
1818
* @param args.request - The protocol request sent to the Snap. This includes
1919
* the method name and parameters.
2020
* @returns The response to the protocol request. This must be a
@@ -25,6 +25,6 @@ export type OnProtocolRequestHandler<
2525
Params extends JsonRpcParams = JsonRpcParams,
2626
> = (args: {
2727
origin: string;
28-
chainId: CaipChainId;
28+
scope: CaipChainId;
2929
request: JsonRpcRequest<Params>;
3030
}) => Promise<Json>;

packages/snaps-utils/src/caveats.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export enum SnapCaveatType {
5555
MaxRequestTime = 'maxRequestTime',
5656

5757
/**
58-
* Caveat specifying a list of RPC methods serviced by an endowment.
58+
* Caveat specifying a list of scopes serviced by an endowment.
5959
*/
60-
ProtocolSnapChains = 'protocolSnapChains',
60+
ProtocolSnapScopes = 'protocolSnapScopes',
6161
}

packages/snaps-utils/src/manifest/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export const MaxRequestTimeStruct = size(
175175
MAXIMUM_REQUEST_TIMEOUT,
176176
);
177177

178-
export const ProtocolChainsStruct = record(
178+
export const ProtocolScopesStruct = record(
179179
CaipChainIdStruct,
180180
object({ methods: array(string()) }),
181181
);
@@ -207,7 +207,7 @@ export const PermissionsStruct: Describe<InitialPermissions> = type({
207207
'endowment:protocol': optional(
208208
mergeStructs(
209209
HandlerCaveatsStruct,
210-
object({ chains: ProtocolChainsStruct }),
210+
object({ chains: ProtocolScopesStruct }),
211211
),
212212
),
213213
'endowment:lifecycle-hooks': optional(HandlerCaveatsStruct),

0 commit comments

Comments
 (0)