aboutsummaryrefslogtreecommitdiffstats
path: root/src/bip353.js
blob: efa172110894e29e34a9da464b38e562d3a52a51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// BIP-353 DNS payment instructions — resolving name@domain to a BIP-21 URI.
//
// The record lives at <name>.user._bitcoin-payment.<domain> as a DNSSEC-signed
// TXT containing one `bitcoin:` URI. Resolution runs full client-side DNSSEC
// chain validation (src/dnssec.js): the DoH resolver is untrusted transport,
// and every answer we act on is provably signed from the ICANN root down.

import { resolveTxtValidated } from './dnssec.js';

// name like "adam", domain like "halwallet.app" → the bitcoin: URI, or null
// when no record exists. Throws when the answer can't be proven authentic.
export async function resolveBip353(name, domain) {
  const label = `${name}.user._bitcoin-payment.${domain}`;
  let res;
  try {
    res = await resolveTxtValidated(label);
  } catch (e) {
    if (/^no TXT record/.test(e.message)) return null;
    throw e;
  }
  const uris = res.txts.filter((t) => /^bitcoin:/i.test(t));
  if (!uris.length) return null;
  // BIP-353: multiple bitcoin: TXT records at one label are invalid
  if (uris.length > 1) throw new Error('multiple payment records — invalid');
  return uris[0];
}

// A pasted payment name: optional ₿, user@domain with a real-looking TLD.
export function parsePaymentName(text) {
  const m = String(text || '').trim().match(/^₿?\s*([a-z0-9._-]{1,64})@([a-z0-9-]+(?:\.[a-z0-9-]+)+)$/i);
  return m ? { name: m[1].toLowerCase(), domain: m[2].toLowerCase() } : null;
}

// bitcoin:<onchain?>?ark=...&lno=...&sp=... → { onchain, params }
export function parseBip21(uri) {
  const m = String(uri || '').match(/^bitcoin:([^?]*)(?:\?(.*))?$/i);
  if (!m) return null;
  const params = {};
  for (const kv of (m[2] || '').split('&')) {
    if (!kv) continue;
    const eq = kv.indexOf('=');
    if (eq < 0) continue;
    params[decodeURIComponent(kv.slice(0, eq)).toLowerCase()] = decodeURIComponent(kv.slice(eq + 1));
  }
  return { onchain: m[1] ? decodeURIComponent(m[1]) : '', params };
}
es(a)); let user_id = accounts[a]; if (!emails[seqno] && user_id) { let cad = parseFloat( buffer .replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1") .replace(",", "") ); emails[seqno] = cad; try { await db.transaction(async transaction => { const account = await db.Account.findOne({ where: { user_id, asset: config.liquid.btcasset, pubkey: null }, include: { model: db.User, as: "user" }, lock: transaction.LOCK.UPDATE, transaction }); let { user } = account; let rate = app.get("rates")["CAD"] * 1.02; let amount = Math.round((cad / rate) * 100000000); await account.increment({ balance: amount }, { transaction }); let payment = await db.Payment.create({ account_id: account.id, user_id: account.user_id, hash: `Interac deposit: ${cad} CAD`, amount, currency: "CAD", rate, received: true, tip: 0, confirmed: true, address: "", network: "COINOS" }); payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); emit(user.username, "account", account); emit(user.username, "payment", payment); l.info( "interac deposit detected for", user.username, cad, amount ); }); } catch (e) { console.log(e); } } }); }); msg.once("end", function() { // console.log(prefix + 'Finished'); }); imap.seq.addFlags(seqno, ["Seen"]); }); }, 10000); }); }); imap.connect();