'init'
This commit is contained in:
148
app/providers/AccountProvider/AccountProvider.tsx
Normal file
148
app/providers/AccountProvider/AccountProvider.tsx
Normal file
@@ -0,0 +1,148 @@
|
||||
import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
|
||||
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
|
||||
import { createContext, useEffect, useState } from "react";
|
||||
|
||||
export interface AccountBase {
|
||||
publicKey: string;
|
||||
privateKey: string;
|
||||
seedPhraseEncrypted: string;
|
||||
}
|
||||
|
||||
export interface Account extends AccountBase {
|
||||
privatePlain: string;
|
||||
privateHash: string;
|
||||
}
|
||||
|
||||
export interface AccountContextValue {
|
||||
allAccounts: AccountBase[];
|
||||
loginedAccount: Account;
|
||||
loginAccount: (account : Account) => void;
|
||||
createAccount: (account : Account) => void;
|
||||
loginDiceAccount: AccountBase;
|
||||
accountProviderLoaded: boolean;
|
||||
removeAccountFromLoginDice: () => void;
|
||||
removeAccountsFromArrayOfAccounts: (accountBase : AccountBase) => void;
|
||||
selectAccountToLoginDice: (accountBase : AccountBase) => void;
|
||||
setAccounts: (accounts: AccountBase[]) => void;
|
||||
}
|
||||
|
||||
export const AccountContext = createContext<any>({});
|
||||
|
||||
interface AccountProviderProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function AccountProvder(props : AccountProviderProps){
|
||||
const {allQuery, runQuery} = useDatabase();
|
||||
const [accounts, setAccounts] = useState<AccountBase[]>([]);
|
||||
const [loginDice, setLoginDice] = useState<AccountBase>({
|
||||
publicKey: "",
|
||||
privateKey: "",
|
||||
seedPhraseEncrypted: ""
|
||||
});
|
||||
const [account, setAccount] = useState<Account>({
|
||||
publicKey: "",
|
||||
privatePlain: "",
|
||||
privateKey: "",
|
||||
privateHash: "",
|
||||
seedPhraseEncrypted: ""
|
||||
});
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const {info} = useConsoleLogger("AccountProvider");
|
||||
|
||||
useEffect(() => {
|
||||
loadAllAccountFromDb();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if(!loaded){
|
||||
return;
|
||||
}
|
||||
loadLastLoginDice();
|
||||
}, [accounts, loaded]);
|
||||
|
||||
const loadLastLoginDice = () => {
|
||||
console.info("2");
|
||||
let publicKey = localStorage.getItem("last_logined_account");
|
||||
if(!publicKey && accounts.length <= 0){
|
||||
console.info("1");
|
||||
return;
|
||||
}
|
||||
if(!publicKey && accounts.length > 0){
|
||||
console.info(accounts);
|
||||
setLoginDice(accounts[0]);
|
||||
return;
|
||||
}
|
||||
for(let i = 0; i < accounts.length; i++){
|
||||
let accountBase = accounts[i];
|
||||
if(accountBase.publicKey == publicKey){
|
||||
setLoginDice(accountBase);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Если last_logined_account плохой - то убираем этот аккаунт
|
||||
*/
|
||||
removeAccountFromLoginDice();
|
||||
}
|
||||
|
||||
const removeAccountsFromArrayOfAccounts = (accountBase : AccountBase) => {
|
||||
setAccounts((prev) => prev.filter((v) => v.publicKey !== accountBase.publicKey));
|
||||
}
|
||||
|
||||
const loadAllAccountFromDb = async () => {
|
||||
const result = await allQuery("SELECT * FROM `accounts`");
|
||||
let resultSet : AccountBase[] = [];
|
||||
for(let i = 0; i < result.length; i++){
|
||||
let acc = result[i];
|
||||
resultSet.push({
|
||||
publicKey: acc.public_key,
|
||||
privateKey: acc.private_key,
|
||||
seedPhraseEncrypted: acc.sfen
|
||||
});
|
||||
}
|
||||
setAccounts(resultSet);
|
||||
setLoaded(true);
|
||||
}
|
||||
|
||||
const loginAccount = (account : Account) => {
|
||||
info("Logging in account with public key: " + account.publicKey);
|
||||
setAccount(account);
|
||||
}
|
||||
|
||||
const createAccount = (account : Account) => {
|
||||
runQuery("INSERT INTO accounts (public_key, private_key, sfen) VALUES (?, ?, ?)", [account.publicKey, account.privateKey, account.seedPhraseEncrypted]);
|
||||
//maybe set state accounts
|
||||
}
|
||||
|
||||
const selectAccountToLoginDice = (accountBase: AccountBase) => {
|
||||
setLoginDice(accountBase);
|
||||
localStorage.setItem("last_logined_account", accountBase.publicKey);
|
||||
}
|
||||
|
||||
const removeAccountFromLoginDice = () => {
|
||||
setLoginDice({
|
||||
publicKey: "",
|
||||
privateKey: "",
|
||||
seedPhraseEncrypted: ""
|
||||
});
|
||||
localStorage.removeItem("last_logined_account");
|
||||
}
|
||||
|
||||
return (
|
||||
<AccountContext.Provider value={{
|
||||
allAccounts: accounts,
|
||||
loginedAccount: account,
|
||||
loginAccount: loginAccount,
|
||||
createAccount: createAccount,
|
||||
loginDiceAccount: loginDice,
|
||||
accountProviderLoaded: loaded,
|
||||
removeAccountsFromArrayOfAccounts: removeAccountsFromArrayOfAccounts,
|
||||
removeAccountFromLoginDice: removeAccountFromLoginDice,
|
||||
selectAccountToLoginDice: selectAccountToLoginDice,
|
||||
setAccounts
|
||||
}}>
|
||||
{loaded && (props.children)}
|
||||
</AccountContext.Provider>
|
||||
)
|
||||
}
|
||||
28
app/providers/AccountProvider/useAccount.ts
Normal file
28
app/providers/AccountProvider/useAccount.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useContext } from "react";
|
||||
import { Account, AccountContext, AccountContextValue } from "./AccountProvider";
|
||||
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
|
||||
|
||||
export function useAccount() : [
|
||||
Account, () => void
|
||||
] {
|
||||
const {runQuery} = useDatabase();
|
||||
const context : AccountContextValue = useContext(AccountContext);
|
||||
if(!context){
|
||||
throw new Error("useAccount must be used within a AccountProvider");
|
||||
}
|
||||
|
||||
const deleteAccount = () => {
|
||||
runQuery("DELETE FROM `accounts` WHERE `public_key` = ?", [context.loginedAccount.publicKey])
|
||||
context.removeAccountsFromArrayOfAccounts(context.loginedAccount);
|
||||
context.loginAccount({
|
||||
privateHash: "",
|
||||
privatePlain: "",
|
||||
publicKey: "",
|
||||
privateKey: "",
|
||||
seedPhraseEncrypted: ""
|
||||
});
|
||||
localStorage.removeItem("last_logined_account");
|
||||
}
|
||||
|
||||
return [context.loginedAccount, deleteAccount];
|
||||
}
|
||||
11
app/providers/AccountProvider/useAccountProvider.tsx
Normal file
11
app/providers/AccountProvider/useAccountProvider.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import { useContext } from "react";
|
||||
import { AccountContext, AccountContextValue } from "./AccountProvider";
|
||||
|
||||
export function useAccountProvider() : AccountContextValue {
|
||||
const context : AccountContextValue = useContext(AccountContext);
|
||||
if(!context){
|
||||
throw new Error("useAccountProvider must be used within a AccountProvider");
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
25
app/providers/AccountProvider/useLastLoginedAccount.ts
Normal file
25
app/providers/AccountProvider/useLastLoginedAccount.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useLastLoginedAccount() : [
|
||||
string,
|
||||
(publicKey: string) => void
|
||||
] {
|
||||
const [lastLoginedAccount, setLastLogginedAccount] =
|
||||
useState<string>("");
|
||||
|
||||
useEffect(() => {
|
||||
let publicKey =
|
||||
localStorage.getItem("last_logined_account");
|
||||
if(!publicKey){
|
||||
return;
|
||||
}
|
||||
setLastLogginedAccount(publicKey);
|
||||
}, []);
|
||||
|
||||
const setLastLogin = (publicKey: string) => {
|
||||
localStorage.setItem("last_logined_account", publicKey);
|
||||
setLastLogginedAccount(publicKey);
|
||||
}
|
||||
|
||||
return [lastLoginedAccount, setLastLogin];
|
||||
}
|
||||
29
app/providers/AccountProvider/useLogout.ts
Normal file
29
app/providers/AccountProvider/useLogout.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
|
||||
import { useMemoryClean } from "../MemoryProvider/useMemoryClean";
|
||||
import { useAccountProvider } from "./useAccountProvider";
|
||||
import { useDialogsList } from "../DialogListProvider/useDialogsList";
|
||||
import { useProtocol } from "../ProtocolProvider/useProtocol";
|
||||
|
||||
export function useLogout() {
|
||||
const {loginAccount} = useAccountProvider();
|
||||
const {info} = useConsoleLogger('useLogout');
|
||||
const memClean = useMemoryClean();
|
||||
const {setDialogs} = useDialogsList();
|
||||
const {protocol} = useProtocol();
|
||||
|
||||
const logout = () => {
|
||||
info("Logging out from account");
|
||||
memClean();
|
||||
loginAccount({
|
||||
publicKey: "",
|
||||
privateKey: "",
|
||||
seedPhraseEncrypted: "",
|
||||
privatePlain: "",
|
||||
privateHash: ""
|
||||
});
|
||||
setDialogs([]);
|
||||
protocol.close();
|
||||
}
|
||||
|
||||
return logout;
|
||||
}
|
||||
13
app/providers/AccountProvider/usePrivateKeyHash.ts
Normal file
13
app/providers/AccountProvider/usePrivateKeyHash.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { useContext } from "react";
|
||||
import { AccountContext, AccountContextValue } from "./AccountProvider";
|
||||
|
||||
export function usePrivateKeyHash() {
|
||||
const context : AccountContextValue = useContext(AccountContext);
|
||||
if(!context){
|
||||
throw new Error("useAccount must be used within a AccountProvider");
|
||||
}
|
||||
if(!context.loginedAccount){
|
||||
return "";
|
||||
}
|
||||
return context.loginedAccount.privateHash;
|
||||
}
|
||||
19
app/providers/AccountProvider/usePrivatePlain.ts
Normal file
19
app/providers/AccountProvider/usePrivatePlain.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { useContext } from "react";
|
||||
import { AccountContext, AccountContextValue } from "./AccountProvider";
|
||||
|
||||
/**
|
||||
* This hook provides access to the private plain text of the logged-in account.
|
||||
* Needs only for decrypting messages or attachments.
|
||||
* Not send to server.
|
||||
* @returns Private plain text for the logged-in account
|
||||
*/
|
||||
export function usePrivatePlain() {
|
||||
const context : AccountContextValue = useContext(AccountContext);
|
||||
if(!context){
|
||||
throw new Error("useAccount must be used within a AccountProvider");
|
||||
}
|
||||
if(!context.loginedAccount){
|
||||
return "";
|
||||
}
|
||||
return context.loginedAccount.privatePlain;
|
||||
}
|
||||
13
app/providers/AccountProvider/usePublicKey.ts
Normal file
13
app/providers/AccountProvider/usePublicKey.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { useContext } from "react";
|
||||
import { AccountContext, AccountContextValue } from "./AccountProvider";
|
||||
|
||||
export function usePublicKey() {
|
||||
const context : AccountContextValue = useContext(AccountContext);
|
||||
if(!context){
|
||||
throw new Error("useAccount must be used within a AccountProvider");
|
||||
}
|
||||
if(!context.loginedAccount){
|
||||
return "";
|
||||
}
|
||||
return context.loginedAccount.publicKey;
|
||||
}
|
||||
Reference in New Issue
Block a user