diff --git a/engine/src/engine.rs b/engine/src/engine.rs index 5d48e4192..4a455dc92 100644 --- a/engine/src/engine.rs +++ b/engine/src/engine.rs @@ -206,9 +206,21 @@ impl AsRef<[u8]> for ERC20Address { self.0.as_bytes() } } -impl From> for ERC20Address { - fn from(bytes: Vec) -> Self { - Self(Address::from_slice(&bytes)) +impl TryFrom> for ERC20Address { + type Error = AddressParseError; + + fn try_from(bytes: Vec) -> Result { + if bytes.len() == 20 { + Ok(Self(Address::from_slice(&bytes))) + } else { + Err(AddressParseError) + } + } +} +pub struct AddressParseError; +impl AsRef<[u8]> for AddressParseError { + fn as_ref(&self) -> &[u8] { + b"ERR_PARSE_ADDRESS" } } @@ -218,9 +230,11 @@ impl AsRef<[u8]> for NEP141Account { self.0.as_bytes() } } -impl From> for NEP141Account { - fn from(bytes: Vec) -> Self { - Self(AccountId::try_from(bytes).unwrap()) +impl TryFrom> for NEP141Account { + type Error = aurora_engine_types::account_id::ParseAccountError; + + fn try_from(bytes: Vec) -> Result { + AccountId::try_from(bytes).map(Self) } } diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 6ca777bc9..558f70b3e 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -749,7 +749,8 @@ mod contract { #[no_mangle] pub extern "C" fn get_nep141_from_erc20() { let mut io = Runtime; - let erc20_address: crate::engine::ERC20Address = io.read_input().to_vec().into(); + let erc20_address: crate::engine::ERC20Address = + io.read_input().to_vec().try_into().sdk_unwrap(); io.return_output( Engine::nep141_erc20_map(io) .lookup_right(&erc20_address) diff --git a/engine/src/map.rs b/engine/src/map.rs index b9eab5deb..faf69b69e 100644 --- a/engine/src/map.rs +++ b/engine/src/map.rs @@ -1,4 +1,4 @@ -pub use crate::prelude::{bytes_to_key, PhantomData, Vec}; +pub use crate::prelude::{bytes_to_key, PhantomData, TryFrom, TryInto, Vec}; use aurora_engine_sdk::io::{StorageIntermediate, IO}; use aurora_engine_types::storage::KeyPrefix; @@ -12,7 +12,9 @@ pub struct BijectionMap { right_phantom: PhantomData, } -impl + From>, R: AsRef<[u8]> + From>, I: IO> BijectionMap { +impl + TryFrom>, R: AsRef<[u8]> + TryFrom>, I: IO> + BijectionMap +{ pub fn new(left_prefix: KeyPrefix, right_prefix: KeyPrefix, io: I) -> Self { Self { left_prefix, @@ -33,12 +35,16 @@ impl + From>, R: AsRef<[u8]> + From>, I: IO> Bije pub fn lookup_left(&self, left: &L) -> Option { let key = self.left_key(left); - self.io.read_storage(&key).map(|v| v.to_vec().into()) + self.io + .read_storage(&key) + .and_then(|v| v.to_vec().try_into().ok()) } pub fn lookup_right(&self, right: &R) -> Option { let key = self.right_key(right); - self.io.read_storage(&key).map(|v| v.to_vec().into()) + self.io + .read_storage(&key) + .and_then(|v| v.to_vec().try_into().ok()) } fn left_key(&self, left: &L) -> Vec {