mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-02 12:53:08 +00:00
create RLN service, add utils
This commit is contained in:
parent
1e74641cd8
commit
0232dd1e06
3006
examples/rln-js/package-lock.json
generated
3006
examples/rln-js/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -9,6 +9,8 @@
|
||||
"lint": "next lint"
|
||||
},
|
||||
"dependencies": {
|
||||
"@waku/rln": "^0.1.1-fa49e29",
|
||||
"ethers": "^5.7.2",
|
||||
"next": "13.5.6",
|
||||
"protobufjs": "^7.2.5",
|
||||
"react": "^18",
|
||||
|
||||
96
examples/rln-js/src/services/rln.ts
Normal file
96
examples/rln-js/src/services/rln.ts
Normal file
@ -0,0 +1,96 @@
|
||||
import { ethers } from "ethers";
|
||||
import {
|
||||
create,
|
||||
Keystore,
|
||||
RLNDecoder,
|
||||
RLNEncoder,
|
||||
RLNContract,
|
||||
SEPOLIA_CONTRACT,
|
||||
RLNInstance,
|
||||
} from "@waku/rln";
|
||||
import { isBrowserProviderValid } from "@/utils/ethereum";
|
||||
|
||||
enum RLNEventsNames {
|
||||
Status = "status",
|
||||
}
|
||||
|
||||
enum StatusEventPayload {
|
||||
WASM_LOADING = "WASM Blob download in progress...",
|
||||
WASM_FAILED = "Failed to download WASM, check console",
|
||||
CONTRACT_LOADING = "Connecting to RLN contract",
|
||||
CONTRACT_FAILED = "Failed to connect to RLN contract",
|
||||
}
|
||||
|
||||
type EmitterProps = Pick<EventTarget, "addEventListener"> &
|
||||
Pick<EventTarget, "removeEventListener">;
|
||||
type IRLN = EmitterProps & {};
|
||||
|
||||
export class RLN implements IRLN {
|
||||
private readonly emitter = new EventTarget();
|
||||
private readonly ethProvider: ethers.providers.Web3Provider;
|
||||
|
||||
private rlnInstance: undefined | RLNInstance;
|
||||
private rlnContract: undefined | RLNContract;
|
||||
|
||||
private initialized = false;
|
||||
|
||||
public constructor() {
|
||||
const ethereum = (<any>window)
|
||||
.ethereum as ethers.providers.ExternalProvider;
|
||||
if (!isBrowserProviderValid(ethereum)) {
|
||||
throw Error(
|
||||
"Invalid Ethereum provider present on the page. Check if MetaMask is connected."
|
||||
);
|
||||
}
|
||||
this.ethProvider = new ethers.providers.Web3Provider(ethereum, "any");
|
||||
}
|
||||
|
||||
public async init(): Promise<void> {
|
||||
if (this.initialized) {
|
||||
console.info("RLN is initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
this.emitStatusEvent(StatusEventPayload.WASM_LOADING);
|
||||
try {
|
||||
this.rlnInstance = await create();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Failed at fetching WASM and creating RLN instance: ",
|
||||
error
|
||||
);
|
||||
this.emitStatusEvent(StatusEventPayload.WASM_FAILED);
|
||||
throw error;
|
||||
}
|
||||
|
||||
this.emitStatusEvent(StatusEventPayload.CONTRACT_LOADING);
|
||||
try {
|
||||
this.rlnContract = await RLNContract.init(this.rlnInstance, {
|
||||
registryAddress: SEPOLIA_CONTRACT.address,
|
||||
provider: this.ethProvider.getSigner(),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to connect to RLN contract: ", error);
|
||||
this.emitStatusEvent(StatusEventPayload.CONTRACT_FAILED);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public addEventListener(
|
||||
name: RLNEventsNames,
|
||||
fn: EventListenerOrEventListenerObject
|
||||
) {
|
||||
return this.emitter.addEventListener(name, fn);
|
||||
}
|
||||
|
||||
public removeEventListener(
|
||||
name: RLNEventsNames,
|
||||
fn: EventListenerOrEventListenerObject
|
||||
) {
|
||||
return this.emitter.removeEventListener(name, fn);
|
||||
}
|
||||
|
||||
private emitStatusEvent(payload: StatusEventPayload) {
|
||||
this.emitter.dispatchEvent(new Event(payload));
|
||||
}
|
||||
}
|
||||
6
examples/rln-js/src/utils/ethereum.ts
Normal file
6
examples/rln-js/src/utils/ethereum.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export const isBrowserProviderValid = (obj: any) => {
|
||||
if (obj && typeof obj.request === "function") {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user