Skip to content

fix: add accounts to caip25 permissions when adding chain permissions to ethereum-provider enabled snaps #33111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/scripts/migrations/160.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe(`migration #${version}`, () => {
expect(newStorage.meta).toStrictEqual({ version });
});

it('adds the network endowment to Snaps with the `endowment:ethereum-provider` permission', async () => {
it('adds the `endowment:caip25` permission with the current globally selected chainId (and no accounts) permissioned to Snaps with the `endowment:ethereum-provider` permission and which do not already a `endowment:caip25` permission', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('adds the `endowment:caip25` permission with the current globally selected chainId (and no accounts) permissioned to Snaps with the `endowment:ethereum-provider` permission and which do not already a `endowment:caip25` permission', async () => {
it('adds the `endowment:caip25` permission with the current globally selected chainId (and no accounts) permissioned to Snaps with the `endowment:ethereum-provider` permission and which do not already have a `endowment:caip25` permission', async () => {

Copy link
Contributor Author

@adonesky1 adonesky1 May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here: 483e2f8

const oldStorage = {
meta: { version: oldVersion },
data: {
Expand Down Expand Up @@ -122,7 +122,7 @@ describe(`migration #${version}`, () => {
});
});

it('merges with an existing permission if the Snap already has `endowment:caip25`', async () => {
it('merges with an existing permission and adds existing account permissions along with the current globally selected chainId if the Snap already has `endowment:caip25`', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('merges with an existing permission and adds existing account permissions along with the current globally selected chainId if the Snap already has `endowment:caip25`', async () => {
it('adds the current globally selected chainId with the existing permitted eth accounts to the existing `endowment:caip25` permission if the Snap already has `endowment:caip25` permission', async () => {

maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here: 9e3276c

const oldStorage = {
meta: { version: oldVersion },
data: {
Expand Down Expand Up @@ -166,7 +166,7 @@ describe(`migration #${version}`, () => {
optionalScopes: {
'wallet:eip155': {
accounts: [
'0x1234567890123456789012345678901234567890',
'wallet:eip155:0x1234567890123456789012345678901234567890',
],
},
},
Expand Down Expand Up @@ -214,11 +214,13 @@ describe(`migration #${version}`, () => {
optionalScopes: {
'wallet:eip155': {
accounts: [
'0x1234567890123456789012345678901234567890',
'wallet:eip155:0x1234567890123456789012345678901234567890',
],
},
'eip155:1': {
accounts: [],
accounts: [
'eip155:1:0x1234567890123456789012345678901234567890',
],
},
},
requiredScopes: {},
Expand Down
22 changes: 21 additions & 1 deletion app/scripts/migrations/160.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
Caip25EndowmentPermissionName,
addPermittedEthChainId,
Caip25CaveatValue,
setEthAccounts,
getEthAccounts,
} from '@metamask/chain-agnostic-permission';

type GenericPermissionControllerSubject =
Expand Down Expand Up @@ -206,6 +208,24 @@
currentChainId,
);

let caip25PermissionCaveatWithCurrentChainIdAndAccountsSet =
caip25PermissionCaveatWithCurrentChainIdSet;

if (subject.permissions[Caip25EndowmentPermissionName]) {
const existingCaveat = subject.permissions[
Caip25EndowmentPermissionName
]?.caveats?.find((caveat) => caveat.type === Caip25CaveatType);
if (existingCaveat && existingCaveat.value) {

Check failure on line 218 in app/scripts/migrations/160.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Prefer using an optional chain expression instead, as it's more concise and easier to read
const alreadyPermissionedAccounts = getEthAccounts(
existingCaveat.value as Caip25CaveatValue,
);
caip25PermissionCaveatWithCurrentChainIdAndAccountsSet = setEthAccounts(
caip25PermissionCaveatWithCurrentChainIdSet,
alreadyPermissionedAccounts,
);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be deduplicated using the getExistingCaip25PermissionCaveat(subject) above.

Copy link
Contributor Author

@adonesky1 adonesky1 May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here: 483e2f8


const {
date = Date.now(),
id = nanoid(),
Expand All @@ -221,7 +241,7 @@
caveats: [
{
type: Caip25CaveatType,
value: caip25PermissionCaveatWithCurrentChainIdSet,
value: caip25PermissionCaveatWithCurrentChainIdAndAccountsSet,
},
],
date,
Expand Down
Loading