Skip to content
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

[FIX] Migrate dapps access into permission controller state #5724

Merged
merged 2 commits into from
Feb 15, 2023
Merged
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
57 changes: 56 additions & 1 deletion app/store/migrations.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v1 as random } from 'uuid';
import { NetworksChainId } from '@metamask/controller-utils';
import AppConstants from '../core/AppConstants';
import { getAllNetworks, isSafeChainId } from '../util/networks';
Expand Down Expand Up @@ -326,6 +327,60 @@ export const migrations = {

return state;
},
13: (state) => {
// If for some reason we already have PermissionController state, bail out.
const hasPermissionControllerState = Boolean(
state.engine.backgroundState.PermissionController?.subjects,
);
if (hasPermissionControllerState) return state;

const { approvedHosts } = state.privacy;
const { selectedAddress } =
state.engine.backgroundState.PreferencesController;
gantunesr marked this conversation as resolved.
Show resolved Hide resolved

const hosts = Object.keys(approvedHosts);
// If no dapps connected, bail out.
if (hosts.length < 1) return state;

const { subjects } = hosts.reduce(
(accumulator, host, index) => ({
subjects: {
...accumulator.subjects,
[host]: {
origin: host,
permissions: {
eth_accounts: {
id: random(),
parentCapability: 'eth_accounts',
invoker: host,
caveats: [
{
type: 'restrictReturnedAccounts',
value: [
{
address: selectedAddress,
lastUsed: Date.now() - index,
Cal-L marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
],
date: Date.now(),
},
},
},
},
}),
{},
);

const newState = { ...state };

newState.engine.backgroundState.PermissionController = {
subjects,
};

return newState;
},
};

export const version = 12;
export const version = 13;