implement signers in anon mode

This commit is contained in:
plebeius.eth
2023-06-26 21:55:26 +02:00
parent af7fbda8cf
commit c630d08acf
5 changed files with 214 additions and 67 deletions
+14 -24
View File
@@ -1,38 +1,28 @@
import { useEffect } from "react";
import { createAccount, setActiveAccount, useAccounts } from "@plebbit/plebbit-react-hooks";
import { useAccount } from "@plebbit/plebbit-react-hooks";
import useAnonModeStore from "./stores/useAnonModeStore";
const useAnonMode = (threadCid, execute) => {
const { accounts } = useAccounts();
const { account } = useAccount();
const { anonymousMode } = useAnonModeStore();
useEffect(() => {
const handleAnonMode = async () => {
let storedAccounts = JSON.parse(localStorage.getItem('storedAccounts')) || {};
useEffect(() => {
const handleAnonMode = async () => {
let storedSigners = JSON.parse(localStorage.getItem('storedSigners')) || {};
if (!anonymousMode) return;
if (!storedAccounts[threadCid] && execute) {
await createAccount();
if(accounts.length === 0) {
console.log("No accounts available");
return;
}
const lastAccount = accounts[accounts.length - 1];
await setActiveAccount(lastAccount.name);
storedAccounts[threadCid] = lastAccount.name;
localStorage.setItem('storedAccounts', JSON.stringify(storedAccounts));
} else {
await setActiveAccount(storedAccounts[threadCid]);
if (!anonymousMode) {
if (execute && storedSigners[threadCid]) {
const signerPrivateKey = storedSigners[threadCid];
await account.plebbit.createSigner({privateKey: signerPrivateKey});
}
}
}
handleAnonMode();
}, [threadCid, execute, account, anonymousMode]);
handleAnonMode();
}, [threadCid, execute, accounts, anonymousMode]);
return;
}
export default useAnonMode;
+11 -11
View File
@@ -1,31 +1,31 @@
import { useEffect } from "react";
import { createAccount, setActiveAccount, useAccounts } from "@plebbit/plebbit-react-hooks";
import { setActiveAccount, useAccount } from "@plebbit/plebbit-react-hooks";
import useAnonModeStore from "./stores/useAnonModeStore";
const useAnonModeRef = (threadCidRef, execute) => {
const {accounts} = useAccounts();
const { account } = useAccount();
const { anonymousMode } = useAnonModeStore();
useEffect(() => {
const handleAnonMode = async () => {
let storedAccounts = JSON.parse(localStorage.getItem('storedAccounts')) || {};
let storedSigners = JSON.parse(localStorage.getItem('storedSigners')) || {};
if (!anonymousMode) return;
if (!storedAccounts[threadCidRef.current] && execute) {
await createAccount();
const lastAccount = accounts[accounts.length - 1];
await setActiveAccount(lastAccount.name);
storedAccounts[threadCidRef.current] = lastAccount.name;
if (!storedSigners[threadCidRef] && execute) {
const signer = await account.plebbit.createSigner();
storedSigners[threadCidRef] = signer.privateKey;
localStorage.setItem('storedAccounts', JSON.stringify(storedAccounts));
localStorage.setItem('storedSigners', JSON.stringify(storedSigners));
} else {
await setActiveAccount(storedAccounts[threadCidRef.current]);
const signerPrivateKey = storedSigners[threadCidRef];
const signer = await account.plebbit.createSigner({privateKey: signerPrivateKey});
await setActiveAccount(account, {signer});
}
}
handleAnonMode();
}, [threadCidRef, execute, accounts, anonymousMode]);
}, [threadCidRef, execute, account, anonymousMode]);
return;
}