v1.43.0: salidas, entradas y direcciones se numeran desde 0, como Bitcoin Core, Sparrow y Mempool
«salida i» y «entrada i» con el índice real en pantalla, informes, PSBT y ayuda (sin «2.ª salida (vout 1)»); nota «se cuentan desde 0» en los encabezados; «recepción 0» en la wallet; «vout» en el glosario. tests/test31.js; test8, test11 y test23 ajustadas. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L5npnUYgSPkAaHkdaeUqBr
This commit is contained in:
+14
-14
@@ -85,8 +85,8 @@
|
||||
// sobrantes dentro pasaban sin error (P-02).
|
||||
if (q !== tx.length) throw new Error(`PSBT inválida: la transacción sin firmar tiene ${tx.length - q} byte(s) de más al final.`);
|
||||
|
||||
const inputs = []; for (let i=0;i<nIn;i++) inputs.push(readMap(`la entrada ${i+1}`));
|
||||
const outputs = []; for (let i=0;i<nOut;i++) outputs.push(readMap(`la salida ${i+1}`));
|
||||
const inputs = []; for (let i=0;i<nIn;i++) inputs.push(readMap(`la entrada ${i}`));
|
||||
const outputs = []; for (let i=0;i<nOut;i++) outputs.push(readMap(`la salida ${i}`));
|
||||
if (p !== bytes.length) throw new Error("PSBT inválida: sobran bytes al final del archivo.");
|
||||
|
||||
// Formato de los campos que luego se usan. BIP174 obliga a rechazar la
|
||||
@@ -96,7 +96,7 @@
|
||||
const mal = (donde, que) => { throw new Error(`PSBT inválida: en ${donde}, ${que} no tiene el formato del estándar.`); };
|
||||
const lee8 = v => { let n=0n; for(let i=7;i>=0;i--) n=(n<<8n)|BigInt(v[i]); return Number(n); };
|
||||
inputs.forEach((m, i) => {
|
||||
const d = `la entrada ${i+1}`;
|
||||
const d = `la entrada ${i}`;
|
||||
for (const e of m) {
|
||||
if (e.type === 0x01) { // witness_utxo: importe (8) + longitud + script
|
||||
if (e.key.length || e.value.length < 9 || e.value[8] >= 0xfd || e.value.length !== 9 + e.value[8]) mal(d, "el importe de la moneda (witness_utxo)");
|
||||
@@ -109,7 +109,7 @@
|
||||
}
|
||||
});
|
||||
outputs.forEach((m, i) => {
|
||||
const d = `la salida ${i+1}`;
|
||||
const d = `la salida ${i}`;
|
||||
for (const e of m) {
|
||||
if (e.type === 0x02 && (!esPub(e.key) || e.value.length < 4 || e.value.length % 4)) mal(d, "la ruta de derivación");
|
||||
if (e.type === 0x07) { // tap_bip32_derivation: clave x-only (32) + nº hashes + hashes + huella + ruta
|
||||
@@ -197,17 +197,17 @@
|
||||
if (!r) return null;
|
||||
try {
|
||||
const t = leerTxPrevia(r.value);
|
||||
if (t.txid !== psbt.vin[i].txid) { incoherentes.push(`la entrada ${i+1} trae una transacción previa que no es la que gasta`); return null; }
|
||||
if (t.txid !== psbt.vin[i].txid) { incoherentes.push(`la entrada ${i} trae una transacción previa que no es la que gasta`); return null; }
|
||||
const o = t.outs[psbt.vin[i].vout];
|
||||
if (!o) { incoherentes.push(`la transacción previa de la entrada ${i+1} no tiene la salida que se gasta`); return null; }
|
||||
if (!o) { incoherentes.push(`la transacción previa de la entrada ${i} no tiene la salida que se gasta`); return null; }
|
||||
return o;
|
||||
} catch { incoherentes.push(`la transacción previa de la entrada ${i+1} no se puede leer`); return null; }
|
||||
} catch { incoherentes.push(`la transacción previa de la entrada ${i} no se puede leer`); return null; }
|
||||
});
|
||||
const inVals = psbt.inputs.map((m, i) => {
|
||||
const w = m.find(e => e.type === 0x01);
|
||||
const wv = w ? (() => { let v=0n; for(let j=7;j>=0;j--) v=(v<<8n)|BigInt(w.value[j]); return Number(v); })() : null;
|
||||
const pv = previas[i] ? previas[i].value : null;
|
||||
if (wv != null && pv != null && wv !== pv) incoherentes.push(`la entrada ${i+1} declara dos importes distintos (${wv} y ${pv} sat)`);
|
||||
if (wv != null && pv != null && wv !== pv) incoherentes.push(`la entrada ${i} declara dos importes distintos (${wv} y ${pv} sat)`);
|
||||
return wv ?? pv;
|
||||
});
|
||||
const conocidos = inVals.every(v => v !== null);
|
||||
@@ -260,7 +260,7 @@
|
||||
avisos.push({
|
||||
nivel: "aviso", id: "valor_redondo",
|
||||
titulo: "Una de las salidas tiene un importe redondo",
|
||||
hecho: `Hecho (certeza): la salida ${redondos[0].i+1} vale exactamente ${fmtBtc(redondos[0].v)}.`,
|
||||
hecho: `Hecho (certeza): la salida ${redondos[0].i} vale exactamente ${fmtBtc(redondos[0].v)}.`,
|
||||
consecuencia: "Las personas pagan cifras redondas y el cambio es lo que sobra, con todos sus decimales. Refuerza la deducción de cuál salida es el pago y cuál el cambio.",
|
||||
quePuedesHacer: "Un importe con satoshis sueltos no da esta señal.",
|
||||
});
|
||||
@@ -378,15 +378,15 @@
|
||||
if (vin.length !== antes.vin.length) dif.push(`El número de entradas cambia (${antes.vin.length} → ${vin.length}).`);
|
||||
antes.vin.forEach((a, i) => {
|
||||
const b = vin[i]; if (!b) return;
|
||||
if (a.txid !== b.txid || a.vout !== b.vout) dif.push(`La entrada ${i+1} gasta otra moneda (${a.txid.slice(0,10)}…:${a.vout} → ${b.txid.slice(0,10)}…:${b.vout}).`);
|
||||
else if (a.sequence !== b.sequence) dif.push(`La entrada ${i+1} cambia su número de secuencia (afecta a RBF o al locktime).`);
|
||||
if (a.txid !== b.txid || a.vout !== b.vout) dif.push(`La entrada ${i} gasta otra moneda (${a.txid.slice(0,10)}…:${a.vout} → ${b.txid.slice(0,10)}…:${b.vout}).`);
|
||||
else if (a.sequence !== b.sequence) dif.push(`La entrada ${i} cambia su número de secuencia (afecta a RBF o al locktime).`);
|
||||
});
|
||||
if (vout.length !== antes.vout.length) dif.push(`El número de salidas cambia (${antes.vout.length} → ${vout.length}).`);
|
||||
antes.vout.forEach((a, i) => {
|
||||
const b = vout[i]; if (!b) return;
|
||||
const mismoScript = hx(a.script) === hx(b.script);
|
||||
if (!mismoScript) dif.push(`La salida ${i+1} va a OTRO destino.`);
|
||||
if (a.value !== b.value) dif.push(`La salida ${i+1} cambia de importe (${fmtBtc(a.value)} → ${fmtBtc(b.value)}).`);
|
||||
if (!mismoScript) dif.push(`La salida ${i} va a OTRO destino.`);
|
||||
if (a.value !== b.value) dif.push(`La salida ${i} cambia de importe (${fmtBtc(a.value)} → ${fmtBtc(b.value)}).`);
|
||||
});
|
||||
// Mismo contenido en otro orden: no es la misma transacción (otro txid),
|
||||
// aunque el dinero vaya a los mismos sitios.
|
||||
@@ -401,7 +401,7 @@
|
||||
return {
|
||||
igual: dif.length === 0, soloOrden, diferencias: dif, forma, firmas,
|
||||
todasFirmadas: sin === 0, completa: finales === firmas.length,
|
||||
sinFirmar: firmas.map((f, i) => f.estado === "sin" ? i + 1 : null).filter(Boolean),
|
||||
sinFirmar: firmas.map((f, i) => f.estado === "sin" ? i : null).filter(i => i !== null),
|
||||
txid, segwit,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user