';
return;
}
let html = "";
let i = 0;
for (const tx of homeTxs) {
// For swap transactions, show the user's own labelled wallet
// address (the one that initiated the swap) instead of the
// contract address which is not useful in the list view.
const counterparty =
tx.direction === "contract" && tx.directionLabel === "Swap"
? tx.from
: tx.direction === "sent" || tx.direction === "contract"
? tx.to
: tx.from;
// directionLabel is the explorer's own method name for a contract
// call, title-cased — attacker-chosen for an attacker's contract.
const dirLabel = escapeHtml(tx.directionLabel);
const sym = displaySymbol(tx.symbol);
const amountStr = tx.value
? escapeHtml(tx.value + " " + sym)
: escapeHtml(sym);
// The counterparty used to be squeezed in beside the amount and
// truncated to whatever was left over. It gets its own row now and
// is shown whole; the title, when it is one of our own addresses,
// names it on the line above rather than replacing it.
const title = addressTitle(counterparty, state.wallets);
const titleStr = title ? escapeHtml(title) : "";
const err = tx.isError ? " (failed)" : "";
const opacity = tx.isError ? " opacity-50" : "";
const ago = escapeHtml(timeAgo(tx.timestamp));
const iso = escapeHtml(isoDate(tx.timestamp));
html += `
`;
html += `
${ago}${dirLabel}${err}
`;
html += txCounterpartyHtml(counterparty, titleStr, amountStr);
html += `
`;
i++;
}
list.innerHTML = html;
list.querySelectorAll(".home-tx-row").forEach((row) => {
row.addEventListener("click", () => {
const idx = parseInt(row.dataset.tx, 10);
const tx = homeTxs[idx];
// Set selectedWallet/selectedAddress so back navigation works
for (let wi = 0; wi < state.wallets.length; wi++) {
for (
let ai = 0;
ai < state.wallets[wi].addresses.length;
ai++
) {
const addr = state.wallets[wi].addresses[ai].address;
if (
addr.toLowerCase() === tx.from.toLowerCase() ||
addr.toLowerCase() === tx.to.toLowerCase()
) {
state.selectedWallet = wi;
state.selectedAddress = ai;
state.selectedToken = null;
ctx.showTransactionDetail(tx);
return;
}
}
}
});
});
}
async function loadHomeTxs(ctx) {
const allAddresses = [];
for (const w of state.wallets) {
for (const a of w.addresses) {
allAddresses.push(a.address);
}
}
if (allAddresses.length === 0) return;
const networkId = state.networkId;
const filters = {
hideSpoofedSymbols: state.hideSpoofedSymbols,
hideLowHolderTokens: state.hideLowHolderTokens,
hideFraudContracts: state.hideFraudContracts,
hideDustTransactions: state.hideDustTransactions,
dustThresholdGwei: state.dustThresholdGwei,
fraudContracts: state.fraudContracts,
};
try {
const fetches = allAddresses.map((addr) =>
fetchRecentTransactions(
addr,
state.blockscoutUrl,
currentNetwork().chainId,
),
);
const results = await Promise.all(fetches);
// If the network changed during the lookups, nothing is saved or
// drawn. See src/shared/storedSettings.js.
if (
state.networkId !== networkId ||
!(await storedNetworkIs(networkId))
) {
return;
}
// Merge, deduplicate by hash, filter, sort, take 25
const seen = new Set();
let merged = [];
for (const txs of results) {
for (const tx of txs) {
if (seen.has(tx.hash)) continue;
seen.add(tx.hash);
merged.push(tx);
}
}
const filtered = filterTransactions(merged, filters);
// Persist any newly discovered fraud contracts, inside the save and
// holding the lock. Nothing is added unless this page and the stored
// record both still name the request's network.
if (filtered.newFraudContracts.length > 0) {
let saved = false;
await saveState((stored) => {
if (state.networkId !== networkId) return false;
if (stored.networkId !== networkId) return false;
for (const addr of filtered.newFraudContracts) {
if (!state.fraudContracts.includes(addr)) {
state.fraudContracts.push(addr);
}
}
saved = true;
});
// If the network changed while the save waited for the lock,
// nothing is drawn.
if (!saved) return;
}
merged = filtered.transactions;
merged.sort((a, b) => b.blockNumber - a.blockNumber);
homeTxs = merged.slice(0, 25);
renderHomeTxList(ctx);
} catch (e) {
// Cancelled by the popup closing, not failed: see pageClosed in
// src/popup/index.js.
if (ctx.pageClosed.aborted) return;
log.errorf("loadHomeTxs failed:", e.message);
// If the network changed, the failure is not shown.
if (
state.networkId !== networkId ||
!(await storedNetworkIs(networkId))
) {
return;
}
const list = $("home-tx-list");
if (list) {
list.innerHTML =
'
Failed to load transactions.
';
}
}
}
// The wallet list markup. Pure: it reads state and returns a string, so the
// list can be asserted on without a DOM.
function walletListHtml() {
let html = "";
state.wallets.forEach((wallet, wi) => {
const defect = walletDefect(wallet);
html += `
`;
html += `
`;
html += `${escapeHtml(wallet.name)}`;
// No "+" on a defective wallet: deriving another address from that
// xpub would only add one more address the key does not produce
// under the standard path.
if (!defect && (wallet.type === "hd" || wallet.type === "xprv")) {
html += ``;
}
html += `
`;
html += walletDefectHtml(wallet);
wallet.addresses.forEach((addr, ai) => {
html += `
`;
const isActive = state.activeAddress === addr.address;
const infoBtn = `[info]`;
// Only where a wallet can spare the address: a wallet holding a
// single address has no remove control, because its last address
// is never removable.
const removeBtn = canRemoveAddress(wallet)
? `[x]`
: "";
const dot = addressDotHtml(addr.address);
const titleBold = isActive ? "font-bold" : "";
// [info] and [x] ride on the "Address N" line, which was empty
// to its right, so the address below gets the row to itself.
// They used to sit beside the address and take about a third of
// the width off it, which is what made a 42-character address
// fold onto a second line here and nowhere else (#380).
html += `
`;
html += `${dot}Address ${ai + 1}`;
html += `${infoBtn}${removeBtn}`;
html += `
`;
if (addr.ensName) {
// An ENS reverse record is whatever the name owner set it
// to; renderAddressHtml() escapes its own copy of this and
// this list was the one that did not.
html += `
${escapeHtml(addr.ensName)}
`;
}
html += `
${escapeHtml(addr.address)}
`;
const addrTotal = formatAddressTotal(getAddressValue(addr));
html += `
${escapeHtml(addrTotal) || " "}
`;
html += balanceLinesForAddress(
addr,
state.trackedTokens,
state.showZeroBalanceTokens,
);
html += `
`;
});
html += `
`;
});
return html;
}
function render(ctx) {
const container = $("wallet-list");
// Only Blockscout reports the tokens the user has not tracked.
$("home-tokens-note").classList.toggle("hidden", !!state.useBlockscout);
if (state.wallets.length === 0) {
container.innerHTML =
'
No wallets yet. Add one to get started.
';
renderTotalValue();
renderActiveAddress();
return;
}
container.innerHTML = walletListHtml();
container.querySelectorAll(".address-row").forEach((row) => {
row.addEventListener("click", async () => {
const wi = parseInt(row.dataset.wallet, 10);
const ai = parseInt(row.dataset.address, 10);
const addr = state.wallets[wi].addresses[ai].address;
if (state.activeAddress !== addr) {
state.activeAddress = addr;
await saveState();
render(ctx);
notify({ type: "AUTISTMASK_ACTIVE_CHANGED" });
}
});
});
container.querySelectorAll(".btn-addr-info").forEach((btn) => {
btn.addEventListener("click", (e) => {
e.stopPropagation();
state.selectedWallet = parseInt(btn.dataset.wallet, 10);
state.selectedAddress = parseInt(btn.dataset.address, 10);
ctx.showAddressDetail();
});
});
container.querySelectorAll(".btn-remove-address").forEach((btn) => {
btn.addEventListener("click", (e) => {
e.stopPropagation();
ctx.showDeleteAddress(
parseInt(btn.dataset.wallet, 10),
parseInt(btn.dataset.address, 10),
);
});
});
container.querySelectorAll(".btn-add-address").forEach((btn) => {
btn.addEventListener("click", async (e) => {
e.stopPropagation();
const wi = parseInt(btn.dataset.wallet, 10);
const wallet = state.wallets[wi];
const newAddr = deriveAddressFromXpub(
wallet.xpub,
wallet.nextIndex,
);
wallet.addresses.push({
address: newAddr,
index: wallet.nextIndex,
balance: "0.0000",
tokenBalances: [],
});
wallet.nextIndex++;
await saveState();
render(ctx);
ctx.doRefreshAndRender();
});
});
container.querySelectorAll(".wallet-name").forEach((span) => {
span.addEventListener("click", (e) => {
e.stopPropagation();
const wi = parseInt(span.dataset.wallet, 10);
const wallet = state.wallets[wi];
const input = document.createElement("input");
input.type = "text";
input.value = wallet.name;
input.className =
"font-bold border border-border p-0 bg-bg text-fg";
input.style.width = "100%";
const save = async () => {
const val = input.value.trim();
if (val && val !== wallet.name) {
wallet.name = val;
await saveState();
}
render(ctx);
};
input.addEventListener("blur", save);
input.addEventListener("keydown", (ev) => {
if (ev.key === "Enter") input.blur();
if (ev.key === "Escape") {
input.value = wallet.name;
input.blur();
}
});
span.replaceWith(input);
input.focus();
input.select();
});
});
renderTotalValue();
renderActiveAddress();
loadHomeTxs(ctx);
}
// The defect of the wallet the selected address belongs to, or null. Call
// after selectActiveAddress().
function selectedWalletDefect() {
if (state.selectedWallet === null) return null;
return walletDefect(state.wallets[state.selectedWallet]);
}
function selectActiveAddress() {
for (let wi = 0; wi < state.wallets.length; wi++) {
for (let ai = 0; ai < state.wallets[wi].addresses.length; ai++) {
if (
state.wallets[wi].addresses[ai].address === state.activeAddress
) {
state.selectedWallet = wi;
state.selectedAddress = ai;
return true;
}
}
}
return false;
}
function init(ctx) {
$("btn-add-wallet-bottom").addEventListener("click", ctx.showAddWalletView);
$("btn-main-send").addEventListener("click", () => {
if (!selectActiveAddress()) {
showFlash("No active address selected.");
return;
}
// Before the balance check and before any password is asked for: this
// wallet cannot sign at all, so the send screen is a dead end.
const defect = selectedWalletDefect();
if (defect) {
showFlash(defect.shortMessage);
return;
}
const addr = currentAddress();
if (!addr.balance || parseFloat(addr.balance) === 0) {
showFlash("Cannot send \u2014 zero balance.");
return;
}
$("send-to").value = "";
$("send-amount").value = "";
$("send-token-dropdown").classList.remove("hidden");
$("send-token-static").classList.add("hidden");
renderSendTokenSelect(addr);
updateSendBalance();
resetSendValidation();
pushCurrentView();
showView("send");
});
$("btn-main-receive").addEventListener("click", () => {
if (!selectActiveAddress()) {
showFlash("No active address selected.");
return;
}
ctx.showReceive();
});
}
module.exports = { init, render, walletListHtml };