Skip to content

Commit

Permalink
feat: move axon-tools to axon
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Nov 1, 2023
1 parent 166506f commit c783383
Show file tree
Hide file tree
Showing 11 changed files with 1,192 additions and 1 deletion.
36 changes: 35 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ members = [
"core/run",
"core/storage",
"devtools/abi-generator",
"devtools/axon-tools",
"devtools/keypair",
"protocol",
]
Expand Down
85 changes: 85 additions & 0 deletions devtools/axon-tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[package]
name = "axon-tools"
version = "0.1.1"
edition = "2021"
authors = ["Axon Dev <[email protected]>"]
license = "MIT"
include = ["src/*", "README.md", "LICENSE"]
readme = "README.md"
keywords = ["axon", "tool"]
categories = ["cryptography"]
repository = "https://github.com/axonweb3/axon"
description = """
Some axon related utilities.
"""

[dev-dependencies]
ethereum = "0.14"
rand = "0.8"

[dependencies]
derive_more = "0.99"
log = "0.4.19"
overlord = "0.4"
protocol = { path = "../../protocol", package = "axon-protocol" }
serde_json = "1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.bit-vec]
version = "0.6"
default_features = false
optional = true

[dependencies.blst]
version = "0.3"
optional = true

[dependencies.bytes]
version = "1.4"
default-features = false
features = ["serde"]

[dependencies.cita_trie]
version = "4.0"
optional = true

[dependencies.ethereum-types]
version = "0.14"
default-features = false
features = ["serialize"]

[dependencies.faster-hex]
version = "0.8"
optional = true

[dependencies.rlp]
version = "0.5"
default-features = false
optional = true

[dependencies.rlp-derive]
version = "0.1"
optional = true

[dependencies.serde]
version = "1.0"
default_features = false
optional = true
features = ["derive"]

[dependencies.tiny-keccak]
version = "2.0"
optional = true
features = ["keccak"]

[features]
default = ["impl-serde", "proof"]
proof = ["blst", "bit-vec", "cita_trie", "hash", "impl-rlp"]
hash = ["tiny-keccak"]
hex = ["faster-hex"]
impl-rlp = ["rlp", "rlp-derive", "ethereum-types/rlp"]
impl-serde = ["serde", "ethereum-types/serialize", "hex"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]
3 changes: 3 additions & 0 deletions devtools/axon-tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This crate is used by forcerelay.
- Data structures like Block, Proposal etc.
- Block and Transaction verification APIs.
16 changes: 16 additions & 0 deletions devtools/axon-tools/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ethereum_types::H160;

pub const METADATA_CONTRACT_ADDRESS: H160 = H160([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x01,
]);

pub const CKB_LIGHT_CLIENT_CONTRACT_ADDRESS: H160 = H160([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x02,
]);

pub const IMAGE_CELL_CONTRACT_ADDRESS: H160 = H160([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x03,
]);
63 changes: 63 additions & 0 deletions devtools/axon-tools/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::fmt::{self, Display};

#[allow(dead_code)]
#[derive(Debug)]
pub enum Error {
InvalidProofBlockHash,
NotEnoughSignatures,
VerifyMptProof,
HexPrefix,

#[cfg(feature = "hex")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hex")))]
Hex(faster_hex::Error),

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
Bls(blst::BLST_ERROR),

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
Trie(cita_trie::TrieError),
}

#[cfg(feature = "hex")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hex")))]
impl From<faster_hex::Error> for Error {
fn from(value: faster_hex::Error) -> Self {
Self::Hex(value)
}
}

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
impl From<blst::BLST_ERROR> for Error {
fn from(e: blst::BLST_ERROR) -> Self {
Self::Bls(e)
}
}

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
impl From<cita_trie::TrieError> for Error {
fn from(e: cita_trie::TrieError) -> Self {
Self::Trie(e)
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::InvalidProofBlockHash => write!(f, "Invalid proof block hash"),
Error::NotEnoughSignatures => write!(f, "Not enough signatures"),
Error::VerifyMptProof => write!(f, "Verify mpt proof"),
Error::HexPrefix => write!(f, "Hex prefix"),
#[cfg(feature = "hex")]
Error::Hex(e) => write!(f, "Hex error: {:?}", e),
#[cfg(feature = "proof")]
Error::Bls(e) => write!(f, "Bls error: {:?}", e),
#[cfg(feature = "proof")]
Error::Trie(e) => write!(f, "Trie error: {:?}", e),
}
}
}
22 changes: 22 additions & 0 deletions devtools/axon-tools/src/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use tiny_keccak::{Hasher, Keccak};

#[cfg(feature = "hash")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hash")))]
pub fn keccak_256(data: &[u8]) -> [u8; 32] {
let mut ret = [0u8; 32];
let mut hasher = Keccak::v256();
hasher.update(data);
hasher.finalize(&mut ret);
ret
}

#[derive(Default)]
pub(crate) struct InnerKeccak;

impl cita_trie::Hasher for InnerKeccak {
const LENGTH: usize = 32;

fn digest(&self, data: &[u8]) -> Vec<u8> {
keccak_256(data).to_vec()
}
}
23 changes: 23 additions & 0 deletions devtools/axon-tools/src/hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::Error;

pub fn hex_encode<T: AsRef<[u8]>>(src: T) -> String {
faster_hex::hex_string(src.as_ref())
}

pub fn hex_decode(src: &str) -> Result<Vec<u8>, Error> {
if src.is_empty() {
return Ok(Vec::new());
}

let src = if src.starts_with("0x") {
src.split_at(2).1
} else {
src
};

let src = src.as_bytes();
let mut ret = vec![0u8; src.len() / 2];
faster_hex::hex_decode(src, &mut ret)?;

Ok(ret)
}
25 changes: 25 additions & 0 deletions devtools/axon-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]

extern crate alloc;

mod error;
#[cfg(feature = "hash")]
pub mod hash;
#[cfg(feature = "hex")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hex")))]
pub mod hex;
#[cfg(feature = "proof")]
mod proof;
pub mod types;

pub use error::Error;

#[cfg(feature = "proof")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "proof")))]
pub use proof::{verify_proof, verify_trie_proof};

#[cfg(feature = "hash")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hash")))]
pub use hash::keccak_256;

pub mod consts;
Loading

0 comments on commit c783383

Please sign in to comment.