-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
212 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//! Utils for bytes conversion. | ||
//! | ||
//! TODO: move this util to other library | ||
/// Trait for converting type to bytes32. | ||
pub trait Bytes32: Sized { | ||
/// Convert type to the lowest significant bytes 32. | ||
fn to_bytes32(&self) -> [u8; 32]; | ||
|
||
/// Convert type to vec of bytes. | ||
fn to_vec(&self) -> Vec<u8> { | ||
self.to_bytes32().to_vec() | ||
} | ||
} | ||
|
||
/// Implement Bytes32 for types. | ||
macro_rules! impl_bytes32 { | ||
($($ty:ident),+) => { | ||
$( | ||
impl Bytes32 for $ty { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
let mut bytes = [0u8; 32]; | ||
let ls_bytes = { | ||
self.to_le_bytes() | ||
.into_iter() | ||
.rev() | ||
.skip_while(|b| *b == 0) | ||
.collect::<Vec<_>>() | ||
.into_iter() | ||
.rev() | ||
.collect::<Vec<_>>() | ||
}; | ||
|
||
bytes[(32 - ls_bytes.len())..].copy_from_slice(&ls_bytes); | ||
bytes | ||
} | ||
|
||
fn to_vec(&self) -> Vec<u8> { | ||
self.to_le_bytes().to_vec() | ||
} | ||
} | ||
)+ | ||
}; | ||
} | ||
|
||
impl Bytes32 for Vec<u8> { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
let mut bytes = [0u8; 32]; | ||
bytes[(32 - self.len())..].copy_from_slice(self); | ||
bytes | ||
} | ||
|
||
fn to_vec(&self) -> Vec<u8> { | ||
self.clone() | ||
} | ||
} | ||
|
||
impl Bytes32 for [u8; 20] { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
let mut bytes = [0u8; 32]; | ||
bytes[12..].copy_from_slice(self); | ||
bytes | ||
} | ||
} | ||
|
||
impl Bytes32 for [u8; 32] { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
*self | ||
} | ||
|
||
fn to_vec(&self) -> Vec<u8> { | ||
self.as_ref().into() | ||
} | ||
} | ||
|
||
impl Bytes32 for &[u8] { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
let mut bytes = [0u8; 32]; | ||
bytes[(32 - self.len())..].copy_from_slice(self); | ||
bytes | ||
} | ||
|
||
fn to_vec(&self) -> Vec<u8> { | ||
(*self).into() | ||
} | ||
} | ||
|
||
impl Bytes32 for () { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
[0; 32] | ||
} | ||
|
||
fn to_vec(&self) -> Vec<u8> { | ||
Default::default() | ||
} | ||
} | ||
|
||
impl Bytes32 for &str { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
let mut bytes = [0u8; 32]; | ||
bytes[(32 - self.len())..].copy_from_slice(self.as_bytes()); | ||
bytes | ||
} | ||
|
||
fn to_vec(&self) -> Vec<u8> { | ||
self.as_bytes().into() | ||
} | ||
} | ||
|
||
impl Bytes32 for bool { | ||
fn to_bytes32(&self) -> [u8; 32] { | ||
let mut output = [0; 32]; | ||
if *self { | ||
output[31] = 1; | ||
} | ||
|
||
output | ||
} | ||
} | ||
|
||
impl_bytes32!(i8, u8, i16, u16, i32, u32, usize, i64, u64, i128, u128); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
//! Zink primitive types | ||
mod address; | ||
mod u256; | ||
|
||
pub use address::Address; | ||
pub use u256::U256; | ||
|
||
pub type Bytes20 = Address; | ||
pub type Bytes32 = U256; | ||
pub type String32 = U256; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::{ffi, storage::StorageValue, Asm}; | ||
|
||
/// Account address | ||
#[repr(C)] | ||
#[derive(Clone, Copy)] | ||
pub struct U256( | ||
#[cfg(target_family = "wasm")] i32, | ||
#[cfg(not(target_family = "wasm"))] [u8; 32], | ||
); | ||
|
||
impl U256 { | ||
/// Returns empty address | ||
#[cfg(not(target_family = "wasm"))] | ||
pub const fn empty() -> Self { | ||
U256([0; 32]) | ||
} | ||
} | ||
|
||
impl Asm for U256 { | ||
fn push(self) { | ||
unsafe { ffi::asm::push_u256(self) } | ||
} | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
fn bytes32(&self) -> [u8; 32] { | ||
self.0 | ||
} | ||
} | ||
|
||
impl StorageValue for U256 { | ||
fn sload() -> Self { | ||
unsafe { ffi::asm::sload_u256() } | ||
} | ||
} |