Skip to content

Commit 6b614ac

Browse files
feat: auto-detect deposits on balance check (Phase 3 of JavaScriptSolidServer#267)
On GET /pay/.balance, scans the user's per-user tweaked address on each enabled chain via mempool API. New UTXOs are auto-credited without a claim step. Idempotent — already-seen UTXOs are skipped. Phase 3 of JavaScriptSolidServer#267
1 parent 77d1999 commit 6b614ac

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/handlers/pay.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,41 @@ export function createPayHandler(options = {}) {
311311
return reply.code(401).send({ error: 'NIP-98 authentication required' });
312312
}
313313
const didUri = pubkeyToDidNostr(pubkey);
314+
315+
// Auto-detect deposits to user's tweaked address (Phase 3)
316+
if (payChains) {
317+
try {
318+
const kp = await loadOrCreateKeypair();
319+
const utxos = await loadUtxos();
320+
const ledger = await readLedger();
321+
let credited = 0;
322+
323+
for (const chainId of payChains) {
324+
const chain = CHAIN_REGISTRY[chainId];
325+
const network = chainId === 'btc' ? 'mainnet' : (chainId === 'tbtc3' ? 'testnet' : 'testnet4');
326+
const userAddr = btAddress(kp.pubkey, [didUri], network);
327+
328+
const resp = await fetch(`${chain.explorer}/address/${userAddr}/utxo`);
329+
if (!resp.ok) continue;
330+
const addrUtxos = await resp.json();
331+
332+
for (const u of addrUtxos) {
333+
if (utxos.find(x => x.txid === u.txid && x.vout === u.vout)) continue;
334+
// New UTXO — auto-credit
335+
const currency = chain.unit;
336+
credit(ledger, didUri, u.value, currency);
337+
utxos.push({ txid: u.txid, vout: u.vout, amount: u.value, scriptpubkey: u.scriptpubkey || '', chain: chainId, tweak: didUri, spent: false });
338+
credited += u.value;
339+
}
340+
}
341+
342+
if (credited > 0) {
343+
await writeLedger(ledger);
344+
await saveUtxos(utxos);
345+
}
346+
} catch { /* scan failure is non-fatal */ }
347+
}
348+
314349
const ledger = await readLedger();
315350
const response = {
316351
did: didUri,

0 commit comments

Comments
 (0)