rewrote primiteves from bytes funcs
@@ -9,6 +9,7 @@ use franklin_crypto::jubjub::{edwards, JubjubEngine, Unknown};
use pairing::bn256::Bn256;
use pairing::{CurveAffine, Engine};
use std::str::FromStr;
+use std::convert::TryInto;
use web3::types::U256;
// TODO: replace Vec with Iterator?
@@ -463,59 +464,38 @@ pub fn u128_to_bigdecimal(n: u128) -> BigDecimal {
pub fn bytes_slice_to_uint32(bytes: &[u8]) -> Option<u32> {
let size = bytes.len();
- if size > 4 || size == 0 {
- return None;
- }
- let mut array: [u8; 4] = Default::default();
- for i in 4 - size..4 {
- array[i] = bytes[i - (4 - size)]
- }
- if size == 4 {
- for i in 0..4 - size {
- array[i] = 0;
- }
+ let mut vec: Vec<u8> = bytes.clone().to_vec();
+ vec.reverse();
+ for _ in 0..4-size {
+ vec.push(0);
}
- Some(u32::from_be_bytes(array))
+ vec.reverse();
+ let new_bytes = vec.as_slice();
+ Some(u32::from_be_bytes(new_bytes.try_into().ok()?))
}
pub fn bytes_slice_to_uint16(bytes: &[u8]) -> Option<u16> {
let size = bytes.len();
- if size > 2 || size == 0 {
- return None;
- }
- let mut array: [u8; 2] = Default::default();
- for i in 0..2 - size {
- array[i] = 0;
- }
- for i in 2 - size..2 {
- array[i] = bytes[i - (2 - size)]
- }
- if size == 2 {
- for i in 0..2 - size {
- array[i] = 0;
- }
+ let mut vec: Vec<u8> = bytes.clone().to_vec();
+ vec.reverse();
+ for _ in 0..2-size {
+ vec.push(0);
}
- Some(u16::from_be_bytes(array))
+ vec.reverse();
+ let new_bytes = vec.as_slice();
+ Some(u16::from_be_bytes(new_bytes.try_into().ok()?))
}
pub fn bytes_slice_to_uint128(bytes: &[u8]) -> Option<u128> {
let size = bytes.len();
- if size > 16 || size == 0 {
- return None;
- }
- let mut array: [u8; 16] = Default::default();
- for i in 0..16 - size {
- array[i] = 0;
- }
- for i in 16 - size..16 {
- array[i] = bytes[i - (16 - size)]
- }
- if size == 16 {
- for i in 0..16 - size {
- array[i] = 0;
- }
+ let mut vec: Vec<u8> = bytes.clone().to_vec();
+ vec.reverse();
+ for _ in 0..16-size {
+ vec.push(0);
}
- Some(u128::from_be_bytes(array))
+ vec.reverse();
+ let new_bytes = vec.as_slice();
+ Some(u128::from_be_bytes(new_bytes.try_into().ok()?))
}
pub fn bytes32_from_slice(bytes: &[u8]) -> Option<[u8; 32]> {
