Skip to content

Commit

Permalink
fix: migrate imported state (#589)
Browse files Browse the repository at this point in the history
Co-authored-by: martines3000 <domajnko.martin@gmail.com>
  • Loading branch information
andyv09 and martines3000 authored Mar 17, 2024
1 parent 4c8af21 commit 96ba0ab
Show file tree
Hide file tree
Showing 21 changed files with 287 additions and 235 deletions.
8 changes: 8 additions & 0 deletions .changeset/warm-jeans-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@blockchain-lab-um/extended-verification": patch
"@blockchain-lab-um/did-provider-key": patch
"@blockchain-lab-um/veramo-datamanager": patch
"@blockchain-lab-um/utils": patch
---

Build packages with Tsup
5 changes: 5 additions & 0 deletions .changeset/witty-apes-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blockchain-lab-um/masca": patch
---

Added migration for legacy state when importing state
3 changes: 3 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
},
"a11y": {
"useKeyWithClickEvents": "off"
},
"performance": {
"noDelete": "off"
}
}
},
Expand Down
4 changes: 1 addition & 3 deletions libs/did-provider-key/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
"scripts": {
"build": "pnpm clean && pnpm compile",
"clean": "rimraf dist",
"compile": "tsc --build tsconfig.build.json",
"compile": "tsup",
"lint": "biome check .",

"lint:fix": "biome check --apply .",

"test": "pnpm cross-env NODE_NO_WARNINGS=1 vitest",
"test:ci": "pnpm run test"
},
Expand Down
9 changes: 2 additions & 7 deletions libs/extended-verification/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@
"module": "./dist/index.js",
"source": "./src/index.ts",
"types": "./dist/index.d.ts",
"files": [
"dist/**",
"README.md",
"package.json",
"CHANGELOG.md"
],
"files": ["dist/**", "README.md", "package.json", "CHANGELOG.md"],
"scripts": {
"build": "pnpm clean && pnpm compile",
"clean": "rimraf dist",
"compile": "tsc --build tsconfig.build.json",
"compile": "tsup",
"lint": "biome check .",
"lint:fix": "biome check --apply .",
"test": "pnpm cross-env NODE_NO_WARNINGS=1 vitest",
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"build": "pnpm clean && pnpm compile",
"clean": "rimraf dist",
"compile": "tsc --build tsconfig.build.json",
"compile": "tsup",
"lint": "biome check .",
"lint:fix": "biome check --apply ."
},
Expand Down
3 changes: 2 additions & 1 deletion packages/datamanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"build": "pnpm clean && pnpm compile",
"clean": "rimraf dist",
"compile": "tsc --build tsconfig.build.json",
"compile": "tsup",
"lint": "biome check .",
"lint:fix": "biome check --apply .",
"prepack": "pnpm build",
Expand All @@ -28,6 +28,7 @@
"@types/uuid": "^9.0.8",
"@vitest/coverage-v8": "1.3.1",
"jest-extended": "^4.0.2",
"tsup": "^8.0.2",
"vite": "^5.1.5",
"vite-tsconfig-paths": "^4.3.1",
"vitest": "1.3.1"
Expand Down
14 changes: 14 additions & 0 deletions packages/datamanager/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Options, defineConfig } from 'tsup';

export default defineConfig((options: Options) => ({
target: 'es2020',
treeshake: true,
splitting: true,
tsconfig: './tsconfig.build.json',
entry: ['src/**/*.ts'],
format: 'esm',
dts: true,
minify: false,
clean: true,
...options,
}));
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"./files/circuits/credentialAtomicQuerySigV2/circuit_final.zkey",
"./files/circuits/credentialAtomicQuerySigV2/verification_key.json"
],
"shasum": "46H5O2d4heKffriCIhp3HWniHoey3EH0gzqicxXBKDE="
"shasum": "CVm1yViB1CFAr/11FLiGrhV85R7GeDpy7WOiB9wvEik="
},
"initialPermissions": {
"endowment:ethereum-provider": {},
Expand Down
7 changes: 5 additions & 2 deletions packages/snap/src/General.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,11 @@ class GeneralService {
const state = JSON.parse(
await EncryptionService.decrypt(params.serializedState)
);
isValidMascaState(state);
StorageService.set(state);

const latestState = StorageService.migrateState(state);

isValidMascaState(latestState);
StorageService.set(latestState);
} catch (error) {
throw new Error('Invalid backup state.');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/utils/stateMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const migrateToV2 = (state: MascaLegacyStateV1): MascaState => {
const newState: any = { v2: state.v1 };

// Remove friendly dapps
newState.v2.config.dApp.friendlyDapps = undefined;
delete newState.v2.config.dApp.friendlyDapps;

// Initialize permissions
newState.v2.config.dApp.permissions = { 'masca.io': getInitialPermissions() };
Expand Down
9 changes: 7 additions & 2 deletions packages/snap/tests/data/defaultSnapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
MascaState,
} from '@blockchain-lab-um/masca-types';

import { getEmptyAccountState } from '../../src/utils/config';
import {
getEmptyAccountState,
getInitialPermissions,
} from '../../src/utils/config';

const defaultSnapState = (address: string): MascaState => {
const accountState: Record<string, MascaAccountState> = {};
Expand All @@ -16,7 +19,9 @@ const defaultSnapState = (address: string): MascaState => {
config: {
dApp: {
disablePopups: false,
permissions: {},
permissions: {
'masca.io': getInitialPermissions(),
},
},
snap: {
acceptedTerms: true,
Expand Down
1 change: 1 addition & 0 deletions packages/snap/tests/data/legacyStates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getLegacyStateV1';
79 changes: 79 additions & 0 deletions packages/snap/tests/data/legacyStates/legacyStateV1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
MascaLegacyAccountStateV1,
MascaLegacyStateV1,
MascaLegacyAccountConfigV1,
PolygonLegacyBaseStateV1,
PolygonLegacyStateV1,
} from '@blockchain-lab-um/masca-types';
import cloneDeep from 'lodash.clonedeep';

const emptyPolygonBaseState: PolygonLegacyBaseStateV1 = {
credentials: {},
identities: {},
profiles: {},
merkleTreeMeta: [],
merkleTree: {},
};

const emptyPolygonState: PolygonLegacyStateV1 = {
polygonid: {
eth: {
main: cloneDeep(emptyPolygonBaseState),
mumbai: cloneDeep(emptyPolygonBaseState), // To satisfy the type checker
},
polygon: {
main: cloneDeep(emptyPolygonBaseState),
mumbai: cloneDeep(emptyPolygonBaseState),
},
},
iden3: {
eth: {
main: cloneDeep(emptyPolygonBaseState),
mumbai: cloneDeep(emptyPolygonBaseState), // To satisfy the type checker
},
polygon: {
main: cloneDeep(emptyPolygonBaseState),
mumbai: cloneDeep(emptyPolygonBaseState),
},
},
};

const emptyAccountState = {
polygon: {
state: emptyPolygonState,
},
veramo: {
credentials: {},
},
general: {
account: {
ssi: {
selectedMethod: 'did:ethr',
storesEnabled: {
snap: true,
ceramic: true,
},
},
} as MascaLegacyAccountConfigV1,
},
} as MascaLegacyAccountStateV1;

export const getLegacyEmptyAccountStateV1 = () => cloneDeep(emptyAccountState);

const initialSnapState: MascaLegacyStateV1 = {
v1: {
accountState: {},
currentAccount: '',
config: {
dApp: {
disablePopups: false,
friendlyDapps: ['masca.io'],
},
snap: {
acceptedTerms: true,
},
},
},
};

export const getLegacyStateV1 = () => cloneDeep(initialSnapState);
Loading

0 comments on commit 96ba0ab

Please sign in to comment.