envio-comprobantes-sri
Version:
Envia comprobantes electronicos al SRI (Ecuador). Recibe un objeto JSON. Lo convierte a XML, lo firma, lo envía al servicio web del SRI y devuelve la respuesta.
22 lines (21 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeXmlForC14N = void 0;
/**
* Normalizes XML output from x2js to match C14N canonicalization behavior.
*
* x2js escapes `'` → `'` and `"` → `"`. The XAdES-BES signature hashes the raw XML,
* but the SRI verifies using C14N which resolves these character references back to literal chars.
* This mismatch causes FIRMA_INVALIDA. We pre-resolve them so the hash matches what the SRI computes.
*
* Safe because:
* - `'` in attributes is valid since x2js uses double-quote delimiters (`useDoubleQuotes: true`)
* - `"` is only replaced in text content (between > and <), not inside attribute values
* - `&`, `<`, `>` are NOT touched — these require escaping in XML
*/
function normalizeXmlForC14N(xml) {
return xml
.replace(/'/g, "'")
.replace(/>([^<]*?)"([^<]*?)</g, (match) => match.replace(/"/g, '"'));
}
exports.normalizeXmlForC14N = normalizeXmlForC14N;