Sign for several verifiers

Put several signatures on one payload in a JWS JSON object, then accept the first that verifies or check every one of them.

A compact token carries exactly one signature. The JSON serialization (RFC 7515 §7.2) carries one payload and a list of signatures, each made by a different key with its own header and algorithm: a contract signed by the vendor and countersigned by a notary, a release approved by two maintainers, or one token signed with both the outgoing and the incoming key during a rotation. signMulti builds that object, verifyMulti accepts it when one signature checks out, and verifyMultiAll reports on every signature so you can require all of them, or a quorum.

#Before you start

  • These functions work on objects, not strings: JSON.stringify the result to send it and JSON.parse it before verifying. Compact tokens go through sign and verify.
  • Give every signing key a kid. Verifiers pick keys by it, and a policy such as "the notary must have signed" needs a name to check.
  • A token carries at most 16 signatures by default (limits.signatures), and one call spends at most 16 signature checks across all of them (limits.keyAttempts).

#Sign a document with two keys

signMulti signs the payload once per signer and returns the General JSON form. verifyMultiAll then checks each signature on its own:

multi-signature.ts
import { signMulti, verifyMulti, verifyMultiAll } from "unjwt/jws";
import type { JWSGeneralSerialization } from "unjwt/jws";
import { generateJWK } from "unjwt/jwk";
import type { JWKSet } from "unjwt/jwk";

// In production each party loads its own private key; generated here so the example runs.
const vendor = await generateJWK("ES256", { kid: "vendor-2026-10" });
const notary = await generateJWK("Ed25519", { kid: "notary-2026-10" });

/** The public halves of everyone allowed to sign a contract. */
export const signers: JWKSet = { keys: [vendor.publicKey, notary.publicKey] };

export interface Contract {
  contractId: string;
  customer: string;
  total: number;
}

/** One payload, two signatures: the vendor's and the notary's. */
export function signContract(contract: Contract) {
  return signMulti(contract, [{ key: vendor.privateKey }, { key: notary.privateKey }], {
    expiresIn: "30D",
  });
}

/** `true` only when every required party signed this exact contract. */
export async function isFullySigned(contract: JWSGeneralSerialization) {
  const outcomes = await verifyMultiAll(contract, () => signers);
  const signedBy = new Set(
    outcomes.filter((outcome) => outcome.verified).map((outcome) => outcome.protectedHeader.kid),
  );
  return ["vendor-2026-10", "notary-2026-10"].every((kid) => signedBy.has(kid));
}
  • One payload, two headers: each signature has its own protected header, built as sign builds one: the key's kid, your members, then alg, and typ: "JWT" for an object payload. Here that's { kid: "vendor-2026-10", alg: "ES256", typ: "JWT" } and { kid: "notary-2026-10", alg: "Ed25519", typ: "JWT" }.
  • Time options apply once: expiresIn: "30D" writes one exp into the shared payload.
  • Counting by kid: the signatures array isn't protected, so anyone can append or duplicate an entry. Count the verified kids you require, never the array length.

The output looks like this:

// {
//   payload: "eyJjb250cmFjdElkIjoiY3RyXzIwNDEi…",
//   signatures: [
//     { protected: "eyJraWQiOiJ2ZW5kb3ItMjAy…", signature: "Tl6xKfXqMSVX…" },
//     { protected: "eyJraWQiOiJub3RhcnktMjAy…", signature: "iF_6dSfIUjMX…" },
//   ],
// }

#Accept any one signature

verifyMulti walks the signatures in order and returns the first one your key verifies, with every verify check applied to it:

multi-signature.ts
/** Accept the contract when the notary's signature alone verifies. */
export async function notarized(contract: JWSGeneralSerialization) {
  const { payload } = await verifyMulti<Contract>(contract, notary.publicKey);
  return payload;
}
await verifyMulti(contract, notaryPublicKey);
// {
//   payload: { contractId: "ctr_2041", customer: "cus_88f1", total: 1200, iat: 1792982312, exp: 1795574312 },
//   protectedHeader: { kid: "notary-2026-10", alg: "Ed25519", typ: "JWT" },
//   signerIndex: 1,
// }
  • A failing signature moves on: a bad header, a disallowed alg, an unusable key or a wrong signature only means "try the next one", so a junk entry placed first can't hide a valid one.
  • When none verifies: with two or more signatures, verifyMulti throws ERR_JWS_SIGNATURE_INVALID, and its cause holds one JWTError per signature, in order, so you can see why each one failed. A token with a single signature throws that signature's own error, the same code verify gives for the same failure, such as ERR_JWS_ALG_NOT_ALLOWED.
  • The first match settles it: once a signature verifies, a crit, payload or claim failure is thrown at once.
  • strictSignerMatch: true tries only the signatures your key could have made (a fitting key type, curve or size); when none fits, it throws ERR_JWS_NO_MATCHING_SIGNER without trying any. When it tried some and none verified, a signature it skipped shows as ERR_JWS_NO_MATCHING_SIGNER in its slot of cause.
const error = await verifyMulti(contract, outsiderPublicKey).catch((e) => e);
error.code; // "ERR_JWS_SIGNATURE_INVALID"
error.cause.map((entry) => entry.code); // ["ERR_JWS_SIGNATURE_INVALID", "ERR_JWS_ALG_NOT_ALLOWED"]

#Check every signature

verifyMultiAll(jws, lookup, options) returns one outcome per signature, in order, and leaves the policy to you. The key argument is always a lookup function, because each signature usually needs a different key; wrap a fixed JWK Set as () => keys.

const outcomes = await verifyMultiAll(contract, () => ({ keys: [vendorPublicKey] }));
outcomes.map((o) => (o.verified ? `${o.signerIndex} ok` : `${o.signerIndex} ${o.error.code}`));
// ["0 ok", "1 ERR_JWS_ALG_NOT_ALLOWED"]

A verified outcome carries payload, protectedHeader and, when present, signerHeader. A failed one carries error, and the headers when they could be read.

A failed signature, a disallowed algorithm, an expired payload or a key the lookup couldn't find all become verified: false with the error. It throws only for an object it can't read (ERR_JWS_INVALID_SERIALIZATION), for more signatures than limits.signatures, and when the key-attempt budget runs out.

#Add headers per signer

Each signer is { key, alg?, protectedHeader?, unprotectedHeader? }:

  • alg comes from the signer's alg, else the JWK's alg, else the one an EC or Ed25519 JWK's curve names, else the CryptoKey's algorithm. An RSA or oct JWK without alg throws ERR_JWS_SIGNER_ALG_INFERENCE.
  • kid is copied from a JWK only; for a CryptoKey, set it in protectedHeader.
  • unprotectedHeader travels next to the signature without being signed, so anyone can change it. It may not repeat a name from the protected header (ERR_JWS_HEADER_PARAMS_NOT_DISJOINT), and crit and b64 belong in the protected one (ERR_INVALID_OPTION). verifyMulti returns it as signerHeader.
  • b64: false must be the same for every signer, since they share one payload (ERR_JWS_B64_INCONSISTENT). In the JSON form the unencoded payload may contain dots.

The header's typ check (options.typ) reads the protected header only. kid is read from both, so an unprotected kid can steer the key lookup; the signature must still verify under whatever key comes back.

#Send a single signature as Flattened JSON

signMulti always returns the General form, even for one signer, so the shape stays stable when you add signers. Some consumers expect the Flattened form, which lifts the one signature to the top level:

const flat = generalToFlattenedJWS(await signMulti(contract, [{ key: vendorPrivateKey }]));
Object.keys(flat); // ["payload", "signature", "protected"]

generalToFlattenedJWS(twoSignatures);
// JWTError ERR_JWS_INVALID_SERIALIZATION: Flattened JWS Serialization requires exactly one signature, got 2.

verifyMulti and verifyMultiAll accept both forms.

#Limits

One call does bounded work, whatever the token holds:

LimitDefaultOver the limit
limits.signatures16ERR_JWS_INVALID before any signature is read.
limits.keyCandidates8ERR_JWK_KEY_NOT_FOUND for that signature: more JWK Set entries fit it than this.
limits.keyAttempts16ERR_JWK_KEY_NOT_FOUND, and the whole call stops, verifyMultiAll included.

limits.keyAttempts is one budget for the whole call: every signature check against a candidate key spends one, summed across all signatures. A single key spends one per signature it's tried on, and a JWK Set whose kid matches spends one per signature too. A token of ten signatures without kid, checked against a set of two keys, needs twenty checks and runs out. Give keys a kid, or raise the limit when your own tokens need more.

#When it fails

SituationWhat happens
No signerssignMulti throws ERR_JWS_INVALID.
A signer has no alg, and its key is an RSA or oct JWK without algsignMulti throws ERR_JWS_SIGNER_ALG_INFERENCE.
Signers disagree on b64signMulti throws ERR_JWS_B64_INCONSISTENT.
A name sits in both header tiers of one signerThrows ERR_JWS_HEADER_PARAMS_NOT_DISJOINT.
crit or b64 in an unprotectedHeader, or any sign option rulesignMulti throws ERR_INVALID_OPTION.
A claim option with a non-JSON payload or with validateClaims: falseverifyMulti and verifyMultiAll throw ERR_INVALID_OPTION, before the token is read.
A string instead of an object, or no payload or signaturesThrows ERR_JWS_INVALID_SERIALIZATION.
More signatures than limits.signaturesThrows ERR_JWS_INVALID.
The key-attempt budget ran outThrows ERR_JWK_KEY_NOT_FOUND.
None of two or more signatures verifiesverifyMulti throws ERR_JWS_SIGNATURE_INVALID with one error per signature as cause.
The only signature failsverifyMulti throws that signature's own error, as verify does.
strictSignerMatch found no signature the key could have madeverifyMulti throws ERR_JWS_NO_MATCHING_SIGNER.
The matching signature's payload or claims failverifyMulti throws the verify code, such as ERR_JWT_EXPIRED.

Handle errors lists every code and what to do about it.

#Options

signMulti(payload, signers, options) takes the time options of sign: expiresIn, expiresAt, notBeforeIn, notBeforeAt, issuedAt, currentDate. They need an object payload; with a string or bytes payload they throw ERR_INVALID_OPTION. Per-signer settings live on each signer.

verifyMulti(jws, key, options) and verifyMultiAll(jws, lookup, options) take every verify option with the same defaults, plus:

OptionDefaultNotes
limits.signatures16Most signatures one object may carry.
limits.keyAttempts16Signature checks one call may run, summed across signatures.
strictSignerMatch (verifyMulti)falseTry only the signatures the key could have made.

#Going further