XML-DSig
Sign XML with signXml and validate with verifyXml: enveloped reference, X.509 in KeyInfo, verification needs no signer.
What you'll do: sign an XML string with signXml over the Signatures boundary, then validate with verifyXml — which needs no signer and runs anywhere.
Install the package
The XML format lives in
@signature-kit/xmland consumes theSignaturesservice provided by a signer.@signature-kit/xmlSign an XML document
signXmltakes anXmlSigningRequestand returnsEffect<string, XmlError | SignatureKitError, Signatures | XmlRuntime>— the signed XML as a string. Satisfy theSignaturesrequirement withEffect.provide(a1SignaturesLayer(...)): A1 provides the signing power, the format module only mutates the document.title="sign-xml.ts"
import { signXml } from "@signature-kit/xml/sign" import { xmlRuntimeLayer } from "@signature-kit/xml/runtime" import { a1SignaturesLayer } from "@signature-kit/a1/signer" import { Effect, Redacted } from "effect" const layer = a1SignaturesLayer({ pfx, // Uint8Array — bytes of the .pfx/.p12 password: Redacted.make(process.env.A1_PASSWORD ?? ""), }) // signXml -> Effect<string, XmlError | SignatureKitError, Signatures | XmlRuntime> const signed: string = yield* signXml({ xml, // string — input document referenceId: "nfe-1", }).pipe(Effect.provide(layer), Effect.provide(xmlRuntimeLayer)) // 'signed' is the signed XML, with the embedded <Signature>Reference and enveloped
referenceIdsets the target of the enveloped signature: passingreferenceId: "nfe-1"produces aReferencewith URI"#nfe-1", pointing to the element with the sameIdin the document. The remainingXmlSigningRequestfields are optional:title="request-xml.ts"
// XmlSigningRequest — only 'xml' is required const signed = yield* signXml({ xml, algorithm: "rsa-sha256", // default; or "rsa-sha512", or legacy "rsa-sha1" referenceId: "nfe-1", // -> Reference URI "#nfe-1" signatureId: "SignatureKit-NFe", // id of the <Signature> element signingTime: new Date(), }).pipe(Effect.provide(layer), Effect.provide(xmlRuntimeLayer))Without
algorithm, the default"rsa-sha256"applies; the other options are"rsa-sha512"and the legacy, discouraged"rsa-sha1". The signer's X.509 certificate is embedded in the signature'sKeyInfo, but the verifier does not trust it automatically — the last two steps below cover supplying the trusted key or certificate out of band.NF-e framing
The NF-e is a common case: the
IdofinfNFeis the access key, and the signature points to it. Use the access key asreferenceIdso the URI becomes"#NFe35...".title="sign-nfe.ts"
// NF-e: the id of <infNFe> is the target of the enveloped reference const xml = `<NFe xmlns="http://www.portalfiscal.inf.br/nfe"> <infNFe Id="NFe35200114200166000187550010000000071234567890" versao="4.00"> <!-- ide, emit, dest, det, total, transp, ... --> </infNFe> </NFe>` const signed = yield* signXml({ xml, referenceId: "NFe35200114200166000187550010000000071234567890", }).pipe(Effect.provide(layer), Effect.provide(xmlRuntimeLayer)) // the <Signature> is inserted as a sibling of <infNFe>, pointing to "#NFe35..."Verify the signature
verifyXmltakes anXmlVerificationRequestand returnsEffect<XmlVerificationResult, XmlError, XmlRuntime>. There is noSignaturesrequirement, but theXmlRuntimeservice is required. Supply exactly one oftrustedCertificateDer(the X.509 certificate DER you trust) orpublicKeyDer(theSubjectPublicKeyInfoDER) soverifyXmlknows which key to check the signature against.When an application consumes a specific signed element, use
requiredReference. Itspathruns from the document element through the target; each segment must match exactly one namespace-aware direct child. UsenamespaceUri: nullfor elements without a namespace.To bound verification work, documents may contain at most 4
<Signature>elements, 4 directSignedInfo<Reference>elements per signature, 8 of those references in total, 3 direct<Transform>elements per reference, and 8 transforms in total. A reference may contain only one canonicalization transform. Exceeding a limit returnsresult.valid === falsebefore cryptographic verification.Only XML 1.0 is accepted. Parser guardrails reject input above 10 MiB, 16,384 nodes, 2,048 attributes, 64 namespace declarations, or depth 1,024.
InclusiveNamespacesPrefixListvalues are capped at 4,096 characters and 64 tokens.Documents must contain exactly one root element. Other than the optional leading XML declaration and XML whitespace, prolog and epilog content is rejected.
signXmlrevalidates its serialized result with these guards and fails withxml.SIGN_FAILEDrather than returning XML thatverifyXmlwould reject.title="verify-xml.ts"
import { verifyXml } from "@signature-kit/xml/verify" import { xmlRuntimeLayer } from "@signature-kit/xml/runtime" import { Effect } from "effect" // verifyXml -> Effect<XmlVerificationResult, XmlError, XmlRuntime> // NO Signatures requirement, but XmlRuntime IS required const result = yield* verifyXml({ xml: signed, trustedCertificateDer, // Uint8Array — X.509 DER you trust out-of-band requiredReference: { uri: "#nfe-1", path: [ { localName: "NFe", namespaceUri: "http://www.portalfiscal.inf.br/nfe" }, { localName: "infNFe", namespaceUri: "http://www.portalfiscal.inf.br/nfe" }, ], }, }).pipe(Effect.provide(xmlRuntimeLayer)) // XmlVerificationResult result.valid // boolean result.signatureCount // number — how many <Signature> elements were found result.referenceUris // readonly string[] — e.g. ["#nfe-1"]Verification with an explicit key
trustedCertificateDerandpublicKeyDerare the only two ways to supply a verification key — passpublicKeyDerinstead when you already hold the rawSubjectPublicKeyInforather than a full certificate.title="verify-key.ts"
import { verifyXml } from "@signature-kit/xml/verify" import { xmlRuntimeLayer } from "@signature-kit/xml/runtime" import { Effect } from "effect" // Verification with an explicit key (instead of a trusted certificate) const result = yield* verifyXml({ xml: signed, publicKeyDer, // Uint8Array — SubjectPublicKeyInfo DER requiredReference: { uri: "#nfe-1", path: [ { localName: "NFe", namespaceUri: "http://www.portalfiscal.inf.br/nfe" }, { localName: "infNFe", namespaceUri: "http://www.portalfiscal.inf.br/nfe" }, ], }, }).pipe(Effect.provide(xmlRuntimeLayer)) // result.valid: boolean
Errors you might see
Every signing failure is a typed SignatureKitError on the error channel; XML parsing, canonicalization, and verification failures arrive as XmlError. The most common: