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

I26 wasm add async #38

Merged
merged 84 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 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
da8f8a6
Added blocking and non-blocking Http clients.
May 17, 2021
89b9dd7
Wasm-pack builds, but client request doesn't seem to work.
May 17, 2021
d575a7c
Able to call json rpc through wasm.
May 17, 2021
c676ae9
Removed some commented out code.
May 18, 2021
58181df
`Web3` client call works without an `async` function call.
May 20, 2021
af8936d
Split connection into sync and async.
May 21, 2021
ba4b5f9
Add async connection for an async contract caller that can be used in
May 21, 2021
414003e
Added non-blocking caller as well.
May 21, 2021
e661c64
Forgot to add a compilation flag.
May 21, 2021
4a88967
Some minor changes:
May 24, 2021
784c07a
Address type was incorrectly displayed.
May 24, 2021
29d569e
Merge branch 'I36-remove-deps' into I26-wasm-add-async
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
aef12b4
Merge branch 'I36-remove-deps' into I26-wasm-add-async
May 24, 2021
d25378c
Baseline wasm implementation tested.
May 25, 2021
c60e9e8
Remove `www` from tracked files.
May 25, 2021
cd24c22
Updated README.
May 25, 2021
652a70a
Use a single usize to track id instead of a vecdeque.
May 26, 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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ Cargo.lock
.idea/
/test-helper/target
/test-helper/Cargo.lock
.env
www/
.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
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,34 @@
## Story

Package originally created by [thojest](https://github.com/thojest) and later maintained by ZGEN DAO.
Created on purpose to make the web3 connection much simpler when using Rust.
This creates a simple alternative to other web3 packages in Rust.
Created with the purpose to provide a simple alternative for `Web3` packages written in Rust.

## Description

Ethane is an alternative web3 implementation with the aim of being slim and simple.
It does not depend on futures or any executors. It currently supports http and
Ethane is an alternative `Web3` implementation with the aim of being slim and
simple. It has two features `blocking` and `non-blocking` that determines the
http connection type it should use.

A blocking http client does not depend on futures or any executors. Furthermore, it currently supports
websockets (both plain and TLS) and inter process communication via Unix domain sockets (Unix only). For
http and websockets it also supports Http Basic and Bearer Authentication. It also has a built-in ABI parser library.
It's hidden under the contract functionalities, but it can be used alongside with the main crate.

If you still need a non-blocking http client (e.g. for `wasm` compatibility),
you may compile `Ethane` with the `non-blocking` feature flag to enable an
`async` http client.

Please also take a look at the [documentation](https://docs.rs/ethane).
If you just want to use this crate, it is also available on crates.io.
([Ethane](https://crates.io/crates/ethane)). If you find any bugs please
do not hesitate to open an issue.

## Usage

Guidelines to use the Ethane library.
Guidelines to use the Ethane library. The examples were worked out for the
blocking client, however the non-blocking version is quite similar. The main
difference is that an `AsyncConnection` can only wrap an `AsyncHttp` client without any
type generics, so there is currently no implementation for a non-blocking websocket.

### Connection

Expand Down Expand Up @@ -55,7 +64,7 @@ use ethane::types::Address;
fn main() {
let conn = Connection::new(Http::new("http://localhost:8545", None));

match conn.call(rpc::eth_get_balance(Address::from_str(ADDRESS1).unwrap(), None)) {
match conn.call(rpc::eth_get_balance(Address::try_from_str(ADDRESS1).unwrap(), None)) {
Ok(res) => res,
Err(err) => println!("{:?}", err),
}
Expand All @@ -78,31 +87,33 @@ fn main() {
let mut caller = Caller::new_from_path(
conn,
"path/to/contract.abi",
Address::from_str("0x141770c471a64bcde74c587e55a1ffd9a1bffd31").unwrap(),
Address::try_from_str("0x141770c471a64bcde74c587e55a1ffd9a1bffd31").unwrap(),
);

// The call function determine the call_type based on the state_mutability.
// This calls to function from an ERC-20 compliant token
// eth_call
let address = Address::try_from_str("0x141770c471a64bcde74c587e55a1ffd9a1bffd31").uwnrap();
let result = caller.call(
"balanceOf",
vec![Parameter::from(Address::from(address))],
vec![Parameter::from(address)],
None,
);
match result {
CallResult::Transaction(_) => panic!("Should be eth_call"),
CallResult::Call(r) => match r[0] {
Parameter::Uint(data, 256) => assert_eq!(data, H256::from_low_u64_be(1000000000_u64)),
Parameter::Uint(data, 256) => assert_eq!(data, H256::from_int_unchecked(1000000000_u64)),
_ => panic!("Invalid data received!"),
},
}

// eth_sendTransaction
let to_address = Address::try_from_str("0x...").unwrap();
let result = caller.call(
"transfer",
vec![
Parameter::from(Address::from(to_address)),
Parameter::from(U256::from(1000)),
Parameter::from(to_address),
Parameter::from(U256::try_from_int(1000_u128).unwrap()),
],
Some(CallOpts {
force_call_type: None, // NOTE: the call_type can be forced
Expand Down
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,
}
Loading