owasm_std/
types.rs

1///! Provides primitive fixed size hash types.
2///! Based on https://github.com/paritytech/pwasm-std/blob/61db712/src/types.rs
3
4construct_uint! {
5    /// A 256-bits (4 64-bit word) fixed-size bigint type.
6    #[derive(Serialize, Deserialize)]
7    pub struct U256(4);
8}
9
10construct_fixed_hash! {
11    /// A 160 bits (20 bytes) hash type (aka `Address`).
12    #[derive(Serialize, Deserialize)]
13    pub struct H160(20);
14}
15
16construct_fixed_hash! {
17    /// A 256-bits (32 bytes) hash type.
18    #[derive(Serialize, Deserialize)]
19    pub struct H256(32);
20}
21
22// Auto-impl `From` conversions between `H256` and `H160`.
23impl_fixed_hash_conversions!(H256, H160);
24
25/// Represents an address in ethereum context.
26pub type Address = H160;
27
28impl From<U256> for H256 {
29    fn from(uint: U256) -> H256 {
30        let mut hash = H256::zero();
31        uint.to_big_endian(hash.as_bytes_mut());
32        hash
33    }
34}
35
36impl<'a> From<&'a U256> for H256 {
37    fn from(uint: &'a U256) -> H256 {
38        let mut hash: H256 = H256::zero();
39        uint.to_big_endian(hash.as_bytes_mut());
40        hash
41    }
42}
43
44impl From<H256> for U256 {
45    fn from(hash: H256) -> U256 {
46        U256::from(hash.as_bytes())
47    }
48}
49
50impl<'a> From<&'a H256> for U256 {
51    fn from(hash: &'a H256) -> U256 {
52        U256::from(hash.as_bytes())
53    }
54}