aboutsummaryrefslogtreecommitdiffstats
path: root/src/format.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-06-14 23:01:03 -0700
committerAdam Soltys <[email protected]>2026-06-14 23:01:03 -0700
commitc5443946031ec32a7b3008fa751eb7916654a9f6 (patch)
tree372e8d0ccaa2dec58a6bcaaa19d3902b80eb4396 /src/format.js
parent683927e4132651b9198e5421fae16e9c60978fbc (diff)
downloadcoinosv3-c5443946031ec32a7b3008fa751eb7916654a9f6.tar.gz
coinosv3-c5443946031ec32a7b3008fa751eb7916654a9f6.zip
Rebuild as single-file offline BIP84 wallet
Replace the SvelteKit/daisyUI prototype with a vanilla JS + hand-rolled CSS app that Bun bundles (code + scure/noble crypto + QR + CSS) into one self-contained dist/index.html runnable offline from file://. Wallet core (src/wallet.js): - BIP84 native-segwit HD wallet from BIP39 seed (+optional passphrase) - gap-limit scan of receive/change chains, aggregate balance, tx history - fresh receive/change addresses; signs with witnessUtxo only (enables offline signing with no prev-tx fetch) - coin selection + fee estimation via btc-signer selectUTXO; send-max; manual coin control - offline JSON snapshot export/import (keyless) + signed-tx hex/QR export - realtime mempool.space WebSocket: track-addresses -> debounced auto refresh of balances/history (no polling); live status badge UI (src/app.js): clean light paper-wallet; create (with optional, skippable backup verification) / import flow; receive (QR) / send / history / coins / tools tabs; clickable logo returns home. API (src/api.js): mempool.space wrapper with retry/backoff for scan-burst rate limiting; single network choke-point for offline enforcement. Verified: BIP84 spec vectors, build/sign/parse for auto/max/manual flows, snapshot roundtrip, live scan + WebSocket, headless-Chrome UI smoke tests. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Diffstat (limited to 'src/format.js')
-rw-r--r--src/format.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/format.js b/src/format.js
new file mode 100644
index 0000000..6191033
--- /dev/null
+++ b/src/format.js
@@ -0,0 +1,56 @@
+// Small formatting helpers. Internally amounts are integer satoshis (Number;
+// safe because max BTC supply ~2.1e15 sats < Number.MAX_SAFE_INTEGER).
+
+export const SATS = 100_000_000;
+
+export function btc(sats) {
+ return (Number(sats) / SATS).toFixed(8);
+}
+
+// "0.01234500" with the trailing-zero portion dimmed is done in CSS; here we
+// just produce a clean fixed-8 string.
+export function fmtBtc(sats) {
+ return btc(sats);
+}
+
+export function fmtSats(sats) {
+ return Number(sats).toLocaleString('en-US');
+}
+
+export function fmtUsd(sats, price) {
+ if (!price) return '';
+ const usd = (Number(sats) / SATS) * price;
+ return usd.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
+}
+
+// Parse a user-entered amount. Accepts BTC (default) or sats.
+export function parseAmount(value, unit) {
+ const n = Number(String(value).trim().replace(/,/g, ''));
+ if (!isFinite(n) || n < 0) return null;
+ return unit === 'sats' ? Math.round(n) : Math.round(n * SATS);
+}
+
+export function shortAddr(a, head = 10, tail = 8) {
+ if (!a || a.length <= head + tail + 1) return a;
+ return `${a.slice(0, head)}…${a.slice(-tail)}`;
+}
+
+export function shortTxid(t) {
+ return shortAddr(t, 8, 8);
+}
+
+export function timeAgo(unixSeconds) {
+ if (!unixSeconds) return 'pending';
+ const s = Math.max(0, Math.floor(Date.now() / 1000 - unixSeconds));
+ const units = [
+ [31536000, 'y'],
+ [2592000, 'mo'],
+ [86400, 'd'],
+ [3600, 'h'],
+ [60, 'm'],
+ ];
+ for (const [secs, label] of units) {
+ if (s >= secs) return `${Math.floor(s / secs)}${label} ago`;
+ }
+ return `${s}s ago`;
+}