This commit is contained in:
rosetta
2026-01-30 05:01:05 +02:00
commit 83f38dc63f
327 changed files with 18725 additions and 0 deletions

View 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>
)
}

View 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];
}

View 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;
}

View 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];
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}