Skip to content

Add receipt-anchor example: commit to a contract invocation, verify it later - #434

Draft
EslaM-X wants to merge 2 commits into
stellar:mainfrom
EslaM-X:add-receipt-anchor-example
Draft

EslaM-X wants to merge 2 commits into
stellar:mainfrom
EslaM-X:add-receipt-anchor-example

Conversation

@EslaM-X

@EslaM-X EslaM-X commented Nov 20, 2026

Copy link
Copy Markdown

Draft, because #433 is still open and one naming question in it is unanswered. The code itself is finished and tested; the draft is about the name and the interface, not about whether it works.

What it is

One contract, five entry points. anchor commits to the facts of a single invocation and stores the receipt under the caller's address; commitment recomputes the same 32 bytes in a pure function, so a verifier never has to trust a call to the contract.

commitment = sha256( "receipt-anchor/1"
                   ++ xdr(target)
                   ++ xdr(fn_name)
                   ++ sha256(xdr(args))
                   ++ sha256(xdr(result))
                   ++ xdr(ledger) )

The domain prefix means a commitment made here cannot be confused with a hash of the same data computed for another purpose, and the two component hashes are fixed-width, so no two different tuples can collide by shifting a boundary in the preimage.

function what it does
anchor(caller, target, fn_name, args, result, ledger) caller.require_auth(), compute, store, return the commitment
commitment(target, fn_name, args, result, ledger) the same 32 bytes, no writes
verify(receipt, args, result) recompute and compare, false on any mismatch
receipt(caller, index) / count(caller) read a caller's receipts in anchoring order

It moves no funds and grants no authority. It records what an invocation did, not whether it should have happened.

Verification

Run in this branch's tree, Rust 1.100.0, stellar-cli 30.0.1:

$ make          # stellar contract build
Build Complete
Exported Functions: 5 found
  anchor, commitment, count, receipt, verify
-rw-r--r-- 5233 target/wasm32v1-none/release/soroban_receipt_anchor_contract.wasm

$ make test
running 9 tests
test result: ok. 9 passed; 0 failed

The nine: the happy path, one tamper case per committed field (args incl. order and length, result, ledger, target, function name), the content-addressed property across two different callers, per-caller indexing, and one test that calls anchor without mocking authorization and expects it to fail.

Note: soroban-sdk 28 no longer builds for wasm32-unknown-unknown; the Makefile uses stellar contract build, which targets wasm32v1-none like the rest of the examples. Cargo.lock is not in this commit — cargo writes it on first build.

Open question from #433

receipt-anchor or commitment-anchor, and should args/result stay Val (works for any invocation, canonical XDR) or be Bytes-only (narrower, easier to verify outside Rust)? Happy to rename or narrow before this leaves draft.

Refs #433

…t later

Commits to the facts of one invocation (target, function, arguments, result,
ledger) with a domain-separated SHA-256 preimage, stores the receipt under the
caller's address behind require_auth, and recomputes the same 32 bytes in a
pure function so a verifier does not have to trust a call to the contract.

Nine tests: the happy path, a tamper case per committed field, the
content-addressed property across two callers, per-caller indexing, and one
test that anchors without mocking authorization.

Refs stellar#433
run fmt/clippy here the way the Rust workflow does. That reformatted several
call sites in test.rs, and clippy was right that result is Copy so the clone in
anchor was redundant. Also adds the one-line entry to the example list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant