TransientAccessControl

TransientAccessControl

Category: Identity | Difficulty: Intermediate | Chapters: Access Control | Concept: FHE.allowTransient() for one-transaction permissions between contracts

Demonstrates FHE.allowTransient() for cross-contract encrypted workflows

Why this example

This example focuses on FHE.allowTransient() for one-transaction permissions between contracts. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/identity/TransientAccessControl.test.ts

Dependencies

None

Deployment plan

Step
Contract
Args
Saves As

1

TransientAccessRegistry

-

registry

2

TransientScoreConsumer

-

consumer

Contract and test

Pitfalls to avoid

  • should revert when the registry does not grant transient permission

  • should show that cached handles outlive transient permissions

API Reference

Overview

Registry that stores encrypted scores and grants transient permissions

Developer Notes

Demonstrates how to grant transient permissions to calling contracts

ScoreStored

Emitted when a user stores their encrypted score

Parameters

Name
Type
Description

user

address

The address that stored a score

NoScore

Error thrown when a user has no stored score

storeScore

Store an encrypted score for msg.sender

Parameters

Name
Type
Description

encryptedScore

externalEuint8

Encrypted score (0-255)

inputProof

bytes

Proof for the encrypted input

getScoreFor

Return a user's encrypted score AND grant the caller transient permission

This must NOT be view, because it mutates transient ACL state.

Parameters

Name
Type
Description

user

address

The score owner

Return Values

Name
Type
Description

[0]

euint8

The user's encrypted score

getScoreNoTransient

Return a user's encrypted score WITHOUT granting transient permission (pitfall)

Consumers that try to compute on the returned handle will revert.

Parameters

Name
Type
Description

user

address

The score owner

Return Values

Name
Type
Description

[0]

euint8

The user's encrypted score (without transient permission)

TransientScoreConsumer

Overview

Consumer contract that demonstrates using transient permissions to access encrypted data

Developer Notes

Shows both correct usage and common pitfalls when working with transient permissions

NoCachedScore

Error thrown when attempting to use a cached score that doesn't exist

NoResult

Error thrown when attempting to retrieve a result that doesn't exist

checkAtLeastWithTransient

Compare a user's score against a plaintext threshold (works with allowTransient)

Parameters

Name
Type
Description

registry

address

Registry contract address

user

address

Score owner

minScore

uint8

Minimum required score (plaintext)

Return Values

Name
Type
Description

[0]

ebool

Encrypted boolean result

checkAtLeastWithoutTransient

Same comparison but using a registry call that does NOT grant transient permission (pitfall)

Parameters

Name
Type
Description

registry

address

Registry contract address

user

address

Score owner

minScore

uint8

Minimum required score (plaintext)

Return Values

Name
Type
Description

[0]

ebool

Encrypted boolean result (will fail without transient permission)

cacheScoreWithTransient

Cache a user's score handle (works because reading doesn't require permission)

The cached handle will NOT be usable in later txs unless permanently allowed.

Parameters

Name
Type
Description

registry

address

Registry contract address

user

address

Score owner

useCachedScore

Attempt to reuse a cached handle in a later transaction (pitfall)

Parameters

Name
Type
Description

minScore

uint8

Minimum required score (plaintext)

Return Values

Name
Type
Description

[0]

ebool

Encrypted boolean result (will revert without permanent permission)

getLastResult

Get the last computed result for a caller

Call checkAtLeastWithTransient first.

Parameters

Name
Type
Description

caller

address

The address to get the result for

Return Values

Name
Type
Description

[0]

ebool

The last computed encrypted boolean result

Last updated