mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-02 12:53:08 +00:00
* use next.js tempalte * use next lint rules * alight UI * fix problem with margins * remove old page, rename to hooks * add constants * create RLN service, add utils * add RLN init * refactor a bit * add init for Keystore * add store functions * add styles * add contract hooks, add store fields, fix multiple downloads issue * add features * add keystore read logic * add import export * combine to useContract * add useWaku stub, add state to Waku block, styles * add creation of a node * add messages functionality * up readme * update CI * update readme * remove and rename, address comments * update readme * move const
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import React from "react";
|
|
import { useStore } from "./useStore";
|
|
import { useRLN } from "./useRLN";
|
|
import { SEPOLIA_CONTRACT } from "@waku/rln";
|
|
import { StatusEventPayload } from "@/constants";
|
|
|
|
type UseKeystoreResult = {
|
|
onReadCredentials: (hash: string, password: string) => void;
|
|
onRegisterCredentials: (password: string) => void;
|
|
};
|
|
|
|
export const useKeystore = (): UseKeystoreResult => {
|
|
const { rln } = useRLN();
|
|
const {
|
|
credentials,
|
|
setActiveCredential,
|
|
setActiveMembershipID,
|
|
setAppStatus,
|
|
setCredentials,
|
|
} = useStore();
|
|
|
|
const onRegisterCredentials = React.useCallback(
|
|
async (password: string) => {
|
|
if (!credentials || !rln?.rlnContract || !password) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setAppStatus(StatusEventPayload.CREDENTIALS_REGISTERING);
|
|
const membershipInfo = await rln.rlnContract.registerWithKey(
|
|
credentials
|
|
);
|
|
const membershipID = membershipInfo!.index.toNumber();
|
|
const keystoreHash = await rln.keystore.addCredential(
|
|
{
|
|
membership: {
|
|
treeIndex: membershipID,
|
|
chainId: SEPOLIA_CONTRACT.chainId,
|
|
address: SEPOLIA_CONTRACT.address,
|
|
},
|
|
identity: credentials,
|
|
},
|
|
password
|
|
);
|
|
setActiveCredential(keystoreHash);
|
|
setActiveMembershipID(membershipID);
|
|
rln.saveKeystore();
|
|
setAppStatus(StatusEventPayload.CREDENTIALS_REGISTERED);
|
|
} catch (error) {
|
|
setAppStatus(StatusEventPayload.CREDENTIALS_FAILURE);
|
|
console.error("Failed to register to RLN Contract: ", error);
|
|
return;
|
|
}
|
|
},
|
|
[credentials, rln, setActiveCredential, setActiveMembershipID, setAppStatus]
|
|
);
|
|
|
|
const onReadCredentials = React.useCallback(
|
|
async (hash: string, password: string) => {
|
|
if (!rln || !hash || !password) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const record = await rln.keystore.readCredential(hash, password);
|
|
if (record) {
|
|
setCredentials(record.identity);
|
|
setActiveCredential(hash);
|
|
setActiveMembershipID(record.membership.treeIndex);
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to read credentials from Keystore.");
|
|
return;
|
|
}
|
|
},
|
|
[rln, setActiveCredential, setActiveMembershipID, setCredentials]
|
|
);
|
|
|
|
return {
|
|
onRegisterCredentials,
|
|
onReadCredentials,
|
|
};
|
|
};
|