Reference

@civitas/program

The Civitas program client: addresses, instruction builders, signing bytes, decoders and the error catalog.

Addresses, instruction builders, signing bytes and decoders for the Civitas program, checked against its IDL. Both SDKs and the CLI are built on it. Use it to build transactions yourself.

Version 0.2.0:

install
npm install ./civitas-contract-0.2.0.tgz ./civitas-program-0.2.0.tgz

Exports

PROGRAM_ID

ts
const PROGRAM_ID: PublicKey;

The Civitas program ID.

configPda

ts
const configPda: () => PublicKey;

The Config account’s address.

agentPda

ts
const agentPda: (agentKey: PublicKey) => PublicKey;

An agent account’s address, from the agent key.

vaultPda

ts
const vaultPda: (agentAddress: PublicKey) => PublicKey;

A vault’s address, from the agent account’s address.

servicePda

ts
const servicePda: (agentAddress: PublicKey) => PublicKey;

A listing’s address, from the agent account’s address.

eventAuthorityPda

ts
const eventAuthorityPda: () => PublicKey;

Anchor’s event authority address.

usdcAta

ts
const usdcAta: (owner: PublicKey, usdcMint: PublicKey) => PublicKey;

A wallet’s USDC associated token account.

createUsdcAtaInstruction

ts
function createUsdcAtaInstruction(payer: PublicKey, owner: PublicKey, usdcMint: PublicKey): TransactionInstruction;

Creates a USDC associated token account if it doesn’t exist yet.

createProgramClient

ts
function createProgramClient(connection: Connection): {
    charterInstructions(args: CharterArgs & {
        consentSignature: Uint8Array;
    }): Promise<TransactionInstruction[]>;
    withdrawInstructions(args: WithdrawArgs): Promise<TransactionInstruction[]>;
    revokeInstructions(args: RevokeArgs): Promise<TransactionInstruction[]>;
    settleInstructions(args: SettleArgs & {
        signature: Uint8Array;
    }): Promise<[TransactionInstruction, TransactionInstruction]>;
    initializeConfig({ admin, usdcMint }: {
        admin: PublicKey;
        usdcMint: PublicKey;
    }): Promise<TransactionInstruction>;
    updateConfig({ admin, feeBps, feeFloor, feeRecipient }: {
        admin: PublicKey;
        feeBps: number;
        feeFloor: bigint;
        feeRecipient: PublicKey;
    }): Promise<TransactionInstruction>;
    charter({ principal, agentKey, usdcMint, cardHash, cardUri, policy, firstDeposit, consentExpiresAt }: CharterArgs): Promise<TransactionInstruction>;
    deposit({ funder, agentKey, usdcMint, amount }: {
        funder: PublicKey;
        agentKey: PublicKey;
        usdcMint: PublicKey;
        amount: bigint;
    }): Promise<TransactionInstruction>;
    withdraw({ principal, agentKey, usdcMint, amount }: WithdrawArgs): Promise<TransactionInstruction>;
    setPolicy({ principal, agentKey, policy }: PrincipalAgent & {
        policy: Policy;
    }): Promise<TransactionInstruction>;
    freeze({ principal, agentKey }: PrincipalAgent): Promise<TransactionInstruction>;
    unfreeze({ principal, agentKey }: PrincipalAgent): Promise<TransactionInstruction>;
    revoke({ principal, agentKey, usdcMint }: RevokeArgs): Promise<TransactionInstruction>;
    listService({ principal, agentKey, endpoint, pricePerCall }: PrincipalAgent & {
        endpoint: string;
        pricePerCall: bigint;
    }): Promise<TransactionInstruction>;
    unlistService({ principal, agentKey }: PrincipalAgent): Promise<TransactionInstruction>;
    settle({ submitter, feeRecipient, payeeIsChartered, authorization }: SettleArgs): Promise<TransactionInstruction>;
};

One builder per instruction, each returning a TransactionInstruction, plus the …Instructions variants that put the instructions around it that it needs. See The client.

authorizationBytes

ts
function authorizationBytes(authorization: Authorization): Uint8Array;

The authorization bytes.

consentBytes

ts
function consentBytes(consent: Consent): Uint8Array;

The consent bytes.

receiptBytes

ts
function receiptBytes(receipt: Receipt): Uint8Array;

The receipt bytes.

signAuthorization

ts
function signAuthorization(agentSecret: Uint8Array, authorization: Authorization): Uint8Array;

Signs an authorization with the agent’s 64-byte secret key.

signConsent

ts
function signConsent(agentSecret: Uint8Array, consent: Consent): Uint8Array;

Signs a consent with the agent’s 64-byte secret key.

consentInstruction

ts
function consentInstruction(consent: Consent, signature: Uint8Array): TransactionInstruction;

The Ed25519 instruction that must sit just before charter.

authorizationInstruction

ts
function authorizationInstruction(authorization: Authorization, signature: Uint8Array): TransactionInstruction;

The Ed25519 instruction that must sit just before settle.

signReceipt

ts
function signReceipt(payeeSecret: Uint8Array, receipt: Receipt): Uint8Array;

Signs a receipt with the payee’s 64-byte secret key.

verifyReceipt

ts
function verifyReceipt(receipt: Receipt, signature: Uint8Array): boolean;

Checks a receipt’s signature against its payee key.

decodeConfig

ts
function decodeConfig(data: Uint8Array): {
    admin: PublicKey;
    feeBps: number;
    feeFloor: bigint;
    feeRecipient: PublicKey;
    usdcMint: PublicKey;
    bump: number;
    feeAtaBump: number;
};

Decodes the Config account’s data.

decodeAgent

ts
function decodeAgent(data: Uint8Array): {
    agentKey: PublicKey;
    principal: PublicKey;
    cardHash: Uint8Array<ArrayBuffer>;
    cardUri: string;
    status: string;
    policyVersion: number;
    policy: Policy;
    createdAt: bigint;
    periodStart: bigint;
    spentInPeriod: bigint;
    highestNonce: bigint;
    nonceBitmap: bigint[];
    bump: number;
    vaultBump: number;
};

Decodes an Agent account’s data.

decodeService

ts
function decodeService(data: Uint8Array): {
    endpoint: string;
    pricePerCall: bigint;
    listed: boolean;
    updatedAt: bigint;
};

Decodes a Service account’s data.

decodeEventInstruction

ts
function decodeEventInstruction(programId: PublicKey | string, data: Uint8Array | string): CivitasEvent | null;

Decodes one Civitas event from an inner instruction’s data, raw or base58, or returns null for anything else.

PROGRAM_ERRORS

ts
const PROGRAM_ERRORS: {
    code: number;
    name: string;
    message: string;
}[];

Every program error with its code, name and message.

programError

ts
function programError(codeOrName: number | string): {
    code: number;
    name: string;
    message: string;
} | undefined;

Looks a program error up by code or by name.

feeFor

ts
function feeFor(amount: bigint, feeBps: number, feeFloor: bigint): bigint;

The protocol fee for an amount, as the program computes it.

IDL

ts
const IDL: { address, metadata, instructions, accounts, events, errors, types }

The program’s IDL.

Policy

ts
interface Policy {
    maxPerCall: bigint;
    maxPerPeriod: bigint;
    periodSeconds: number;
    counterparties: readonly PublicKey[] | null;
    expiresAt: bigint;
}

A policy as the builders take it.

Policy
maxPerCallbigintrequired
Most per settlement, amount and fee together.
maxPerPeriodbigintrequired
Most per period, amount and fee together.
periodSecondsnumberrequired
Period length.
counterpartiesreadonly PublicKey[] | nullrequired
Payees allowed, or null for open.
expiresAtbigintrequired
Unix seconds; 0n for none.

PrincipalAgent

ts
interface PrincipalAgent {
    principal: PublicKey;
    agentKey: PublicKey;
}

The principal and the agent key, which every lifecycle builder takes.

PrincipalAgent
principalPublicKeyrequired
The principal wallet.
agentKeyPublicKeyrequired
The agent key.

CharterArgs

ts
interface CharterArgs extends PrincipalAgent {
    usdcMint: PublicKey;
    cardHash: Uint8Array;
    cardUri: string;
    policy: Policy;
    firstDeposit: bigint;
    consentExpiresAt: bigint;
}

What charter and charterInstructions take.

CharterArgs
usdcMintPublicKeyrequired
The settlement mint.
cardHashUint8Arrayrequired
SHA-256 of the card JSON, 32 bytes.
cardUristringrequired
Where the card is served.
policyPolicyrequired
The first policy.
firstDepositbigintrequired
Base units to move into the vault.
consentExpiresAtbigintrequired
The expiry the agent key signed in its consent, Unix seconds.

WithdrawArgs

ts
interface WithdrawArgs extends PrincipalAgent {
    usdcMint: PublicKey;
    amount: bigint;
}

What withdraw and withdrawInstructions take.

WithdrawArgs
usdcMintPublicKeyrequired
The settlement mint.
amountbigintrequired
Base units.

RevokeArgs

ts
interface RevokeArgs extends PrincipalAgent {
    usdcMint: PublicKey;
}

What revoke and revokeInstructions take.

RevokeArgs
usdcMintPublicKeyrequired
The settlement mint.

SettleArgs

ts
interface SettleArgs {
    submitter: PublicKey;
    feeRecipient: PublicKey;
    payeeIsChartered: boolean;
    authorization: Authorization;
}

What settle and settleInstructions take.

SettleArgs
submitterPublicKeyrequired
The key that signs and pays the transaction.
feeRecipientPublicKeyrequired
The fee recipient from Config.
payeeIsCharteredbooleanrequired
Whether the payee has a charter, which decides the destination account.
authorizationAuthorizationrequired
The signed authorization.

Authorization

ts
interface Authorization {
    mint: PublicKey;
    agentKey: PublicKey;
    payee: PublicKey;
    amount: bigint;
    fee: bigint;
    nonce: bigint;
    callId: Uint8Array;
    expiresAt: bigint;
}

The fields an agent signs for one call.

Authorization
mintPublicKeyrequired
The settlement mint.
agentKeyPublicKeyrequired
The payer’s agent key.
payeePublicKeyrequired
The payee’s key.
amountbigintrequired
Base units.
feebigintrequired
The protocol fee, base units.
noncebigintrequired
The agent’s nonce.
callIdUint8Arrayrequired
The quote’s 32-byte call ID.
expiresAtbigintrequired
Unix seconds.
ts
interface Consent {
    principal: PublicKey;
    agentKey: PublicKey;
    cardHash: Uint8Array;
    expiresAt: bigint;
}

The fields an agent key signs to consent to its charter.

Consent
principalPublicKeyrequired
The principal that will charter the agent.
agentKeyPublicKeyrequired
The agent key.
cardHashUint8Arrayrequired
SHA-256 of the card JSON, 32 bytes.
expiresAtbigintrequired
Unix seconds.

Receipt

ts
interface Receipt {
    settlementSignature: Uint8Array;
    callId: Uint8Array;
    payer: PublicKey;
    payee: PublicKey;
    amount: bigint;
    servedAt: bigint;
    responseHash: Uint8Array;
}

The fields a payee signs for one served call.

Receipt
settlementSignatureUint8Arrayrequired
The settlement’s 64-byte signature.
callIdUint8Arrayrequired
The 32-byte call ID.
payerPublicKeyrequired
The payer’s agent key.
payeePublicKeyrequired
The payee’s key.
amountbigintrequired
Base units.
servedAtbigintrequired
Unix seconds.
responseHashUint8Arrayrequired
SHA-256 of the response body, 32 bytes.

ConfigAccount

ts
type ConfigAccount = ReturnType<typeof decodeConfig>;

A decoded Config.

AgentAccount

ts
type AgentAccount = ReturnType<typeof decodeAgent>;

A decoded Agent.

ServiceAccount

ts
type ServiceAccount = ReturnType<typeof decodeService>;

A decoded Service.

CivitasEvent

ts
type CivitasEvent = {
    [Name in keyof Events]: {
        name: Name;
        data: Events[Name];
    };
}[keyof Events];

A decoded event: its name and its fields under the IDL’s names.

Civitas

ts
type Civitas = typeof IDL // as Anchor generates it

The IDL’s type, for Anchor clients.

The client

createProgramClient(connection) returns these builders. Every one resolves to instructions; none signs or sends. Amounts are bigint base units.

MethodBuilds
charterInstructionsCreates the principal’s USDC account if needed, adds the Ed25519 instruction with the agent’s consentSignature, then charter.
withdrawInstructionsCreates the principal’s USDC account if needed, then withdraw.
revokeInstructionsCreates the principal’s USDC account if needed, then revoke.
settleInstructionsThe Ed25519 instruction for the signature, then settle, in the order the program needs.
initializeConfigThe one-time Config creation, for the designated deployer.
updateConfigA fee change, for the admin.
charterThe charter instruction.
depositA deposit from funder’s USDC account.
withdrawA withdrawal to the principal.
setPolicyA new policy.
freezeA freeze.
unfreezeAn unfreeze.
revokeA revoke.
listServiceA listing or a listing change.
unlistServiceAn unlisting.
settleThe settle instruction alone.