Skip to content

Commit

Permalink
* store encrypted mnemonic
Browse files Browse the repository at this point in the history
* update process changed to process active wallet first
* allow force to start sync process.
  • Loading branch information
vorujack committed Jul 1, 2024
1 parent 69c451b commit aea20ed
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 48 deletions.
3 changes: 3 additions & 0 deletions src/action/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class WalletDbAction {
extended_public_key: string,
network_type: string,
requiredSign: number,
encryptedMnemonic: string,
) => {
const wallet = {
name: name,
Expand All @@ -67,6 +68,7 @@ class WalletDbAction {
extended_public_key: extended_public_key,
network_type: network_type,
required_sign: requiredSign,
encrypted_mnemonic: encryptedMnemonic,
};
await this.walletRepository.save(wallet);
const res = await this.walletRepository.findOneBy({
Expand All @@ -76,6 +78,7 @@ class WalletDbAction {
extended_public_key: extended_public_key,
network_type: network_type,
required_sign: requiredSign,
encrypted_mnemonic: encryptedMnemonic,
});
if (!res) throw Error('Can not store wallet');
return res;
Expand Down
56 changes: 29 additions & 27 deletions src/action/sync.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Address from '@/db/entities/Address';
import { AddressValueType } from '@/db/entities/AddressValueInfo';
import Wallet from '@/db/entities/Wallet';
import store from '@/store';
import { setBalances } from '@/store/reducer/wallet';
import { setBalances, StateWallet } from '@/store/reducer/wallet';
import { CONFIRMATION_HEIGHT } from '@/utils/const';
import getChain from '@/utils/networks';
import { AbstractNetwork } from '@/utils/networks/abstractNetwork';
Expand Down Expand Up @@ -156,39 +155,42 @@ const verifyAddress = async (addressId: number) => {
}
};

const syncWallet = async (wallet: Wallet) => {
const chain = getChain(wallet.network_type);
const syncWallet = async (wallet: StateWallet) => {
const chain = getChain(wallet.networkType);
const network = chain.getNetwork();
const height = await chain.getNetwork().getHeight();
const addresses = await AddressDbAction.getInstance().getWalletAddresses(
wallet.id,
);
for (const address of addresses) {
try {
await syncInfo(network, address);
} catch (e) {
console.log(e);
}
try {
await Promise.all(
addresses.map(async address => {
await syncInfo(network, address)
})
)
} catch (e) {
console.log(e);
}
for (const address of addresses) {
try {
if (await network.syncBoxes(address)) {
const verifyHeight = await network.getHeight();
const dbAddresses = await AddressDbAction.getInstance().getAddressById(
address.id,
);
if (
dbAddresses.length > 0 &&
dbAddresses[0].process_height === verifyHeight
) {
await verifyAddress(address.id);
await Promise.all(
addresses.map(async (address) => {
try {
if (await network.syncBoxes(address)) {
const verifyHeight = await network.getHeight();
const dbAddresses =
await AddressDbAction.getInstance().getAddressById(address.id);
if (
dbAddresses.length > 0 &&
dbAddresses[0].process_height === verifyHeight
) {
await verifyAddress(address.id);
}
}
} catch (e) {
console.log(e);
}
} catch (e) {
console.log(e);
}
}
await syncAssets(network, wallet.network_type, height);
}),
);
await syncAssets(network, wallet.networkType, height);
};

export { syncWallet };
20 changes: 15 additions & 5 deletions src/action/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { setActiveWallet } from '@/store/reducer/config';
import { mnemonicToSeedSync } from 'bip39';
import * as wasm from 'ergo-lib-wasm-browser';
import { WalletType } from '@/db/entities/Wallet';
import store from '@/store';
import {
invalidateWallets,
addedWallets,
StateAddress,
StateWallet,
} from '@/store/reducer/wallet';
Expand All @@ -13,7 +14,7 @@ import {
bip32,
getBase58ExtendedPublicKey,
isValidAddress,
} from '../utils/functions';
} from '@/utils/functions';
import {
RootPathWithoutIndex,
getWalletAddressSecret,
Expand Down Expand Up @@ -46,16 +47,21 @@ const createWallet = async (
const storedSeed = encryptionPassword
? encrypt(seed, encryptionPassword)
: seed.toString('hex');
const encryptedMnemonic = encryptionPassword
? encrypt(Buffer.from(mnemonic, 'utf-8'), encryptionPassword)
: '';
const wallet = await WalletDbAction.getInstance().createWallet(
name,
type,
storedSeed,
extended_public_key.toBase58(),
network_type,
1,
encryptedMnemonic,
);
await addAllWalletAddresses(walletEntityToWalletState(wallet));
store.dispatch(invalidateWallets());
store.dispatch(addedWallets());
store.dispatch(setActiveWallet({ activeWallet: wallet.id }));
};

const createReadOnlyWallet = async (
Expand All @@ -71,6 +77,7 @@ const createReadOnlyWallet = async (
extended_public_key,
network_type,
1,
'',
);
if (extended_public_key) {
await addAllWalletAddresses(walletEntityToWalletState(walletEntity));
Expand All @@ -83,7 +90,8 @@ const createReadOnlyWallet = async (
0,
);
}
store.dispatch(invalidateWallets());
store.dispatch(addedWallets());
store.dispatch(setActiveWallet({ activeWallet: walletEntity.id }));
};

const createMultiSigWallet = async (
Expand All @@ -103,6 +111,7 @@ const createMultiSigWallet = async (
is_derivable ? wallet.extended_public_key : '',
wallet.network_type,
minSig,
'',
);
await MultiSigDbAction.getInstance().createKey(
createdWallet,
Expand All @@ -122,7 +131,8 @@ const createMultiSigWallet = async (
}
}
await addAllWalletAddresses(walletEntityToWalletState(createdWallet));
store.dispatch(invalidateWallets());
store.dispatch(addedWallets());
store.dispatch(setActiveWallet({ activeWallet: wallet.id }));
}
};

Expand Down
79 changes: 63 additions & 16 deletions src/hooks/useUpdater.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,79 @@
import {
addUpdatedWallets,
clearUpdatedWallets,
forceRefresh,
} from '@/store/reducer/wallet';
import { useEffect, useState } from 'react';
import { WalletDbAction } from '@/action/db';
import { syncWallet } from '@/action/sync';
import { REFRESH_INTERVAL } from '@/utils/const';
import { useSelector } from 'react-redux';
import { GlobalStateType } from '@/store';
import store, { GlobalStateType } from '@/store';

const useUpdater = () => {
const [loading, setLoading] = useState(false);
const [loading, setLoading] = useState(true);
const initialized = useSelector(
(state: GlobalStateType) => state.wallet.initialized,
);
const wallets = useSelector((state: GlobalStateType) => state.wallet.wallets);
const updatedWallets = useSelector(
(state: GlobalStateType) => state.wallet.updatedWallets,
);
const activeWallet = useSelector(
(state: GlobalStateType) => state.config.activeWallet,
);
const refresh = useSelector((state: GlobalStateType) => state.wallet.refresh);
const [timer, setTimer] = useState<NodeJS.Timeout | undefined>();

useEffect(() => {
setTimeout(() => setLoading(false), 10000);
}, []);

useEffect(() => {
if (!loading && initialized) {
setLoading(true);
WalletDbAction.getInstance()
.getWallets()
.then(async (wallets) => {
for (const wallet of wallets) {
try {
await syncWallet(wallet);
} catch (exp) {
console.log(exp);
}
}
setTimeout(() => setLoading(false), REFRESH_INTERVAL);
});
const filtered = wallets.filter(
(item) => updatedWallets.indexOf(item.id) === -1,
);
const filteredActive = filtered.filter(
(item) => item.id !== activeWallet,
);
const wallet =
filteredActive.length === 0 ? filtered[0] : filteredActive[0];
if (wallet) {
syncWallet(wallet)
.then(() => {
store.dispatch(addUpdatedWallets(wallet.id));
setLoading(false);
})
.catch(() => {
setLoading(false);
});
} else {
console.log(
`Completed syncing process. synced ${updatedWallets.length} wallets`,
);
setLoading(false);
}
}
}, [activeWallet, loading, initialized, wallets, updatedWallets]);
useEffect(() => {
if (!loading && refresh) {
store.dispatch(clearUpdatedWallets());
if (timer) {
clearTimeout(timer);
setTimer(undefined);
}
}
}, [refresh, loading, timer]);
useEffect(() => {
if (updatedWallets.length === wallets.length && !timer) {
const timer = setTimeout(() => {
store.dispatch(forceRefresh(true));
setTimer(undefined);
}, REFRESH_INTERVAL);
setTimer(timer);
}
}, [loading, initialized]);
}, [refresh, updatedWallets, wallets, timer]);
return { loading };
};

Expand Down
23 changes: 23 additions & 0 deletions src/store/reducer/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface WalletStateType {
walletsValid: boolean;
addressesValid: boolean;
initialized: boolean;
refresh: boolean;
updatedWallets: Array<number>;
}

export const walletInitialState: WalletStateType = {
Expand All @@ -66,6 +68,8 @@ export const walletInitialState: WalletStateType = {
walletsValid: false,
addressesValid: false,
initialized: false,
refresh: false,
updatedWallets: [],
};

const updateWalletBalance = (
Expand Down Expand Up @@ -160,6 +164,21 @@ const walletSlice = createSlice({
invalidateAddresses(state) {
state.addressesValid = false;
},
forceRefresh(state, action: PayloadAction<boolean>) {
state.refresh = action.payload;
},
clearUpdatedWallets(state) {
state.updatedWallets = [];
state.refresh = false;
},
addUpdatedWallets(state, action: PayloadAction<number>) {
state.updatedWallets.push(action.payload);
},
addedWallets(state) {
state.walletsValid = false;
state.addressesValid = false;
state.refresh = false;
},
},
});

Expand All @@ -171,4 +190,8 @@ export const {
setWallets,
setAddresses,
setBalances,
addUpdatedWallets,
clearUpdatedWallets,
forceRefresh,
addedWallets,
} = walletSlice.actions;

0 comments on commit aea20ed

Please sign in to comment.