Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I36 remove deps #37

Closed
wants to merge 69 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
a3db7c8
New attempt at implementing wasm bindings for eip1193.
May 5, 2021
2a1b35c
Added www folder.
May 5, 2021
b9b7380
eth_request_accounts WIP
May 5, 2021
a1297a7
try to fix eth_request example
May 5, 2021
958835d
add first working solution
May 5, 2021
373ea11
add params to requst
May 5, 2021
3c59ff1
started implementing a wasm wrapper around rpc calls.
May 5, 2021
8721633
Maybe there's a way to bind ethane to wasm by passing raw RPC strings
May 9, 2021
d4025c1
Implemented call for wasm client.
May 10, 2021
1f6e457
Missing `env` is an issue here as well.
May 10, 2021
447f6c5
Removed log crate
May 11, 2021
c04d569
WIP websocket fix
May 11, 2021
141e407
Got rid of Http crate.
May 11, 2021
253eee0
Http now uses `reqwest::blocking::Client` instead of `ureq`.
May 11, 2021
bb8ae72
Removed `funty`.
May 12, 2021
1ebfbff
WIP removing thiserror crate.
May 12, 2021
fcad22a
Removed thiserror crate.
May 12, 2021
d599287
Add a (maybe temporary) types crate.
May 12, 2021
7a372c7
Implemented a const generic EthereumType struct.
May 12, 2021
009bfbf
Implemented most conversions.
May 12, 2021
40ceb99
Added support for all unsigned primitive types.
May 12, 2021
4f4645c
Testing from fixed array.
May 12, 2021
7dcde26
Added a trait for simpler code.
May 12, 2021
099f291
Use `impl Trait` as type to get rid of references.
May 12, 2021
87e0785
Uncommented test.
May 13, 2021
ac7ad46
Nicer error enum and tests.
May 13, 2021
730abd6
Tests made nicer.
May 13, 2021
5478a48
Cargo format.
May 13, 2021
50ca1de
Merge branch 'I36-remove-deps' into I36-reimplement-ethereum-types
May 13, 2021
3ae092b
Added all types used by ethane
May 13, 2021
4d3523c
Some more constructors added and tested.
May 13, 2021
7b64fb8
Tons of changes for ethane to use own eth types.
May 13, 2021
20d32b8
Ethereum type needs a fix.
May 13, 2021
d9a21b5
WIP figuring out how to parse a string shorter than the data container.
May 13, 2021
76a9b0d
`EthereumType` can be build from any `&str` that fits.
May 13, 2021
4593c47
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 13, 2021
777cc58
`EthereumTypes` works with `ethane-abi`.
May 13, 2021
18aaec5
Some tests fail because of the way `EthereumType` is serialized.
May 13, 2021
b38e9b4
Derived `Serialize` and `Deserialize` for `EthereumType<N>`.
May 14, 2021
257ad71
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 14, 2021
82b3dd6
Removing leading zeros when representing data as string.
May 14, 2021
f2d8919
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 14, 2021
5573457
Fixed ethane-abi tests.
May 14, 2021
c5b0361
Fixed `to_string()`, because `Address` should not have its leading zeros
May 14, 2021
a2da0ce
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 14, 2021
0ca51b1
Fix display.
May 14, 2021
11ecb9a
Bloom hash has 256 Bytes not bits!
May 14, 2021
f7f29e7
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 14, 2021
ab3d9da
Fixed zero type formatting.
May 14, 2021
44a2d44
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 14, 2021
4ef3490
Added another constant to differentiate between hash and uint types.
May 14, 2021
bec2e75
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 14, 2021
e9e5fc5
Forgot to handle 0 uints.
May 14, 2021
aa2033e
Merge branch 'I36-reimplement-ethereum-types' into I36-fix-ethane
May 14, 2021
e55793f
Remove println statement
May 14, 2021
9dfeb20
Fixed some tests.
May 14, 2021
df85ca2
Removed thiserror dependency.
May 14, 2021
9e57369
Removed unnecessary dependency and made bytes nicer.
May 14, 2021
b06543f
Removed byteorder dependency.
May 14, 2021
626d749
Use `tiny-keccak` everywhere instead of `sha3`; it's more lightweight.
May 14, 2021
c96d3df
Moved `Bytes` to `ethane-types`.
May 14, 2021
f23ff2c
Removed hex dependency.
May 14, 2021
b05b4b2
Removed `hex` dependency.
May 14, 2021
cda6155
Remove `package-lock.json` from tracked files.
May 14, 2021
d2f3868
Some more tests for ethereum type.
May 17, 2021
4a88967
Some minor changes:
May 24, 2021
784c07a
Address type was incorrectly displayed.
May 24, 2021
f711d1e
Added a utility function that prints ethereum types as decimal string.
May 24, 2021
f0a4973
Unsigned integer parameters can now be printed as a decimal string.
May 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ Cargo.lock
.idea/
/test-helper/target
/test-helper/Cargo.lock
.env
.env

node_modules
dist
package-lock.json
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
members = [
"ethane",
"ethane-abi",
"ethane-types",
"ethane-wasm",
]

[profile.dev]
opt-level = 3

[profile.release]
opt-level = 3
opt-level = 3

[profile.test]
opt-level = 3
opt-level = 3
6 changes: 2 additions & 4 deletions ethane-abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ categories = ["cryptography::cryptocurrencies", "web-programming"]
readme = "../README.md"

[dependencies]
byteorder = "1.4.3"
ethereum-types = "0.11"
ethane-types = { path = "../ethane-types" }
serde_json = "1.0"
sha3 = "0.9.1"
thiserror = "1.0"
tiny-keccak = { version = "2.0", features = ["keccak"] }

[dev-dependencies]
hex-literal = "0.3"
30 changes: 13 additions & 17 deletions ethane-abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use sha3::{Digest, Keccak256};
use thiserror::Error;
use tiny_keccak::{Hasher, Keccak};

mod function;
mod parameter;
Expand Down Expand Up @@ -59,9 +57,10 @@ impl Abi {

/// Parses an ABI `.json` file into the `Abi` instance.
pub fn parse_file(&mut self, path_to_abi: &Path) -> Result<(), AbiParserError> {
let file = File::open(path_to_abi)?;
let reader = BufReader::new(file);
let abi: serde_json::Value = serde_json::from_reader(reader)?;
let reader =
File::open(path_to_abi).map_err(|e| AbiParserError::FileIoError(e.to_string()))?;
let abi: serde_json::Value =
serde_json::from_reader(reader).map_err(|e| AbiParserError::Serde(e.to_string()))?;

self.parse_json(abi)
}
Expand Down Expand Up @@ -98,10 +97,12 @@ impl Abi {
}
}
let signature = format!("{}({})", function_name, abi_arguments.join(","));
let mut hasher = Keccak256::new();
hasher.update(signature);
let mut hasher = Keccak::v256();
hasher.update(signature.as_bytes());
// Take first 4 bytes of the Keccak hash
let mut hash = hasher.finalize()[0..4].to_vec();
let mut out = [0_u8; 32];
hasher.finalize(&mut out);
let mut hash = out[0..4].to_vec();
// Append the encoded parameters to the hash
parameter::encode_into(&mut hash, parameters);
Ok(hash)
Expand Down Expand Up @@ -140,16 +141,11 @@ impl Abi {
}
}

#[derive(Error, Debug)]
#[derive(Debug)]
pub enum AbiParserError {
#[error("Couldn't open ABI file: {0}")]
FileIoError(#[from] std::io::Error),
#[error("De-/Serialization error: {0}")]
Serde(#[from] serde_json::Error),
#[error("Missing data error: {0}")]
FileIoError(String),
Serde(String),
MissingData(String),
#[error("Invalid ABI encoding error: {0}")]
InvalidAbiEncoding(String),
#[error("Parameter type doesn't match the internal data type")]
TypeError,
}
82 changes: 26 additions & 56 deletions ethane-abi/src/parameter/construction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::utils::*;
use super::Parameter;
use ethereum_types::{Address, H256, U128, U256, U64};
use ethane_types::{Address, H256, U128, U256, U64};

use std::convert::From;

Expand Down Expand Up @@ -31,103 +31,79 @@ impl Parameter {
impl From<u8> for Parameter {
#[inline]
fn from(input: u8) -> Self {
Self::Uint(H256::from_slice(&left_pad_to_32_bytes(&[input])), 8)
Self::Uint(H256::from_int_unchecked(input), 8)
}
}

impl From<u16> for Parameter {
#[inline]
fn from(input: u16) -> Self {
Self::Uint(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
16,
)
Self::Uint(H256::from_int_unchecked(input), 16)
}
}

impl From<u32> for Parameter {
#[inline]
fn from(input: u32) -> Self {
Self::Uint(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
32,
)
Self::Uint(H256::from_int_unchecked(input), 32)
}
}

impl From<u64> for Parameter {
#[inline]
fn from(input: u64) -> Self {
Self::Uint(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
64,
)
Self::Uint(H256::from_int_unchecked(input), 64)
}
}

impl From<u128> for Parameter {
#[inline]
fn from(input: u128) -> Self {
Self::Uint(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
128,
)
Self::Uint(H256::from_int_unchecked(input), 128)
}
}

// Signed elementary integer types
impl From<i8> for Parameter {
#[inline]
fn from(input: i8) -> Self {
Self::Int(H256::from_slice(&left_pad_to_32_bytes(&[input as u8])), 8)
Self::Int(H256::from_int_unchecked(input), 8)
}
}

impl From<i16> for Parameter {
#[inline]
fn from(input: i16) -> Self {
Self::Int(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
16,
)
Self::Int(H256::from_int_unchecked(input), 16)
}
}

impl From<i32> for Parameter {
#[inline]
fn from(input: i32) -> Self {
Self::Int(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
32,
)
Self::Int(H256::from_int_unchecked(input), 32)
}
}

impl From<i64> for Parameter {
#[inline]
fn from(input: i64) -> Self {
Self::Int(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
64,
)
Self::Int(H256::from_int_unchecked(input), 64)
}
}

impl From<i128> for Parameter {
#[inline]
fn from(input: i128) -> Self {
Self::Int(
H256::from_slice(&left_pad_to_32_bytes(&input.to_be_bytes())),
128,
)
Self::Int(H256::from_int_unchecked(input), 128)
}
}

// Boolean type
impl From<bool> for Parameter {
#[inline]
fn from(input: bool) -> Self {
Self::Bool(H256::from_slice(&left_pad_to_32_bytes(&[u8::from(input)])))
Self::Bool(H256::from_int_unchecked(u8::from(input)))
}
}

Expand All @@ -143,42 +119,36 @@ impl From<&str> for Parameter {
impl From<Address> for Parameter {
#[inline]
fn from(input: Address) -> Self {
Self::Address(H256::from_slice(&left_pad_to_32_bytes(&input.as_bytes())))
Self::Address(H256::from(&left_pad_to_32_bytes(input.as_bytes())))
}
}

impl From<U64> for Parameter {
#[inline]
fn from(input: U64) -> Self {
let mut bytes = [0u8; 8];
input.to_big_endian(&mut bytes);
Self::Uint(H256::from_slice(&left_pad_to_32_bytes(&bytes)), 64)
Self::Uint(H256::from(&left_pad_to_32_bytes(input.as_bytes())), 64)
}
}

impl From<U128> for Parameter {
#[inline]
fn from(input: U128) -> Self {
let mut bytes = [0u8; 16];
input.to_big_endian(&mut bytes);
Self::Uint(H256::from_slice(&left_pad_to_32_bytes(&bytes)), 128)
Self::Uint(H256::from(&left_pad_to_32_bytes(input.as_bytes())), 128)
}
}

impl From<U256> for Parameter {
#[inline]
fn from(input: U256) -> Self {
let mut padded = [0u8; 32];
input.to_big_endian(&mut padded);
Self::Uint(H256::from_slice(&padded), 256)
Self::Uint(H256::from(input.into_bytes()), 256)
}
}

#[cfg(test)]
mod test {
use super::*;
use hex_literal::hex;
use std::str::FromStr;
use std::convert::TryFrom;

#[test]
fn parameter_from_elementary_numeric_type() {
Expand Down Expand Up @@ -306,40 +276,40 @@ mod test {
let param = Parameter::from(U64::zero());
if let Parameter::Uint(value, len) = param {
assert_eq!(len, 64);
assert_eq!(value.to_fixed_bytes(), [0u8; 32]);
assert_eq!(value.into_bytes(), [0u8; 32]);
} else {
panic!("From U64 test failed")
}

// from U128
let param = Parameter::from(U128::from_str("12345").unwrap());
let param = Parameter::from(U128::try_from("12345").unwrap());
if let Parameter::Uint(value, len) = param {
let mut expected = [0u8; 32];
expected[28..].copy_from_slice(&0x12345u32.to_be_bytes());
assert_eq!(len, 128);
assert_eq!(value.to_fixed_bytes(), expected);
assert_eq!(value.into_bytes(), expected);
} else {
panic!("From U128 test failed")
}

// from U256
let param = Parameter::from(U256::from_str("123456789").unwrap());
let param = Parameter::from(U256::try_from("123456789").unwrap());
if let Parameter::Uint(value, len) = param {
let mut expected = [0u8; 32];
expected[24..].copy_from_slice(&0x123456789u64.to_be_bytes());
assert_eq!(len, 256);
assert_eq!(value.to_fixed_bytes(), expected);
assert_eq!(value.into_bytes(), expected);
} else {
panic!("From U256 test failed")
}

// from Address
let param = Parameter::from(
Address::from_str("0x95eDA452256C1190947f9ba1fD19422f0120858a").unwrap(),
Address::try_from("0x95eDA452256C1190947f9ba1fD19422f0120858a").unwrap(),
);
if let Parameter::Address(value) = param {
let expected = hex!("00000000000000000000000095eDA452256C1190947f9ba1fD19422f0120858a");
assert_eq!(value.to_fixed_bytes(), expected);
assert_eq!(value.into_bytes(), expected);
} else {
panic!("From U256 test failed")
}
Expand Down Expand Up @@ -389,7 +359,7 @@ mod test {
let param = Parameter::new_int([14; 32], true); // signed = true
if let Parameter::Int(value, len) = param {
assert_eq!(len, 256);
assert_eq!(value.to_fixed_bytes(), [14u8; 32]);
assert_eq!(value.into_bytes(), [14u8; 32]);
} else {
panic!("Signed integer from fixed slice test failed");
}
Expand All @@ -398,7 +368,7 @@ mod test {
let param = Parameter::new_int([12; 32], false); // signed = false
if let Parameter::Uint(value, len) = param {
assert_eq!(len, 256);
assert_eq!(value.to_fixed_bytes(), [12u8; 32]);
assert_eq!(value.into_bytes(), [12u8; 32]);
} else {
panic!("Unsigned integer from fixed slice test failed");
}
Expand Down
Loading