sui_move_natives_v1/
address.rsuse crate::NativesCostTable;
use move_binary_format::errors::PartialVMResult;
use move_core_types::{account_address::AccountAddress, gas_algebra::InternalGas, u256::U256};
use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext};
use move_vm_types::{
loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value,
};
use smallvec::smallvec;
use std::collections::VecDeque;
const E_ADDRESS_PARSE_ERROR: u64 = 0;
#[derive(Clone)]
pub struct AddressFromBytesCostParams {
pub address_from_bytes_cost_base: InternalGas,
}
pub fn from_bytes(
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.len() == 1);
let address_from_bytes_cost_params = context
.extensions_mut()
.get::<NativesCostTable>()
.address_from_bytes_cost_params
.clone();
native_charge_gas_early_exit!(
context,
address_from_bytes_cost_params.address_from_bytes_cost_base
);
let addr_bytes = pop_arg!(args, Vec<u8>);
let cost = context.gas_used();
Ok(match AccountAddress::from_bytes(addr_bytes) {
Ok(addr) => NativeResult::ok(cost, smallvec![Value::address(addr)]),
Err(_) => NativeResult::err(cost, E_ADDRESS_PARSE_ERROR),
})
}
#[derive(Clone)]
pub struct AddressToU256CostParams {
pub address_to_u256_cost_base: InternalGas,
}
pub fn to_u256(
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.len() == 1);
let address_to_u256_cost_params = context
.extensions_mut()
.get::<NativesCostTable>()
.address_to_u256_cost_params
.clone();
native_charge_gas_early_exit!(
context,
address_to_u256_cost_params.address_to_u256_cost_base
);
let addr = pop_arg!(args, AccountAddress);
let mut addr_bytes_le = addr.to_vec();
addr_bytes_le.reverse();
let u256_val = Value::u256(U256::from_le_bytes(&addr_bytes_le.try_into().unwrap()));
Ok(NativeResult::ok(context.gas_used(), smallvec![u256_val]))
}
#[derive(Clone)]
pub struct AddressFromU256CostParams {
pub address_from_u256_cost_base: InternalGas,
}
pub fn from_u256(
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.len() == 1);
let address_from_u256_cost_params = context
.extensions_mut()
.get::<NativesCostTable>()
.address_from_u256_cost_params
.clone();
native_charge_gas_early_exit!(
context,
address_from_u256_cost_params.address_from_u256_cost_base
);
let u256 = pop_arg!(args, U256);
let mut u256_bytes = u256.to_le_bytes().to_vec();
u256_bytes.reverse();
let addr_val = Value::address(AccountAddress::from_bytes(&u256_bytes[..]).unwrap());
Ok(NativeResult::ok(context.gas_used(), smallvec![addr_val]))
}