Attestcoin Smart Contracts
Please note that all information and code snippets provided in this section are for educational purposes only and not to be directly deployed in production.
What is the Attestcoin Smart Contract?
Attestcoin Smart Contract (ASC): A smart contract on Creditcoin that uses Attestcoin Protocol Readability or Writability.
Unlike traditional omnichain or cross-chain solutions that focus narrowly on token transfers or specific assets, the Attestcoin Protocol provides a general-purpose execution layer. This enables contracts to act on externally verified data without needing to rewrite core logic.
By adopting the Attestcoin Protocol into their tech stack, developers can transform their contracts into universal components powered by seamless cross-chain data, allowing for novel patterns of interoperability across multiple blockchains.
Attestcoin Smart Contract Architecture
ASCs verify cross-chain proofs and execute business logic. DApp Business Logic Contracts are contracts deployed on Creditcoin that contain the dApp's state and business logic.
In the example implementation (SimpleMinterASC), the business logic (ERC20 token minting) is integrated directly into the ASC itself. While this combined pattern works well for simple use cases, for more complex dApps developers can separate concerns by deploying distinct contracts:
An Attestcoin smart contract that handles the core cross-chain read/write responsibilities
And separate business logic contracts that the ASC contract calls after verification succeeds.
Both patterns are valid; the choice depends on the complexity and requirements of the dApp.
How it works
ASCs verify cross-chain transaction data using the Block Prover Precompile (address 0x0FD2), a built-in runtime component that provides synchronous verification of Merkle and continuity proofs.
ASCs integrate with it by calling its verify() (or alternatively verifyAndEmit() ) function directly to verify proofs before processing cross-chain data. Once a transaction is verified, the ASC extracts transaction and event data directly from the verified transaction bytes and executes dApp-specific business logic.
Key characteristics:
Synchronous verification: Proofs are verified in the same transaction, no async processing
Direct data extraction: Transaction and event data is extracted directly from verified transaction bytes
Replay protection: ASCs implement mechanisms to prevent duplicate processing
Native-speed execution: The precompile runs as native Rust code for optimal performance
The block prover precompile does not validate if a transaction was successful or not. It only validates if a transaction is included in a block and that block is really a part of the confirmed source chain. Therefore, a dApp's ASC MUST check the "status" field of the transaction to ensure security 0x1 → ✅ Success
Core Attestcoin Smart Contract Pattern
A typical ASC follows this pattern:
Receives proofs and transaction data from an off-chain worker
Implements replay protection to prevent duplicate processing
Calls the Block Prover Precompile to verify proofs synchronously
Extracts transaction/event data from verified transaction bytes
Executes business logic based on the verified data
Example ASC Contract
See ASCMinter.sol for a complete ASC implementation. The contract:
Receives proofs and transaction data from offchain worker
Implements replay protection using a
processedQueriesmappingUses the Block Prover Precompile to verify proofs
Validates transaction type and receipt status (must be successful)
Extracts event data from verified transaction bytes using
EvmV1DecoderExecutes business logic (ERC20 token minting) within the same contract that mints tokens once a burn event is verified from the source chain
Key function signature:
dApp Business Logic Contracts
dApp Business Logic Contracts are smart contracts deployed on Creditcoin that contain the dApp's state and business logic.
In the example implementation (SimpleMinterASC), the business logic is integrated directly into the ASC. The contract:
Stores dApp state (e.g., token balances via ERC20)
Implements dApp-specific logic (e.g., minting tokens)
Executes business logic immediately after verifying cross-chain proofs and validating transaction contents
Validates inputs and updates state accordingly
Transaction Data Extraction
After verification succeeds, ASCs extract transaction and event data from the encodedTransaction bytes as part of the transaction content validation process. The transaction encoding follows a deterministic format that includes:
Transaction fields: Type, chain ID, nonce, from address, to address, value, etc.
Receipt fields: Status, gas used, logs (events)
Event data: Topics and data from transaction receipt logs
ASCs can use libraries like EvmV1Decoder to selectively extract specific events or transaction fields only needed for their business logic. This selective extraction allows ASCs to efficiently validate specific events or transaction fields needed for their business logic without decoding the entire transaction structure.
Query Processing Flow
When an oracle query worker provides proof data for a source chain transaction:
Worker generates proofs using the Proof Builder service
Worker calls ASC contract with proofs and encoded transaction data
ASC contract verifies proofs synchronously using the Block Prover Precompile
ASC contract extracts data from verified transaction bytes
ASC contract executes business logic immediately in the same transaction
All of this happens synchronously in a single transaction—there is no async query processing or result storage.
Attestcoin Smart Contract Implementation Example
The following sections break down a complete ASC implementation based on ASCMinter.sol
Since the creation of this article, the ASCMinter was updated to better reflect a production ready design. The minter responsibilities were split off into several contracts handling portions of the bridge token minting process. The code here, though not fit for production, more simply and succinctly demonstrates ASC design. So it remains unchanged.
Contract Structure
The Block Prover Precompile was previously called Native Query Verifier, so you'll see that term throughout these code examples
Key components:
Inherits from ERC20: The contract uses the combined pattern—it's both an ASC (requests proof verification and decodes tx data) and a business logic contract (
ERC20token with minting logic)VERIFIER: Immutable reference to the Block Prover Precompile at address
0x0FD2processedQueries: Mapping for replay protection, preventing duplicate processing of the same transaction
Main Entry Point: mintFromQuery
Description:
Parameters: Receives all proof components and transaction data from the off-chain worker
Transaction Index Calculation: Calculates the transaction index from the Merkle proof path using
_calculateTransactionIndex()Transaction Key Generation: Creates a unique key from
chainKey,blockHeight, andtransactionIndexusing assembly for gas efficiencyReplay Protection: Checks if this transaction has already been processed
Proof Verification: Calls
_verifyProof()to verify the Merkle and continuity proofs synchronouslyState Update (replay protection): Marks the transaction as processed in
processedQueriesmappingTransaction Content Validation: Validates the transaction contents by checking transaction type and receipt status.
Business Logic Execution: If validation passes, executes business logic (minting tokens)
Event Emission: Emits
TokensMintedevent with the transaction details
Constructor and Initialization
Description:
Initializes the ERC20 token with name and symbol
Sets the
VERIFIERimmutable variable to the precompile instanceThe precompile address is constant and always available
Replay Protection
Description:
processedQueries: Maps transaction keys to boolean values to track processed transactions
Proof Verification
Description:
Constructs the
MerkleProofandContinuityProofstructs from the provided componentsCalls the precompile's
verifyAndEmit()function synchronously at address0x0FD2Returns
trueif both Merkle proof (transaction inclusion) and continuity proof (block attestation chain) are valid; reverts on failure (transaction reverts if verification fails)Emits
TransactionVerifiedevent on successful verificationVerification happens in the same transaction - no async processing
Transaction Data Extraction
The contract includes helper functions for extracting and validating transaction data from encodedTransaction bytes:
Description:
Uses
EvmV1Decoderlibrary to decode the transaction bytesValidates transaction type and receipt status
Extracts event logs matching the
Transferevent signatureValidates that a burn transfer occurred (transfer to address < 128)
Complete Example
See ASCMinter.sol for the complete implementation with all helper functions and event processing logic. A corresponding helper script and instructions to use this code are available in the hello-bridge example.
Last updated