diff options
| author | Adam Soltys <[email protected]> | 2026-11-22 14:03:26 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-11-22 14:03:26 -0700 |
| commit | e6fac3626b0bc5d7b3d753ed69d0d621928478fc (patch) | |
| tree | 982cc9266b7fbe5806b332db449a6cf3f53fca6c | |
| parent | a1040ecb9908a8290ed9809795b1dc8bdea9ed38 (diff) | |
| download | coinosv3-e6fac3626b0bc5d7b3d753ed69d0d621928478fc.tar.gz coinosv3-e6fac3626b0bc5d7b3d753ed69d0d621928478fc.zip | |
A scanned bitcoin: request with an amount switches the form to BTC. "I scanned a BIP21 on-chain Bitcoin payment request with an amount encoded in it, and it filled in the amount. I guess I had CAD, Canadian dollars, selected as my unit, so it filled it in as that. It should always switch over to BTC as the units if I'm scanning a BIP21 URL like that from a QR code" (Adam, 2026-11-22). The request's amount is written in BTC; the scan turned it into sats when the unit was sats and otherwise put the BTC figure in the box as it was, so with dollars selected 0.0005 BTC read as 0.0005 CAD. A bitcoin: request that carries an amount, scanned or handed over by the Android app, now sets the unit to BTC (kept, as a tap on the unit is; anything typed for other recipients is converted as that tap converts it) and fills the amount from the exact sats. A request with no amount leaves the unit alone. amount-field-unit-test scans a 0.0005 BTC request with CAD selected (unit becomes BTC, the box parses to 50,000 sats) and one with no amount (unit unchanged); features-smoke passes.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6Upic5L6MMJzkTn8zg
| -rw-r--r-- | src/app.js | 33 | ||||
| -rw-r--r-- | tools/amount-field-unit-test.js | 15 |
2 files changed, 34 insertions, 14 deletions
@@ -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); } |
