aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/app.js33
-rw-r--r--tools/amount-field-unit-test.js15
2 files changed, 34 insertions, 14 deletions
diff --git a/src/app.js b/src/app.js
index 0337bd7..fae16e0 100644
--- a/src/app.js
+++ b/src/app.js
@@ -1248,8 +1248,13 @@ let unit = (() => {
// been fetched — a third position showing blanks is worse than two that work.
function toggleUnit() {
const ring = haveRate() ? ['sats', 'btc', 'fiat'] : ['sats', 'btc'];
+ setUnit(ring[(ring.indexOf(unit) + 1) % ring.length]);
+ render();
+}
+function setUnit(next) {
+ if (next === unit) return;
const was = unit;
- unit = ring[(ring.indexOf(unit) + 1) % ring.length];
+ unit = next;
try {
localStorage.setItem(UNIT_KEY, unit);
} catch {}
@@ -1261,7 +1266,6 @@ function toggleUnit() {
const sat = parseField(r.amount, was);
if (sat) r.amount = amountField(sat);
}
- render();
}
// A sats figure as the amount field types it in the current unit — what a
@@ -6088,15 +6092,21 @@ function handleScanned(raw) {
if (featureMatchSend(text)) return;
if (/^bitcoin:/i.test(text)) {
- const { address, amount } = parseBip21(text);
+ const { address, sats } = parseBip21(text);
if (!address) {
ui.sendError = t('scanUnrecognized');
render();
return;
}
s.recipients[0].address = address;
- if (amount != null) {
- s.recipients[0].amount = amount;
+ if (sats != null) {
+ // A request's amount is written in BTC, so the form shows it in BTC,
+ // whatever unit was chosen: with dollars selected the BTC figure used
+ // to land in the box as it was and read as that many dollars ("it
+ // should always switch over to BTC as the units if I'm scanning a
+ // BIP21 URL like that", Adam, 2026-11-22).
+ setUnit('btc');
+ s.recipients[0].amount = amountField(sats);
s.max = false;
}
render();
@@ -6121,21 +6131,18 @@ function handleScanned(raw) {
render();
}
-// bitcoin:<address>?amount=<btc>&label=... — amount is in BTC; convert to the
-// current display unit so it lands in the form's amount field correctly.
+// bitcoin:<address>?amount=<btc>&label=... — amount is in BTC; handed back
+// in sats.
function parseBip21(uri) {
const m = /^bitcoin:([^?]*)(?:\?(.*))?$/i.exec(uri.trim());
if (!m) return {};
const address = decodeURIComponent((m[1] || '').trim());
- let amount = null;
+ let sats = null;
if (m[2]) {
const amt = new URLSearchParams(m[2]).get('amount');
- if (amt && isFinite(Number(amt)) && Number(amt) > 0) {
- const sats = Math.round(Number(amt) * SATS);
- amount = unit === 'sats' ? String(sats) : String(Number(amt));
- }
+ if (amt && isFinite(Number(amt)) && Number(amt) > 0) sats = Math.round(Number(amt) * SATS);
}
- return { address, amount };
+ return { address, sats };
}
function broadcastConfirmView() {
diff --git a/tools/amount-field-unit-test.js b/tools/amount-field-unit-test.js
index 596969d..a25432e 100644
--- a/tools/amount-field-unit-test.js
+++ b/tools/amount-field-unit-test.js
@@ -11,7 +11,7 @@ import puppeteer from './puppeteer.js';
const bundle = await Bun.build({ entrypoints: ['src/app.js'], target: 'browser', minify: true,
plugins: [{ name: 'amount-field-test', setup(build) {
build.onLoad({ filter: /src\/app\.js$/ }, async ({ path }) => ({ loader: 'js', contents: await Bun.file(path).text() + `
-window.amtApp = { ctx, ui, toggleUnit, get unit() { return unit; } };
+window.amtApp = { ctx, ui, toggleUnit, handleScanned, get unit() { return unit; } };
` }));
build.onLoad({ filter: /src\/video-compress\.js$/ }, () => ({ loader: 'js', contents: 'export async function compressVideo(f) { return f; }' }));
} }] });
@@ -46,6 +46,16 @@ try {
out.ring = [];
for (let i = 0; i < 3; i++) { amtApp.toggleUnit(); out.ring.push([amtApp.unit, ...rs.map((r) => r.amount)]); }
rs.splice(1);
+ // a scanned payment request with an amount, with dollars as the unit
+ while (amtApp.unit !== 'fiat') amtApp.toggleUnit();
+ rs[0].amount = ''; rs[0].address = '';
+ amtApp.handleScanned('bitcoin:bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kygt080?amount=0.0005&label=x');
+ out.scan = [amtApp.unit, localStorage.getItem('btc-wallet-unit'), rs[0].address, rs[0].amount, ctx.parseAmount(rs[0].amount)];
+ // without an amount the unit is left alone
+ amtApp.toggleUnit();
+ const before = amtApp.unit;
+ amtApp.handleScanned('bitcoin:bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kygt080');
+ out.plain = [before, amtApp.unit];
return out;
});
console.log(r);
@@ -60,6 +70,9 @@ try {
['fiat', '93.31', '0.13', '', 'abc'],
['sats', '62212', '93', '', 'abc'],
], 'a typed amount follows the unit around the ring and comes back exact; blanks and junk are left alone');
+ assert.deepEqual(r.scan, ['btc', 'btc', 'bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kygt080', '0.00050000', 50000],
+ 'a scanned bitcoin: request with an amount switches the unit to BTC and fills 50,000 sats, not 0.0005 dollars');
+ assert.deepEqual(r.plain, ['fiat', 'fiat'], 'a request with no amount leaves the unit as it was');
assert.deepEqual(errors, []);
console.log('✅ amount fills follow the display unit');
} finally { await browser.close(); server.stop(true); }