-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from redstone-finance/radix
feat: added extension for Radix
- Loading branch information
Showing
24 changed files
with
361 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
imports_granularity = "Crate" |
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,3 +1,2 @@ | ||
pub(crate) mod recover; | ||
|
||
mod keccak256; | ||
pub(crate) mod recover; |
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,25 +1,16 @@ | ||
//! # RedStone | ||
//! | ||
//! `redstone` is a collection of utilities to make deserializing&decrypting RedStone payload. | ||
//! It contains a pure Rust implementation and also an extension for the Casper network. | ||
//! It includes a pure Rust implementation, along with extensions for certain networks. | ||
//! | ||
//! Different crypto-mechanisms are easily injectable. | ||
//! The current implementation contains `secp256k1`- and `k256`-based variants. | ||
#[cfg(feature = "core")] | ||
pub mod core; | ||
|
||
#[cfg(feature = "core")] | ||
mod crypto; | ||
|
||
#[cfg(feature = "core")] | ||
pub mod network; | ||
mod protocol; | ||
|
||
#[cfg(feature = "core")] | ||
mod utils; | ||
|
||
#[cfg(feature = "network")] | ||
pub mod network; | ||
|
||
#[cfg(feature = "helpers")] | ||
pub mod helpers; |
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,7 +1,10 @@ | ||
use crate::network::specific::{FromBytesRepr, U256}; | ||
use crate::network::{ | ||
from_bytes_repr::{FromBytesRepr, Sanitized}, | ||
specific::U256, | ||
}; | ||
|
||
impl FromBytesRepr<Vec<u8>> for U256 { | ||
fn from_bytes_repr(bytes: Vec<u8>) -> Self { | ||
bytes.as_slice().into() | ||
bytes.sanitized().as_slice().into() | ||
} | ||
} |
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,128 @@ | ||
use crate::network::specific::VALUE_SIZE; | ||
|
||
pub trait FromBytesRepr<T> { | ||
fn from_bytes_repr(bytes: T) -> Self; | ||
} | ||
|
||
pub trait Sanitized { | ||
fn sanitized(self) -> Self; | ||
} | ||
|
||
impl Sanitized for Vec<u8> { | ||
fn sanitized(self) -> Self { | ||
if self.len() <= VALUE_SIZE { | ||
return self; | ||
} | ||
|
||
let index = self.len().max(VALUE_SIZE) - VALUE_SIZE; | ||
let remainder = &self[0..index]; | ||
|
||
if remainder != vec![0; index] { | ||
panic!("Number to big: {:?} digits", self.len()) | ||
} | ||
|
||
self[index..].into() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::network::{ | ||
from_bytes_repr::FromBytesRepr, | ||
specific::{U256, VALUE_SIZE}, | ||
}; | ||
|
||
#[cfg(feature = "network_radix")] | ||
use crate::network::radix::u256_ext::U256Ext; | ||
|
||
#[test] | ||
fn test_from_bytes_repr_single() { | ||
let vec = vec![1]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(1u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_double() { | ||
let vec = vec![1, 2]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(258u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_simple() { | ||
let vec = vec![1, 2, 3]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(66051u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_bigger() { | ||
let vec = vec![101, 202, 255]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(6671103u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_empty() { | ||
let result = U256::from_bytes_repr(Vec::new()); | ||
|
||
assert_eq!(result, U256::from(0u8)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_trailing_zeroes() { | ||
let vec = vec![1, 2, 3, 0]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(16909056u32)); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_leading_zeroes() { | ||
let vec = vec![0, 1, 2, 3]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(66051u32)); | ||
} | ||
|
||
#[allow(clippy::legacy_numeric_constants)] | ||
#[test] | ||
fn test_from_bytes_repr_max() { | ||
let vec = vec![255; VALUE_SIZE]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::max_value()); | ||
} | ||
|
||
#[test] | ||
fn test_from_bytes_repr_min() { | ||
let vec = vec![0; VALUE_SIZE]; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::from(0u8)); | ||
} | ||
|
||
#[should_panic(expected = "Number to big")] | ||
#[test] | ||
fn test_from_bytes_repr_too_long() { | ||
let x = VALUE_SIZE as u8 + 1; | ||
let vec = (1..=x).collect(); | ||
|
||
U256::from_bytes_repr(vec); | ||
} | ||
|
||
#[allow(clippy::legacy_numeric_constants)] | ||
#[test] | ||
fn test_from_bytes_repr_too_long_but_zeroes() { | ||
let mut vec = vec![255; VALUE_SIZE + 1]; | ||
vec[0] = 0; | ||
let result = U256::from_bytes_repr(vec); | ||
|
||
assert_eq!(result, U256::max_value()); | ||
} | ||
} |
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
Oops, something went wrong.