Skip to content

Commit cce5daa

Browse files
authored
feat: useAccountGroupForPermissions 2 (#35402)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This PR create a new hook to retrieve the supported and connected multichain accounts, and caip25account ids from a specific caveat value. The hook is not used at the moment and will be used once the multichain account versions of the dapp connection components are completed. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/35111?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: null ## **Related issues** ## **Manual testing steps** N/a this hook is not being used right now. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> N/a this hook is not being used right now. ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent 251a358 commit cce5daa

7 files changed

Lines changed: 1138 additions & 126 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { EthScope } from '@metamask/keyring-api';
2+
import { anyScopesMatch, scopeMatches } from './scope-utils';
3+
4+
describe('scope-utils', () => {
5+
describe('anyScopesMatch', () => {
6+
it('returns false for empty account scopes', () => {
7+
expect(anyScopesMatch([], 'eip155:1')).toBe(false);
8+
// @ts-expect-error - testing undefined scope
9+
expect(anyScopesMatch(undefined, 'eip155:1')).toBe(false);
10+
});
11+
12+
it('returns true for direct scope match', () => {
13+
const accountScopes = ['eip155:1', 'solana:mainnet'];
14+
expect(anyScopesMatch(accountScopes, 'eip155:1')).toBe(true);
15+
expect(anyScopesMatch(accountScopes, 'solana:mainnet')).toBe(true);
16+
});
17+
18+
it('returns false for non-matching scope', () => {
19+
const accountScopes = ['eip155:1', 'solana:mainnet'];
20+
expect(anyScopesMatch(accountScopes, 'eip155:137')).toBe(false);
21+
expect(
22+
anyScopesMatch(
23+
accountScopes,
24+
'bip122:000000000019d6689c085ae165831e93',
25+
),
26+
).toBe(false);
27+
});
28+
29+
describe('eip155:0 wildcard handling', () => {
30+
it('returns true when requesting eip155:0 and account has any EVM scope', () => {
31+
const accountScopes = ['eip155:1', 'eip155:137'];
32+
expect(anyScopesMatch(accountScopes, 'eip155:0')).toBe(true);
33+
});
34+
35+
it('returns true when requesting eip155:0 and account has eip155:0 scope', () => {
36+
const accountScopes = ['eip155:0'];
37+
expect(anyScopesMatch(accountScopes, 'eip155:0')).toBe(true);
38+
});
39+
40+
it('returns false when requesting eip155:0 and account has no EVM scopes', () => {
41+
const accountScopes = [
42+
'solana:mainnet',
43+
'bip122:000000000019d6689c085ae165831e93',
44+
];
45+
expect(anyScopesMatch(accountScopes, 'eip155:0')).toBe(false);
46+
});
47+
});
48+
49+
describe('specific EVM chain handling', () => {
50+
it('returns true when requesting specific EVM chain and account has eip155:0 scope', () => {
51+
const accountScopes = [EthScope.Eoa]; // eip155:0
52+
expect(anyScopesMatch(accountScopes, 'eip155:1')).toBe(true);
53+
expect(anyScopesMatch(accountScopes, 'eip155:137')).toBe(true);
54+
});
55+
56+
it('returns true when requesting specific EVM chain and account has exact match', () => {
57+
const accountScopes = ['eip155:1'];
58+
expect(anyScopesMatch(accountScopes, 'eip155:1')).toBe(true);
59+
});
60+
61+
it('returns false when requesting specific EVM chain and account has different EVM chain', () => {
62+
const accountScopes = ['eip155:137'];
63+
expect(anyScopesMatch(accountScopes, 'eip155:1')).toBe(false);
64+
});
65+
});
66+
67+
describe('non-EVM scope handling', () => {
68+
it('returns true for exact non-EVM scope match', () => {
69+
const accountScopes = [
70+
'solana:mainnet',
71+
'bip122:000000000019d6689c085ae165831e93',
72+
];
73+
expect(anyScopesMatch(accountScopes, 'solana:mainnet')).toBe(true);
74+
expect(
75+
anyScopesMatch(
76+
accountScopes,
77+
'bip122:000000000019d6689c085ae165831e93',
78+
),
79+
).toBe(true);
80+
});
81+
82+
it('returns false for non-matching non-EVM scope', () => {
83+
const accountScopes = ['solana:mainnet'];
84+
expect(
85+
anyScopesMatch(
86+
accountScopes,
87+
'bip122:000000000019d6689c085ae165831e93',
88+
),
89+
).toBe(false);
90+
});
91+
});
92+
93+
describe('malformed scope handling', () => {
94+
it('returns false for malformed target scope', () => {
95+
const accountScopes = ['eip155:1'];
96+
expect(anyScopesMatch(accountScopes, 'invalid-scope')).toBe(false);
97+
expect(anyScopesMatch(accountScopes, 'eip155')).toBe(false);
98+
expect(anyScopesMatch(accountScopes, '')).toBe(false);
99+
});
100+
});
101+
});
102+
103+
describe('scopeMatches', () => {
104+
it('returns true for matching single scope', () => {
105+
expect(scopeMatches('eip155:1', 'eip155:1')).toBe(true);
106+
expect(scopeMatches('solana:mainnet', 'solana:mainnet')).toBe(true);
107+
});
108+
109+
it('returns false for non-matching single scope', () => {
110+
expect(scopeMatches('eip155:1', 'eip155:137')).toBe(false);
111+
expect(scopeMatches('solana:mainnet', 'eip155:1')).toBe(false);
112+
});
113+
114+
it('handles eip155:0 wildcard correctly', () => {
115+
expect(scopeMatches('eip155:1', 'eip155:0')).toBe(true);
116+
expect(scopeMatches('eip155:0', 'eip155:1')).toBe(true);
117+
expect(scopeMatches('solana:mainnet', 'eip155:0')).toBe(false);
118+
});
119+
});
120+
});
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import {
2+
KnownCaipNamespace,
3+
parseCaipChainId,
4+
CaipChainId,
5+
CaipNamespace,
6+
} from '@metamask/utils';
7+
import { EthScope } from '@metamask/keyring-api';
8+
9+
/**
10+
* Helper function to check if any of the account scopes match the target scope.
11+
* Handles the special case of eip155:0 (wildcard) which matches any EVM scope.
12+
*
13+
* This function provides a consistent approach to scope matching across the codebase,
14+
* ensuring that eip155:0 wildcard logic is handled uniformly in both selectors and
15+
* permission/handler level code.
16+
*
17+
* @param accountScopes - Array of scope strings from the account
18+
* @param targetScope - The target scope to match against
19+
* @returns True if any scope matches, false otherwise
20+
*/
21+
export const anyScopesMatch = (
22+
accountScopes: string[],
23+
targetScope: string,
24+
): boolean => {
25+
if (!Array.isArray(accountScopes) || accountScopes.length === 0) {
26+
return false;
27+
}
28+
29+
// Direct match
30+
if (accountScopes.includes(targetScope)) {
31+
return true;
32+
}
33+
34+
try {
35+
const parsed = parseCaipChainId(targetScope as `${string}:${string}`);
36+
const { namespace, reference } = parsed;
37+
38+
if (namespace === KnownCaipNamespace.Eip155) {
39+
// If requesting eip155:0 (wildcard), include any account that has any EVM scope
40+
if (reference === '0') {
41+
return accountScopes.some((scope) =>
42+
scope.startsWith(`${KnownCaipNamespace.Eip155}:`),
43+
);
44+
}
45+
46+
// For a specific EVM chain, include accounts that have the wildcard scope
47+
return accountScopes.includes(EthScope.Eoa);
48+
}
49+
} catch {
50+
// If parsing fails, fall back to exact match only
51+
}
52+
53+
return false;
54+
};
55+
56+
/**
57+
* Helper function to check if a single account scope matches the target scope.
58+
* This is a convenience wrapper around anyScopesMatch for single scope comparisons.
59+
*
60+
* @param accountScope - Single scope string from the account
61+
* @param targetScope - The target scope to match against
62+
* @returns True if the scope matches, false otherwise
63+
*/
64+
export const scopeMatches = (
65+
accountScope: string,
66+
targetScope: string,
67+
): boolean => {
68+
return anyScopesMatch([accountScope], targetScope);
69+
};
70+
71+
/**
72+
* Checks if an account supports the requested chain IDs
73+
*
74+
* @param accountScopes - Array of account scopes to check
75+
* @param requestedChainIds - Array of requested chain IDs to match against
76+
* @returns True if any scope matches any requested chain ID
77+
*/
78+
export const hasChainIdSupport = (
79+
accountScopes: string[] | undefined,
80+
requestedChainIds: CaipChainId[],
81+
): boolean => {
82+
if (!accountScopes || requestedChainIds.length === 0) {
83+
return false;
84+
}
85+
86+
return accountScopes.some((accountScope) =>
87+
requestedChainIds.some((requestedChainId) =>
88+
anyScopesMatch([accountScope], requestedChainId),
89+
),
90+
);
91+
};
92+
93+
/**
94+
* Checks if an account supports the requested namespaces
95+
*
96+
* @param accountScopes - Array of account scopes to check
97+
* @param requestedNamespaces - Set of requested namespaces to match against
98+
* @returns True if any scope namespace matches any requested namespace
99+
*/
100+
export const hasNamespaceSupport = (
101+
accountScopes: string[] | undefined,
102+
requestedNamespaces: Set<CaipNamespace>,
103+
): boolean => {
104+
if (!accountScopes || requestedNamespaces.size === 0) {
105+
return false;
106+
}
107+
108+
for (const scope of accountScopes) {
109+
const [scopeNamespace] = scope.split(':');
110+
if (
111+
scopeNamespace &&
112+
requestedNamespaces.has(scopeNamespace as CaipNamespace)
113+
) {
114+
return true;
115+
}
116+
}
117+
return false;
118+
};

0 commit comments

Comments
 (0)