v1.35.0: QR animados, xpub de SeedSigner y lo firmado frente a lo auditado
El lector junta los trozos de los QR animados (UR, BBQr y Specter) con la cámara abierta y barra de progreso. Lee la xpub animada de SeedSigner (crypto-account) y la convierte en zpub con huella y ruta. En la auditoría de PSBT, después del análisis, compara la PSBT firmada (o la transacción final) con la auditada y dice qué cambia o qué entradas llevan firma. Pieza nueva src/06-qr-animado.js, sin librerías nuevas. Pruebas en tests/test23.js con los vectores oficiales de Blockchain Commons y BBQr de la implementación de referencia de Coinkite. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L5npnUYgSPkAaHkdaeUqBr
This commit is contained in:
+47
-8
@@ -1,4 +1,4 @@
|
||||
// ── Leer QR con la cámara (v1.34) ─────────────────────────────────────
|
||||
// ── Leer QR con la cámara (v1.34; animados desde la v1.35) ─────────────────────────────────────
|
||||
// La librería (qr de Paul Miller) vive en vendor/qr/ y solo se carga al
|
||||
// abrir el lector. Todo ocurre en el navegador: la imagen de la cámara no
|
||||
// sale del aparato y leer un QR no hace ninguna consulta de red.
|
||||
@@ -40,15 +40,19 @@
|
||||
const [fase, setFase] = useState("cargando"); // cargando · camara · sin-camara · leido · otro · secreto · sin-lector
|
||||
const [aviso, setAviso] = useState("");
|
||||
const [leido, setLeido] = useState(null);
|
||||
// QR animados (v1.35): los trozos se van juntando mientras la cámara
|
||||
// sigue abierta. El colector vive fuera del estado de React.
|
||||
const colectorRef = useRef(null);
|
||||
const [progreso, setProgreso] = useState(null); // { formato, hechos, total }
|
||||
|
||||
const pararCamara = () => {
|
||||
if (streamRef.current) { streamRef.current.getTracks().forEach(t => t.stop()); streamRef.current = null; }
|
||||
if (videoRef.current) videoRef.current.srcObject = null;
|
||||
};
|
||||
const procesar = texto => {
|
||||
const procesar = texto => mostrar(clasificarQR(texto));
|
||||
const mostrar = r => {
|
||||
pararCamara();
|
||||
setAviso("");
|
||||
const r = clasificarQR(texto);
|
||||
setAviso(""); setProgreso(null);
|
||||
if (r.tipo === "secreto") { setLeido(null); setFase("secreto"); return; }
|
||||
const e = encajaQR(r, campo);
|
||||
setLeido(r);
|
||||
@@ -65,8 +69,30 @@
|
||||
catch { return null; }
|
||||
};
|
||||
|
||||
// Lo que lee la cámara pasa primero por el colector de QR animados. Si
|
||||
// no es un trozo de uno, se trata como un QR normal.
|
||||
const recibir = (t, enFoto) => {
|
||||
const c = colectorRef.current || (colectorRef.current = crearColectorQR());
|
||||
const e = c.recibir(t);
|
||||
if (e.estado === "nada") { if (c.formato && !enFoto) return false; procesar(t); return true; }
|
||||
if (e.estado === "secreto") { mostrar({ tipo: "secreto" }); return true; }
|
||||
if (e.estado === "error") { setAviso(`Un trozo no se ha podido leer (${e.mensaje}). Sigue apuntando: se vuelve a empezar.`); setProgreso(null); return false; }
|
||||
if (e.estado === "progreso") {
|
||||
if (enFoto) { c.reiniciar(); setAviso(`Es un trozo (${e.hechos} de ${e.total}) de un QR animado: hace falta la cámara en directo, que vaya leyendo los trozos mientras pasan.`); return true; }
|
||||
setProgreso(p => p && p.hechos === e.hechos && p.total === e.total && p.formato === e.formato ? p : { formato: e.formato, hechos: e.hechos, total: e.total });
|
||||
return false;
|
||||
}
|
||||
// listo
|
||||
pararCamara();
|
||||
c.resultado().then(r => { if (vivoRef.current) mostrar(r); })
|
||||
.catch(err => { if (!vivoRef.current) return; setProgreso(null); setLeido(null); setFase("otro"); setAviso(`No se ha podido reconstruir el QR animado: ${err.message}`); })
|
||||
.finally(() => { colectorRef.current = null; });
|
||||
return true;
|
||||
};
|
||||
|
||||
const arrancar = async () => {
|
||||
setAviso(""); setLeido(null); setFase("cargando");
|
||||
setAviso(""); setLeido(null); setProgreso(null); setFase("cargando");
|
||||
colectorRef.current = crearColectorQR();
|
||||
let decode;
|
||||
try { decode = await cargarLectorQR(); }
|
||||
catch { if (vivoRef.current) setFase("sin-lector"); return; }
|
||||
@@ -91,7 +117,7 @@
|
||||
ultimo = ahora;
|
||||
const esc = Math.min(1, 720 / Math.max(v.videoWidth, v.videoHeight));
|
||||
const t = leerImagen(decode, v, Math.round(v.videoWidth * esc), Math.round(v.videoHeight * esc));
|
||||
if (t != null) { procesar(t); return; }
|
||||
if (t != null && recibir(t, false)) return;
|
||||
}
|
||||
requestAnimationFrame(bucle);
|
||||
};
|
||||
@@ -114,7 +140,7 @@
|
||||
const esc = Math.min(1, 1600 / Math.max(bmp.width, bmp.height));
|
||||
const t = leerImagen(decode, bmp, Math.round(bmp.width * esc), Math.round(bmp.height * esc), { effort: Infinity, timeLimit: 3000 });
|
||||
if (bmp.close) bmp.close();
|
||||
if (t != null) procesar(t);
|
||||
if (t != null) { colectorRef.current = crearColectorQR(); recibir(t, true); }
|
||||
else setAviso("No encuentro ningún QR en esa imagen. Prueba con otra más cerca y con luz.");
|
||||
} catch {
|
||||
setAviso("No he podido leer esa imagen.");
|
||||
@@ -149,7 +175,19 @@
|
||||
{fase === "cargando" && <div style={{ position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", color: C.t2, fontFamily: C.mono, fontSize: "0.7rem" }}>abriendo la cámara…</div>}
|
||||
</div>
|
||||
)}
|
||||
{fase === "camara" && <div style={{ fontSize: "0.66rem", color: C.t2, lineHeight: 1.5 }}>Apunta al QR. La imagen no sale de este aparato.</div>}
|
||||
{fase === "camara" && !progreso && <div style={{ fontSize: "0.66rem", color: C.t2, lineHeight: 1.5 }}>Apunta al QR. Si es animado (una PSBT, la xpub de SeedSigner), mantenlo delante hasta que se complete. La imagen no sale de este aparato.</div>}
|
||||
{fase === "camara" && progreso && (
|
||||
<div role="status" aria-live="polite">
|
||||
<div style={{ display: "flex", justifyContent: "space-between", fontFamily: C.mono, fontSize: "0.64rem", color: C.t1, marginBottom: 5 }}>
|
||||
<span>QR animado ({progreso.formato === "ur" ? "UR" : progreso.formato === "bbqr" ? "BBQr" : "Specter"})</span>
|
||||
<span>{progreso.hechos} de {progreso.total} trozos</span>
|
||||
</div>
|
||||
<div style={{ height: 6, background: C.bg, borderRadius: 3, overflow: "hidden", border: `1px solid ${C.border}` }}>
|
||||
<div style={{ width: `${Math.round(100 * progreso.hechos / progreso.total)}%`, height: "100%", background: C.green, transition: "width 0.2s" }} />
|
||||
</div>
|
||||
<div style={{ fontSize: "0.62rem", color: C.t2, marginTop: 5, lineHeight: 1.5 }}>Mantén el QR delante. No hace falta leerlos en orden{progreso.formato === "ur" ? ", y si se escapa alguno, los siguientes lo reponen" : ""}.</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{fase === "sin-lector" && (
|
||||
<div style={{ fontSize: "0.7rem", color: C.amber, lineHeight: 1.6 }}>
|
||||
@@ -167,6 +205,7 @@
|
||||
<div style={{ padding: "10px 12px", background: C.bg, border: `1px solid ${fase === "leido" ? C.green : C.amber}60`, borderRadius: 8 }}>
|
||||
<div style={{ fontSize: "0.6rem", color: C.t2, fontFamily: C.mono, marginBottom: 4 }}>HE LEÍDO {(QR_NOMBRE[leido.tipo] || "").toUpperCase()}</div>
|
||||
{leido.valor && <div style={{ fontFamily: C.mono, fontSize: "0.72rem", color: C.t1, wordBreak: "break-all" }}>{resumenQR(leido)}</div>}
|
||||
{leido.tipo === "xpub" && leido.script && <div style={{ fontSize: "0.64rem", color: C.t2, marginTop: 4, lineHeight: 1.5 }}>Cuenta de direcciones {TIPO_SCRIPT[leido.script]}{leido.otras ? ` (el QR traía ${leido.otras + 1} tipos; se usa este)` : ""}.{leido.sinPadre ? " El QR no traía la huella de la clave madre: la clave puede verse distinta de la de tu monedero, pero da las mismas direcciones." : ""}</div>}
|
||||
{leido.importe && <div style={{ fontSize: "0.64rem", color: C.t2, marginTop: 4 }}>El QR pedía un pago de {leido.importe} BTC{leido.etiqueta ? ` («${leido.etiqueta}»)` : ""}. Txoko solo usa la dirección.</div>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user