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 all 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 which have a `endowment:ethereum-provider` permission but which do not already have a `endowment:caip25` permission', async () => {
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('adds the current globally selected chainId as a new scope with the existing permitted eth accounts to an existing snap `endowment:caip25` permission', async () => {
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: 20 additions & 2 deletions app/scripts/migrations/160.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
Caip25EndowmentPermissionName,
addPermittedEthChainId,
Caip25CaveatValue,
setEthAccounts,
getEthAccounts,
} from '@metamask/chain-agnostic-permission';

type GenericPermissionControllerSubject =
Expand Down Expand Up @@ -201,11 +203,27 @@ function transformState(state: Record<string, unknown>) {

updatedSubjects.push(key);

const existingCaip25Caveat = getExistingCaip25PermissionCaveat(subject);
const caip25PermissionCaveatWithCurrentChainIdSet = addPermittedEthChainId(
getExistingCaip25PermissionCaveat(subject),
existingCaip25Caveat,
currentChainId,
);

let caip25PermissionCaveatWithCurrentChainIdAndAccountsSet =
caip25PermissionCaveatWithCurrentChainIdSet;

if (existingCaip25Caveat) {
const existingPermittedAccounts = getEthAccounts(existingCaip25Caveat);
// if there are existing permitted accounts in the `wallet:eip155` scope
// we add them to the newly added (currentChainId) scope
if (existingPermittedAccounts.length > 0) {
caip25PermissionCaveatWithCurrentChainIdAndAccountsSet = setEthAccounts(
caip25PermissionCaveatWithCurrentChainIdSet,
existingPermittedAccounts,
);
}
}

const {
date = Date.now(),
id = nanoid(),
Expand All @@ -221,7 +239,7 @@ function transformState(state: Record<string, unknown>) {
caveats: [
{
type: Caip25CaveatType,
value: caip25PermissionCaveatWithCurrentChainIdSet,
value: caip25PermissionCaveatWithCurrentChainIdAndAccountsSet,
},
],
date,
Expand Down
Loading