UNPKG

ec-sri-invoice-signer

Version:
43 lines (42 loc) 2.07 kB
"use strict"; /** * Inclusive XML canonicalization (http://www.w3.org/TR/2001/REC-xml-c14n-20010315). * * This delegates to the spec-compliant implementation shipped by `xml-crypto` * (parsing via `@xmldom/xmldom`) instead of a hand-rolled canonicalizer. * * A canonicalization target that is only a fragment of a larger document (e.g. * the SignedInfo/KeyInfo/SignedProperties sections, which live inside the * ds:Signature element and inherit its namespace declarations) references * prefixes that are declared on an ancestor rather than in the fragment itself. * A namespace-aware parser rejects such undeclared prefixes, and inclusive c14n * must in any case render those inherited namespaces on the fragment's apex * element. Both concerns are solved by declaring them on the apex before * parsing; pass them via `options.inheritedNamespaces`. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.c14nCanonicalize = void 0; const xmldom_1 = require("@xmldom/xmldom"); const xml_crypto_1 = require("xml-crypto"); /** * Declare the given namespaces on the root element's start tag so a * namespace-aware parser accepts inherited prefixes and c14n renders them on * the apex element. */ const declareNamespacesOnRoot = (xml, namespaces) => { if (namespaces.length === 0) { return xml; } const declarations = namespaces .map((ns) => ` xmlns${ns.prefix ? `:${ns.prefix}` : ''}="${ns.uri}"`) .join(''); // Insert right after the root element name in its opening tag. return xml.replace(/^(\s*<[^?!\s/>]+)/, `$1${declarations}`); }; const c14nCanonicalize = (xml, options) => { var _a; const preparedXml = declareNamespacesOnRoot(xml, (_a = options === null || options === void 0 ? void 0 : options.inheritedNamespaces) !== null && _a !== void 0 ? _a : []); const document = new xmldom_1.DOMParser().parseFromString(preparedXml, 'text/xml'); return new xml_crypto_1.C14nCanonicalization().process(document.documentElement, {}); }; exports.c14nCanonicalize = c14nCanonicalize;