Five defects, one of which destroyed every wallet, came from src/background reading and writing the module-level state singleton the MV3 worker never populates, which silently served DEFAULT_STATE. Each point fix created the next defect. The background now has its own per-call getState() and a queued read-modify-write updateState(); the singleton is unreachable from it, and an unpopulated read throws instead of serving defaults. The prohibition is enforced by the build, not by review: build.js asserts over esbuild's own metafile that no forbidden module is an input of a background bundle, so every specifier syntax esbuild resolves is covered, and both halves of the table are checked for rot -- a stale key, a stale module, an empty list, or an unlisted entry point under src/background/ all fail the build. The ESLint rule remains as fast local feedback and reads the same shared table. Known bounds are documented where the table lives. Also closes #320: getProvider() now requires a validated network id, so a cold worker no longer prepares a non-mainnet dApp transaction for mainnet and gets refused by the wallet's own verifier. backgroundRefresh() no longer mutates address objects across a network round trip, the broadcast path takes its endpoint and chain id from one snapshot, and eight test storage stubs now structured-clone on get as the real chrome.storage.local does. closes #320
167 lines
5.8 KiB
JavaScript
167 lines
5.8 KiB
JavaScript
const { $, showView, showFlash, escapeHtml, goBack } = require("./helpers");
|
|
const { getTopTokens } = require("../../shared/tokenList");
|
|
const { state, saveState } = require("../../shared/state");
|
|
const { lookupTokenInfo } = require("../../shared/balances");
|
|
const { isScamAddress } = require("../../shared/scamlist");
|
|
const { log } = require("../../shared/log");
|
|
|
|
let ctx;
|
|
|
|
function isTracked(address) {
|
|
const lower = address.toLowerCase();
|
|
return state.trackedTokens.some((t) => t.address.toLowerCase() === lower);
|
|
}
|
|
|
|
function tokenLabel(t) {
|
|
return t.name ? t.name + " (" + t.symbol + ")" : t.symbol;
|
|
}
|
|
|
|
function renderTop10() {
|
|
const el = $("settings-addtoken-top10");
|
|
el.innerHTML = getTopTokens(10)
|
|
.map((t) => {
|
|
const tracked = isTracked(t.address);
|
|
const cls = tracked
|
|
? "border border-border px-1 text-xs opacity-40 cursor-default"
|
|
: "border border-border px-1 hover:bg-fg hover:text-bg cursor-pointer text-xs";
|
|
return (
|
|
`<button class="settings-addtoken-quick ${cls}"` +
|
|
` data-address="${escapeHtml(t.address)}"` +
|
|
` data-symbol="${escapeHtml(t.symbol)}"` +
|
|
` data-decimals="${escapeHtml(t.decimals)}"` +
|
|
` data-name="${escapeHtml(t.name || "")}"` +
|
|
`${tracked ? " disabled" : ""}>${escapeHtml(t.symbol)}</button>`
|
|
);
|
|
})
|
|
.join("");
|
|
el.querySelectorAll(".settings-addtoken-quick:not([disabled])").forEach(
|
|
(btn) => {
|
|
btn.addEventListener("click", async () => {
|
|
const token = {
|
|
address: btn.dataset.address,
|
|
symbol: btn.dataset.symbol,
|
|
decimals: parseInt(btn.dataset.decimals, 10),
|
|
name: btn.dataset.name || btn.dataset.symbol,
|
|
};
|
|
state.trackedTokens.push(token);
|
|
await saveState();
|
|
showFlash("Added " + token.symbol);
|
|
renderTop10();
|
|
renderDropdown();
|
|
ctx.doRefreshAndRender();
|
|
});
|
|
},
|
|
);
|
|
}
|
|
|
|
function renderDropdown() {
|
|
const sel = $("settings-addtoken-select");
|
|
const tokens = getTopTokens(100);
|
|
let html = '<option value="">-- select --</option>';
|
|
for (const t of tokens) {
|
|
const tracked = isTracked(t.address);
|
|
const label = tokenLabel(t) + (tracked ? " (tracked)" : "");
|
|
html +=
|
|
`<option value="${escapeHtml(t.address)}"` +
|
|
` data-symbol="${escapeHtml(t.symbol)}"` +
|
|
` data-decimals="${escapeHtml(t.decimals)}"` +
|
|
` data-name="${escapeHtml(t.name || "")}"` +
|
|
`${tracked ? " disabled" : ""}>${escapeHtml(label)}</option>`;
|
|
}
|
|
sel.innerHTML = html;
|
|
}
|
|
|
|
function show() {
|
|
$("settings-addtoken-address").value = "";
|
|
$("settings-addtoken-info").textContent = "";
|
|
$("settings-addtoken-info").style.visibility = "hidden";
|
|
renderTop10();
|
|
renderDropdown();
|
|
showView("settings-addtoken");
|
|
}
|
|
|
|
function init(_ctx) {
|
|
ctx = _ctx;
|
|
|
|
$("btn-settings-addtoken-back").addEventListener("click", () => {
|
|
goBack();
|
|
});
|
|
|
|
$("btn-settings-addtoken-select").addEventListener("click", async () => {
|
|
const sel = $("settings-addtoken-select");
|
|
const opt = sel.options[sel.selectedIndex];
|
|
if (!opt || !opt.value) {
|
|
showFlash("Please select a token.");
|
|
return;
|
|
}
|
|
if (isTracked(opt.value)) {
|
|
showFlash("Already tracked.");
|
|
return;
|
|
}
|
|
const token = {
|
|
address: opt.value,
|
|
symbol: opt.dataset.symbol,
|
|
decimals: parseInt(opt.dataset.decimals, 10),
|
|
name: opt.dataset.name || opt.dataset.symbol,
|
|
};
|
|
state.trackedTokens.push(token);
|
|
await saveState();
|
|
showFlash("Added " + token.symbol);
|
|
renderTop10();
|
|
renderDropdown();
|
|
ctx.doRefreshAndRender();
|
|
});
|
|
|
|
$("btn-settings-addtoken-manual").addEventListener("click", async () => {
|
|
const addr = $("settings-addtoken-address").value.trim();
|
|
if (!addr || !addr.startsWith("0x")) {
|
|
showFlash(
|
|
"Please enter a valid contract address starting with 0x.",
|
|
);
|
|
return;
|
|
}
|
|
if (isTracked(addr)) {
|
|
showFlash("Already tracked.");
|
|
return;
|
|
}
|
|
if (isScamAddress(addr)) {
|
|
showFlash("This address is on a known scam/fraud list.");
|
|
return;
|
|
}
|
|
const infoEl = $("settings-addtoken-info");
|
|
infoEl.textContent = "Looking up token...";
|
|
infoEl.style.visibility = "visible";
|
|
log.debugf("Looking up token contract", addr);
|
|
try {
|
|
const info = await lookupTokenInfo(
|
|
addr,
|
|
state.rpcUrl,
|
|
state.networkId,
|
|
);
|
|
log.infof("Adding token", info.symbol, addr);
|
|
state.trackedTokens.push({
|
|
address: addr,
|
|
symbol: info.symbol,
|
|
decimals: info.decimals,
|
|
name: info.name,
|
|
});
|
|
await saveState();
|
|
showFlash("Added " + info.symbol);
|
|
$("settings-addtoken-address").value = "";
|
|
infoEl.textContent = "";
|
|
infoEl.style.visibility = "hidden";
|
|
renderTop10();
|
|
renderDropdown();
|
|
ctx.doRefreshAndRender();
|
|
} catch (e) {
|
|
const detail = e.shortMessage || e.message || String(e);
|
|
log.errorf("Token lookup failed for", addr, detail);
|
|
showFlash(detail);
|
|
infoEl.textContent = "";
|
|
infoEl.style.visibility = "hidden";
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = { init, show };
|