-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starknet tx, tx receipt support (#13)
* wip * wip * wip * wipd * proof/verify abstract * cleaner * dependency to sn-trie * omg it works * update test cases * wip * abstract bitvec, clean up test, embed gateway * quick update with poseidon * feat: tx support done * wip clean up * ci: add sub rpc * folder structure change * sketch refactoring * image * alter dependency * receipt support * chore: minor * readme * sub rpc
- Loading branch information
Showing
23 changed files
with
1,630 additions
and
555 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,114 +1,75 @@ | ||
![](.github/readme.png) | ||
# trie-proofs | ||
|
||
# eth-trie-proofs | ||
A comprehensive proofs handler for Ethereum and Starknet tries. This repository provides proof-building functionalities and includes a CLI tool. | ||
|
||
A comprehensive proofs handler for Ethereum trie. Tested with various EIPs including Legacy, EIP-2930, EIP-1559, and EIP-4844. This repository exposes the proof building functionalities, and a CLI version. | ||
## Supported Crates | ||
|
||
## Features | ||
- [x] [Ethereum Transaction/Receipt MPT Handler](./crates/eth-trie-proofs/README.md): Constructs transaction and receipt tries using a target block number or transaction hash, following Ethereum's Merkle Patricia Tree (MPT) specification. | ||
|
||
- **Transaction Trie Handler** | ||
|
||
- [x] Build a trie with a target block number | ||
- [x] Build a trie with a target transaction hash | ||
- [x] Retrieve proof by transaction index | ||
- [x] Verify proof | ||
|
||
- **Transaction Receipt Trie Handler** | ||
- [x] Build a trie with a target block number | ||
- [x] Build a trie with a target transaction hash | ||
- [x] Retrieve proof by transaction index | ||
- [x] Verify proof | ||
|
||
## CLI Tool | ||
The CLI tool supports generating proofs for transactions and receipts. Use the following commands based on your requirements: | ||
|
||
Install with: `cargo install --path ./` | ||
- [x] [Starknet Transaction/Receipt MPT Handler](./crates/sn-trie-proofs/README.md): Constructs transaction and receipt tries using a target block number, following Starknet's Merkle Patricia Tree (MPT) specification. | ||
|
||
Or, run without installing: `cargo run --bin etp-cli` | ||
## Trie Handler | ||
|
||
- **Transaction Trie Handler** | ||
|
||
**Generate a Proof via CLI** | ||
- [x] Builds a trie with a target block number. | ||
- [x] Builds a trie with a target transaction hash. | ||
- [x] Retrieves proof by transaction index. | ||
- [x] Verifies proof. | ||
|
||
To generate a proof for a transaction, use the following command: | ||
- **Transaction Receipt Trie Handler** | ||
|
||
`etp-cli tx <TRANSACTION_HASH> [RPC_URL]` | ||
- [x] Builds a trie with a target block number. | ||
- [x] Builds a trie with a target transaction hash. | ||
- [x] Retrieves proof by transaction index. | ||
- [x] Verifies proof. | ||
|
||
To generate a receipt proof: | ||
## CLI Tool | ||
|
||
`etp-cli receipt <TRANSACTION_HASH> [RPC_URL]` | ||
_Currently only supports Ethereum MPT._ | ||
|
||
As a default, `https://ethereum-rpc.publicnode.com` is used as an RPC provider. This will probably work for recent transactions, but it is advised to use a dedicated RPC. | ||
The CLI tool supports generating proofs for transactions and receipts. Use the following commands based on your requirements. | ||
|
||
## Installation | ||
### Installation | ||
|
||
Add dependency `eth-trie-proofs` to your project: | ||
Install the CLI tool using Cargo: | ||
|
||
```shell | ||
cargo install --path ./ | ||
``` | ||
eth-trie-proofs = { git = "https://github.com/HerodotusDev/eth-trie-proofs.git", branch = "main" } | ||
``` | ||
|
||
## Example | ||
|
||
### Building a Transaction Trie with a Target Block Number or Target Transaction Hash | ||
Or run it without installing: | ||
|
||
```rust | ||
let target_tx_hash = B256::from(hex!( | ||
"1fcb1196d8a3bff0bcf13309d2d2bb1a23ae1ac13f5674c801be0ff9254d5ab5" | ||
)); | ||
```shell | ||
cargo run --bin etp-cli | ||
``` | ||
|
||
let mut txs_mpt_handler = TxsMptHandler::new(MAINNET_RPC_URL)?; | ||
### Generate a Proof via CLI | ||
|
||
txs_mpt_handler | ||
.build_tx_tree_from_block(4370000) | ||
.await?; | ||
To generate a proof for a transaction: | ||
|
||
let tx_index = txs_mpt_handler.tx_hash_to_tx_index(target_tx_hash)?; | ||
let proof = txs_mpt_handler.get_proof(tx_index)?; | ||
txs_mpt_handler | ||
.verify_proof(tx_index, proof.clone())?; | ||
```shell | ||
etp-cli tx <TRANSACTION_HASH> [RPC_URL] | ||
``` | ||
|
||
// You can either build with target tx hash. Both roots match. | ||
let mut txs_mpt_handler2 = TxsMptHandler::new(MAINNET_RPC_URL)?; | ||
txs_mpt_handler2 | ||
.build_tx_tree_from_tx_hash(target_tx_hash) | ||
.await?; | ||
To generate a receipt proof: | ||
|
||
assert_eq!( | ||
txs_mpt_handler.get_root().unwrap(), | ||
txs_mpt_handler2.get_root().unwrap() | ||
); | ||
```shell | ||
etp-cli receipt <TRANSACTION_HASH> [RPC_URL] | ||
``` | ||
|
||
### Building a Transaction Receipts Trie with a Target Block Number | ||
|
||
```rust | ||
// 4844 transaction | ||
let target_tx_hash = B256::from(hex!( | ||
"9c1fbda4f649ac806ab0faefbe94e1a60282eb374ead6aa01bac042f52b28a8c" | ||
)); | ||
|
||
let mut tx_receipts_mpt_handler = TxReceiptsMptHandler::new(MAINNET_RPC_URL)?; | ||
tx_receipts_mpt_handler | ||
.build_tx_receipts_tree_from_block(19426589) | ||
.await?; | ||
|
||
let tx_index = tx_receipts_mpt_handler | ||
.tx_hash_to_tx_index(target_tx_hash) | ||
.await?; | ||
let proof = tx_receipts_mpt_handler.get_proof(tx_index)?; | ||
tx_receipts_mpt_handler | ||
.verify_proof(tx_index, proof.clone())?; | ||
``` | ||
By default, `https://ethereum-rpc.publicnode.com` is used as the RPC provider. While this may work for recent transactions, it is advisable to use a dedicated RPC provider for better reliability. | ||
|
||
### Credit | ||
## Contributing | ||
|
||
For trie implementation, this project depends on the [eth_trie](https://crates.io/crates/eth_trie). | ||
For transaction and transaction receipt types, heavily depends on the [alloy](https://github.com/alloy-rs/alloy). | ||
Contributions are welcome! If you'd like to contribute to this project, please fork the repository and submit a pull request. For major changes, please open an issue first to discuss what you would like to change. | ||
|
||
## License | ||
|
||
`eth-trie-proofs` is licensed under the [GNU General Public License v3.0](./LICENSE). | ||
`trie-proofs` is licensed under the [GNU General Public License v3.0](./LICENSE). | ||
|
||
--- | ||
|
||
Herodotus Dev Ltd - 2024 | ||
|
||
--- |
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,14 @@ | ||
[package] | ||
name = "cli" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
eth-trie-proofs.workspace = true | ||
tokio.workspace = true | ||
clap.workspace = true | ||
serde_with.workspace = true | ||
serde.workspace = true | ||
serde_json.workspace = true | ||
alloy-primitives.workspace = true | ||
url.workspace = true |
File renamed without changes.
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,22 @@ | ||
[package] | ||
name = "eth-trie-proofs" | ||
version = "0.1.1" | ||
edition = "2021" | ||
description = "Ethereum Transaction Trie Proofs" | ||
readme = "README.md" | ||
license-file = "LICENSE" | ||
repository = "https://github.com/HerodotusDev/eth-trie-proofs" | ||
keywords = ["mpt", "trie", "ethereum"] | ||
categories = ["cryptography", "data-structures", "blockchain"] | ||
exclude = [".github"] | ||
|
||
[dependencies] | ||
tokio.workspace = true | ||
alloy-primitives.workspace = true | ||
alloy.workspace = true | ||
url.workspace = true | ||
reqwest.workspace = true | ||
alloy-rlp.workspace = true | ||
eth_trie.workspace = true | ||
ethereum-types.workspace = true | ||
thiserror.workspace = true |
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,81 @@ | ||
# eth-trie-proofs | ||
|
||
![CI](https://img.shields.io/github/actions/workflow/status/HerodotusDev/eth-trie-proofs/ci.yml?style=flat-square&logo=githubactions&logoColor=white&label=CI) | ||
[![Crates.io](https://img.shields.io/crates/v/eth-trie-proofs?style=flat-square&logo=lootcrate)](https://crates.io/crates/eth-trie-proofs) | ||
[![Documentation](https://img.shields.io/docsrs/eth-trie-proofs)](https://docs.rs/eth-trie-proofs) | ||
|
||
![](.github/readme.png) | ||
|
||
A comprehensive transaction/receipt inclusion proofs handler for [Ethereum trie](https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/). Tested with various EIPs including Legacy, EIP-2930, EIP-1559, and EIP-4844. This library exposes various proof building functionalities, verification, trie construction etc. | ||
|
||
## Installation | ||
|
||
Add dependency `eth-trie-proofs` to your project: | ||
|
||
``` | ||
eth-trie-proofs = { version= "0.1.1" } | ||
``` | ||
|
||
## Usage | ||
|
||
- **Transaction Trie Handler** | ||
|
||
```rust | ||
#[tokio::test] | ||
async fn test_tx_mpt_frontier() { | ||
let url = Url::parse(MAINNET_RPC_URL_SUB).unwrap(); | ||
let target_tx_hash = B256::from(hex!( | ||
"5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060" | ||
)); | ||
let mut txs_mpt_handler = TxsMptHandler::new(url).unwrap(); | ||
txs_mpt_handler | ||
.build_tx_tree_from_block(46147) | ||
.await | ||
.unwrap(); | ||
let tx_index = txs_mpt_handler.tx_hash_to_tx_index(target_tx_hash).unwrap(); | ||
let proof = txs_mpt_handler.get_proof(tx_index).unwrap(); | ||
txs_mpt_handler | ||
.verify_proof(tx_index, proof.clone()) | ||
.unwrap(); | ||
} | ||
``` | ||
|
||
- **Transaction Receipt Trie Handler** | ||
|
||
```rust | ||
#[tokio::test] | ||
async fn test_tx_receipt_1559() { | ||
let url = Url::parse(MAINNET_RPC_URL).unwrap(); | ||
let target_tx_hash = B256::from(hex!( | ||
"2055b7e01304f87f9412cd44758cd248bc2da2dab95c97026064ffb084711735" | ||
)); | ||
|
||
let mut tx_receipts_mpt_handler = TxReceiptsMptHandler::new(url).unwrap(); | ||
tx_receipts_mpt_handler | ||
.build_tx_receipts_tree_from_block(12965000) | ||
.await | ||
.unwrap(); | ||
|
||
let tx_index = tx_receipts_mpt_handler | ||
.tx_hash_to_tx_index(target_tx_hash) | ||
.await | ||
.unwrap(); | ||
let proof = tx_receipts_mpt_handler.get_proof(tx_index).unwrap(); | ||
tx_receipts_mpt_handler | ||
.verify_proof(tx_index, proof.clone()) | ||
.unwrap(); | ||
} | ||
``` | ||
|
||
### Credit | ||
|
||
For trie implementation, this project depends on the [eth_trie](https://crates.io/crates/eth_trie). | ||
For transaction and transaction receipt types, heavily depends on the [alloy](https://github.com/alloy-rs/alloy). | ||
|
||
## License | ||
|
||
`eth-trie-proofs` is licensed under the [GNU General Public License v3.0](../../LICENSE). | ||
|
||
--- | ||
|
||
Herodotus Dev Ltd - 2024 |
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,8 @@ | ||
pub mod error; | ||
mod rpc; | ||
pub mod tx; | ||
pub mod tx_receipt; | ||
pub mod tx_receipt_trie; | ||
pub mod tx_trie; | ||
|
||
pub use error::EthTrieError; |
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
Oops, something went wrong.