Skip to content

Commit

Permalink
fix: migrate to amoy
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 committed Apr 19, 2024
1 parent 02e740b commit bbf2142
Show file tree
Hide file tree
Showing 28 changed files with 1,141 additions and 594 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
"email": "blockchain-lab@um.si",
"url": "https://blockchain-lab.um.si"
},
"workspaces": [
"packages/*",
"apps/**/*",
"libs/**/*"
],
"workspaces": ["packages/*", "apps/**/*", "libs/**/*"],
"scripts": {
"build": "pnpm nx run-many --target=build",
"build:docker": "./scripts/build-docker.sh",
Expand Down
2 changes: 1 addition & 1 deletion packages/connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"did-session": "2.0.1",
"eip-712-types-generation": "^0.1.6",
"mipd": "^0.0.5",
"viem": "^2.7.20"
"viem": "^2.9.23"
},
"devDependencies": {
"esbuild-loader": "^4.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MAINNET_RPC_URL=
SEPOLIA_RPC_URL=
IPFS_GATEWAY=
POLYGON_RPC_URL=
POLYGON_MUMBAI_RPC_URL=
POLYGON_AMOY_RPC_URL=

# Campaign Issuer
CAMPAIGN_ISSUER_DID=did:ens:mainnet:masca.eth
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"siwe": "^2.1.4",
"swr": "^2.2.5",
"tailwind-scrollbar": "^3.1.0",
"viem": "^2.7.20",
"wagmi": "^2.5.7",
"viem": "^2.9.23",
"wagmi": "^2.5.20",
"zustand": "^4.5.2"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/dapp/src/config/wagmiConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { http, createConfig } from 'wagmi';
import { mainnet, polygon, polygonMumbai, sepolia } from 'wagmi/chains';
import { mainnet, polygon, polygonAmoy, sepolia } from 'wagmi/chains';
import { injected } from 'wagmi/connectors';

export const config = createConfig({
chains: [mainnet, sepolia, polygon, polygonMumbai],
chains: [mainnet, sepolia, polygon, polygonAmoy],
connectors: [injected({ target: 'metaMask' })],
ssr: true,
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
[polygon.id]: http(),
[polygonMumbai.id]: http(),
[polygonAmoy.id]: http(),
},
});
6 changes: 3 additions & 3 deletions packages/dapp/src/utils/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ export const NETWORKS: Record<string, string> = {
'0x1': 'Ethereum',
'0xaa36a7': 'Sepolia',
'0x89': 'Polygon',
'0x13881': 'Polygon Mumbai',
'0x13882': 'Polygon Amoy',
};

export const NETWORKS_BY_DID: Record<string, string[]> = {
'did:ethr': ['*'],
'did:pkh': ['0x1', '0x89'],
'did:ens': ['0x1'],
'did:polygonid': ['0x1', '0x89', '0x13881'],
'did:iden3': ['0x1', '0x89', '0x13881'],
'did:polygonid': ['0x89', '0x13882'],
'did:iden3': ['0x89', '0x13882'],
};

export function getAvailableNetworksList(method: string): string[] {
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/docs/integrate-masca/polygonid.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This part handles credential offers, which are received from Polygon ID issuers.
// The credential offer received from a Polygon ID issuer
// The credential offer is a JSON string
const credentialOffer: string =
'{"body":{"credentials":[{"description":"KYCAgeCredential","id":"348a8620-40fd-11ee-beda-0242ac1d0006"}],"url":"https://dev.polygonid.me/api/v1/agent"},"from":"did:polygonid:polygon:mumbai:2qLPqvayNQz9TA2r5VPxUugoF18teGU583zJ859wfy","id":"3a0fbba4-601c-4773-baca-0b8f0cfc7f43","thid":"3a0fbba4-601c-4773-baca-0b8f0cfc7f43","to":"did:polygonid:polygon:main:2q6KWUhghmPGRuh8GFMzDX3EYp1WVspWN9ZdQEk4D2","typ":"application/iden3comm-plain-json","type":"https://iden3-communication.io/credentials/1.0/offer"}';
'{"body":{"credentials":[{"description":"KYCAgeCredential","id":"348a8620-40fd-11ee-beda-0242ac1d0006"}],"url":"https://dev.polygonid.me/api/v1/agent"},"from":"did:polygonid:polygon:amoy:2qLPqvayNQz9TA2r5VPxUugoF18teGU583zJ859wfy","id":"3a0fbba4-601c-4773-baca-0b8f0cfc7f43","thid":"3a0fbba4-601c-4773-baca-0b8f0cfc7f43","to":"did:polygonid:polygon:main:2q6KWUhghmPGRuh8GFMzDX3EYp1WVspWN9ZdQEk4D2","typ":"application/iden3comm-plain-json","type":"https://iden3-communication.io/credentials/1.0/offer"}';

const handleCredentialOfferResponse = api.handleCredentialOffer({
credentialOffer,
Expand Down Expand Up @@ -48,7 +48,7 @@ This part handles authorization requests, which are received from Polygon ID ver
// The authorization request received from a Polygon ID verifier
// The authorization request is a JSON string
const authorizationRequest: string =
'{"body":{"callbackUrl":"https://issuer-v2.polygonid.me/api/callback?sessionId=334944","reason":"test flow","scope":[]},"from":"did:polygonid:polygon:mumbai:2qLPqvayNQz9TA2r5VPxUugoF18teGU583zJ859wfy","id":"ea786170-f45f-4f13-b631-c4a7bbc03905","thid":"ea786170-f45f-4f13-b631-c4a7bbc03905","typ":"application/iden3comm-plain-json","type":"https://iden3-communication.io/authorization/1.0/request"}';
'{"body":{"callbackUrl":"https://issuer-v2.polygonid.me/api/callback?sessionId=334944","reason":"test flow","scope":[]},"from":"did:polygonid:polygon:amoy:2qLPqvayNQz9TA2r5VPxUugoF18teGU583zJ859wfy","id":"ea786170-f45f-4f13-b631-c4a7bbc03905","thid":"ea786170-f45f-4f13-b631-c4a7bbc03905","typ":"application/iden3comm-plain-json","type":"https://iden3-communication.io/authorization/1.0/request"}';

const handleAuthorizationRequestResponse = api.handleAuthorizationRequest({
authorizationRequest,
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"lodash.clonedeep": "^4.5.0",
"multiformats": "^13.1.0",
"qs": "^6.11.2",
"viem": "^2.7.20"
"viem": "^2.9.23"
},
"devDependencies": {
"@ceramicnetwork/streamid": "2.17.0",
Expand Down
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": "vuJsS0i7d7N7vOxg1+dLQlcbW3cp6ReSx/hvm2TFIdM="
"shasum": "fM/AnUhRzKtwHqlM1C65vmi4PwGAKYH1dKOEuWiKHiY="
},
"initialPermissions": {
"endowment:ethereum-provider": {},
Expand Down
10 changes: 5 additions & 5 deletions packages/snap/src/polygon-id/Polygon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ class PolygonService {
DidMethod.Iden3 | DidMethod.PolygonId,
Record<
Blockchain.Polygon,
Record<NetworkId.Main | NetworkId.Mumbai, PolygonServicBaseInstance>
Record<NetworkId.Main | NetworkId.Amoy, PolygonServicBaseInstance>
>
> = {
polygonid: {
polygon: {
main: {} as PolygonServicBaseInstance,
mumbai: {} as PolygonServicBaseInstance,
amoy: {} as PolygonServicBaseInstance,
},
},
iden3: {
polygon: {
main: {} as PolygonServicBaseInstance,
mumbai: {} as PolygonServicBaseInstance,
amoy: {} as PolygonServicBaseInstance,
},
},
};
Expand Down Expand Up @@ -190,7 +190,7 @@ class PolygonService {
static async createBaseInstance(params: {
method: DidMethod.Iden3 | DidMethod.PolygonId;
blockchain: Blockchain.Polygon;
networkId: NetworkId.Main | NetworkId.Mumbai;
networkId: NetworkId.Main | NetworkId.Amoy;
circuitData: CircuitData;
}) {
const { method, blockchain, networkId, circuitData } = params;
Expand Down Expand Up @@ -369,7 +369,7 @@ class PolygonService {
static async createWallet(params: {
method: DidMethod.Iden3 | DidMethod.PolygonId;
blockchain: Blockchain.Polygon;
networkId: NetworkId.Main | NetworkId.Mumbai;
networkId: NetworkId.Main | NetworkId.Amoy;
}) {
const { method, blockchain, networkId } = params;
const state = StorageService.get();
Expand Down
20 changes: 10 additions & 10 deletions packages/snap/src/polygon-id/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
export const RHS_URL = 'https://rhs-staging.polygonid.me';

export const POLYGON_MAINNET_RPC_URL = 'https://polygon.llamarpc.com';
export const POLYGON_MUMBAI_RPC_URL =
'https://polygon-mumbai.blockpi.network/v1/rpc/public';
export const POLYGON_AMOY_RPC_URL =
'https://polygon-amoy.blockpi.network/v1/rpc/public';

export const CONTRACT_POLYGON_MAINNET =
'0x624ce98D2d27b20b8f8d521723Df8fC4db71D79D';

export const CONTRACT_POLYGON_MUMBAI =
'0x134B1BE34911E39A8397ec6289782989729807a4';
export const CONTRACT_POLYGON_AMOY =
'0x1a4cC30f2aA0377b0c3bc9848766D90cb4404124';

export const getDefaultEthConnectionConfig = (
blockchain: (typeof BLOCKCHAINS)[number],
Expand All @@ -28,9 +28,9 @@ export const getDefaultEthConnectionConfig = (
if (networkId === NetworkId.Main) {
url = POLYGON_MAINNET_RPC_URL;
contractAddress = CONTRACT_POLYGON_MAINNET;
} else if (networkId === NetworkId.Mumbai) {
url = POLYGON_MUMBAI_RPC_URL;
contractAddress = CONTRACT_POLYGON_MUMBAI;
} else if (networkId === NetworkId.Amoy) {
url = POLYGON_AMOY_RPC_URL;
contractAddress = CONTRACT_POLYGON_AMOY;
}
}

Expand All @@ -56,16 +56,16 @@ export const getDefaultEthConnectionConfig = (

export const METHODS = [DidMethod.PolygonId, DidMethod.Iden3] as const;
export const BLOCKCHAINS = [Blockchain.Polygon] as const;
export const NETWORKS = [NetworkId.Mumbai, NetworkId.Main] as const;
export const NETWORKS = [NetworkId.Amoy, NetworkId.Main] as const;

export const CHAIN_ID_TO_BLOCKCHAIN_AND_NETWORK_ID = {
'0x89': {
blockchain: Blockchain.Polygon,
networkId: NetworkId.Main,
},
'0x13881': {
'0x13882': {
blockchain: Blockchain.Polygon,
networkId: NetworkId.Mumbai,
networkId: NetworkId.Amoy,
},
} as Record<
string,
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/polygon-id/storage/SnapDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class SnapDataSource<T> implements IDataSource<T> {
private readonly account: string,
private readonly method: DidMethod.Iden3 | DidMethod.PolygonId,
private readonly blockchain: Blockchain.Polygon,
private readonly networkId: NetworkId.Main | NetworkId.Mumbai,
private readonly networkId: NetworkId.Main | NetworkId.Amoy,
private readonly STORAGE_KEY: StorageKey
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SnapMerkleTreeStorage implements IMerkleTreeStorage {
private readonly account: string,
private readonly method: DidMethod.Iden3 | DidMethod.PolygonId,
private readonly blockchain: Blockchain.Polygon,
private readonly networkId: NetworkId.Main | NetworkId.Mumbai,
private readonly networkId: NetworkId.Main | NetworkId.Amoy,
private readonly depth: number
) {}

Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/polygon-id/storage/SnapTreeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SnapTreeStorage implements ITreeStorage {
private readonly account: string,
private readonly method: DidMethod.Iden3 | DidMethod.PolygonId,
private readonly blockchain: Blockchain.Polygon,
private readonly networkId: NetworkId.Main | NetworkId.Mumbai,
private readonly networkId: NetworkId.Main | NetworkId.Amoy,
private readonly prefix: Bytes
) {
this.currentRoot = ZERO_HASH;
Expand Down
6 changes: 5 additions & 1 deletion packages/snap/src/storage/Storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

import { getInitialSnapState } from '../utils/config';
import SnapStorage from './Snap.storage';
import { migrateToV2, migrateToV3 } from 'src/utils/stateMigration';
import { migrateToV2, migrateToV3, migrateToV4 } from '../utils/stateMigration';

class StorageService {
static instance: MascaState;
Expand Down Expand Up @@ -55,6 +55,10 @@ class StorageService {
newState = migrateToV3(newState);
}

if (newState.v3) {
newState = migrateToV4(newState);
}

return newState;
};
}
Expand Down
23 changes: 12 additions & 11 deletions packages/snap/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type {
DappPermissions,
MascaAccountConfig,
MascaAccountState,
MascaState,
PolygonBaseState,
PolygonState,
import {
CURRENT_STATE_VERSION,
type DappPermissions,
type MascaAccountConfig,
type MascaAccountState,
type MascaState,
type PolygonBaseState,
type PolygonState,
} from '@blockchain-lab-um/masca-types';
import cloneDeep from 'lodash.clonedeep';

export const UNIRESOLVER_PROXY_URL = 'https://masca.io/api/proxy/uniresolver';

const emptyPolygonBaseState: PolygonBaseState = {
export const emptyPolygonBaseState: PolygonBaseState = {
credentials: {},
identities: {},
profiles: {},
Expand All @@ -22,13 +23,13 @@ const emptyPolygonState: PolygonState = {
polygonid: {
polygon: {
main: cloneDeep(emptyPolygonBaseState),
mumbai: cloneDeep(emptyPolygonBaseState),
amoy: cloneDeep(emptyPolygonBaseState),
},
},
iden3: {
polygon: {
main: cloneDeep(emptyPolygonBaseState),
mumbai: cloneDeep(emptyPolygonBaseState),
amoy: cloneDeep(emptyPolygonBaseState),
},
},
};
Expand Down Expand Up @@ -96,7 +97,7 @@ const initialPermissions: DappPermissions = {
export const getInitialPermissions = () => cloneDeep(initialPermissions);

const initialSnapState: MascaState = {
v3: {
[CURRENT_STATE_VERSION]: {
accountState: {},
currentAccount: '',
config: {
Expand Down
23 changes: 22 additions & 1 deletion packages/snap/src/utils/stateMigration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type {
MascaLegacyStateV1,
MascaLegacyStateV2,
MascaLegacyStateV3,
MascaState,
} from '@blockchain-lab-um/masca-types';
import { getInitialPermissions } from './config';
import { emptyPolygonBaseState, getInitialPermissions } from './config';
import cloneDeep from 'lodash.clonedeep';

export const migrateToV2 = (state: MascaLegacyStateV1): MascaState => {
const newState: any = { v2: state.v1 };
Expand Down Expand Up @@ -43,3 +45,22 @@ export const migrateToV3 = (state: MascaLegacyStateV2): MascaState => {

return newState as MascaState;
};

export const migrateToV4 = (state: MascaLegacyStateV3): MascaState => {
const newState: any = { v4: state.v3 };

// Remove mumbai testnet from polygon state from all accounts and add amoy testnet
const accounts = Object.keys(newState.v4.accountState);

for (const account of accounts) {
for (const method of ['polygonid', 'iden3']) {
delete newState.v4.accountState[account].polygon.state[method].polygon
.mumbai;

newState.v4.accountState[account].polygon.state[method].polygon.amoy =
cloneDeep(emptyPolygonBaseState);
}
}

return newState as MascaState;
};
1 change: 1 addition & 0 deletions packages/snap/tests/data/legacyStates/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './legacyStateV1';
export * from './legacyStateV2';
export * from './legacyStateV3';
Loading

0 comments on commit bbf2142

Please sign in to comment.