SDK
prepareWitness()
Wrap an operation so attest observes it before and after it runs.
Usage #
attest.prepareWitness(event) builds the assembly observation synchronously, with no network call, and returns a prepared pair. Calling prepared.execute(operation) records the assembly, runs your operation once, records the delivery, and finalizes the pair.
Because the identity exists before anything runs, you can persist it first and recover after a crash.
import { Attest } from "@attestinfra/sdk";
const attest = new Attest({ apiKey: process.env.ATTEST_API_KEY });
// Your operation. Replace with the real call.
async function transferFunds() {
return { transferId: "tr_82" };
}
const prepared = attest.prepareWitness({
eventType: "transfer.requested",
source: "checkout",
destination: "payments",
action: "transfer",
evidence: { actionId: "transfer_82", amount: 500, currency: "USD" },
});
// Persist before executing.
console.log(prepared.pairId, prepared.assemblyIdentity.timestamp);
const { result, witness, currentInvocationOperationExecuted } =
await prepared.execute(() => transferFunds());
console.log(result, witness.reconciliationStatus, currentInvocationOperationExecuted);oN4T9mJ0Xq2Lr8vYb3KcWd7Pz1Ae6HsF 2026-10-19T14:03:07.120Z
{ transferId: 'tr_82' } matched trueParameters #
| Name | Type | Required | Description |
|---|---|---|---|
eventType | string | Required | Classification of the operation. Compared across stages. |
action | string | Required | The operation. Compared across stages. |
evidence | object | Required | Plain JSON object of boundary values. Compared across stages. Validated locally against the evidence limits and frozen as a snapshot. |
pairId | string | Optional | Defaults to 24 random bytes, base64url-encoded (32 characters). |
eventId | string | Optional | Defaults to a random UUID. |
timestamp | Date | string | number | Optional | Defaults to the time prepareWitness is called. |
source | string | Optional | Where the operation starts. |
destination | string | Optional | Where the operation is headed. The delivery observation uses it as its source. |
outcome | string | Optional | Sent on the assembly observation. The delivery outcome is set by execute. |
Returns #
prepareWitness returns { pairId, assemblyIdentity, execute }.
| Field | Description |
|---|---|
pairId | The pair identifier. Persist it. |
assemblyIdentity | The complete assembly observation that will be sent, including timestamp and eventId. Persist it. |
execute(operation) | Runs the round-trip. Resolves with { result, witness, currentInvocationOperationExecuted: true }, where result is your operation's return value and witness is the finalized pair state. |
What execute does #
- Sends the assembly observation. If the pair already has this assembly, it throws
AttestWitnessAssemblyExistsErrorwithout running your operation. - Calls
operation()once. It never retries it. - If the operation throws: sends a delivery with
outcome: "error"and finalizes, both best-effort, then rethrows your error unchanged. - If the operation succeeds: sends a delivery with
outcome: "ok"and finalizes. If either call fails, it throwsAttestWitnessDeliveryErrorcarryingpairIdanddeliveryObservation. Your operation has already run.