pub struct EnvAccess<'a, E> { /* private fields */ }Expand description
The API behind the self.env() and Self::env() syntax in ink!.
This allows ink! messages to make use of the environment efficiently and user-friendly while also maintaining access invariants.
Implementations§
Source§impl<E> EnvAccess<'_, E>where
E: Environment,
impl<E> EnvAccess<'_, E>where
E: Environment,
Sourcepub fn gas_limit(self) -> u64
pub fn gas_limit(self) -> u64
Returns the block’s ref_time limit, akin to the EVM
GASLIMIT opcode.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_limit(&self) -> u64 {
self.env().gas_limit()
}
}
}§Note
For more details visit: [ink_env::gas_limit]
Sourcepub fn gas_price(self) -> u64
pub fn gas_price(self) -> u64
Returns the price per ref_time, akin to the EVM
GASPRICE opcode.
See https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink for more information.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_gas_price(&self) -> u64 {
self.env().gas_price()
}
}
}§Note
For more details visit: [ink_env::gas_price]
Sourcepub fn gas_left(self) -> u64
pub fn gas_left(self) -> u64
Returns the amount of gas left.
This is the ref_time left.
See https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink for more information.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_gas_left(&self) -> u64 {
self.env().gas_left()
}
}
}§Note
For more details visit: [ink_env::gas_left]
Sourcepub fn call_data_size(self) -> u64
pub fn call_data_size(self) -> u64
Returns the total size of the contract call input data. This is akin to the EVM CALLDATASIZE opcode.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_call_data_size(&self) -> u64 {
self.env().call_data_size()
}
}
}§Note
For more details visit: [ink_env::call_data_size]
Sourcepub fn return_data_size(self) -> u64
pub fn return_data_size(self) -> u64
Returns the size of the returned data of the last contract call or instantiation. This is akin to the EVM RETURNDATASIZE opcode.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_return_data_size(&self) -> u64 {
self.env().return_data_size()
}
}
}§Note
For more details visit: [ink_env::return_data_size]
Sourcepub fn chain_id(self) -> U256
pub fn chain_id(self) -> U256
Returns the EIP-155 chain ID, akin to the EVM CHAINID opcode.
§Example
#[ink::contract]
mod my_contract {
use ink::U256;
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_chain_id(&self) -> U256 {
self.env().chain_id()
}
}
}§Note
For more details visit: [ink_env::chain_id]
Sourcepub fn balance_of(self, addr: Address) -> U256
pub fn balance_of(self, addr: Address) -> U256
Returns the reducible native balance of the supplied address. This is akin to the EVM BALANCE opcode.
§Example
#[ink::contract]
mod my_contract {
use ink::U256;
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_my_balance(&self) -> U256 {
let caller = self.env().caller();
self.env().balance_of(caller)
}
}
}§Note
For more details visit: [ink_env::balance_of]
Sourcepub fn base_fee(self) -> U256
pub fn base_fee(self) -> U256
Returns the base fee. This is akin to the EVM BASEFEE opcode.
§Example
#[ink::contract]
mod my_contract {
use ink::U256;
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_base_fee(&self) -> U256 {
self.env().base_fee()
}
}
}§Note
For more details visit: [ink_env::base_fee]
Sourcepub fn origin(self) -> Address
pub fn origin(self) -> Address
Returns the origin address (initator of the call stack). This is akin to the EVM ORIGIN opcode.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_origin(&self) -> Address {
self.env().origin()
}
}
}§Note
For more details visit: [ink_env::origin]
Sourcepub fn code_size(self, addr: Address) -> u64
pub fn code_size(self, addr: Address) -> u64
Returns the code size for a specified contract address. This is akin to the EVM CODESIZE opcode.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_code_size_of_this_contract(&self) -> u64 {
let this_addr = self.env().address();
self.env().code_size(this_addr)
}
}
}§Note
If addr is not a contract the output will be zero.
For more details visit: [ink_env::code_size]
Sourcepub fn block_hash(self, block_number: E::BlockNumber) -> H256
pub fn block_hash(self, block_number: E::BlockNumber) -> H256
Returns the block hash of the given block number. This is akin to the EVM BLOCKHASH opcode.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_block_hash(
&self,
block_number: BlockNumber,
) -> ink_primitives::H256 {
self.env().block_hash(block_number)
}
}
}§Note
For more details visit: [ink_env::block_hash]
Returns the current block author. This is akin to the EVM COINBASE opcode.
§Example
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract;
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn get_block_author(&self) -> Address {
self.env().block_author()
}
}
}§Note
For more details visit: [ink_env::block_author]
Sourcepub fn transferred_value(self) -> U256
pub fn transferred_value(self) -> U256
Returns the transferred value for the contract execution.
§Example
/// Allows funding the contract. Prints a debug message with the transferred value.
#[ink(message, payable)]
pub fn fund(&mut self) {
let caller = self.env().caller();
let value = self.env().transferred_value();
}§Note
For more details visit: [ink_env::transferred_value]
Sourcepub fn weight_to_fee(self, gas: u64) -> U256
pub fn weight_to_fee(self, gas: u64) -> U256
Returns the price for the specified amount of gas.
§Example
#[ink(message)]
pub fn foo(&self) {}
// todo
// /// Returns a tuple of
// /// - the result of adding the `rhs` to the `lhs`
// /// - the gas costs of this addition operation
// /// - the price for the gas
// #[ink(message)]
// pub fn addition_gas_cost(&self, rhs: i32, lhs: i32) -> (i32, u64, Balance) {
// let before = self.env().gas_left();
// let result = rhs + lhs;
// let after = self.env().gas_left();
// let gas_used = after - before;
// let gas_cost = self.env().weight_to_fee(gas_used);
// (result, gas_used, gas_cost)
//}§Note
For more details visit: [ink_env::weight_to_fee]
Sourcepub fn block_timestamp(self) -> E::Timestamp
pub fn block_timestamp(self) -> E::Timestamp
Returns the timestamp of the current block.
§Example
#[ink::contract]
pub mod my_contract {
#[ink(storage)]
pub struct MyContract {
last_invocation: Timestamp,
}
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {
last_invocation: Self::env().block_timestamp(),
}
}
/// Records the last time the message was invoked.
#[ink(message)]
pub fn execute_me(&mut self) {
self.last_invocation = self.env().block_timestamp();
}
}
}§Note
The Substrate default for the timestamp type is the milliseconds since the Unix epoch. However, this is not guaranteed: the specific timestamp is defined by the chain environment on which this contract runs.
For more details visit: [ink_env::block_timestamp]
Sourcepub fn to_account_id(self, addr: Address) -> E::AccountId
pub fn to_account_id(self, addr: Address) -> E::AccountId
Retrieves the account id for a specified address.
§Example
#[ink::contract]
pub mod only_owner {
#[ink(storage)]
pub struct OnlyOwner {
owner: AccountId,
value: u32,
}
impl OnlyOwner {
#[ink(constructor)]
pub fn new(owner: AccountId) -> Self {
Self { owner, value: 0 }
}
/// Allows incrementing the contract's `value` only
/// for the owner.
///
/// The contract panics if the caller is not the owner.
#[ink(message)]
pub fn increment(&mut self) {
let caller = self.env().address();
let caller_acc = self.env().to_account_id(caller);
assert!(self.owner == caller_acc);
self.value = self.value + 1;
}
}
}§Note
For more details visit: [ink_env::to_account_id]
Sourcepub fn account_id(self) -> E::AccountId
pub fn account_id(self) -> E::AccountId
Returns the account ID of the executed contract.
§Example
#[ink::contract]
pub mod only_owner {
#[ink(storage)]
pub struct OnlyOwner {
owner: AccountId,
value: u32,
}
impl OnlyOwner {
#[ink(constructor)]
pub fn new() -> Self {
Self {
owner: Self::env().account_id(),
value: 0,
}
}
/// Allows incrementing the contract's `value` only
/// for the owner.
///
/// The contract panics if the caller is not the owner.
#[ink(message)]
pub fn increment(&mut self) {
let caller = self.env().account_id();
assert!(self.owner == caller);
self.value = self.value + 1;
}
}
}§Note
For more details visit: [ink_env::account_id]
Sourcepub fn address(self) -> Address
pub fn address(self) -> Address
Returns the address of the executed contract.
For more details visit: [ink_env::address]
Sourcepub fn block_number(self) -> E::BlockNumber
pub fn block_number(self) -> E::BlockNumber
Returns the current block number.
§Example
#[ink::contract]
pub mod my_contract {
#[ink(storage)]
pub struct MyContract {
last_invocation: BlockNumber,
}
impl MyContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {
last_invocation: Self::env().block_number(),
}
}
/// The function can be executed at most once every 100 blocks.
#[ink(message)]
pub fn execute_me(&mut self) {
let now = self.env().block_number();
assert!(now - self.last_invocation > 100);
self.last_invocation = now;
}
}
}§Note
For more details visit: [ink_env::block_number]
Sourcepub fn minimum_balance(self) -> U256
pub fn minimum_balance(self) -> U256
Sourcepub fn emit_event<Evt>(self, event: Evt)where
Evt: Event<DefaultAbi>,
pub fn emit_event<Evt>(self, event: Evt)where
Evt: Event<DefaultAbi>,
Sourcepub fn emit_event_ink<Evt>(self, event: Evt)where
Evt: Event<Ink>,
pub fn emit_event_ink<Evt>(self, event: Evt)where
Evt: Event<Ink>,
Emits an event using the ink! ABI encoding (i.e. with SCALE codec for event data encode/decode).
Sourcepub fn emit_event_sol<Evt>(self, event: Evt)where
Evt: Event<Sol>,
pub fn emit_event_sol<Evt>(self, event: Evt)where
Evt: Event<Sol>,
Emits an event using the Solidity ABI encoding.
Sourcepub fn instantiate_contract<ContractRef, Args, R, Abi>(
self,
params: &CreateParams<E, ContractRef, LimitParamsV2, Args, R, Abi>,
) -> Result<ConstructorResult<<R as ConstructorReturnType<ContractRef, Abi>>::Output>>where
ContractRef: FromAddr + ContractReverseReference,
<ContractRef as ContractReverseReference>::Type: ContractConstructorDecoder,
Args: EncodeArgsWith<Abi>,
R: ConstructorReturnType<ContractRef, Abi>,
pub fn instantiate_contract<ContractRef, Args, R, Abi>(
self,
params: &CreateParams<E, ContractRef, LimitParamsV2, Args, R, Abi>,
) -> Result<ConstructorResult<<R as ConstructorReturnType<ContractRef, Abi>>::Output>>where
ContractRef: FromAddr + ContractReverseReference,
<ContractRef as ContractReverseReference>::Type: ContractConstructorDecoder,
Args: EncodeArgsWith<Abi>,
R: ConstructorReturnType<ContractRef, Abi>,
Instantiates another contract using the supplied code hash.
Invokes the instantiate_v2 host function which allows passing all weight and
storage limit parameters.
§Example
use ink::env::{
call::{
build_create,
ExecutionInput,
Selector,
},
DefaultEnvironment,
};
use other_contract::OtherContractRef;
/// Instantiates another contract.
#[ink(message)]
pub fn instantiate_contract(&self) -> MyContractRef {
let create_params = build_create::<OtherContractRef>()
.code_hash(ink::H256::from([0x42; 32]))
.ref_time_limit(500_000_000)
.proof_size_limit(100_000)
.storage_deposit_limit(ink::U256::from(500_000_000_000u64))
.endowment(25.into())
.exec_input(
ExecutionInput::new(Selector::new(ink::selector_bytes!(Abi::Ink, "new")))
.push_arg(42)
.push_arg(true)
.push_arg(&[0x10u8; 32]),
)
.salt_bytes(Some([0x13; 32]))
.returns::<OtherContractRef>()
.params();
self.env()
.instantiate_contract(&create_params)
.unwrap_or_else(|error| {
panic!(
"Received an error from `pallet-revive` while instantiating: {error:?}"
)
})
.unwrap_or_else(|error| {
panic!("Received a `LangError` while instantiating: {error:?}")
})
}See our delegator example
for a complete contract example.
§Note
For more details visit: [ink_env::instantiate_contract]
Sourcepub fn invoke_contract<Args, R, Abi>(
self,
params: &CallParams<E, Call, Args, R, Abi>,
) -> Result<MessageResult<R>>where
Args: EncodeArgsWith<Abi>,
R: DecodeMessageResult<Abi>,
pub fn invoke_contract<Args, R, Abi>(
self,
params: &CallParams<E, Call, Args, R, Abi>,
) -> Result<MessageResult<R>>where
Args: EncodeArgsWith<Abi>,
R: DecodeMessageResult<Abi>,
Invokes a contract message and returns its result.
§Example
use ink::env::{
DefaultEnvironment,
call::{
ExecutionInput,
Selector,
build_call,
},
};
/// Invokes a contract message and fetches the result.
#[ink(message)]
pub fn invoke_contract(&self) -> i32 {
let call_params = build_call::<DefaultEnvironment>()
.call(ink::Address::from([0x42; 20]))
.ref_time_limit(500_000_000)
.proof_size_limit(100_000)
.storage_deposit_limit(1_000_000_000.into())
.exec_input(
ExecutionInput::new(Selector::new([0xCA, 0xFE, 0xBA, 0xBE]))
.push_arg(42u8)
.push_arg(true)
.push_arg(&[0x10u8; 32]),
)
.returns::<i32>()
.params();
self.env()
.invoke_contract(&call_params)
.unwrap_or_else(|env_err| {
panic!("Received an error from the Environment: {:?}", env_err)
})
.unwrap_or_else(|lang_err| panic!("Received a `LangError`: {:?}", lang_err))
}§Note
For more details visit: [ink_env::invoke_contract]
Sourcepub fn invoke_contract_delegate<Args, R, Abi>(
self,
params: &CallParams<E, DelegateCall, Args, R, Abi>,
) -> Result<MessageResult<R>>where
Args: EncodeArgsWith<Abi>,
R: DecodeMessageResult<Abi>,
pub fn invoke_contract_delegate<Args, R, Abi>(
self,
params: &CallParams<E, DelegateCall, Args, R, Abi>,
) -> Result<MessageResult<R>>where
Args: EncodeArgsWith<Abi>,
R: DecodeMessageResult<Abi>,
Invokes in delegate manner a code message and returns its result.
§Example
use ink::env::{
DefaultEnvironment,
call::{
DelegateCall,
ExecutionInput,
Selector,
build_call,
utils::ReturnType,
},
};
use ink_primitives::Clear;
/// Invokes in delegate manner a contract message and fetches the result.
#[ink(message)]
pub fn invoke_contract_delegate(&self) -> i32 {
let call_params = build_call::<DefaultEnvironment>()
.call_type(DelegateCall::new(ink::Address::zero()))
.exec_input(
ExecutionInput::new(Selector::new([0xCA, 0xFE, 0xBA, 0xBE]))
.push_arg(42u8)
.push_arg(true)
.push_arg(&[0x10u8; 32]),
)
.returns::<i32>()
.params();
self.env()
.invoke_contract_delegate(&call_params)
.unwrap_or_else(|env_err| {
panic!("Received an error from the Environment: {:?}", env_err)
})
.unwrap_or_else(|lang_err| panic!("Received a `LangError`: {:?}", lang_err))
}§Note
For more details visit: [ink_env::invoke_contract_delegate]
Sourcepub fn terminate_contract(self, beneficiary: Address) -> !
pub fn terminate_contract(self, beneficiary: Address) -> !
Terminates the existence of a contract.
§Example
/// Terminates with the caller as beneficiary.
#[ink(message)]
pub fn terminate_me(&mut self) {
// todo check this example. if caller returns origin it's no longer possible.
self.env().terminate_contract(self.env().caller());
}§Note
For more details visit: [ink_env::terminate_contract]
pub fn transfer(self, destination: Address, value: U256) -> Result<()>
Sourcepub fn hash_bytes<H>(self, input: &[u8]) -> <H as HashOutput>::Typewhere
H: CryptoHash,
pub fn hash_bytes<H>(self, input: &[u8]) -> <H as HashOutput>::Typewhere
H: CryptoHash,
Computes the hash of the given bytes using the cryptographic hash H.
§Example
use ink_env::hash::{
HashOutput,
Sha2x256,
};
let input: &[u8] = &[13, 14, 15];
let mut output = <Sha2x256 as HashOutput>::Type::default(); // 256-bit buffer
let hash = ink_env::hash_bytes::<Sha2x256>(input, &mut output);§Note
For more details visit: [ink_env::hash_bytes]
Sourcepub fn hash_encoded<H, V>(self, value: &V) -> <H as HashOutput>::Typewhere
H: CryptoHash,
V: Encode,
pub fn hash_encoded<H, V>(self, value: &V) -> <H as HashOutput>::Typewhere
H: CryptoHash,
V: Encode,
Computes the hash of the given SCALE encoded value using the cryptographic hash
H.
§Example
use ink_env::hash::{
HashOutput,
Sha2x256,
};
let encodable = (42, "foo", true); // Implements `scale::Encode`
let mut output = <Sha2x256 as HashOutput>::Type::default(); // 256-bit buffer
ink_env::hash_encoded::<Sha2x256, _>(&encodable, &mut output);
const EXPECTED: [u8; 32] = [
243, 242, 58, 110, 205, 68, 100, 244, 187, 55, 188, 248, 29, 136, 145, 115, 186,
134, 14, 175, 178, 99, 183, 21, 4, 94, 92, 69, 199, 207, 241, 179,
];
assert_eq!(output, EXPECTED);§Note
For more details visit: [ink_env::hash_encoded]
Sourcepub fn bn128_add(self, x1: U256, y1: U256, x2: U256, y2: U256) -> (U256, U256)
pub fn bn128_add(self, x1: U256, y1: U256, x2: U256, y2: U256) -> (U256, U256)
Calls the bn128_add G1 addition precompile.
Inputs are affine G1 coordinates over Fq. Returns the resulting affine point or (0, 0) if the result is ∞ / invalid.
§Note
For more details visit: [ink_env::bn128_add]
Sourcepub fn bn128_mul(self, x1: U256, y1: U256, scalar: U256) -> (U256, U256)
pub fn bn128_mul(self, x1: U256, y1: U256, scalar: U256) -> (U256, U256)
Calls the bn128_mul G1 scalar-mul precompile.
Multiplies an affine G1 point (x1, y1) by a scalar ∈ Fr. Returns the resulting affine point or (0, 0) if the result is ∞ / invalid.
§Note
For more details visit: [ink_env::bn128_mul]
Sourcepub fn bn128_pairing(self, input: &[u8]) -> bool
pub fn bn128_pairing(self, input: &[u8]) -> bool
Calls the bn128_pairing precompile.
Input is the Solidity-ABI-encoded sequence of (G1, G2) pairs.
Returns true iff the product of pairings evaluates to the identity.
§Note
For more details visit: [ink_env::bn128_pairing]
Sourcepub fn ecdsa_recover(
self,
signature: &[u8; 65],
message_hash: &[u8; 32],
) -> Result<[u8; 33]>
pub fn ecdsa_recover( self, signature: &[u8; 65], message_hash: &[u8; 32], ) -> Result<[u8; 33]>
Recovers the compressed ECDSA public key for given signature and message_hash,
and stores the result in output.
§Example
/// Recovery from pre-defined signature and message hash
#[ink(message)]
pub fn ecdsa_recover(&self) {
const signature: [u8; 65] = [
195, 218, 227, 165, 226, 17, 25, 160, 37, 92, 142, 238, 4, 41, 244, 211, 18,
94, 131, 116, 231, 116, 255, 164, 252, 248, 85, 233, 173, 225, 26, 185, 119,
235, 137, 35, 204, 251, 134, 131, 186, 215, 76, 112, 17, 192, 114, 243, 102,
166, 176, 140, 180, 124, 213, 102, 117, 212, 89, 89, 92, 209, 116, 17, 28,
];
const message_hash: [u8; 32] = [
167, 124, 116, 195, 220, 156, 244, 20, 243, 69, 1, 98, 189, 205, 79, 108,
213, 78, 65, 65, 230, 30, 17, 37, 184, 220, 237, 135, 1, 209, 101, 229,
];
const EXPECTED_COMPRESSED_PUBLIC_KEY: [u8; 33] = [
3, 110, 192, 35, 209, 24, 189, 55, 218, 250, 100, 89, 40, 76, 222, 208, 202,
127, 31, 13, 58, 51, 242, 179, 13, 63, 19, 22, 252, 164, 226, 248, 98,
];
let result = self.env().ecdsa_recover(&signature, &message_hash);
assert!(result.is_ok());
assert_eq!(
result.unwrap().as_ref(),
EXPECTED_COMPRESSED_PUBLIC_KEY.as_ref()
);
// Pass invalid zero message hash
let failed_result = self.env().ecdsa_recover(&signature, &[0; 32]);
assert!(failed_result.is_err());
if let Err(e) = failed_result {
assert_eq!(e, ink::env::ReturnErrorCode::EcdsaRecoveryFailed.into());
}
}Sourcepub fn ecdsa_to_eth_address(self, pubkey: &[u8; 33]) -> Result<[u8; 20]>
pub fn ecdsa_to_eth_address(self, pubkey: &[u8; 33]) -> Result<[u8; 20]>
Returns an Ethereum address from the ECDSA compressed public key.
§Example
#[ink(message)]
pub fn ecdsa_to_eth_address(&self) {
let pub_key = [
3, 110, 192, 35, 209, 24, 189, 55, 218, 250, 100, 89, 40, 76, 222, 208, 202,
127, 31, 13, 58, 51, 242, 179, 13, 63, 19, 22, 252, 164, 226, 248, 98,
];
let EXPECTED_ETH_ADDRESS = [
253, 240, 181, 194, 143, 66, 163, 109, 18, 211, 78, 49, 177, 94, 159, 79,
207, 37, 21, 191,
];
let output = self
.env()
.ecdsa_to_eth_address(&pub_key)
.unwrap_or_else(|err| {
panic!(
"must return an Ethereum address for the compressed public key: {:?}",
err
)
});
assert_eq!(output, EXPECTED_ETH_ADDRESS);
}§Note
For more details visit: [ink_env::ecdsa_to_eth_address]
Sourcepub fn sr25519_verify(
self,
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32],
) -> Result<()>
pub fn sr25519_verify( self, signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32], ) -> Result<()>
Verifies a SR25519 signature against a message and a public key.
§Example
#[ink(message)]
pub fn sr25519_verify(&self) {
let mut signature: [u8; 64] = [
10, 125, 162, 182, 49, 112, 76, 220, 254, 147, 199, 64, 228, 18, 23, 185,
172, 102, 122, 12, 135, 85, 216, 218, 26, 130, 50, 219, 82, 127, 72, 124,
135, 231, 128, 210, 237, 193, 137, 106, 235, 107, 27, 239, 11, 199, 195, 141,
157, 242, 19, 91, 99, 62, 171, 139, 251, 23, 119, 232, 47, 173, 58, 143,
];
let mut message: [u8; 49] = [
60, 66, 121, 116, 101, 115, 62, 48, 120, 52, 54, 102, 98, 55, 52, 48, 56,
100, 52, 102, 50, 56, 53, 50, 50, 56, 102, 52, 97, 102, 53, 49, 54, 101, 97,
50, 53, 56, 53, 49, 98, 60, 47, 66, 121, 116, 101, 115, 62,
];
let mut public_key: [u8; 32] = [
212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130,
44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125,
];
let result = ink::env::sr25519_verify(&signature, &message, &public_key);
assert_eq!(result, Ok(()));
}§Note
The context for sr25519 signing is hard-coded to “substrate” to match sr25519 signing in substrate.
For more details visit: [ink_env::sr25519_verify]
WARNING: this function is from the unstable interface, which is unsafe and normally is not available on production chains.
Sourcepub fn is_contract(self, addr: &Address) -> bool
pub fn is_contract(self, addr: &Address) -> bool
Checks whether addr is a contract.
§Notes
If addr references a precompile address, the return value will be true.
The function Self::caller_is_origin performs better when checking whether your
contract is being called by a contract or an account. It performs better
for this case as it does not require any storage lookups.
§Example
#[ink(message)]
pub fn is_contract(&mut self, addr: ink::Address) -> bool {
self.env().is_contract(&addr)
}
#[ink(message)]
pub fn check(&mut self) {
let this_contract = self.env().address();
assert!(self.env().is_contract(&this_contract));
assert!(!self.env().is_contract(&self.env().caller()));
}§Note
For more details visit: [ink_env::is_contract]
Sourcepub fn caller_is_origin(self) -> bool
pub fn caller_is_origin(self) -> bool
Sourcepub fn caller_is_root(self) -> bool
pub fn caller_is_root(self) -> bool
Sourcepub fn own_code_hash(self) -> H256
pub fn own_code_hash(self) -> H256
Sourcepub fn set_code_hash(self, code_hash: &H256) -> Result<()>
pub fn set_code_hash(self, code_hash: &H256) -> Result<()>
Replace the contract code at the specified address with new code.
§Example
#[ink(message)]
pub fn set_code_hash(&mut self, code_hash: ink::H256) {
self.env()
.set_code_hash(&code_hash)
.unwrap_or_else(|err| panic!("failed to set code hash: {:?}", err))
}§Note
For more details visit: [ink_env::set_code_hash]
pub fn xcm_weigh<Call: Encode>(self, msg: &VersionedXcm<Call>) -> Result<Weight>
pub fn xcm_execute<Call: Encode>( self, msg: &VersionedXcm<Call>, weight: Weight, ) -> Result<()>
pub fn xcm_send<Call: Encode>( self, dest: &VersionedLocation, msg: &VersionedXcm<Call>, ) -> Result<()>
Trait Implementations§
impl<'a, E: Copy> Copy for EnvAccess<'a, E>
Auto Trait Implementations§
impl<'a, E> Freeze for EnvAccess<'a, E>
impl<'a, E> RefUnwindSafe for EnvAccess<'a, E>
impl<'a, E> Send for EnvAccess<'a, E>
impl<'a, E> Sync for EnvAccess<'a, E>
impl<'a, E> Unpin for EnvAccess<'a, E>
impl<'a, E> UnwindSafe for EnvAccess<'a, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CheckedConversion for T
impl<T> CheckedConversion for T
§fn checked_from<T>(t: T) -> Option<Self>where
Self: TryFrom<T>,
fn checked_from<T>(t: T) -> Option<Self>where
Self: TryFrom<T>,
§fn checked_into<T>(self) -> Option<T>where
Self: TryInto<T>,
fn checked_into<T>(self) -> Option<T>where
Self: TryInto<T>,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T, U> DefensiveTruncateInto<U> for Twhere
U: DefensiveTruncateFrom<T>,
impl<T, U> DefensiveTruncateInto<U> for Twhere
U: DefensiveTruncateFrom<T>,
§fn defensive_truncate_into(self) -> U
fn defensive_truncate_into(self) -> U
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<Src, Dest> IntoTuple<Dest> for Srcwhere
Dest: FromTuple<Src>,
impl<Src, Dest> IntoTuple<Dest> for Srcwhere
Dest: FromTuple<Src>,
fn into_tuple(self) -> Dest
§impl<T> IsType<T> for T
impl<T> IsType<T> for T
§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T. Read more§impl<T, U> TryIntoKey<U> for Twhere
U: TryFromKey<T>,
impl<T, U> TryIntoKey<U> for Twhere
U: TryFromKey<T>,
type Error = <U as TryFromKey<T>>::Error
fn try_into_key(self) -> Result<U, <U as TryFromKey<T>>::Error>
§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from.§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T.