Skip to content

Hash Package

The hash package provides functions for calculating transaction hashes and class hashes in Starknet. Use it to verify transactions, compute class hashes before deployment, and validate contract classes.

What This Package Does

Transaction Hash Calculation
  • Calculate hashes for Invoke, Declare, and Deploy Account transactions
  • Support for versions V0, V1, V2, and V3
  • Generate unique identifiers for transaction verification
Class Hash Calculation
  • Compute hashes for Sierra contract classes
  • Calculate compiled class hashes (CASM format)
  • Verify contract class integrity
Utility Functions
  • Helper functions for V3 transaction components
  • Data availability mode hashing
  • Resource bounds hashing

Import

import "github.com/NethermindEth/starknet.go/hash"

Quick Example

Calculate a transaction hash for an Invoke V1 transaction:

package main
 
import (
	"fmt"
	"log"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/NethermindEth/starknet.go/hash"
	"github.com/NethermindEth/starknet.go/rpc"
)
 
func main() {
	// Create an Invoke V1 transaction
	txn := &rpc.InvokeTxnV1{
		Version:       "0x1",
		SenderAddress: new(felt.Felt).SetUint64(12345),
		Calldata:      []*felt.Felt{new(felt.Felt).SetUint64(1)},
		MaxFee:        new(felt.Felt).SetUint64(1000000),
		Nonce:         new(felt.Felt).SetUint64(1),
	}
 
	// Chain ID for Sepolia testnet
	chainID := new(felt.Felt).SetBytes([]byte("SN_SEPOLIA"))
 
	// Calculate transaction hash
	txHash, err := hash.TransactionHashInvokeV1(txn, chainID)
	if err != nil {
		log.Fatal("Failed to calculate hash:", err)
	}
 
	fmt.Printf("Transaction Hash: %s\n", txHash.String())
}

Available Functions

Transaction Hash Functions

Calculate hashes for different transaction types and versions:

Invoke Transactions:
  • TransactionHashInvokeV0 - Legacy invoke transactions (deprecated)
  • TransactionHashInvokeV1 - Invoke V1 with MaxFee
  • TransactionHashInvokeV3 - Invoke V3 with resource bounds
Declare Transactions:
  • TransactionHashDeclareV1 - Declare V1 with MaxFee
  • TransactionHashDeclareV2 - Declare V2 with compiled class hash
  • TransactionHashDeclareV3 - Declare V3 with resource bounds
  • TransactionHashBroadcastDeclareV3 - Broadcast variant for V3
Deploy Account Transactions:
  • TransactionHashDeployAccountV1 - Deploy account V1 with MaxFee
  • TransactionHashDeployAccountV3 - Deploy account V3 with resource bounds

See Transaction Hash Functions for details.

Class Hash Functions

Compute hashes for contract classes:

  • ClassHash - Calculate hash for Sierra contract class
  • CompiledClassHash - Calculate hash for compiled CASM class

See Class Hash Functions for details.

Utility Functions

Helper functions for V3 transactions:

  • TipAndResourcesHash - Hash tip and resource bounds
  • Data AvailabilityModeConc - Concatenate DA modes
  • CalculateDeprecatedTransactionHashCommon - Common deprecated hash calculation

See Utility Functions for details.

Transaction Versions

Different transaction versions use different hash algorithms:

  • V0 Transactions - Deprecated legacy format (Pedersen hash)
  • V1 Transactions - Invoke, Declare, Deploy Account with MaxFee (Pedersen hash)
  • V2 Transactions - Declare with compiled class hash (Pedersen hash)
  • V3 Transactions - Modern format with resource bounds (Poseidon hash)

Hash Algorithms

  • Pedersen Hash - Used in V0, V1, V2 transactions
  • Poseidon Hash - Used in V3 transactions and contract class hashes (more efficient)

Common Use Cases

Transaction Verification

Calculate expected hash to verify transaction integrity:

expectedHash, err := hash.TransactionHashInvokeV3(txn, chainID)
if expectedHash.String() != receivedHash.String() {
    // Transaction has been modified
}

Contract Deployment

Compute class hash before declaring:

classHash, err := hash.ClassHash(contractClass)
fmt.Printf("Class Hash: %s\n", classHash.String())

Transaction Monitoring

Track transactions using their unique hash:

txHash, _ := hash.TransactionHashInvokeV1(txn, chainID)
receipt, err := provider.TransactionReceipt(ctx, txHash)

Error Handling

Hash functions return specific errors:

  • ErrNotAllParametersSet - Missing required transaction parameters
  • ErrFeltToBigInt - Conversion error from felt to BigInt

Always check for errors:

txHash, err := hash.TransactionHashInvokeV3(txn, chainID)
if err != nil {
    if err == hash.ErrNotAllParametersSet {
        // Handle missing parameters
    }
    return err
}

Test Examples

See the test files in docs/tests/hash/ for working examples:

  • transaction_hash_invoke_v0.go - Invoke V0 example
  • transaction_hash_declare_v1.go - Declare V1 example
  • transaction_hash_declare_v3.go - Declare V3 with resource bounds
  • transaction_hash_deploy_account_v1.go - Deploy account V1 example
  • transaction_hash_deploy_account_v3.go - Deploy account V3 example

Related Packages

Reference

For more information: