Decryption Lifecycle
This page explains the shared production-side lifecycle behind both Decrypting to View and Decrypting to Transact.
It is not about ACPs, return types, or builder methods. It is about what happens after you already have a ctHash and ask CoFHE to decrypt it.
Why decryption is not always immediate
A decrypt is one submit request to the Threshold Network. Its answer depends on how far the network has got with the ctHash:
- No access grant seen yet. The network answers
401. The SDK fails at once. - Access granted, result not ready yet. The network answers
204. The SDK asks again. - Ready. The network answers
200with the decrypted result in the same response.
A freshly produced handle moves through these in order. The first two are short: on the current networks they are over within about a block of the transaction that produced the handle, so a decrypt that waits for its receipt usually gets the result on the first request.
State 1: no access grant seen yet
Before anything else, the network checks that the caller may decrypt the ctHash. It does this against the chain: the grant comes from the transaction that produced the handle (for example FHE.allowSender in your contract).
Until the network has seen that transaction, it finds no grant and answers 401 with acp_denied (403 with not_publicly_allowed when no ACP is sent). It answers the same way for a handle that never existed. By default the SDK does not retry this: the decrypt fails at once with ACPDenied, and .onPoll(...) is not called.
You reach this state when:
- you decrypt a handle before the transaction that produces it is mined,
- you decrypt right after the receipt, and the network has not observed that block yet,
- the ACP really does not cover the handle (wrong account, scope, or chain).
Wait for the receipt before decrypting. If you decrypt right after the receipt and know the grant is on its way, add .setAccessDeniedRetryTimeout(timeoutMs) to the builder: the SDK then keeps asking for that long instead of failing on the first denial, and calls .onPoll(...) with an empty requestId for each retry. It is off by default because the network cannot tell a grant it has not seen yet from one that will never come; only the caller knows.
State 2: access granted, result not ready yet
Once the grant is visible, the result itself may not be ready: it is still being computed, or the network is busy. The submit endpoint then answers 204 No Content, with no body.
This means:
- the caller is allowed to decrypt the handle,
- the result is on its way,
- retrying the submit call makes sense.
The SDK handles this automatically. For both decryptForTx and decryptForView, it will:
- retry the submit call,
- wait between attempts,
- keep using the same overall timeout budget,
- emit
.onPoll(...)callbacks during these retries.
State 3: ready
The submit endpoint answers 200 and the response carries the result. The SDK returns it. There is no separate status endpoint to ask: every answer comes from the submit call.
Answers outside the sequence
404: no ciphertext for the handle
A 404 (ct_not_found) means the caller is allowed to decrypt the handle, but the network has no ciphertext for it. This is not a normal step for a new handle: a handle the network has not seen yet is answered with 401 (State 1), and one that is still being computed with 204 (State 2).
The SDK does not retry a 404: it fails at once with CtNotFound (apiErrorCode: 'ct_not_found'). Waiting does not fix it, because the answer points to a problem on the network side, not to a handle that needs more time.
Shared timeout budget
The SDK uses one timeout budget for the whole decrypt:
- initial submit attempts,
- repeated
204submit retries, - access-denied submit retries within the window set by
.setAccessDeniedRetryTimeout(...).
How .onPoll(...) fits in
Both decryption builders support .onPoll(...):
The callback context is:
{
operation: 'decrypt' | 'sealoutput';
attemptIndex: number;
elapsedMs: number;
intervalMs: number;
timeoutMs: number;
}The callback runs once before each retry of the submit call (a 204, or a denial within the access-denied window). It does not run when the first answer is final.
operation === 'decrypt':decryptForTxflow.operation === 'sealoutput':decryptForViewflow.
End-to-end summary
decrypt call starts
-> submit request
-> 401 acp_denied: fail with ACPDenied (also before the network has seen the transaction)
with setAccessDeniedRetryTimeout: SDK retries submit until that window ends
-> 204: SDK retries submit automatically
-> 200 with the result: return decrypt result
-> 404: fail with CtNotFoundPractical guidance
- Wait for the receipt before decrypting. A handle the network has not seen yet fails with
ACPDenied, notCtNotFound. - If you decrypt right after the receipt, set
.setAccessDeniedRetryTimeout(...)to a few seconds so a network that is a block behind does not fail the decrypt. - A
204needs no handling on your side: the SDK retries it until the result is ready or the timeout runs out. CtNotFoundis not a sign that the handle needs more time, and the SDK does not retry it. Check the chain and environment the handle belongs to.- If you want progress UI, use
.onPoll(...). It fires while the SDK retries the submit call.
Error codes
Decrypt/sealoutput failures throw a typed CofheError (see Migrating from cofhejs for the general isCofheError/err.code pattern). The threshold network's stable backend error codes each map to a dedicated CofheErrorCode, for example:
acp_denied,acp_expired,acp_invalid,acp_malformed— ACP was rejected or malformed.not_publicly_allowed— no ACP was supplied and the handle isn't publicly decryptable.ct_not_found— the network has no ciphertext for the handle.unsupported_security_zone,unsupported_type— the handle's zone or encryption type isn't supported.internal_error,signing_failed,ct_source_error,acp_verifier_error,ct_source_timeout,acp_verifier_timeout— backend-side failures.acp_required,seal_failed— sealoutput-specific.
err.apiErrorCode carries the raw backend string verbatim (e.g. 'acp_denied'), independent of which CofheErrorCode it mapped to — useful for logging, or if the backend introduces a new code before the SDK adds a dedicated enum member for it (in that case err.code falls back to the operation's generic failure code, but err.apiErrorCode still tells you exactly what the backend reported).