Trait levana_perpswap_cosmos::prelude::Api

pub trait Api {
Show 15 methods // Required methods fn addr_validate(&self, human: &str) -> Result<Addr, StdError>; fn addr_canonicalize(&self, human: &str) -> Result<CanonicalAddr, StdError>; fn addr_humanize(&self, canonical: &CanonicalAddr) -> Result<Addr, StdError>; fn secp256k1_verify( &self, message_hash: &[u8], signature: &[u8], public_key: &[u8], ) -> Result<bool, VerificationError>; fn secp256k1_recover_pubkey( &self, message_hash: &[u8], signature: &[u8], recovery_param: u8, ) -> Result<Vec<u8>, RecoverPubkeyError>; fn ed25519_verify( &self, message: &[u8], signature: &[u8], public_key: &[u8], ) -> Result<bool, VerificationError>; fn ed25519_batch_verify( &self, messages: &[&[u8]], signatures: &[&[u8]], public_keys: &[&[u8]], ) -> Result<bool, VerificationError>; fn debug(&self, message: &str); // Provided methods fn bls12_381_aggregate_g1( &self, g1s: &[u8], ) -> Result<[u8; 48], VerificationError> { ... } fn bls12_381_aggregate_g2( &self, g2s: &[u8], ) -> Result<[u8; 96], VerificationError> { ... } fn bls12_381_pairing_equality( &self, ps: &[u8], qs: &[u8], r: &[u8], s: &[u8], ) -> Result<bool, VerificationError> { ... } fn bls12_381_hash_to_g1( &self, hash_function: HashFunction, msg: &[u8], dst: &[u8], ) -> Result<[u8; 48], VerificationError> { ... } fn bls12_381_hash_to_g2( &self, hash_function: HashFunction, msg: &[u8], dst: &[u8], ) -> Result<[u8; 96], VerificationError> { ... } fn secp256r1_verify( &self, message_hash: &[u8], signature: &[u8], public_key: &[u8], ) -> Result<bool, VerificationError> { ... } fn secp256r1_recover_pubkey( &self, message_hash: &[u8], signature: &[u8], recovery_param: u8, ) -> Result<Vec<u8>, RecoverPubkeyError> { ... }
}
Expand description

Api are callbacks to system functions implemented outside of the wasm modules. Currently it just supports address conversion but we could add eg. crypto functions here.

This is a trait to allow mocks in the test code. Its members have a read-only reference to the Api instance to allow accessing configuration. Implementations must not have mutable state, such that an instance can freely be copied and shared between threads without affecting the behaviour. Given an Api instance, all members should return the same value when called with the same arguments. In particular this means the result must not depend in the state of the chain. If you need to access chaim state, you probably want to use the Querier. Side effects (such as logging) are allowed.

We can use feature flags to opt-in to non-essential methods for backwards compatibility in systems that don’t have them all.

Required Methods§

fn addr_validate(&self, human: &str) -> Result<Addr, StdError>

Takes a human readable address and validates if it is valid. If it the validation succeeds, a Addr containing the same data as the input is returned.

This validation checks two things:

  1. The address is valid in the sense that it can be converted to a canonical representation by the backend.
  2. The address is normalized, i.e. humanize(canonicalize(input)) == input.

Check #2 is typically needed for upper/lower case representations of the same address that are both valid according to #1. This way we ensure uniqueness of the human readable address. Clients should perform the normalization before sending the addresses to the CosmWasm stack. But please note that the definition of normalized depends on the backend.

§Examples
let api = MockApi::default().with_prefix("juno");
let input = "juno1v82su97skv6ucfqvuvswe0t5fph7pfsrtraxf0x33d8ylj5qnrysdvkc95";
let validated: Addr = api.addr_validate(input).unwrap();
assert_eq!(validated.as_str(), input);

fn addr_canonicalize(&self, human: &str) -> Result<CanonicalAddr, StdError>

Takes a human readable address and returns a canonical binary representation of it. This can be used when a compact representation is needed.

Please note that the length of the resulting address is defined by the chain and can vary from address to address. On Cosmos chains 20 and 32 bytes are typically used. But that might change. So your contract should not make assumptions on the size.

fn addr_humanize(&self, canonical: &CanonicalAddr) -> Result<Addr, StdError>

Takes a canonical address and returns a human readble address. This is the inverse of addr_canonicalize.

fn secp256k1_verify( &self, message_hash: &[u8], signature: &[u8], public_key: &[u8], ) -> Result<bool, VerificationError>

fn secp256k1_recover_pubkey( &self, message_hash: &[u8], signature: &[u8], recovery_param: u8, ) -> Result<Vec<u8>, RecoverPubkeyError>

fn ed25519_verify( &self, message: &[u8], signature: &[u8], public_key: &[u8], ) -> Result<bool, VerificationError>

fn ed25519_batch_verify( &self, messages: &[&[u8]], signatures: &[&[u8]], public_keys: &[&[u8]], ) -> Result<bool, VerificationError>

fn debug(&self, message: &str)

Emits a debugging message that is handled depending on the environment (typically printed to console or ignored). Those messages are not persisted to chain.

Provided Methods§

fn bls12_381_aggregate_g1( &self, g1s: &[u8], ) -> Result<[u8; 48], VerificationError>

fn bls12_381_aggregate_g2( &self, g2s: &[u8], ) -> Result<[u8; 96], VerificationError>

fn bls12_381_pairing_equality( &self, ps: &[u8], qs: &[u8], r: &[u8], s: &[u8], ) -> Result<bool, VerificationError>

Checks the following pairing equality:

e(p_1, q_1) × e(p_2, q_2) × … × e(p_n, q_n) = e(s, q)

The argument ps contain the points p_1, …, p_n ∈ G1 as a concatenation of 48 byte elements. The argument qs contain the points q_1, …, q_n ∈ G2 as a concatenation of 96 byte elements.

§Examples

A simple signature check with one pairing on the left hand side (e(p, q) = e(s, q)):

pub fn verify(
    api: &dyn Api,
    g1_generator: &[u8],
    signature: &[u8],
    pubkey: &[u8],
    msg: &[u8],
    dst: &[u8],
) -> StdResult<bool> {
    let msg_hashed = api.bls12_381_hash_to_g2(HashFunction::Sha256, msg, dst)?;
    api.bls12_381_pairing_equality(g1_generator, signature, pubkey, &msg_hashed)
        .map_err(Into::into)
}

fn bls12_381_hash_to_g1( &self, hash_function: HashFunction, msg: &[u8], dst: &[u8], ) -> Result<[u8; 48], VerificationError>

fn bls12_381_hash_to_g2( &self, hash_function: HashFunction, msg: &[u8], dst: &[u8], ) -> Result<[u8; 96], VerificationError>

fn secp256r1_verify( &self, message_hash: &[u8], signature: &[u8], public_key: &[u8], ) -> Result<bool, VerificationError>

fn secp256r1_recover_pubkey( &self, message_hash: &[u8], signature: &[u8], recovery_param: u8, ) -> Result<Vec<u8>, RecoverPubkeyError>

Implementors§

§

impl Api for MockApi