Skip to content

Commit

Permalink
rearranged types
Browse files Browse the repository at this point in the history
  • Loading branch information
lukachi committed Mar 6, 2024
1 parent 2ee4d3c commit 1321869
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 63 deletions.
108 changes: 103 additions & 5 deletions packages/connector/src/zkp/helpers/state-v2-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/* eslint-disable camelcase */

import type { TransactionRequest } from '@ethersproject/providers';
import { utils } from 'ethers';
import { providers, utils } from 'ethers';

import { FetcherError } from '@/helpers/error-helper';
import { sleep } from '@/helpers/promise';
import { CORE_POLLING_INTERVAL } from '@/zkp/consts';
import type {
RarimoChainInfo,
ChainZkpInfo,
StateInfo,
Operation,
OperationProof,
UpdateStateDetails,
StateProof,
OperationResponse,
} from '@/zkp/types';
import { StateV2__factory } from '@/zkp/types';
import { LightweightStateV2__factory } from '@/zkp/types/contracts';

export const loadDataFromRarimoCore = async <T>(
Expand Down Expand Up @@ -87,7 +90,7 @@ export const getUpdateStateDetails = async (

export const getUpdateStateTx = async (
accountId: string,
chainInfo: RarimoChainInfo,
chainInfo: ChainZkpInfo,
state: StateInfo,
operation: Operation,
rarimoCoreUrl: string,
Expand All @@ -105,9 +108,104 @@ export const getUpdateStateTx = async (
proof,
]);
return {
to: chainInfo.stateContractAddress,
to: chainInfo.targetStateContractAddress,
from: accountId,
chainId: chainInfo.id,
chainId: chainInfo.targetChainId,
data: txData,
};
};

export const getGISTProof = async ({
rpcUrl,
contractAddress,
userId,
rootHash,
}: {
rpcUrl: string;
contractAddress: string;
userId: string;
rootHash?: string;
}): Promise<StateProof> => {
const rawProvider = new providers.JsonRpcProvider(rpcUrl);

const contractInstance = StateV2__factory.connect(
contractAddress,
rawProvider,
);

console.log('contractInstance', contractInstance);

const data = rootHash
? await contractInstance.getGISTProofByRoot(userId, rootHash)
: await contractInstance.getGISTProof(userId);

console.log('data', data);

return {
root: BigInt(data.root.toString()),
existence: data.existence,
siblings: data.siblings?.map((sibling) => BigInt(sibling.toString())),
index: BigInt(data.index.toString()),
value: BigInt(data.value.toString()),
auxExistence: data.auxExistence,
auxIndex: BigInt(data.auxIndex.toString()),
auxValue: BigInt(data.auxValue.toString()),
};
};

// getRarimoGISTRoot returns the latest GIST root from the Rarimo state contract
export const getRarimoGISTRoot = async (
chainInfo: ChainZkpInfo,
): Promise<bigint> => {
const rawProvider = new providers.JsonRpcProvider(
chainInfo.targetRpcUrl,
'any',
);

const contractInstance = StateV2__factory.connect(
chainInfo.targetStateContractAddress,
rawProvider,
);

const root = await contractInstance.getGISTRoot();

return BigInt(root.toString());
};

// getCurrentChainGISTRoot returns the GIST root from a lightweight state contract deployed on the current chain
export const getCurrentChainGISTRoot = async (
chainInfo: ChainZkpInfo,
): Promise<bigint> => {
const provider = new providers.JsonRpcProvider(chainInfo.targetRpcUrl, 'any');

const contractInstance = LightweightStateV2__factory.connect(
chainInfo.targetStateContractAddress,
provider,
);
const root = await contractInstance.getGISTRoot();
return BigInt(root.toString());
};

// checkIfStateSynced returns true if the GIST root from the Rarimo state contract matches the GIST root from the current chain
export const checkIfStateSynced = async (
chainInfo: ChainZkpInfo,
): Promise<boolean> => {
/*
NOTE: for now we assume that the state must be synced if the GIST roots don't match
some more sophisticated logic could be added here in the future
*/
const rarimoGISTRoot = await getRarimoGISTRoot(chainInfo);

const currentChainGISTRoot = await getCurrentChainGISTRoot(chainInfo);
return rarimoGISTRoot === currentChainGISTRoot;
};

export const getCoreOperationByIndex = async (
chainInfo: ChainZkpInfo,
index: string,
) => {
return loadDataFromRarimoCore<OperationResponse>(
`/rarimo/rarimo-core/rarimocore/operation/${index}`,
chainInfo.rarimoApiUrl,
);
};
49 changes: 39 additions & 10 deletions packages/connector/src/zkp/types/zkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ declare global {
}
}

// TODO: remove
export type RarimoNetworkType = 'mainnet' | 'beta';

export type ChainZkpInfo = {
targetChainId: number;
targetRpcUrl: string;
targetStateContractAddress: string;

rarimoApiUrl: string;
rarimoEvmRpcApiUrl: string;
rarimoStateContractAddress: string;

rarimoNetworkType: RarimoNetworkType;
};

export type SaveCredentialsResponse = Pick<W3CCredential, 'type'> &
Pick<W3CCredential, 'issuer'>;

Expand Down Expand Up @@ -114,22 +129,13 @@ export type Operation = {
timestamp: string;
};

export type RarimoNetworkType = 'mainnet' | 'beta';

export type RarimoChainInfo = {
id: number;
rpcUrl: string;
stateContractAddress: string;
rarimoNetworkType: RarimoNetworkType;
};

export type OperationProof = {
path: string[];
signature: string;
};

export type ZKPProofSnapResponse = {
chainInfo: RarimoChainInfo;
chainInfo: ChainZkpInfo;
rarimoCoreUrl: string;
isSynced: boolean;

Expand All @@ -151,3 +157,26 @@ export type CreateProofRequest = {
challenge?: string; // bigint string
query: ProofQuery;
};

export type StateProof = {
root: bigint;
existence: boolean;
siblings: bigint[];
index: bigint;
value: bigint;
auxExistence: boolean;
auxIndex: bigint;
auxValue: bigint;
};

export type MerkleProof = {
proof: string[];
};

export type GetStateInfoResponse = {
state: StateInfo;
};

export type OperationResponse = {
operation: Operation;
};
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/rarimo/rarime.git"
},
"source": {
"shasum": "Ms7L0rMmYYffBfujJZ8pCOsb1Uhda71snhVKmMTWIbI=",
"shasum": "VyboKH5bWW2WuoWRbo70xcl/yJhReuLAuKLJkTQ34Go=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
70 changes: 50 additions & 20 deletions packages/snap/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChainInfo } from '@/types';
import type { ChainZkpInfo } from '@rarimo/rarime-connector';

export const config = {
AUTH_BJJ_CREDENTIAL_HASH: 'cca3371a6cb1b715004407e325bd993c',
Expand Down Expand Up @@ -50,41 +50,71 @@ enum CHAINS {
Goerly = 5,
}

export const SUPPORTED_CHAINS: Record<number, ChainInfo> = {
export const SUPPORTED_CHAINS: Record<number, ChainZkpInfo> = {
[CHAINS.Polygon]: {
id: CHAINS.Polygon,
rpcUrl: 'https://polygon-rpc.com',
stateContractAddress: '0xf9bA419ad9c82451d31d89917db61253a1e46B3C',
targetChainId: CHAINS.Polygon,
targetRpcUrl: 'https://polygon-rpc.com',
targetStateContractAddress: '0xf9bA419ad9c82451d31d89917db61253a1e46B3C',

rarimoApiUrl: config.RARIMO_CORE_URL.mainnet,
rarimoEvmRpcApiUrl: config.RARIMO_EVM_RPC_URL.mainnet,
rarimoStateContractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS.mainnet,

rarimoNetworkType: 'mainnet',
},
[CHAINS.EthereumMainet]: {
id: CHAINS.EthereumMainet,
rpcUrl: 'https://eth.llamarpc.com',
stateContractAddress: '0xB11D49e873A1B4a8c54520A9b6a3c8E017AfE7BB',
targetChainId: CHAINS.EthereumMainet,
targetRpcUrl: 'https://eth.llamarpc.com',
targetStateContractAddress: '0xB11D49e873A1B4a8c54520A9b6a3c8E017AfE7BB',

rarimoApiUrl: config.RARIMO_CORE_URL.mainnet,
rarimoEvmRpcApiUrl: config.RARIMO_EVM_RPC_URL.mainnet,
rarimoStateContractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS.mainnet,

rarimoNetworkType: 'mainnet',
},
[CHAINS.Bsc]: {
id: CHAINS.Bsc,
rpcUrl: 'https://bsc-dataseed.binance.org',
stateContractAddress: '0xF3e2491627b9eF3816A4143010B39B2B67F33E55',
targetChainId: CHAINS.Bsc,
targetRpcUrl: 'https://bsc-dataseed.binance.org',
targetStateContractAddress: '0xF3e2491627b9eF3816A4143010B39B2B67F33E55',

rarimoApiUrl: config.RARIMO_CORE_URL.mainnet,
rarimoEvmRpcApiUrl: config.RARIMO_EVM_RPC_URL.mainnet,
rarimoStateContractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS.mainnet,

rarimoNetworkType: 'mainnet',
},
[CHAINS.Avalance]: {
id: CHAINS.Avalance,
rpcUrl: 'https://avax.meowrpc.com',
stateContractAddress: '0xF3e2491627b9eF3816A4143010B39B2B67F33E55',
targetChainId: CHAINS.Avalance,
targetRpcUrl: 'https://avax.meowrpc.com',
targetStateContractAddress: '0xF3e2491627b9eF3816A4143010B39B2B67F33E55',

rarimoApiUrl: config.RARIMO_CORE_URL.mainnet,
rarimoEvmRpcApiUrl: config.RARIMO_EVM_RPC_URL.mainnet,
rarimoStateContractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS.mainnet,

rarimoNetworkType: 'mainnet',
},
[CHAINS.Sepolia]: {
id: CHAINS.Sepolia,
rpcUrl: 'https://endpoints.omniatech.io/v1/eth/sepolia/public',
stateContractAddress: '0x8a9F505bD8a22BF09b0c19F65C17426cd33f3912',
targetChainId: CHAINS.Sepolia,
targetRpcUrl: 'https://endpoints.omniatech.io/v1/eth/sepolia/public',
targetStateContractAddress: '0x8a9F505bD8a22BF09b0c19F65C17426cd33f3912',

rarimoApiUrl: config.RARIMO_CORE_URL.beta,
rarimoEvmRpcApiUrl: config.RARIMO_EVM_RPC_URL.beta,
rarimoStateContractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS.beta,

rarimoNetworkType: 'beta',
},
[CHAINS.Goerly]: {
id: CHAINS.Goerly,
rpcUrl: 'https://ethereum-goerli.publicnode.com',
stateContractAddress: '0x0F08e8EA245E63F2090Bf3fF3772402Da9c047ee',
targetChainId: CHAINS.Goerly,
targetRpcUrl: 'https://ethereum-goerli.publicnode.com',
targetStateContractAddress: '0x0F08e8EA245E63F2090Bf3fF3772402Da9c047ee',

rarimoApiUrl: config.RARIMO_CORE_URL.beta,
rarimoEvmRpcApiUrl: config.RARIMO_EVM_RPC_URL.beta,
rarimoStateContractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS.beta,

rarimoNetworkType: 'beta',
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/zkp/handlers/CheckStateContractSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
RPCMethods,
SnapRequestsResponses,
} from '@rarimo/rarime-connector';
import { checkIfStateSynced } from '@rarimo/zkp-iden3';
import { checkIfStateSynced } from '@rarimo/rarime-connector';

import { getProviderChainInfo } from '@/zkp/helpers';

Expand All @@ -11,5 +11,5 @@ export const checkStateContractSync = async (): Promise<
> => {
const chainInfo = await getProviderChainInfo();

return checkIfStateSynced(chainInfo.id);
return checkIfStateSynced(chainInfo);
};
Loading

0 comments on commit 1321869

Please sign in to comment.