SDK
Recovery and helpers
Inspect, recover, and record witness pairs step by step, and handle SDK errors.
resumeWitness() #
attest.resumeWitness(pairId, { deliveryObservation }) inspects a pair after a crash or an AttestWitnessDeliveryError and finishes it only when that is safe. It never calls your operation and never invents a delivery.
| Pair state | What resumeWitness does |
|---|---|
| Already finalized | Returns it unchanged. |
| Delivery recorded, not finalized | Finalizes and returns the result. |
No delivery, no deliveryObservation passed | Returns the pair with recoveryStatus: "delivery_required". The pair stays OPEN. |
No delivery, deliveryObservation passed | Records that delivery, then finalizes. |
To declare a pair whose delivery will never come incomplete, call finalizeWitness() explicitly.
import { Attest } from "@attestinfra/sdk";
const attest = new Attest({ apiKey: process.env.ATTEST_API_KEY });
const pairId = "pr_1b7c3e9d5a2f4c80";
const event = {
pairId,
eventType: "invoice.sent",
action: "send",
evidence: { invoiceId: "inv_5520" },
};
await attest.witnessAssembly(event);
// After a restart: inspect without running anything.
const pending = await attest.resumeWitness(pairId);
console.log(pending.lifecycle, pending.recoveryStatus);
// You confirmed the delivery happened and persisted its observation.
const done = await attest.resumeWitness(pairId, { deliveryObservation: event });
console.log(done.lifecycle, done.reconciliationStatus);OPEN delivery_required
FINALIZED matchedgetWitness() #
attest.getWitness(pairId) calls GET /v1/witness/{pairId} and returns the pair as stored. Reading never changes the pair.
const state = await attest.getWitness("pr_1b7c3e9d5a2f4c80");
console.log(state.lifecycle, state.assemblyObserved, state.deliveryObserved);witnessAssembly() and witnessDelivery() #
These send one observation each to POST /v1/witness, for cases where assembly and delivery happen in different processes. The second argument overrides fields from the first: pairId, eventId, timestamp, source, destination, action, outcome, and evidence.
Each call resolves with the pair state plus accepted and duplicate. Neither call finalizes the pair.
// In the sending service
await attest.witnessAssembly(event, { destination: "payments" });
// In the receiving service
await attest.witnessDelivery(event, { source: "payments", outcome: "ok" });
await attest.finalizeWitness(event.pairId);witness() #
attest.witness(event, operation) is shorthand for attest.prepareWitness(event).execute(operation).
SDK errors #
Every error extends AttestError and has a code. HTTP errors also have a status.
| Class | code | When |
|---|---|---|
AttestValidationError | VALIDATION_ERROR, WITNESS_INVALID_EVIDENCE | Input failed a local check. Nothing was sent. |
AttestAuthenticationError | AUTHENTICATION_ERROR | HTTP 401 or 403. |
AttestServerError | SERVER_ERROR, RATE_LIMIT_ERROR, or the server's witness code | Any other non-2xx response. For witness calls, code is the server's code when one is returned, for example WITNESS_CONFLICT. |
AttestNetworkError | NETWORK_ERROR, TIMEOUT_ERROR | The request failed or exceeded timeout. |
AttestResponseError | MALFORMED_RESPONSE | The response body was not the expected JSON. |
AttestWitnessAssemblyExistsError | WITNESS_ASSEMBLY_EXISTS | execute found the assembly already recorded and did not run your operation. Has pairId. |
AttestWitnessDeliveryError | WITNESS_DELIVERY_ERROR | Your operation ran, then delivery or finalize failed. Has pairId and deliveryObservation. |
AttestError | WITNESS_ASSEMBLY_REJECTED | The assembly response did not report accepted: true. |