SignatureKit

First Use

Load an A1 certificate, inspect the identity, and sign your first bytes — then an XML and a PDF — in a single Effect program.

What you'll do: assemble an Effect program that loads the A1 certificate, reads the identity, signs and verifies bytes — then signs an XML and a PDF reusing the same boundary.

  1. Install

    Add the core boundary and the A1 signer. Pull in effect for the Effect runtime and Redacted.

    bun add @signature-kit/signatures @signature-kit/a1 effect
  2. Load the A1, inspect, sign and verify

    a1SignaturesLayer takes the .pfx bytes and the Redacted password and provides the Signatures service. Inside one Effect.gen, signatures.inspect() returns a normalized SignerIdentity — document carries the e-CPF or e-CNPJ when present — then signatures.sign and signatures.verify run over the same boundary. verify returns a value: result.valid is a boolean, and a real failure surfaces as a SignatureKitError in the error channel, never a thrown exception.

    title="quickstart.ts"

    import { a1SignaturesLayer } from "@signature-kit/a1/signer"
    import { signatures } from "@signature-kit/signatures"
    import { Effect, Layer, Redacted } from "effect"
    
    const pdfCoreLayer = pdfCoreFileLayer.pipe(Layer.provide(NodeServices.layer))
    const layer = a1SignaturesLayer({
      pfx,                                          // Uint8Array — bytes of the .pfx/.p12
      password: Redacted.make(process.env.A1_PASSWORD ?? ""),
    })
    
    export const program = Effect.gen(function* () {
      // normalized identity — identity.document carries e-CPF / e-CNPJ
      const identity = yield* signatures.inspect()
    
      const content = new TextEncoder().encode("content to sign")
      const artifact = yield* signatures.sign({
        content,
        algorithm: "rsa-sha256",
      })
    
      const result = yield* signatures.verify({
        content,
        signature: artifact.signature,
        algorithm: artifact.algorithm,
      })
    
      return { identity, artifact, result }
    }).pipe(Effect.provide(layer))
  3. Run the program

    Run the Effect with Effect.runPromise, or call it from another Effect program.

    title="run.ts"

    import { Effect } from "effect"
    
    const { result } = await Effect.runPromise(program)
    console.log(result.valid) // boolean — true if the signature checks out
  4. Sign XML and PDF

    Both formats consume the same boundary via Effect.provide; the layer line is identical to the previous step. XML also needs xmlRuntimeLayer and PDF needs the PdfCore engine layer (pdfCoreFileLayer on Node/Bun, pdfCoreBrowserLayer in browsers); verifyXml / verifyPdf read the public key from the document and do not require the boundary.

    title="documents.ts"

    import { signXml } from "@signature-kit/xml/sign"
    import { xmlRuntimeLayer } from "@signature-kit/xml/runtime"
    import { signPdf } from "@signature-kit/pdf/sign"
    import { pdfCoreFileLayer } from "@signature-kit/pdf-core/file"
    import * as NodeServices from "@effect/platform-node/NodeServices"
    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 ?? ""),
    })
    
    const signedXml = signXml({ xml, referenceId: "nfe-1" })
      .pipe(Effect.provide(layer), Effect.provide(xmlRuntimeLayer))
    
    const signedPdf = signPdf({ pdf, policy: "pades-icp-brasil" })
      .pipe(Effect.provide(layer), Effect.provide(pdfCoreLayer))

Errors you might see

Further reading

How is this guide?