Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module 0x1::transaction_context

use 0x1::error;
use 0x1::features;
use 0x1::option;
use 0x1::string;
use 0x1::timestamp;

Struct AUID

A wrapper denoting aptos unique identifer (AUID) for storing an address

struct AUID has drop, store
Fields
unique_address: address

Struct EntryFunctionPayload

Represents the entry function payload.

struct EntryFunctionPayload has copy, drop
Fields
account_address: address
module_name: string::String
function_name: string::String
ty_args_names: vector<string::String>
args: vector<vector<u8>>

Struct MultisigPayload

Represents the multisig payload.

struct MultisigPayload has copy, drop
Fields
multisig_address: address
entry_function_payload: option::Option<transaction_context::EntryFunctionPayload>

Constants

The monotonically increasing counter has overflowed (too many calls in a single session).

const EMONOTONICALLY_INCREASING_COUNTER_OVERFLOW: u64 = 4;

The transaction context extension feature is not enabled.

const ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED: u64 = 2;

Transaction context is only available in the user transaction prologue, execution, or epilogue phases.

const ETRANSACTION_CONTEXT_NOT_AVAILABLE: u64 = 1;

Transaction index is not avaulable in this execution context.

const ETRANSACTION_INDEX_NOT_AVAILABLE: u64 = 5;

Function get_txn_hash

Returns the transaction hash of the current transaction.

fun get_txn_hash(): vector<u8>
Implementation
native fun get_txn_hash(): vector<u8>;

Function get_transaction_hash

Returns the transaction hash of the current transaction. Internally calls the private function get_txn_hash. This function is created for to feature gate the get_txn_hash function.

public fun get_transaction_hash(): vector<u8>
Implementation
public fun get_transaction_hash(): vector<u8> {
    get_txn_hash()
}

Function generate_unique_address

Returns a universally unique identifier (of type address) generated by hashing the transaction hash of this transaction and a sequence number specific to this transaction. This function can be called any number of times inside a single transaction. Each such call increments the sequence number and generates a new unique address. Uses Scheme in types/src/transaction/authenticator.rs for domain separation from other ways of generating unique addresses.

fun generate_unique_address(): address
Implementation
native fun generate_unique_address(): address;

Function generate_auid_address

Returns a aptos unique identifier. Internally calls the private function generate_unique_address. This function is created for to feature gate the generate_unique_address function.

public fun generate_auid_address(): address
Implementation
public fun generate_auid_address(): address {
    generate_unique_address()
}

Function get_script_hash

Returns the script hash of the current entry function.

public fun get_script_hash(): vector<u8>
Implementation
public native fun get_script_hash(): vector<u8>;

Function generate_auid

This method runs generate_unique_address native function and returns the generated unique address wrapped in the AUID class.

public fun generate_auid(): transaction_context::AUID
Implementation
public fun generate_auid(): AUID {
    return AUID {
        unique_address: generate_unique_address()
    }
}

Function auid_address

Returns the unique address wrapped in the given AUID struct.

public fun auid_address(auid: &transaction_context::AUID): address
Implementation
public fun auid_address(auid: &AUID): address {
    auid.unique_address
}

Function sender

Returns the sender’s address for the current transaction. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun sender(): address
Implementation
public fun sender(): address {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    sender_internal()
}

Function sender_internal

fun sender_internal(): address
Implementation
native fun sender_internal(): address;

Function secondary_signers

Returns the list of the secondary signers for the current transaction. If the current transaction has no secondary signers, this function returns an empty vector. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun secondary_signers(): vector<address>
Implementation
public fun secondary_signers(): vector<address> {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    secondary_signers_internal()
}

Function secondary_signers_internal

fun secondary_signers_internal(): vector<address>
Implementation
native fun secondary_signers_internal(): vector<address>;

Function gas_payer

Returns the gas payer address for the current transaction. It is either the sender’s address if no separate gas fee payer is specified for the current transaction, or the address of the separate gas fee payer if one is specified. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun gas_payer(): address
Implementation
public fun gas_payer(): address {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    gas_payer_internal()
}

Function gas_payer_internal

fun gas_payer_internal(): address
Implementation
native fun gas_payer_internal(): address;

Function max_gas_amount

Returns the max gas amount in units which is specified for the current transaction. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun max_gas_amount(): u64
Implementation
public fun max_gas_amount(): u64 {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    max_gas_amount_internal()
}

Function max_gas_amount_internal

fun max_gas_amount_internal(): u64
Implementation
native fun max_gas_amount_internal(): u64;

Function gas_unit_price

Returns the gas unit price in Octas which is specified for the current transaction. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun gas_unit_price(): u64
Implementation
public fun gas_unit_price(): u64 {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    gas_unit_price_internal()
}

Function gas_unit_price_internal

fun gas_unit_price_internal(): u64
Implementation
native fun gas_unit_price_internal(): u64;

Function chain_id

Returns the chain ID specified for the current transaction. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun chain_id(): u8
Implementation
public fun chain_id(): u8 {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    chain_id_internal()
}

Function chain_id_internal

fun chain_id_internal(): u8
Implementation
native fun chain_id_internal(): u8;

Function entry_function_payload

Returns the entry function payload if the current transaction has such a payload. Otherwise, return None. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun entry_function_payload(): option::Option<transaction_context::EntryFunctionPayload>
Implementation
public fun entry_function_payload(): Option<EntryFunctionPayload> {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    entry_function_payload_internal()
}

Function entry_function_payload_internal

fun entry_function_payload_internal(): option::Option<transaction_context::EntryFunctionPayload>
Implementation
native fun entry_function_payload_internal(): Option<EntryFunctionPayload>;

Function account_address

Returns the account address of the entry function payload.

public fun account_address(payload: &transaction_context::EntryFunctionPayload): address
Implementation
public fun account_address(payload: &EntryFunctionPayload): address {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    payload.account_address
}

Function module_name

Returns the module name of the entry function payload.

public fun module_name(payload: &transaction_context::EntryFunctionPayload): string::String
Implementation
public fun module_name(payload: &EntryFunctionPayload): String {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    payload.module_name
}

Function function_name

Returns the function name of the entry function payload.

public fun function_name(payload: &transaction_context::EntryFunctionPayload): string::String
Implementation
public fun function_name(payload: &EntryFunctionPayload): String {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    payload.function_name
}

Function type_arg_names

Returns the type arguments names of the entry function payload.

public fun type_arg_names(payload: &transaction_context::EntryFunctionPayload): vector<string::String>
Implementation
public fun type_arg_names(payload: &EntryFunctionPayload): vector<String> {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    payload.ty_args_names
}

Function args

Returns the arguments of the entry function payload.

public fun args(payload: &transaction_context::EntryFunctionPayload): vector<vector<u8>>
Implementation
public fun args(payload: &EntryFunctionPayload): vector<vector<u8>> {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    payload.args
}

Function multisig_payload

Returns the multisig payload if the current transaction has such a payload. Otherwise, return None. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun multisig_payload(): option::Option<transaction_context::MultisigPayload>
Implementation
public fun multisig_payload(): Option<MultisigPayload> {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    multisig_payload_internal()
}

Function multisig_payload_internal

fun multisig_payload_internal(): option::Option<transaction_context::MultisigPayload>
Implementation
native fun multisig_payload_internal(): Option<MultisigPayload>;

Function multisig_address

Returns the multisig account address of the multisig payload.

public fun multisig_address(payload: &transaction_context::MultisigPayload): address
Implementation
public fun multisig_address(payload: &MultisigPayload): address {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    payload.multisig_address
}

Function inner_entry_function_payload

Returns the inner entry function payload of the multisig payload.

public fun inner_entry_function_payload(payload: &transaction_context::MultisigPayload): option::Option<transaction_context::EntryFunctionPayload>
Implementation
public fun inner_entry_function_payload(payload: &MultisigPayload): Option<EntryFunctionPayload> {
    assert!(features::transaction_context_extension_enabled(), error::invalid_state(ETRANSACTION_CONTEXT_EXTENSION_NOT_ENABLED));
    payload.entry_function_payload
}

Function is_encrypted_txn

Returns whether the current transaction is an encrypted transaction. This function aborts if called outside of the transaction prologue, execution, or epilogue phases.

public fun is_encrypted_txn(): bool
Implementation
public fun is_encrypted_txn(): bool {
    is_encrypted_txn_internal()
}

Function is_encrypted_txn_internal

fun is_encrypted_txn_internal(): bool
Implementation
native fun is_encrypted_txn_internal(): bool;

Function monotonically_increasing_counter

Returns a monotonically increasing counter value that combines timestamp, transaction index, session counter, and local counter into a 128-bit value. Format: <reserved_byte (8 bits)> || timestamp_us (64 bits) || transaction_index (32 bits) || session_counter (8 bits) || local_counter (16 bits) The function aborts if the local counter overflows (after 65535 calls in a single session). When compiled for testing, this function bypasses feature checks and returns a simplified counter value.

public fun monotonically_increasing_counter(): u128
Implementation
public fun monotonically_increasing_counter(): u128 {
    if (__COMPILE_FOR_TESTING__) {
        monotonically_increasing_counter_internal_for_test_only()
    } else {
        monotonically_increasing_counter_internal(timestamp::now_microseconds())
    }
}

Function monotonically_increasing_counter_internal

fun monotonically_increasing_counter_internal(timestamp_us: u64): u128
Implementation
native fun monotonically_increasing_counter_internal(timestamp_us: u64): u128;

Function monotonically_increasing_counter_internal_for_test_only

Test-only version of monotonically_increasing_counter that returns increasing values without requiring a user transaction context. This allows unit tests to verify the monotonically increasing behavior.

fun monotonically_increasing_counter_internal_for_test_only(): u128
Implementation
native fun monotonically_increasing_counter_internal_for_test_only(): u128;

Specification

Function get_txn_hash

fun get_txn_hash(): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures result == spec_get_txn_hash();

fun spec_get_txn_hash(): vector<u8>;

Function get_transaction_hash

public fun get_transaction_hash(): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures result == spec_get_txn_hash();
// This enforces high-level requirement 1:
ensures [abstract] len(result) == 32;

Function generate_unique_address

fun generate_unique_address(): address
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_generate_unique_address();

fun spec_generate_unique_address(): address;

Function generate_auid_address

public fun generate_auid_address(): address
pragma opaque;
aborts_if [abstract] false;
// This enforces high-level requirement 3:
ensures [abstract] result == spec_generate_unique_address();

Function get_script_hash

public fun get_script_hash(): vector<u8>

High-level Requirements

No.RequirementCriticalityImplementationEnforcement
1 Fetching the transaction hash should return a vector with 32 bytes. Medium The get_transaction_hash function calls the native function get_txn_hash, which fetches the NativeTransactionContext struct and returns the txn_hash field. Audited that the native function returns the txn hash, whose size is 32 bytes. This has been modeled as the abstract postcondition that the returned vector is of length 32. Formally verified via get_txn_hash.
2 Fetching the unique address should never abort. Low The function auid_address returns the unique address from a supplied AUID resource. Formally verified via auid_address.
3 Generating the unique address should return a vector with 32 bytes. Medium The generate_auid_address function checks calls the native function generate_unique_address which fetches the NativeTransactionContext struct, increments the auid_counter by one, and then creates a new authentication key from a preimage, which is then returned. Audited that the native function returns an address, and the length of an address is 32 bytes. This has been modeled as the abstract postcondition that the returned vector is of length 32. Formally verified via generate_auid_address.
4 Fetching the script hash of the current entry function should never fail and should return a vector with 32 bytes if the transaction payload is a script, otherwise an empty vector. Low The native function get_script_hash returns the NativeTransactionContext.script_hash field. Audited that the native function holds the required property. This has been modeled as the abstract spec. Formally verified via get_script_hash.

Module-level Specification

pragma opaque;
// This enforces high-level requirement 4:
aborts_if [abstract] false;
ensures [abstract] result == spec_get_script_hash();
ensures [abstract] len(result) == 32;

fun spec_get_script_hash(): vector<u8>;

Function auid_address

public fun auid_address(auid: &transaction_context::AUID): address
// This enforces high-level requirement 2:
aborts_if false;

Function sender_internal

fun sender_internal(): address
pragma opaque;

Function secondary_signers_internal

fun secondary_signers_internal(): vector<address>
pragma opaque;

Function gas_payer_internal

fun gas_payer_internal(): address
pragma opaque;

Function max_gas_amount_internal

fun max_gas_amount_internal(): u64
pragma opaque;

Function gas_unit_price_internal

fun gas_unit_price_internal(): u64
pragma opaque;

Function chain_id_internal

fun chain_id_internal(): u8
pragma opaque;

Function entry_function_payload_internal

fun entry_function_payload_internal(): option::Option<transaction_context::EntryFunctionPayload>
pragma opaque;

Function multisig_payload_internal

fun multisig_payload_internal(): option::Option<transaction_context::MultisigPayload>
pragma opaque;

Function is_encrypted_txn_internal

fun is_encrypted_txn_internal(): bool
pragma opaque;

Function monotonically_increasing_counter_internal

fun monotonically_increasing_counter_internal(timestamp_us: u64): u128
pragma opaque;

Function monotonically_increasing_counter_internal_for_test_only

fun monotonically_increasing_counter_internal_for_test_only(): u128
pragma opaque;