1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use js_sys::JsString;
use primitive_types::{H128, H160, H256, H512, U128, U256, U512};

use super::{BNError, BN};

macro_rules! try_from {
    ($type:ty, $byte_length:expr, $from_fn:ident) => {
        impl TryFrom<BN> for $type {
            type Error = BNError;

            fn try_from(value: BN) -> Result<$type, Self::Error> {
                // TODO: handle non-uint std types (isize/i8/i16/i32/i64/i128)
                if value.negative() == 1 {
                    return Err(BNError::NegativeUint);
                }

                if value.byte_length() > $byte_length {
                    return Err(BNError::Overflow(stringify!($type).into()));
                }

                // TODO: use `BN::words()` to transform bn.js 26 word-size array to [u64]
                let bytes = <[u8; $byte_length as usize]>::try_from(
                    &*value
                        .to_array("be".into(), $byte_length)
                        .map_err(|err| BNError::Throw(JsString::from(err).into()))?,
                )
                .unwrap();

                Ok(<$type>::$from_fn(bytes))
            }
        }
    };
}

macro_rules! try_from_primitive {
    ($type:ty) => {
        try_from!($type, std::mem::size_of::<$type>() as u32, from);
    };
}

macro_rules! try_from_std {
    ($type:ty) => {
        try_from!($type, <$type>::BITS / 8, from_be_bytes);
    };
}

try_from_primitive!(U128);
try_from_primitive!(U256);
try_from_primitive!(U512);
try_from_primitive!(H128);
try_from_primitive!(H160);
try_from_primitive!(H256);
try_from_primitive!(H512);
// try_from_std!(isize);
// try_from_std!(i8);
// try_from_std!(i16);
// try_from_std!(i32);
// try_from_std!(i64);
// try_from_std!(i128);
try_from_std!(usize);
try_from_std!(u8);
try_from_std!(u16);
try_from_std!(u32);
try_from_std!(u64);
try_from_std!(u128);

#[cfg(test)]
mod tests {
    use primitive_types::{H160, H256, H512, U128, U256, U512};
    use wasm_bindgen_test::*;

    use super::{BNError, BN};

    #[wasm_bindgen_test]
    fn primitive_uint() {
        let bn = BN::new(U256::MAX.to_string(), 10);
        let middle_bn = BN::new((U256::MAX - U256::from(U128::MAX)).to_string(), 10);

        assert!(matches!(
            U128::try_from(BN::from(bn.clone())).err(),
            Some(BNError::Overflow(_))
        ));
        assert_eq!(U256::try_from(BN::from(bn.clone())).unwrap(), U256::MAX);
        assert_eq!(
            U256::try_from(middle_bn).unwrap(),
            U256::MAX - U256::from(U128::MAX)
        );
        assert_eq!(U512::try_from(bn).unwrap(), U512::from(U256::MAX));
    }

    #[wasm_bindgen_test]
    fn primitive_hash() {
        let h256 = H256::from([u8::MAX; 32]);
        let bn = BN::new(format!("{:x}", h256), 16);
        let middle_h256 = H256::from(
            <[u8; 32]>::try_from([[0; 8], [u8::MAX; 8], [u8::MAX; 8], [0; 8]].concat()).unwrap(),
        );
        let middle_bn = BN::new(format!("{:x}", middle_h256), 16);

        assert!(matches!(
            H160::try_from(BN::from(bn.clone())).err(),
            Some(BNError::Overflow(_))
        ));
        assert_eq!(
            H256::try_from(BN::from(bn.clone())).unwrap(),
            H256::from([u8::MAX; 32]),
        );
        assert_eq!(H256::try_from(middle_bn).unwrap(), middle_h256);
        assert_eq!(
            H512::try_from(bn).unwrap(),
            H512::from(<[u8; 64]>::try_from([[0; 32], [u8::MAX; 32]].concat()).unwrap()),
        );
    }

    // #[wasm_bindgen_test]
    // fn std_int() {
    //     let bn = BN::new(i64::MAX.to_string(), 10);
    //     let middle_i64 = i64::MAX - i32::MAX as i64;
    //     let middle_bn = BN::new(middle_i64.to_string(), 10);
    //
    //     assert!(matches!(
    //         i8::try_from(BN::from(bn.clone())).err(),
    //         Some(BNError::Overflow(_))
    //     ));
    //     assert_eq!(i64::try_from(middle_bn).unwrap(), middle_i64);
    //     assert_eq!(i128::try_from(bn).unwrap(), i64::MAX as i128);
    // }

    #[wasm_bindgen_test]
    fn std_uint() {
        let bn = BN::new(u64::MAX.to_string(), 10);
        let middle_u64 = u64::MAX - u32::MAX as u64;
        let middle_bn = BN::new(middle_u64.to_string(), 10);

        assert!(matches!(
            u8::try_from(BN::from(bn.clone())).err(),
            Some(BNError::Overflow(_))
        ));
        assert_eq!(u64::try_from(middle_bn).unwrap(), middle_u64);
        assert_eq!(u128::try_from(bn).unwrap(), u64::MAX as u128);
    }
}