Skip to content

Commit

Permalink
feat: move axon-tools to axon (#1519)
Browse files Browse the repository at this point in the history
* feat: move axon-tools to axon

* refactor: improve code organization

* test: add axon-tools unit test
  • Loading branch information
wenyuanhust authored Nov 2, 2023
1 parent 552b328 commit 68aa8a9
Show file tree
Hide file tree
Showing 18 changed files with 2,010 additions and 2 deletions.
499 changes: 497 additions & 2 deletions Cargo.lock

Large diffs are not rendered by default.

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
87 changes: 87 additions & 0 deletions devtools/axon-tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[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]
eth_light_client_in_ckb-prover = { version = "0.3.0-alpha", git = "https://github.com/synapseweb3/eth-light-client-in-ckb" }
ethereum = "0.14"
ethers-core = "2.0.10"
hex = "0.4"
rand = "0.8"

[dependencies]
derive_more = "0.99"
log = "0.4.19"
overlord = "0.4"
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,
]);
121 changes: 121 additions & 0 deletions devtools/axon-tools/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use derive_more::{Display, From};
use ethereum_types::H256;
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),
}
}
}

#[derive(Debug, Display, From)]
pub enum TypesError {
#[display(fmt = "Expect {:?}, get {:?}.", expect, real)]
LengthMismatch { expect: usize, real: usize },

#[display(
fmt = "Eip1559Transaction hash mismatch origin {:?}, computed {:?}",
origin,
calc
)]
TxHashMismatch { origin: H256, calc: H256 },

#[display(fmt = "{:?}", _0)]
FromHex(faster_hex::Error),

#[display(fmt = "{:?} is an invalid address", _0)]
InvalidAddress(String),

#[display(fmt = "Hex should start with 0x")]
HexPrefix,

#[display(fmt = "Invalid public key")]
InvalidPublicKey,

#[display(fmt = "Invalid check sum")]
InvalidCheckSum,

#[display(fmt = "Unsigned")]
Unsigned,

// #[display(fmt = "Crypto error {:?}", _0)]
// Crypto(CryptoError),
#[display(fmt = "Missing signature")]
MissingSignature,

#[display(fmt = "Invalid crosschain direction")]
InvalidDirection,

#[display(fmt = "Signature R is empty")]
SignatureRIsEmpty,

#[display(fmt = "Invalid signature R type")]
InvalidSignatureRType,

#[display(fmt = "Invalid address source type")]
InvalidAddressSourceType,

#[display(fmt = "Missing interoperation sender")]
MissingInteroperationSender,

#[display(fmt = "InvalidBlockVersion {:?}", _0)]
InvalidBlockVersion(u8),
}

impl std::error::Error for TypesError {}
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)
}
29 changes: 29 additions & 0 deletions devtools/axon-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![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;
mod rlp_codec;
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;

#[cfg(test)]
mod tests;
Loading

0 comments on commit 68aa8a9

Please sign in to comment.