Skip to content

Commit

Permalink
Use Rarimo's beta network when user switches to a testnet in MetaMask (
Browse files Browse the repository at this point in the history
…#7)

* separate rarimo config to beta and mainnet

* bump version, fix connector package version in span to be latest

* changelog

* fixec changelog and versions deps

* update default version in connector
  • Loading branch information
lukachi authored and ihordiachenko committed Sep 27, 2023
1 parent b1ae120 commit 76de5bd
Show file tree
Hide file tree
Showing 14 changed files with 595 additions and 530 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.4.0] - 2023-09-27
### Changed
- `@rarimo/rarime`: use Rarimo's beta network when user switches to a testnet in MetaMask

## [0.3.2] - 2023-09-26
### Fixed

Expand Down Expand Up @@ -44,7 +48,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Implemented `@rarimo/rarime-connector` and `@rarimo/rarime` packages

[Unreleased]: https://github.com/rarimo/rarime/compare/0.3.2...HEAD
[Unreleased]: https://github.com/rarimo/rarime/compare/0.4.0...HEAD
[0.4.0]: https://github.com/rarimo/rarime/compare/0.3.2...0.4.0
[0.3.2]: https://github.com/rarimo/rarime/compare/0.3.1...0.3.2
[0.3.1]: https://github.com/rarimo/rarime/compare/0.3.0...0.3.1
[0.3.0]: https://github.com/rarimo/rarime/compare/0.2.0...0.3.0
Expand Down
2 changes: 1 addition & 1 deletion packages/connector/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rarimo/rarime-connector",
"version": "0.3.2",
"version": "0.4.0",
"description": "Facilitates interaction between a DApp and RariMe MetaMask snap",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/connector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const defaultSnapOrigin = 'npm:@rarimo/rarime';

export const enableSnap = async (
snapOrigin?: string,
version = '0.3.x',
version = '0.4.x',
): Promise<MetamaskSnap> => {
const snapId = snapOrigin ?? defaultSnapOrigin;

Expand Down
2 changes: 1 addition & 1 deletion packages/site/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "site",
"version": "0.3.2",
"version": "0.4.0",
"private": true,
"license": "(MIT-0 OR Apache-2.0)",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rarimo/rarime",
"version": "0.3.2",
"version": "0.4.0",
"description": "RariMe is a MetaMask Snap that safely holds any of your credentials and allows you to prove your identity without revealing any personal data. Powered by Rarimo Protocol and Zero-Knowledge Proof technology.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,7 +45,7 @@
"@metamask/snaps-jest": "0.35.2-flask.1",
"@metamask/snaps-types": "0.32.2",
"@metamask/snaps-ui": "0.32.2",
"@rarimo/rarime-connector": "0.3.2",
"@rarimo/rarime-connector": "0.4.0",
"buffer": "6.0.3",
"ethers": "5.7.2",
"intl": "1.2.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "0.3.2",
"version": "0.4.0",
"description": "Securely store and manage all of your identity credentials. Use them across chains with ZK-protected privacy guarantees.",
"proposedName": "RariMe",
"repository": {
"type": "git",
"url": "https://github.com/rarimo/rarime.git"
},
"source": {
"shasum": "ARaGpxTRetiQNbBx3xvH9u8460FSGwg+LduL1fGQqjs=",
"shasum": "lv/PkqOxGt10NAk8owemNQppfyP01GEDI8X5WMy2xFM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
8 changes: 5 additions & 3 deletions packages/snap/src/auth-zkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { config } from './config';
import { W3CCredential, ClaimOffer } from './types';
import {
getGISTProof,
getNodeAuxValue,
getNodeAuxValue, getProviderChainInfo, getRarimoEvmRpcUrl, getRarimoStateContractAddress,
prepareSiblingsStr,
readBytesFile,
toGISTProof,
Expand Down Expand Up @@ -77,10 +77,12 @@ export class AuthZkp {
async #prepareInputs(messageHash: Uint8Array): Promise<Uint8Array> {
const messageHashBigInt = fromBigEndian(messageHash);

const providerChainInfo = await getProviderChainInfo()

const signature = this.identity.privateKey.signPoseidon(messageHashBigInt);
const gistInfo = await getGISTProof({
rpcUrl: config.RARIMO_EVM_RPC_URL,
contractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS,
rpcUrl: getRarimoEvmRpcUrl(providerChainInfo.id),
contractAddress: getRarimoStateContractAddress(providerChainInfo.id),
userId: this.identity.identityIdBigIntString,
});

Expand Down
21 changes: 18 additions & 3 deletions packages/snap/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ export const config = {
AUTH_BJJ_CREDENTIAL_HASH: 'cca3371a6cb1b715004407e325bd993c',
ID_TYPE: Uint8Array.from([1, 0]),

RARIMO_EVM_RPC_URL: 'https://rpc.evm.mainnet.rarimo.com',
RARIMO_CORE_URL: 'https://rpc-api.mainnet.rarimo.com',
RARIMO_STATE_CONTRACT_ADDRESS: '0x5ac96945a771d417B155Cb07A3D7E4b8e2F33FdE',
RARIMO_EVM_RPC_URL: {
beta: 'https://rpc.evm.node1.mainnet-beta.rarimo.com',
mainnet: 'https://rpc.evm.mainnet.rarimo.com',
},
RARIMO_CORE_URL: {
beta: 'https://rpc-api.node1.mainnet-beta.rarimo.com',
mainnet: 'https://rpc-api.mainnet.rarimo.com',
},
RARIMO_STATE_CONTRACT_ADDRESS: {
beta: '0x753a8678c85d5fb70A97CFaE37c84CE2fD67EDE8',
mainnet: '0x5ac96945a771d417B155Cb07A3D7E4b8e2F33FdE',
},

CIRCUIT_AUTH_WASM_URL:
'https://ipfs.tokend.io/ipfs/ipfs/QmYd41GHrKQLqbk96zHbmHU5rGVcxwmAgBpRqLCGLK7LQu',
Expand Down Expand Up @@ -44,30 +53,36 @@ export const SUPPORTED_CHAINS: Record<number, ChainInfo> = {
id: CHAINS.Polygon,
rpcUrl: 'https://polygon-rpc.com',
stateContractAddress: '0xf9bA419ad9c82451d31d89917db61253a1e46B3C',
rarimoNetworkType: 'mainnet',
},
[CHAINS.EthereumMainet]: {
id: CHAINS.EthereumMainet,
rpcUrl: 'https://eth.llamarpc.com',
stateContractAddress: '0xB11D49e873A1B4a8c54520A9b6a3c8E017AfE7BB',
rarimoNetworkType: 'mainnet',
},
[CHAINS.Bsc]: {
id: CHAINS.Bsc,
rpcUrl: 'https://bsc-dataseed.binance.org',
stateContractAddress: '0xF3e2491627b9eF3816A4143010B39B2B67F33E55',
rarimoNetworkType: 'mainnet',
},
[CHAINS.Avalance]: {
id: CHAINS.Avalance,
rpcUrl: 'https://avax.meowrpc.com',
stateContractAddress: '0xF3e2491627b9eF3816A4143010B39B2B67F33E55',
rarimoNetworkType: 'mainnet',
},
[CHAINS.Sepolia]: {
id: CHAINS.Sepolia,
rpcUrl: 'https://endpoints.omniatech.io/v1/eth/sepolia/public',
stateContractAddress: '0x8a9F505bD8a22BF09b0c19F65C17426cd33f3912',
rarimoNetworkType: 'beta',
},
[CHAINS.Goerly]: {
id: CHAINS.Goerly,
rpcUrl: 'https://ethereum-goerli.publicnode.com',
stateContractAddress: '0x0F08e8EA245E63F2090Bf3fF3772402Da9c047ee',
rarimoNetworkType: 'beta',
},
};
24 changes: 23 additions & 1 deletion packages/snap/src/helpers/common-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SUPPORTED_CHAINS } from '../config';
import { providers } from 'ethers';
import { config, SUPPORTED_CHAINS } from '../config';
import { ChainInfo } from '../types';

export const getChainInfo = (chainId: number): ChainInfo => {
Expand All @@ -9,3 +10,24 @@ export const getChainInfo = (chainId: number): ChainInfo => {

return chainInfo;
};

export const getProviderChainInfo = async (): Promise<ChainInfo> => {
const provider = new providers.Web3Provider(window.ethereum);
const network = await provider.getNetwork();

return getChainInfo(network.chainId);
};

export const getRarimoEvmRpcUrl = (chainId: number) => {
return config.RARIMO_EVM_RPC_URL[SUPPORTED_CHAINS[chainId].rarimoNetworkType];
};

export const getRarimoCoreUrl = (chainId: number) => {
return config.RARIMO_CORE_URL[SUPPORTED_CHAINS[chainId].rarimoNetworkType];
};

export const getRarimoStateContractAddress = (chainId: number) => {
return config.RARIMO_STATE_CONTRACT_ADDRESS[
SUPPORTED_CHAINS[chainId].rarimoNetworkType
];
};
44 changes: 27 additions & 17 deletions packages/snap/src/helpers/state-v2-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import {
StateProof,
StateV2__factory,
} from '../types';
import { config } from '../config';
import { getChainInfo } from './common-helpers';
import {
getChainInfo,
getProviderChainInfo,
getRarimoCoreUrl,
getRarimoEvmRpcUrl,
getRarimoStateContractAddress,
} from './common-helpers';
import { FetcherError } from './error-helper';

export const getGISTProof = async ({
Expand Down Expand Up @@ -45,22 +50,23 @@ export const getGISTProof = async ({
};

// getRarimoGISTRoot returns the latest GIST root from the Rarimo state contract
export const getRarimoGISTRoot = async (
{
rpcUrl,
contractAddress,
}: {
rpcUrl: string;
contractAddress: string;
} = {
rpcUrl: config.RARIMO_EVM_RPC_URL,
contractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS,
},
): Promise<bigint> => {
const rawProvider = new providers.JsonRpcProvider(rpcUrl, 'any');
export const getRarimoGISTRoot = async ({
rpcUrl,
contractAddress,
}: {
rpcUrl?: string;
contractAddress?: string;
} = {}): Promise<bigint> => {
const providerChainInfo = await getProviderChainInfo();

const _rpcUrl = rpcUrl ?? getRarimoEvmRpcUrl(providerChainInfo.id);
const _contractAddress =
contractAddress ?? getRarimoStateContractAddress(providerChainInfo.id);

const rawProvider = new providers.JsonRpcProvider(_rpcUrl, 'any');

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

Expand Down Expand Up @@ -98,7 +104,11 @@ export const loadDataFromRarimoCore = async <T>(
url: string,
blockHeight?: string,
): Promise<T> => {
const response = await fetch(`${config.RARIMO_CORE_URL}${url}`, {
const providerChainInfo = await getProviderChainInfo();

const rarimoCoreUrl = getRarimoCoreUrl(providerChainInfo.id);

const response = await fetch(`${rarimoCoreUrl}${url}`, {
headers: {
'Content-Type': 'application/json',
...(blockHeight && { 'X-Cosmos-Block-Height': blockHeight }),
Expand Down
7 changes: 2 additions & 5 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import './polyfill';
import { OnRpcRequestHandler } from '@metamask/snaps-types';
import { panel, text, divider, heading, copyable } from '@metamask/snaps-ui';
import { RPCMethods } from '@rarimo/rarime-connector';
import { providers } from 'ethers';
import { DID } from '@iden3/js-iden3-core';
import { Identity } from './identity';
import { getItemFromStore, setItemInStore } from './rpc';
Expand All @@ -23,8 +22,8 @@ import {
saveCredentials,
checkIfStateSynced,
getUpdateStateTx,
getChainInfo,
loadDataFromRarimoCore,
getProviderChainInfo,
} from './helpers';
import { ZkpGen } from './zkp-gen';
import {
Expand Down Expand Up @@ -220,9 +219,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({

let updateStateTx;

const provider = new providers.Web3Provider(window.ethereum);
const network = await provider.getNetwork();
const chainInfo = getChainInfo(network.chainId);
const chainInfo = await getProviderChainInfo();

const isSynced = await checkIfStateSynced();

Expand Down
3 changes: 3 additions & 0 deletions packages/snap/src/types/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ declare global {
}
}

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

export type ChainInfo = {
id: number;
rpcUrl: string;
stateContractAddress: string;
rarimoNetworkType: RarimoNetworkType;
};
8 changes: 5 additions & 3 deletions packages/snap/src/zkp-gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { proving, type ZKProof } from '@iden3/js-jwz';


import { Hex, Signature } from '@iden3/js-crypto';
import { fromLittleEndian } from '@iden3/js-iden3-core';
import { type Identity } from './identity';
Expand All @@ -9,7 +10,7 @@ import {
CircuitClaim,
getGISTProof,
getNodeAuxValue,
getPreparedCredential,
getPreparedCredential, getProviderChainInfo, getRarimoEvmRpcUrl, getRarimoStateContractAddress,
newCircuitClaimData,
prepareCircuitArrayValues,
prepareSiblingsStr,
Expand Down Expand Up @@ -121,10 +122,11 @@ export class ZkpGen {
this.proofRequest.circuitId === CircuitId.AtomicQuerySigV2OnChain ||
this.proofRequest.circuitId === CircuitId.AtomicQueryMTPV2OnChain
) {
const providerChainInfo = await getProviderChainInfo();

const gistInfo = await getGISTProof({
rpcUrl: config.RARIMO_EVM_RPC_URL,
contractAddress: config.RARIMO_STATE_CONTRACT_ADDRESS,
rpcUrl: getRarimoEvmRpcUrl(providerChainInfo.id),
contractAddress: getRarimoStateContractAddress(providerChainInfo.id),
userId: this.identity.identityIdBigIntString,
});
this.gistProof = toGISTProof(gistInfo);
Expand Down
Loading

0 comments on commit 76de5bd

Please sign in to comment.