Skip to main content
A Node.js worker that pays out from a treasury’s private balance. Your application inserts payout jobs into a table; the worker takes them one at a time, pays each exactly once, and records the result. It survives restarts, retries what can be retried, and parks what needs a human. The patterns are explained in Backend integration. This page is the code.

Prerequisites

  • Node 22 or later, @cloak.dev/sdk 0.2.20.
  • A database with two tables: a key-value table for SDK state and a jobs table. Any database works; the code below talks to them through small interfaces you implement.
  • In your secret store: the treasury account key (32 random bytes as hex) and a storage secret (32 random bytes as hex).

The jobs table

The worker

worker.ts

Why it is safe to restart

  • Idempotency. Each job’s ref is derived from its id. After a crash, the worker picks the job up again and send returns the stored receipt with repeated: true instead of paying again.
  • Unknown outcomes are not retried blindly. ref.unsettled means a call started and never recorded its end, for example a process killed mid-payment. The worker parks the job for review.
  • State outlives the process. encryptedStorage keeps receipts, refs and open cross-chain orders in your database, encrypted, so the next process sees them.

Funding the treasury

Shield into the treasury from a wallet the organization controls, or have anyone send to cloak.address. To shield from a server-held key, open the same account with both the key and the wallet: createCloak({ account, wallets: [fundingSigner], storage, rpc }), then cloak.shield({ from: "solana", ... }).

Monitoring