Skip to content

transaction_context - [mainnet]

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

Constants

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;

Structs

AUID

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

struct AUID has drop, store
Fields
unique_address: address

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>>

MultisigPayload

Represents the multisig payload.

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

Functions

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>;

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()
}

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;

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()
}

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>;

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()
}
}

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
}

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()
}

sender_internal

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

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()
}

secondary_signers_internal

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

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()
}

gas_payer_internal

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

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()
}

max_gas_amount_internal

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

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()
}

gas_unit_price_internal

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

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()
}

chain_id_internal

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

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()
}

entry_function_payload_internal

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

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
}

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_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
}

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
}

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
}

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()
}

multisig_payload_internal

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

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
}

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
}

Specification

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>;

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;

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;

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();

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>;

auid_address

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

sender_internal

fun sender_internal(): address
pragma opaque;

secondary_signers_internal

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

gas_payer_internal

fun gas_payer_internal(): address
pragma opaque;

max_gas_amount_internal

fun max_gas_amount_internal(): u64
pragma opaque;

gas_unit_price_internal

fun gas_unit_price_internal(): u64
pragma opaque;

chain_id_internal

fun chain_id_internal(): u8
pragma opaque;

entry_function_payload_internal

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

multisig_payload_internal

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