v2.1.0: Taproot (BIP86) en «Cargar mi wallet»
Hasta ahora la app analizaba transacciones Taproot pero no sabía calcular las direcciones bc1p… de una wallet: rechazaba tr(…), y la xpub suelta de una wallet Taproot daba direcciones bc1q… que nunca coincidían. - Derivación: clave retocada (BIP341, «TapTweak») y bech32m (BIP350). - Descriptores tr(KEY) de una sola clave, con las mismas comprobaciones que wpkh, sh(wpkh) y pkh. Siguen fuera, con su motivo: árbol de scripts, rawtr y multi_a. - xpub suelta: cuarto tipo, «Taproot (bc1p…)». - QR: la cuenta Taproot de un UR (crypto-account / crypto-output) y el QR de texto [huella/86h/0h/0h]xpub se entregan como descriptor tr(…) con su suma. - Aviso cuando la ruta del origen no casa con el tipo del descriptor. - Simulador de gasto: el cambio de una wallet Taproot es Taproot. Pruebas: tests/test49.js (68 casos: vectores oficiales de BIP350, BIP341 y BIP86) y 17 casos nuevos en tests/test23.js (vector de BCR-2020-015). Las direcciones se contrastaron además con una derivación independiente por el lado privado. 25 cambios hechos a mano para romper cada pieza: 25 cazados. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L5npnUYgSPkAaHkdaeUqBr
This commit is contained in:
+12
-4
@@ -226,7 +226,8 @@
|
||||
const prof = om && typeof om.get(3) === "number" ? om.get(3) : pasos.length;
|
||||
const padre = typeof m.get(8) === "number" ? m.get(8) : null;
|
||||
const hijo = pasos.length ? (pasos[pasos.length - 1].i + (pasos[pasos.length - 1].h ? 0x80000000 : 0)) >>> 0 : 0;
|
||||
const VER = { p2wpkh: [0x04b24746, 0x045f1cf6], "p2sh-p2wpkh": [0x049d7cb2, 0x044a5262], p2pkh: [0x0488b21e, 0x043587cf] };
|
||||
// Taproot no tiene bytes de versión propios: va como xpub/tpub corriente.
|
||||
const VER = { p2wpkh: [0x04b24746, 0x045f1cf6], "p2sh-p2wpkh": [0x049d7cb2, 0x044a5262], p2pkh: [0x0488b21e, 0x043587cf], p2tr: [0x0488b21e, 0x043587cf] };
|
||||
const p = new Uint8Array(78);
|
||||
p.set(u32aBytes(VER[script][red ? 1 : 0]), 0); p[4] = prof & 255;
|
||||
p.set(u32aBytes(padre ?? 0), 5); p.set(u32aBytes(hijo), 9); p.set(cc, 13); p.set(key, 45);
|
||||
@@ -246,11 +247,15 @@
|
||||
// Mira dentro por si hay una clave privada, para rechazarla igual.
|
||||
const buscaPriv = x => !x || typeof x !== "object" ? false : x instanceof Map ? (x.get(2) === true && x.get(3) instanceof Uint8Array) || [...x.values()].some(buscaPriv) : Array.isArray(x) ? x.some(buscaPriv) : "tag" in x ? buscaPriv(x.valor) : false;
|
||||
if (buscaPriv(o)) return { secreto: true };
|
||||
if (esTag(o, 409)) return { noAdmitido: "Taproot (bc1p…)" };
|
||||
// v2.1.0: tr(clave), Taproot de una sola clave (etiqueta 409; en el
|
||||
// vector de BCR-2020-015 es la salida de la ruta m/86'/0'/0'). Si dentro
|
||||
// no hay una clave HD sola (un árbol de scripts, una clave suelta), no.
|
||||
if (esTag(o, 409) && esTag(o.valor, 303)) return { script: "p2tr", ...hdkeyAXpub(o.valor.valor, "p2tr") };
|
||||
if (esTag(o, 409)) return { noAdmitido: "Taproot con scripts" };
|
||||
if (esTag(o, 401) || (esTag(o, 400) && esTag(o.valor, 401))) return { noAdmitido: "multifirma" };
|
||||
return { noAdmitido: "un tipo de script que Txoko no reconoce" };
|
||||
}
|
||||
const UR_PREFERENCIA = ["p2wpkh", "p2sh-p2wpkh", "p2pkh"];
|
||||
const UR_PREFERENCIA = ["p2wpkh", "p2tr", "p2sh-p2wpkh", "p2pkh"];
|
||||
function cuentaAXpub(c, esSalidaSola) {
|
||||
const salidas = esSalidaSola ? [c] : (c instanceof Map && Array.isArray(c.get(2)) ? c.get(2) : null);
|
||||
if (!salidas) throw new Error("UR: la cuenta no tiene el formato del estándar.");
|
||||
@@ -259,7 +264,10 @@
|
||||
const buena = UR_PREFERENCIA.map(s => leidas.find(l => l.script === s)).find(Boolean);
|
||||
if (!buena) return { tipo: "noAdmitido", mensaje: `El QR trae una clave de cuenta de ${[...new Set(leidas.map(l => l.noAdmitido))].join(" y ")}. La auditoría de wallet de Txoko todavía no deriva ese tipo de direcciones.` };
|
||||
const huella = !esSalidaSola && typeof c.get(1) === "number" ? hexDe(u32aBytes(c.get(1))) : buena.huella;
|
||||
return { tipo: "xpub", valor: buena.valor, huella, ruta: buena.ruta, script: buena.script, sinPadre: buena.sinPadre, otras: leidas.filter(l => l.valor && l !== buena).length };
|
||||
const otras = leidas.filter(l => l.valor && l !== buena).length;
|
||||
// Taproot: como descriptor (ver descriptorTaprootQR), no como xpub suelta.
|
||||
if (buena.script === "p2tr") return { tipo: "xpub", valor: descriptorTaprootQR(buena.valor, huella, buena.ruta), xpub: buena.valor, descriptor: true, huella, ruta: buena.ruta, script: "p2tr", sinPadre: buena.sinPadre, otras };
|
||||
return { tipo: "xpub", valor: buena.valor, huella, ruta: buena.ruta, script: buena.script, sinPadre: buena.sinPadre, otras };
|
||||
}
|
||||
|
||||
// UR completo (tipo + bytes CBOR) → resultado clasificado.
|
||||
|
||||
Reference in New Issue
Block a user