@cloak.dev/sdk 0.2.20. Catch the typed errors below instead of
matching message strings: the classes and codes are the stable surface, the wording is not.
createCloak errors: CloakCallError
Every call on the object createCloak returns throws one error type,
CloakCallError (a CloakError), and it carries what a caller acts on:
code: stable and dotted (payout.refused,wallet.rejected,deposit.finish_failed). Match on it withisCloakCallError(e, code), which also matches every code under a prefix (isCloakCallError(e, "payout")is anypayout.*).retryable: whether the same call can succeed if it is made again.moneyAtandnext, when money is in flight: where it is now, and what happens to it. The message already includes both, written for a person.
cloak.resume()), which is why many in-flight errors are retryable with a next that says so.
Every CloakCallError code
Generated from the 0.2.20 source. “Retryable” is the error’s own retryable flag; trust the flag on
the instance over this table.
Two errors that are not
CloakCallError can also come out of createCloak:
WalletSignatureChangedError (the wallet now signs the account message differently than when the
account was set up on this device) and StoredAccountRequiredError (the wallet cannot sign
repeatably). Catch them by name.
The free functions’ errors
The free functions (transact, transfer, partialWithdraw, …) expose three layers:
CloakError(runtime SDK operations, with acategoryand aretryableflag)- Typed errors for the outcomes a caller must act on:
RootNotFoundError,UtxoAlreadySpentError,SanctionsQuoteError,RelayInternalError,SettlementVerificationError,TransactionConfirmationError parseError/parseTransactionError(user-facing normalization)
CloakError
retryable === true as backoff-eligible.
Typed errors
confirmTransactSettlement re-checks a signature from chain state when an outcome was reported as
unknown.
Persist the notes before you report success
transact, transfer, partialWithdraw, swapUtxo and swapWithChange all return
result.outputUtxos, and the change note among them is the user’s remaining shielded balance.
Outputs with amount === 0n are padding and can be skipped. fullWithdraw leaves nothing shielded.
The notes the SDK builds for you are derived from your nk and a salt published in the encrypted
chain note, so a lost change note can be found again by a scan (recoverNotesFromIndex,
scanTransactions). The exception is a transfer’s change: it is output 1, the chain note
describes only output 0, so a scan cannot learn its amount and the note is spendable again only once
you supply that amount. Writing it down is still the fast path and gives you the leaf index at once.
The notes that cannot be found again are the ones built with createUtxo (by you, or by a client
that predates recoverable change), whose blinding is random and written nowhere, and notes owned by
a random generateUtxoKeypair() key you did not keep. Those are unspendable by anyone if the only
copy is dropped.
This is an error-handling concern because every example below is a try/catch. The catch is the
failure path; the tail of the try is the success path, and that is where the write belongs.
saveNotes, dropNotes and saveSwapRefund in these snippets are your own storage, not SDK
exports; Where your notes live between sessions
covers what to build them on.
When fullWithdraw merges first
The circuit spends at most two notes per transaction, so both fullWithdraw and partialWithdraw
consolidate first when you pass three or more inputs. Each consolidation round is its own on-chain
transaction: it spends the two smallest notes and creates one merged note owned by your
ownerUtxoKeypair. onProgress fires Consolidating notes (n remaining)... once per round, and
that string is the signal a caller gets that consolidation is happening at all.
Rounds that land are permanent, and the failure is what makes this an error-handling problem. If a
later round or the final withdrawal throws (stale-root retries exhausted, a rejected wallet approval,
a submission failure, a closed tab), then the two notes that round consumed are spent, the merged
commitment is in the tree, and the call returned nothing, so there is no result.outputUtxos to
write down.
The merged note is not destroyed. Its blinding is derived from your nk and a salt the round
published in its chain note, and the merged note is output 0, whose amount that chain note carries,
so a rescan with nk finds it again (recoveredChangeNotes). Until you rescan, though, your app does not
know it exists, and retrying with the original inputUtxos fails: the first merge round preflights
their nullifiers and throws UtxoAlreadySpentError.
What you can do, in order of usefulness:
Unauthorized and bare 401 on a send, withdraw or swap
Every request Cloak submits for you carries a signed sender, and every way that can go wrong arrives
as the same opaque rejection. explainRelayAuthRejection turns one into user-facing prose, and
returns null for anything that is not an authentication rejection, so it is safe to call
unconditionally:
verifyUtxos before retrying rather than reusing inputUtxos. The full contract,
including how to supply a signer at all, is in
Request authentication.
A call that has no usable signer never reaches the network. It throws up front, before any proof is
generated, with a message that begins Cloak <flow> requires an authenticated sender (for a
transfer, withdrawal or swap), <flow> requires options.ownerUtxoKeypair (a spend flow with no
owner key), or Viewing key registration is mandatory (no nk, or nothing to sign the registration
challenge). Those are setup mistakes, not runtime failures: fix the options rather than catching them.
Stale Merkle root (0x1001)
Use helpers for RootNotFound detection:
maxRootRetries); this usually appears only
after retries are exhausted.
On-chain error message mapping
ShieldPoolErrors maps known custom program codes:
User-facing normalization
Practical retry strategy
- Retry network and service failures with exponential backoff.
- Avoid retrying deterministic validation failures.
- On stale-root errors, rebuild proof data before retrying.
- Never retry a
SettlementVerificationErrorwhoseoutcomeis not"not-landed".
UTXO stale-root retry template
transact, partialWithdraw and fullWithdraw already retry a stale root internally, so an outer
loop is only for the case where those retries are exhausted:
inputUtxos on every attempt, which is safe only while that array holds one or
two notes. With three or more, attempt 1 can land consolidation transactions before it throws, and
attempt 2 then spends notes that are already nullified: the first merge round preflights them and
throws UtxoAlreadySpentError instead of retrying. Consolidate down to two notes yourself before
entering a loop like this, as in
When fullWithdraw merges first.
Swap partialWithdraw for transact, transfer, swapUtxo or swapWithChange and the loop is
unchanged, but the tail still has to run: each of those returns a change note too, and the two swap
helpers add a refund authorization. fullWithdraw is the only substitution that can skip the
tail, and only when it is given one or two notes, because that is when it leaves nothing shielded
and performs no merges of its own.