// v1.26 — ronda 2 de la revisión externa: casos propios que completan test13. // // Cubre lo que test13 no prueba directamente: que la corroboración no rebaja // una CERTEZA (H-04), que «sin gastar» sin comprobar no es quieta (H-05), que // la huella genérica no resta ni se compara entre saltos (H-14), la frase del // titular con un fallo POSIBLE (D-05) y la reutilización de direcciones con un // backend Electrum (W-02). // // Uso: node tests/test14.js const fs = require("fs"); const path = require("path"); const lines = require("./fuente.js")().split("\n"); const cut = (desde, hasta) => { const s = lines.findIndex(l => l.includes(desde)); const e = lines.findIndex((l, i) => i > s && l.includes(hasta)); if (s === -1 || e === -1) { console.error(`No se localiza ${desde}`); process.exit(1); } return lines.slice(s, e).join("\n"); }; const E = new Function(`const C = { green:"g", amber:"a", red:"r", t2:"t", blue:"b", purple:"p" }; const ENTITY_INDEX = new Map(); const OFAC_SET = new Set(); ${cut("// ── FORMATO: lógica pura", "// ── fin formato")} ${cut("function detectWallets", "function analyzeAddress")} ${cut("function analyzeAddress", "// ── fetch con timeout")} ${cut("function behavioralChangeGuess", "// Busca la tx que gasta un output concreto")} return { analyzeTx, analyzeAddress, guessChangeOutput, detectWallets, fraseBanda, behavioralChangeGuess, combineChangeSignals, compareFingerprints };`)(); let pass = 0, fail = 0; const ok = (n, c, x) => { console.log(` ${c ? "✓" : "✗"} ${n}`); if (!c && x) console.log(" " + x); c ? pass++ : fail++; }; const IN = (v,t,a,x={}) => ({ prevout:{ value:v, scriptpubkey_type:t, scriptpubkey_address:a }, txid:"aa", vout:0, sequence:0xffffffff, ...x }); const OUT = (v,t,a,spk) => ({ value:v, scriptpubkey_type:t, scriptpubkey_address:a, scriptpubkey: spk || "0014"+"11".repeat(20) }); const TX = (vin, vout, x={}) => ({ txid:"ff".repeat(32), vin, vout, weight:800, fee:2000, locktime:0, status:{ confirmed:true, block_height:955000 }, ...x }); const WP = "v0_p2wpkh"; console.log("=== Cruce conductual ===\n"); { const c = E.combineChangeSignals({ identifiable:true, index:0, certainty:"CERTEZA" }, { quiet:[0], fast:[1], gastables:[0,1] }); ok("[H-04] CERTEZA que coincide con el comportamiento sigue siendo CERTEZA", c.certainty === "CERTEZA" && c.agreement === "coincide", JSON.stringify(c)); const p = E.combineChangeSignals({ identifiable:true, index:0, certainty:"POSIBLE" }, { quiet:[0], fast:[1], gastables:[0,1] }); ok("POSIBLE que coincide sube a PROBABLE", p.certainty === "PROBABLE"); } { const tx = TX([IN(1000000,WP,"A")],[OUT(400000,WP,"B"),OUT(599000,WP,"C")]); const b = E.behavioralChangeGuess(tx, new Map([[0,{ spent:false, blocksLater:null, comprobado:false }],[1,{ spent:true, blocksLater:2 }]])); ok("[H-05] «sin gastar» con historial incompleto: desconocida, no quieta", b.quiet.length === 0 && b.outputs[0].estado === "desconocida", JSON.stringify(b.outputs)); const b2 = E.behavioralChangeGuess(tx, new Map([[0,{ spent:false, blocksLater:null, comprobado:true }],[1,{ spent:true, blocksLater:2 }]])); ok("«sin gastar» comprobado: quieta", b2.quiet.includes(0)); } console.log("\n=== Huella ===\n"); { // La transacción real de BlueWallet (revisión H-14): 1 entrada, 1 salida, RBF opt-in. const t = TX([IN(925000,WP,"A",{ sequence:0xfffffffc })],[OUT(924776,WP,"B")], { weight:440 }); const r = E.analyzeTx(t); const h = r.checks.find(c => c.id === "wallet_fingerprint"); ok("[H-14] transacción de BlueWallet: no nombra ningún programa", !r.wallets.some(w => /Sparrow|Electrum|Core/.test(w.name)), JSON.stringify(r.wallets.map(w=>w.name))); ok("[H-14] y la huella no resta", h.pass === true, JSON.stringify({ pass:h.pass, inf:h.informational })); // Locktime en la altura + RBF: rasgos comunes, sin nombre. const m = TX([IN(1000000,WP,"A",{ sequence:0xfffffffd })],[OUT(100000,WP,"B"),OUT(898765,WP,"C")], { locktime:954999 }); const w = E.detectWallets(m); ok("locktime en la altura + RBF: «rasgos comunes», genérico", w.length === 1 && w[0].generico === true, JSON.stringify(w.map(x=>x.name))); const cmp = E.compareFingerprints(w, w); ok("dos saltos con rasgos genéricos: no se comparan («misma huella» no dice nada)", cmp.changed === null, JSON.stringify(cmp)); } console.log("\n=== Titulares ===\n"); { const a = { band:"ALTA", checks:[{ pass:false, certainty:"POSIBLE" }] }; ok("[D-05] ALTA con un fallo POSIBLE: no dice «ningún fallo propio»", !/ningún fallo propio/.test(E.fraseBanda(a)), E.fraseBanda(a)); ok("ALTA sin fallos: sí lo dice", /ningún fallo propio/.test(E.fraseBanda({ band:"ALTA", checks:[] }))); ok("dirección: la frase habla de la dirección", /esta dirección/.test(E.fraseBanda({ band:"ALTA", checks:[] }, "dirección"))); } console.log("\n=== Reutilización de direcciones (Electrum) ===\n"); { const base = { address:"bc1qX", electrum:true, chain_stats:{ tx_count:2, funded_txo_count:0, spent_txo_count:0, funded_txo_sum:0 }, utxos:[] }; const sinHist = E.analyzeAddress(base); const c1 = sinHist.checks.find(c => c.id === "addr_reuse"); ok("[W-02] 2 tx en modo Electrum, sin historial: «no se sabe», no reutilizada", c1.pass && c1.informational, c1.label); const dos = E.analyzeAddress({ ...base, txs:[ TX([IN(5,WP,"Y")],[OUT(4,WP,"bc1qX")],{txid:"1"}), TX([IN(5,WP,"Z")],[OUT(3,WP,"bc1qX")],{txid:"2"}) ] }); ok("2 tx con el historial completo y dos recepciones: reutilizada", dos.checks.find(c => c.id === "addr_reuse").pass === false); ok("reutilizada con CERTEZA: la banda no es ALTA", dos.band !== "ALTA", `band=${dos.band} score=${dos.score}`); const tres = E.analyzeAddress({ ...base, chain_stats:{ ...base.chain_stats, tx_count:3 } }); ok("3 tx: reutilizada aunque no haya recuentos (una moneda cabe en 2 como mucho)", tres.checks.find(c => c.id === "addr_reuse").pass === false); } console.log(`\n${pass} correctos, ${fail} fallos`); process.exit(fail ? 1 : 0);