Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the ability to register using the endpoint of the validator #3

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "del-osx",
"name": "del-sdk",
"version": "1.0.0",
"description": "JS SDK for links e-mail and wallet addresses",
"repository": "https://github.com/bosagora/del-sdk",
Expand Down
10 changes: 9 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@
}
],
"devDependencies": {
"@ethersproject/experimental": "^5.7.0",
"@size-limit/preset-small-lib": "^7.0.8",
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/node-cron": "^3.0.1",
"bigint-buffer": "^1.1.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-validator": "^6.14.0",
"ganache": "^7.9.1",
"glob": "^8.0.3",
"husky": "^7.0.4",
"node-cron": "^3.0.0",
"size-limit": "^7.0.8",
"solc": "0.4.17",
"tsdx": "^0.14.1",
Expand All @@ -72,7 +79,8 @@
"@ethersproject/wallet": "^5.7.0",
"del-osx-lib": "^1.0.6",
"del-sdk-common": "^1.0.0",
"dotenv": "^16.3.1"
"dotenv": "^16.3.1",
"unfetch": "^5.0.0"
},
"jest": {
"testEnvironment": "../../test-environment.js",
Expand Down
9 changes: 9 additions & 0 deletions packages/client/src/client-common/interfaces/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ export type SupportedNetworks = typeof SupportedNetworksArray[number];
export type NetworkDeployment = {
LinkCollection: string;
};

export type GenericRecord = Record<string, string | number | boolean | null | undefined>;

export interface IHttpConfig {
/** IPFS Cluster URL */
url: URL;
/** Additional headers to be included with requests */
headers?: Record<string, string>;
}
6 changes: 6 additions & 0 deletions packages/client/src/client-common/interfaces/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { Signer } from "@ethersproject/abstract-signer";
import { Contract, ContractInterface } from "@ethersproject/contracts";
import { JsonRpcProvider } from "@ethersproject/providers";
import { GenericRecord } from "./common";
import { UnfetchResponse } from "unfetch";

export interface IClientWeb3Core {
useSigner: (signer: Signer) => void;
Expand All @@ -14,6 +16,10 @@ export interface IClientWeb3Core {
ensureOnline: () => Promise<void>;
attachContract: <T>(address: string, abi: ContractInterface) => Contract & T;
getLinkCollectionAddress: () => string;
isRelayUp: () => Promise<boolean>;
assignValidatorEndpoint: () => Promise<void>;
get: (path: string, data?: GenericRecord) => Promise<UnfetchResponse>;
post: (path: string, data?: GenericRecord) => Promise<UnfetchResponse>;
}

export interface IClientCore {
Expand Down
46 changes: 45 additions & 1 deletion packages/client/src/client-common/modules/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { JsonRpcProvider } from "@ethersproject/providers";
import { Contract, ContractInterface } from "@ethersproject/contracts";
import { Signer } from "@ethersproject/abstract-signer";
import { IClientWeb3Core } from "../interfaces/core";
import { NoLinkCollection } from "del-sdk-common";
import { NoLinkCollection, NoProviderError, NoSignerError } from "del-sdk-common";
import { GenericRecord, IHttpConfig } from "../interfaces/common";
import { LinkCollection__factory } from "del-osx-lib";
import { NoValidator } from "../../utils/errors";
import { Network } from "../../utils/network";
import { UnfetchResponse } from "unfetch";

const linkCollectionAddressMap = new Map<Web3Module, string>();
const providersMap = new Map<Web3Module, JsonRpcProvider[]>();
Expand All @@ -28,6 +33,10 @@ export class Web3Module implements IClientWeb3Core {
linkCollectionAddressMap.set(this, context.linkCollectionAddress);
}

this.config = {
url: new URL("http://localhost"),
headers: {},
};
Object.freeze(Web3Module.prototype);
Object.freeze(this);
}
Expand Down Expand Up @@ -155,4 +164,39 @@ export class Web3Module implements IClientWeb3Core {
}
return this.linkCollectionAddress;
}
public config: IHttpConfig;

public async assignValidatorEndpoint(): Promise<void> {
const signer = this.getConnectedSigner();
if (!signer) {
throw new NoSignerError();
} else if (!signer.provider) {
throw new NoProviderError();
}

const contract = LinkCollection__factory.connect(this.getLinkCollectionAddress(), signer);
const validators = await contract.getValidators();
if (validators.length === 0) {
throw new NoValidator();
}
const idx = Math.floor(Math.random() * validators.length);
this.config.url = new URL(validators[idx].endpoint);
}

public async isRelayUp(): Promise<boolean> {
try {
const res = await this.get("/");
return (res.status === 200 && (await res.json())) === "OK";
} catch {
return false;
}
}

public async get(path: string, data?: GenericRecord): Promise<UnfetchResponse> {
return Network.get(this.config, path, data);
}

public async post(path: string, data?: GenericRecord): Promise<UnfetchResponse> {
return Network.post(this.config, path, data);
}
}
32 changes: 29 additions & 3 deletions packages/client/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { BigNumber } from "ethers";
export interface IClientMethods extends IClientCore {
addRequest: (email: string) => AsyncGenerator<AddRequestValue>;
toAddress: (email: string) => Promise<string>;
toEmail: (wallet: string) => Promise<string>;
nonceOf: (wallet: string) => Promise<BigNumber>;
toEmail: (address: string) => Promise<string>;
nonceOf: (address: string) => Promise<BigNumber>;
getValidators: () => Promise<ValidatorInfoValue[]>;
isRelayUp: () => Promise<boolean>;
assignValidatorEndpoint: () => Promise<void>;
register: (email: string) => AsyncGenerator<RegisterValue>;
getRegisterStatus: (id: string) => Promise<number>;
}

export interface IClient {
Expand All @@ -29,17 +34,38 @@ export type NonceOfParams = {
wallet: string;
};

export type ValidatorInfoValue = {
address: string;
index: number;
endpoint: string;
status: number;
};

export type AddRequestValue =
| { key: AddRequestSteps.ADDING; txHash: string }
| {
key: AddRequestSteps.DONE;
email: string;
id: string;
emailHash: string;
wallet: string;
address: string;
};

export enum AddRequestSteps {
ADDING = "adding",
DONE = "done",
}

export type RegisterValue =
| { key: RegisterSteps.DOING; requestId: string; email: string; address: string }
| {
key: RegisterSteps.DONE;
requestId: string;
email: string;
address: string;
};

export enum RegisterSteps {
DOING = "doing",
DONE = "done",
}
Loading
Loading