account.rs
Overview
This file provides a utility function to determine the routing information of an account within the system. Specifically, it defines a function that produces an AccountRouting tuple, composed of a decentralized application (DAPP) identifier and an account address, based on input parameters representing an account's address and an optional DAPP identifier.
The primary purpose of this file is to encapsulate the logic required to extract or construct routing data for an account, which is essential for directing transactions, messages, or interactions correctly within the broader distributed application environment.
Function Details
get_account_routing_for_account
pub(crate) fn get_account_routing_for_account(
account_address: SliceData,
dapp_id: Option<UInt256>,
) -> AccountRouting
Purpose
This function returns the routing information of an account as an AccountRouting tuple. The routing consists of two parts:
The DAPP identifier associated with the account (if any).
The account's address.
Parameters
account_address: SliceData
A binary slice representing the account address. This is expected to be non-Noneand is cast to theAccountIdtype internally.dapp_id: Option<UInt256>
An optional 256-bit unsigned integer representing the DAPP identifier. IfNone, the function treats the account address itself as the DAPP identifier.
Returns
AccountRouting
A tuple combining the DAPP identifier (or its substitute) and the account address, wrapped in theAccountRoutingtype. This type is imported from the crate'stypesmodule and encapsulates routing details.
Usage Example
use tvm_types::{SliceData, UInt256};
use crate::types::AccountRouting;
let account_addr: SliceData = /* obtain valid SliceData */;
let maybe_dapp_id: Option<UInt256> = Some(UInt256::from_bytes(&[0u8; 32]));
let routing = get_account_routing_for_account(account_addr, maybe_dapp_id);
// routing now contains the DAPP identifier and the account address as a routing tuple.
Implementation Details
If the
dapp_idis provided, it is wrapped first intoAccountAddressand then intoDAppIdentifier.If
dapp_idisNone, the function treats theaccount_addressitself as the DAPP identifier. This is done by mapping the optional DAPP id, and if absent, effectively falling back to the account address.The function panics if the passed
account_addressisNone(implicit by the function's expectation and casting).The conversion
(account_address as AccountId).into()converts the rawSliceDatainto a strongly typed account identifier, ensuring type safety for downstream usage.
Types and Modules Interaction
tvm_typescrate: Provides core types likeAccountId,SliceData, andUInt256used to represent accounts and identifiers at a low level.crate::typesmodule: Defines domain-specific abstractions such asAccountAddress,DAppIdentifier, andAccountRouting. These are wrappers or newtypes that add semantic meaning and type safety around raw data types fromtvm_types.
The file acts as a bridge between raw account data and the structured routing information required by other components of the system. It abstracts the logic of how a DAPP identifier is resolved or defaulted and how routing tuples are constructed.
Mermaid Class Diagram
classDiagram
class get_account_routing_for_account {
+account_address: SliceData
+dapp_id: Option<UInt256>
+AccountRouting
}
class AccountRouting {
<<type>>
}
class AccountAddress {
<<wrapper>>
}
class DAppIdentifier {
<<wrapper>>
}
get_account_routing_for_account --> AccountRouting
get_account_routing_for_account --> AccountAddress
get_account_routing_for_account --> DAppIdentifier
This diagram illustrates the function and its relationships with the key types involved in producing the routing data. The function depends on these types to wrap and convert the inputs into the final routing tuple.
Interaction with the System
This function is intended for internal use within the crate (not public API), as indicated by
pub(crate).It likely supports higher-level modules that manage account routing, message dispatch, or address resolution in decentralized applications.
By standardizing how routing information is derived, it promotes consistent handling of accounts and their associated DAPP contexts across the system.
It interacts indirectly with modules that consume
AccountRoutingtuples for routing logic, as well as modules that manage or validate accounts and DAPP identifiers.
For more details on account addressing and routing, see the related topics on Account Addressing and Routing Mechanisms.