diff --git a/scripts/global.js b/scripts/global.js index d0e9cfb64..60f23df06 100644 --- a/scripts/global.js +++ b/scripts/global.js @@ -870,6 +870,9 @@ export async function updateActivityGUI(fStaking = false, fNewOnly = false) { // Prevent the user from spamming refreshes if (cNet.historySyncing) return; + // Remember how much history we had previously + const nPrevHistory = cNet.arrTxHistory.length; + // Choose the Dashboard or Staking UI accordingly let domLoadMore = doms.domActivityLoadMore; let domLoadMoreIcon = doms.domActivityLoadMoreIcon; @@ -883,6 +886,9 @@ export async function updateActivityGUI(fStaking = false, fNewOnly = false) { const arrTXs = await cNet.syncTxHistoryChunk(fNewOnly); domLoadMoreIcon.classList.remove('fa-spin'); + // If there's no change in history size post-sync, then we can cancel here, there's nothing new to render + if (nPrevHistory === cNet.arrTxHistory.length) return; + // Check if all transactions are loaded if (cNet.isHistorySynced) { // Hide the load more button @@ -2163,12 +2169,9 @@ export function refreshChainData() { if (!masterKey) return; // Fetch block count + UTXOs, update the UI for new transactions - const nPrevBlock = cNet.cachedBlockCount; cNet.getBlockCount().then((_) => { - // Update the Activity if a new block arrived - if (cNet.cachedBlockCount > nPrevBlock) { - updateActivityGUI(false, true); - } + // Fetch latest Activity + updateActivityGUI(false, true); }); getBalance(true); } diff --git a/scripts/index.js b/scripts/index.js index b4f4ed73a..7af9821a3 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -40,7 +40,6 @@ export { unblurPrivKey, toggleBottomMenu, createProposal, - updateStakingRewardsGUI, switchSettings, updateActivityGUI, } from './global.js'; diff --git a/scripts/network.js b/scripts/network.js index d61f0727e..da78857f8 100644 --- a/scripts/network.js +++ b/scripts/network.js @@ -545,9 +545,17 @@ export class ExplorerNetwork extends Network { .filter((tx) => tx.amount != 0); } - setMasterKey(masterKey) { + async setMasterKey(masterKey) { + // If the public Master Key (xpub, address...) is different, then wipe TX history + if ( + (await this.masterKey?.keyToExport) !== + (await masterKey.keyToExport) + ) { + this.arrTxHistory = []; + } + + // Set the key this.masterKey = masterKey; - this.arrTxHistory = []; } async getTxInfo(txHash) { diff --git a/scripts/wallet.js b/scripts/wallet.js index 60e5a6b9d..6766f7dca 100644 --- a/scripts/wallet.js +++ b/scripts/wallet.js @@ -548,7 +548,7 @@ export async function importWallet({ if (!publicKey) return; // Derive our hardware address and import! - setMasterKey(new HardwareWalletMasterKey()); + await setMasterKey(new HardwareWalletMasterKey()); // Hide the 'export wallet' button, it's not relevant to hardware wallets doms.domExportWallet.hidden = true; @@ -582,18 +582,18 @@ export async function importWallet({ privateImportValue, passphrase ); - setMasterKey(new HdMasterKey({ seed })); + await setMasterKey(new HdMasterKey({ seed })); } else { // Public Key Derivation try { if (privateImportValue.startsWith('xpub')) { - setMasterKey( + await setMasterKey( new HdMasterKey({ xpub: privateImportValue, }) ); } else if (privateImportValue.startsWith('xprv')) { - setMasterKey( + await setMasterKey( new HdMasterKey({ xpriv: privateImportValue, }) @@ -604,7 +604,7 @@ export async function importWallet({ privateImportValue[0] ) ) { - setMasterKey( + await setMasterKey( new LegacyMasterKey({ address: privateImportValue, }) @@ -614,7 +614,7 @@ export async function importWallet({ const pkBytes = parseWIF(privateImportValue); // Import the raw private key - setMasterKey(new LegacyMasterKey({ pkBytes })); + await setMasterKey(new LegacyMasterKey({ pkBytes })); } } catch (e) { return createAlert( @@ -685,10 +685,14 @@ export async function importWallet({ } } -function setMasterKey(mk) { +/** + * Set or replace the active Master Key with a new Master Key + * @param {MasterKey} mk - The new Master Key to set active + */ +async function setMasterKey(mk) { masterKey = mk; // Update the network master key - getNetwork().setMasterKey(masterKey); + await getNetwork().setMasterKey(masterKey); } // Wallet Generation @@ -708,7 +712,7 @@ export async function generateWallet(noUI = false) { const seed = await mnemonicToSeed(mnemonic, passphrase); // Prompt the user to encrypt the seed - setMasterKey(new HdMasterKey({ seed })); + await setMasterKey(new HdMasterKey({ seed })); fWalletLoaded = true; if (!cChainParams.current.isTestnet)