Skip to content

Commit 6d14c94

Browse files
committed
Cleanup in transaction types, added patricia_tree
1 parent 1866ee4 commit 6d14c94

20 files changed

+572
-428
lines changed

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
members = [
33
"src/evm_utils",
44
]
5+
6+
[profile.release]
7+
opt-level="s"
8+
lto = true
9+
codegen-units = 1

readme.MD

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ Link [https://icdevs.org/bounties/2023/01/09/28-EVM-Utility-Canister-Rust.html]
1313

1414
## Transaction
1515

16-
- [ ] `create_transaction` - Returns transaction hash and encoded bytes
17-
- [ ] `parse_transaction` - Returns parsed transaction
16+
- [x] `create_transaction` - Returns transaction hash and encoded bytes
17+
- [x] `parse_transaction` - Returns parsed transaction
1818

1919
## EVM Verification
2020

21-
- [ ] `verify_patricia_tree` - Verifies if send hash is a part of a tree
21+
- [x] `verify_proof` - Verifies if send hash is a part of a tree
2222

2323
## Hashing
2424

25-
- [ ] `keccak256(vec u8) -> vec u8` - Hashes incoming data using keccak and returns hash
25+
- [x] `keccak256(vec u8) -> vec u8` - Hashes incoming data using keccak and returns hash
2626

2727
## Utils
2828
For now I have decided to skip functions related to private keys processing. I do believe it is not secure to pass public keys.
2929

3030

3131
- [x] `recover_public_key(vec u8, vec u8)` - recovers public key from ethereum style recoverable signature (65 bytes), equivalent to ecrecover
3232

33-
- [ ] `is_valid_public` - checks if public key is valid
33+
- [x] `is_valid_public` - checks if public key is valid
3434

35-
- [ ] `is_valid_siganature` - validates ECDSA signature
35+
- [x] `is_valid_siganature` - validates ECDSA signature
3636

3737
- [x] `pub_to_address` - converts public key to ethereum address
3838

src/evm_utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["cdylib"]
1111
[dependencies]
1212
bytes = "1.3.0"
1313
candid = "0.8.2"
14+
cita_trie = "4.0.0"
1415
hex = "0.4.3"
1516
ic-cdk = "0.6.0"
1617
ic-cdk-macros = "0.6.0"
@@ -19,5 +20,6 @@ secp256k1 = { version = "0.26.0", features = [
1920
"global-context",
2021
"recovery",
2122
] }
23+
hasher = { version = "0.1", features = ["hash-keccak"] }
2224
serde = "1.0.152"
2325
sha3 = "0.10.6"

src/evm_utils/src/hash.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::{types::num::U256, utils};
2+
use ic_cdk::query;
3+
4+
/// Returns hash of given data using keccak256
5+
#[query]
6+
fn keccak256(data: Vec<u8>) -> U256 {
7+
utils::keccak256(&[&data[..]])
8+
}

src/evm_utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
mod hash;
12
mod rlp;
23
mod transaction;
4+
mod tree;
35
mod types;
46
mod utils;

src/evm_utils/src/rlp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn rlp_encode(data: List) -> Result<Vec<u8>, String> {
1111

1212
#[query]
1313
fn rlp_decode(raw: Vec<u8>) -> Result<List, String> {
14-
let item: List = rlp::decode(&raw).map_err(|x| format!("{}", x))?;
14+
let item: List = rlp::decode(&raw).map_err(|x| format!("{x}"))?;
1515

1616
Ok(item)
1717
}

src/evm_utils/src/transaction.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,19 @@ use crate::types::transaction::Transaction;
44

55
/// Encodes transaction in rlp, ready to be signed
66
#[query]
7-
fn get_transaction_for_signing(data: Transaction) -> Result<Vec<u8>, String> {
7+
fn create_transaction(data: Transaction) -> Result<Vec<u8>, String> {
88
let raw = data
99
.encode()
10-
.map_err(|x| format!("Error while encoding transaction {}", x))?;
10+
.map_err(|x| format!("Error while encoding transaction {x}"))?;
1111

1212
Ok(raw)
1313
}
1414

15-
/// Converts transaction to a RAW format, returns encoded transaction and its hash
16-
fn create_signed_transaction(data: Transaction) -> Result<Vec<u8>, String> {
17-
Err("not implemented".to_string())
18-
}
19-
2015
/// Parses raw transaction, supports Legacy, EIP1559, EIP2930
2116
#[query]
2217
fn parse_transaction(data: Vec<u8>) -> Result<Transaction, String> {
23-
let item = Transaction::decode(&data)
24-
.map_err(|x| format!("Error while decoding transaction {}", x))?;
18+
let item =
19+
Transaction::decode(&data).map_err(|x| format!("Error while decoding transaction {x}"))?;
2520

2621
Ok(item)
2722
}

src/evm_utils/src/tree.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use hasher::HasherKeccak;
2+
use ic_cdk::query;
3+
4+
#[query]
5+
fn verify_proof(
6+
root: Vec<u8>,
7+
key: Vec<u8>,
8+
proof: Vec<Vec<u8>>,
9+
) -> Result<Option<Vec<u8>>, String> {
10+
let hasher = HasherKeccak::new();
11+
12+
let data = cita_trie::verify_proof(&root, &key, proof, hasher)
13+
.map_err(|x| format!("Error while verifying proof {x}"))?;
14+
15+
Ok(data)
16+
}

src/evm_utils/src/types/address.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Display;
22

33
use ic_cdk::export::candid::{CandidType, Deserialize};
4-
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
4+
use rlp::{Decodable, DecoderError, Encodable, RlpStream};
55
use secp256k1::PublicKey;
66

77
use crate::utils::keccak256;
@@ -51,8 +51,8 @@ impl From<PublicKey> for Address {
5151
//todo: add tests for conversion from public key to address!
5252
#[cfg(test)]
5353
mod test {
54-
use std::str::FromStr;
5554
use std::error::Error;
55+
use std::str::FromStr;
5656

5757
use secp256k1::PublicKey;
5858

@@ -69,4 +69,4 @@ mod test {
6969

7070
Ok(())
7171
}
72-
}
72+
}

0 commit comments

Comments
 (0)