Skip to content

Commit

Permalink
Restructurize connector
Browse files Browse the repository at this point in the history
  • Loading branch information
ardier16 committed Mar 20, 2024
1 parent b0eba8d commit afe53f1
Show file tree
Hide file tree
Showing 38 changed files with 2,180 additions and 507 deletions.
564 changes: 564 additions & 0 deletions packages/connector/abis/LightweightStateV2.json

Large diffs are not rendered by default.

945 changes: 945 additions & 0 deletions packages/connector/abis/StateV2.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"build:esm": "npx swc src -d ./dist/esm --config-file ./.swcrc -C module.type=es6 isModule=true --copy-files",
"build:types": "tsc -p tsconfig.build.json --outDir ./dist/types --declaration --emitDeclarationOnly",
"clean": "rm -rf dist",
"generate-ether-types": "typechain --target=ethers-v5 'abis/**/*.json' --out-dir src/zkp/types/contracts",
"lint": "yarn run lint:style && yarn run lint:types",
"lint:style": "eslint --color 'src/**/*.{js,ts,tsx}'",
"lint:style:fix": "yarn run lint:style --fix",
Expand All @@ -50,7 +51,9 @@
"@swc/cli": "^0.1.62",
"@swc/core": "1.3.53",
"@swc/jest": "^0.2.26",
"tsc-alias": "^1.8.8"
"@typechain/ethers-v5": "11.1.1",
"tsc-alias": "^1.8.8",
"typechain": "8.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
1 change: 0 additions & 1 deletion packages/connector/src/consts/general.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const CORE_POLLING_INTERVAL = 5_000;
export const SUPPORTED_METAMASK_VERSION = '11.5.0';
1 change: 1 addition & 0 deletions packages/connector/src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './snap';
export * from './general';
1 change: 1 addition & 0 deletions packages/connector/src/consts/snap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const defaultSnapOrigin = 'npm:@rarimo/rarime';
8 changes: 0 additions & 8 deletions packages/connector/src/contracts/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/connector/src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './snap';
11 changes: 11 additions & 0 deletions packages/connector/src/enums/snap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum RPCMethods {
CreateIdentity = 'create_identity',
SaveCredentials = 'save_credentials',
RemoveCredentials = 'remove_credentials',
CheckCredentialExistence = 'check_credential_existence',
CreateProof = 'create_proof',
CheckStateContractSync = 'check_state_contract_sync',
GetCredentials = 'get_credentials',
ExportIdentity = 'export_identity',
GetIdentity = 'get_identity',
}
4 changes: 3 additions & 1 deletion packages/connector/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './state-v2-helpers';
export * from './snap';
export * from './promise';
export * from './error-helper';
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { compare } from 'compare-versions';

import { defaultSnapOrigin } from '.';
import { SUPPORTED_METAMASK_VERSION } from './consts';
import type { GetSnapsResponse } from './types';
import { SUPPORTED_METAMASK_VERSION } from '@/consts';
import type { RPCMethods } from '@/enums';
import type {
GetSnapsResponse,
SnapRequestParams,
SnapRequestsResponses,
} from '@/types';

export const getProvider = async () => {
let mmFound = false;
Expand Down Expand Up @@ -45,12 +49,6 @@ export const getProvider = async () => {
return window.ethereum;
};

export const getWalletSnaps = async (): Promise<GetSnapsResponse> => {
return await window.ethereum.request({
method: 'wallet_getSnaps',
});
};

export const isMetamaskInstalled = async (): Promise<boolean> => {
try {
const provider = await getProvider();
Expand All @@ -60,26 +58,6 @@ export const isMetamaskInstalled = async (): Promise<boolean> => {
}
};

export const isSnapInstalled = async (
snapOrigin?: string,
version?: string,
): Promise<boolean> => {
try {
await getProvider();
const snapId = snapOrigin ?? defaultSnapOrigin;
return Boolean(
Object.values(await getWalletSnaps()).find(
(permission) =>
permission.id === snapId &&
(!version || permission.version === version),
),
);
} catch (e) {
console.log('Failed to obtain installed snaps', e);
return false;
}
};

export const checkSnapSupport = async () => {
const version = await window?.ethereum?.request?.({
method: 'web3_clientVersion',
Expand All @@ -89,3 +67,26 @@ export const checkSnapSupport = async () => {
const isMobile = version.endsWith('Mobile');
return compare(currentVersion, SUPPORTED_METAMASK_VERSION, '>=') && !isMobile;
};

export const getWalletSnaps = async (): Promise<GetSnapsResponse> => {
const provider = await getProvider();

return await provider.request({
method: 'wallet_getSnaps',
});
};

export const sendSnapMethod = async <Method extends RPCMethods>(
request: { method: Method; params?: SnapRequestParams[Method] },
snapId: string,
): Promise<SnapRequestsResponses[Method]> => {
const provider = await getProvider();

return await provider.request({
method: 'wallet_invokeSnap',
params: {
request,
snapId,
},
});
};
113 changes: 0 additions & 113 deletions packages/connector/src/helpers/state-v2-helpers.ts

This file was deleted.

36 changes: 4 additions & 32 deletions packages/connector/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
import { MetamaskSnap } from './snap';
import { checkSnapSupport, isMetamaskInstalled } from './utils';
import versionJson from './version.json';

export { MetamaskSnap } from './snap';
export * from './helpers';
export * from './consts';
export * from './enums';
export * from './types';
export { isMetamaskInstalled, isSnapInstalled } from './utils';

export const defaultSnapOrigin = 'npm:@rarimo/rarime';

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

if (!(await isMetamaskInstalled())) {
throw new Error('Metamask is not installed');
}

if (!(await checkSnapSupport())) {
throw new Error('Current version of MetaMask is not supported');
}

await window.ethereum.request({
method: 'wallet_requestSnaps',
params: {
[snapId]: { ...(version && { version }) },
},
});

return new MetamaskSnap(snapId);
};
export * from './zkp';
1 change: 1 addition & 0 deletions packages/connector/src/instances/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './snap';
83 changes: 83 additions & 0 deletions packages/connector/src/instances/snap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import versionJson from '../version.json';

import { defaultSnapOrigin } from '@/consts';
import type { RPCMethods } from '@/enums';
import {
checkSnapSupport,
getProvider,
getWalletSnaps,
isMetamaskInstalled,
} from '@/helpers';
import type { SnapRequestParams, SnapRequestsResponses } from '@/types';

export class RarimeSnapBase {
public readonly snapId: string;

public readonly version: string;

public constructor(
snapId = defaultSnapOrigin,
version = versionJson.version,
) {
this.snapId = snapId;
this.version = version;
}

public async sendSnapRequest<Method extends RPCMethods>(
method: Method,
params?: SnapRequestParams[Method],
): Promise<SnapRequestsResponses[Method]> {
const provider = await getProvider();

return await provider.request({
method: 'wallet_invokeSnap',
params: {
request: {
method,
params,
},
snapId: this.snapId,
},
});
}

public async enable() {
const snapId = this.snapId ?? defaultSnapOrigin;

if (!(await isMetamaskInstalled())) {
throw new Error('Metamask is not installed');
}

if (!(await checkSnapSupport())) {
throw new Error('Current version of MetaMask is not supported');
}

const provider = await getProvider();

return await provider.request({
method: 'wallet_requestSnaps',
params: {
[snapId]: { ...(this.version && { version: this.version }) },
},
});
}

async isInstalled() {
try {
await getProvider();

const walletSnaps = await getWalletSnaps();

return Boolean(
Object.values(walletSnaps).find(
(permission) =>
permission.id === this.snapId &&
(!this.version || permission.version === this.version),
),
);
} catch (error) {
console.log('Failed to obtain installed snaps', error);
return false;
}
}
}
Loading

0 comments on commit afe53f1

Please sign in to comment.