Skip to content

Commit

Permalink
Activity Improvements (PIVX-Labs#141)
Browse files Browse the repository at this point in the history
* Fix Activity bugs and improve responsiveness

* Prettier
  • Loading branch information
JSKitty authored and Liquid369 committed Jul 27, 2023
1 parent f7cd4f5 commit 630cc0e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
13 changes: 8 additions & 5 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export {
unblurPrivKey,
toggleBottomMenu,
createProposal,
updateStakingRewardsGUI,
switchSettings,
updateActivityGUI,
} from './global.js';
Expand Down
12 changes: 10 additions & 2 deletions scripts/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 13 additions & 9 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
})
Expand All @@ -604,7 +604,7 @@ export async function importWallet({
privateImportValue[0]
)
) {
setMasterKey(
await setMasterKey(
new LegacyMasterKey({
address: privateImportValue,
})
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 630cc0e

Please sign in to comment.