Skip to content

Transaction Hash Functions

Transaction hash functions calculate unique identifiers for Starknet transactions. Each transaction type and version has its own hash calculation method following the Starknet specification.

For conceptual understanding of transaction hashing, see Transaction Hashing Concepts.

Quick Reference

Use this table to find the right hash function for your transaction:

Transaction TypeVersionFunctionHash AlgorithmFee Model
InvokeV0 (deprecated)TransactionHashInvokeV0Pedersenmax_fee
InvokeV1TransactionHashInvokeV1Pedersenmax_fee
InvokeV3 (current)TransactionHashInvokeV3Poseidonresource_bounds
DeclareV1TransactionHashDeclareV1Pedersenmax_fee
DeclareV2TransactionHashDeclareV2Pedersenmax_fee
DeclareV3 (current)TransactionHashDeclareV3Poseidonresource_bounds
Deploy AccountV1TransactionHashDeployAccountV1Pedersenmax_fee
Deploy AccountV3 (current)TransactionHashDeployAccountV3Poseidonresource_bounds

Version Comparison

V0/V1/V2 Transactions (Legacy)

Hash Function: Pedersen hash Fee Model: Single max_fee field

These versions are deprecated but still supported for historical transactions:

  • V0: Original invoke format (deprecated)
  • V1: Added nonce and account abstraction support
  • V2: Added compiled class hash for Declare transactions

V3 Transactions (Current Standard)

Hash Function: Poseidon hash Fee Model: resource_bounds with separate limits

V3 introduces modern features:

  • Resource Bounds: Separate limits for L1 gas, L2 gas, and L1 data gas
  • Data Availability Modes: Control where fee and nonce data is stored (L1 vs L2)
  • Tip: Optional priority fee for faster inclusion
  • PayMaster Support: Third-party fee payment capability
  • Poseidon Hashing: More efficient than Pedersen

Migration Note: For new applications, always use V3 transactions. V0/V1/V2 support is maintained for compatibility only.

Usage Patterns

Pattern 1: Calculate Hash for Existing Transaction

When you have a transaction object and need its hash:

// For V3 Invoke transaction
txHash, err := hash.TransactionHashInvokeV3(invokeV3Txn, chainID)
if err != nil {
    log.Fatal("Failed to calculate hash:", err)
}

Pattern 2: Verify Transaction Hash

When verifying a transaction matches the expected hash:

// Calculate hash from transaction data
calculatedHash, _ := hash.TransactionHashInvokeV3(txn, chainID)
 
// Compare with received hash
if !calculatedHash.Equal(receivedHash) {
    log.Fatal("Transaction hash mismatch")
}

Pattern 3: Pre-calculate Hash for Signing

Account signing methods calculate the hash internally, but you can pre-calculate for verification:

// Pre-calculate hash
txHash, _ := hash.TransactionHashInvokeV3(txn, chainID)
 
// Later during signing, the account will calculate the same hash
signature, _ := account.Sign(ctx, txHash)

Function Categories

Invoke Transaction Hashes

Execute contract functions:

Declare Transaction Hashes

Register new contract classes:

Deploy Account Transaction Hashes

Create new account contracts:

Utility Functions

Low-level helpers:

Common Parameters

All transaction hash functions require:

  • Transaction Object: The specific transaction type (InvokeTxnV3, DeclareTxnV2, etc.)
  • Chain ID: Network identifier (use SN_MAIN, SN_SEPOLIA, or custom for devnet)

Some functions (Deploy Account) also require:

  • Contract Address: Pre-computed address for the account being deployed

Error Handling

All functions validate required parameters and return descriptive errors:

txHash, err := hash.TransactionHashInvokeV3(txn, chainID)
if err != nil {
    // Check for specific error types
    if err == hash.ErrNotAllParametersSet {
        log.Fatal("Transaction missing required fields")
    }
    log.Fatal("Hash calculation failed:", err)
}
// Safe to use txHash

Chain ID Values

Common chain IDs for hash calculation:

// Mainnet
chainID := new(felt.Felt).SetBytes([]byte("SN_MAIN"))
 
// Sepolia testnet
chainID := new(felt.Felt).SetBytes([]byte("SN_SEPOLIA"))
 
// Devnet (example)
chainID := new(felt.Felt).SetBytes([]byte("SN_DEVNET"))

Related Documentation

Specifications

All hash functions follow official Starknet specifications: