Hash#

Transaction hashes#

enum starknet_py.hash.transaction.TransactionHashPrefix(value)#

Enum representing possible transaction prefixes.

Member Type:

int

Valid values are as follows:

DECLARE = <TransactionHashPrefix.DECLARE: 28258975365558885>#
DEPLOY = <TransactionHashPrefix.DEPLOY: 110386840629113>#
DEPLOY_ACCOUNT = <TransactionHashPrefix.DEPLOY_ACCOUNT: 2036277798190617858034555652763252>#
INVOKE = <TransactionHashPrefix.INVOKE: 115923154332517>#
L1_HANDLER = <TransactionHashPrefix.L1_HANDLER: 510926345461491391292786>#
class starknet_py.hash.transaction.CommonTransactionV3Fields#

CommonTransactionV3Fields(tx_prefix: starknet_py.hash.transaction.TransactionHashPrefix, version: int, address: int, tip: int, resource_bounds: starknet_py.net.client_models.ResourceBoundsMapping, paymaster_data: List[int], chain_id: int, nonce: int, nonce_data_availability_mode: starknet_py.net.client_models.DAMode, fee_data_availability_mode: starknet_py.net.client_models.DAMode)

__init__(tx_prefix: TransactionHashPrefix, version: int, address: int, tip: int, resource_bounds: ResourceBoundsMapping, paymaster_data: List[int], chain_id: int, nonce: int, nonce_data_availability_mode: DAMode, fee_data_availability_mode: DAMode) None#
starknet_py.hash.transaction.compute_transaction_hash(tx_hash_prefix: TransactionHashPrefix, version: int, contract_address: int, entry_point_selector: int, calldata: Sequence[int], max_fee: int, chain_id: int, additional_data: Sequence[int] | None = None) int#

Calculates the transaction hash in the Starknet network - a unique identifier of the transaction. The transaction hash is a hash chain of the following information:

  1. A prefix that depends on the transaction type.

  2. The transaction’s version.

  3. Contract address.

  4. Entry point selector.

  5. A hash chain of the calldata.

  6. The transaction’s maximum fee.

  7. The network’s chain ID.

Each hash chain computation begins with 0 as initialization and ends with its length appended. The length is appended in order to avoid collisions of the following kind: H([x,y,z]) = h(h(x,y),z) = H([w, z]) where w = h(x,y).

Parameters:
  • tx_hash_prefix – A prefix that depends on the transaction type.

  • version – The transaction’s version.

  • contract_address – Contract address.

  • entry_point_selector – Entry point selector.

  • calldata – Calldata of the transaction.

  • max_fee – The transaction’s maximum fee.

  • chain_id – The network’s chain ID.

  • additional_data – Additional data, required for some transactions (e.g. DeployAccount, Declare).

Returns:

Hash of the transaction.

starknet_py.hash.transaction.compute_invoke_transaction_hash(*, version: int, sender_address: int, calldata: Sequence[int], max_fee: int, chain_id: int, nonce: int) int#

Computes hash of an Invoke transaction.

Parameters:
  • version – The transaction’s version.

  • sender_address – Sender address.

  • calldata – Calldata of the function.

  • max_fee – The transaction’s maximum fee.

  • chain_id – The network’s chain ID.

  • nonce – Nonce of the transaction.

Returns:

Hash of the transaction.

starknet_py.hash.transaction.compute_invoke_v3_transaction_hash(*, account_deployment_data: List[int], calldata: List[int], common_fields: CommonTransactionV3Fields) int#

Computes hash of an Invoke transaction version 3.

Parameters:
  • account_deployment_data – This will contain the class_hash, salt, and the calldata needed for the constructor. Currently, this value is always empty.

  • calldata – Calldata of the function.

  • common_fields – Common fields for V3 transactions.

Returns:

Hash of the transaction.

starknet_py.hash.transaction.compute_deploy_account_transaction_hash(version: int, contract_address: int, class_hash: int, constructor_calldata: Sequence[int], max_fee: int, nonce: int, salt: int, chain_id: int) int#

Computes hash of a DeployAccount transaction.

Parameters:
  • version – The transaction’s version.

  • contract_address – Contract address.

  • class_hash – The class hash of the contract.

  • constructor_calldata – Constructor calldata of the contract.

  • max_fee – The transaction’s maximum fee.

  • nonce – Nonce of the transaction.

  • salt – The contract’s address salt.

  • chain_id – The network’s chain ID.

Returns:

Hash of the transaction.

starknet_py.hash.transaction.compute_deploy_account_v3_transaction_hash(*, class_hash: int, constructor_calldata: List[int], contract_address_salt: int, common_fields: CommonTransactionV3Fields) int#

Computes hash of a DeployAccount transaction version 3.

Parameters:
  • class_hash – The class hash of the contract.

  • constructor_calldata – Constructor calldata of the contract.

  • contract_address_salt – A random salt that determines the account address.

  • common_fields – Common fields for V3 transactions.

Returns:

Hash of the transaction.

starknet_py.hash.transaction.compute_declare_transaction_hash(contract_class: ContractClass, chain_id: int, sender_address: int, max_fee: int, version: int, nonce: int) int#

Computes hash of a Declare transaction.

Parameters:
  • contract_class – ContractClass of the contract.

  • chain_id – The network’s chain ID.

  • sender_address – Address which sends the transaction.

  • max_fee – The transaction’s maximum fee.

  • version – The transaction’s version.

  • nonce – Nonce of the transaction.

Returns:

Hash of the transaction.

starknet_py.hash.transaction.compute_declare_v2_transaction_hash(*, contract_class: SierraContractClass | None = None, class_hash: int | None = None, compiled_class_hash: int, chain_id: int, sender_address: int, max_fee: int, version: int, nonce: int) int#

Computes class hash of a Declare transaction version 2.

Parameters:
  • contract_class – SierraContractClass of the contract.

  • class_hash – Class hash of the contract.

  • compiled_class_hash – Compiled class hash of the program.

  • chain_id – The network’s chain ID.

  • sender_address – Address which sends the transaction.

  • max_fee – The transaction’s maximum fee.

  • version – The transaction’s version.

  • nonce – Nonce of the transaction.

Returns:

Hash of the transaction.

starknet_py.hash.transaction.compute_declare_v3_transaction_hash(*, contract_class: SierraContractClass | None = None, class_hash: int | None = None, account_deployment_data: List[int], compiled_class_hash: int, common_fields: CommonTransactionV3Fields) int#

Computes class hash of a Declare transaction version 3.

Parameters:
  • contract_class – SierraContractClass of the contract.

  • class_hash – Class hash of the contract.

  • account_deployment_data – This will contain the class_hash and the calldata needed for the constructor. Currently, this value is always empty.

  • compiled_class_hash – Compiled class hash of the program.

  • common_fields – Common fields for V3 transactions.

Returns:

Hash of the transaction.

Class hash#

starknet_py.hash.class_hash.compute_class_hash(contract_class: ContractClass) int#

Calculate class hash of a ContractClass.

Sierra class hash#

starknet_py.hash.sierra_class_hash.compute_sierra_class_hash(sierra_contract_class: SierraContractClass) int#

Calculate class hash of a SierraContractClass.

Casm class hash#

starknet_py.hash.casm_class_hash.compute_casm_class_hash(casm_contract_class: CasmClass) int#

Calculate class hash of a CasmClass.

starknet_py.hash.casm_class_hash.create_bytecode_segment_structure(bytecode: List[int], bytecode_segment_lengths: int | List[Any], visited_pcs: Sequence[int] | None) BytecodeSegmentStructure#

Creates a BytecodeSegmentStructure instance from the given bytecode and bytecode_segment_lengths.

Address#

starknet_py.hash.address.compute_address(*, class_hash: int, constructor_calldata: Sequence[int], salt: int, deployer_address: int = 0) int#

Computes the contract address in the Starknet network - a unique identifier of the contract.

Parameters:
  • class_hash – class hash of the contract

  • constructor_calldata – calldata for the contract constructor

  • salt – salt used to calculate contract address

  • deployer_address – address of the deployer (if not provided default 0 is used)

Returns:

Contract’s address

starknet_py.hash.address.get_checksum_address(address: str) str#

Outputs formatted checksum address.

Follows implementation of starknet.js. It is not compatible with EIP55 as it treats hex string as encoded number, instead of encoding it as ASCII string.

Parameters:

address – Address to encode

Returns:

Checksum address

starknet_py.hash.address.is_checksum_address(address: str) bool#

Checks if provided string is in a checksum address format.

Selector#

starknet_py.hash.selector.get_selector_from_name(func_name: str) int#

Returns the selector of a contract’s function name.

Storage#

starknet_py.hash.storage.get_storage_var_address(var_name: str, *args: int) int#

Returns the storage address of a Starknet storage variable given its name and arguments.

Pedersen hash#

starknet_py.hash.utils.pedersen_hash(left: int, right: int) int#

One of two hash functions (along with _starknet_keccak) used throughout Starknet.

Private to stark key#

starknet_py.hash.utils.private_to_stark_key(priv_key: int) int#

Deduces the public key given a private key.

Message signature#

starknet_py.hash.utils.message_signature(msg_hash: int, priv_key: int, seed: int | None = 32) Tuple[int, int]#

Signs the message with private key.

Verify message signature#

starknet_py.hash.utils.verify_message_signature(msg_hash: int, signature: List[int], public_key: int) bool#

Verifies ECDSA signature of a given message hash with a given public key. Returns true if public_key signs the message.