-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move axon-tools to axon (#1519)
* feat: move axon-tools to axon * refactor: improve code organization * test: add axon-tools unit test
- Loading branch information
1 parent
552b328
commit 68aa8a9
Showing
18 changed files
with
2,010 additions
and
2 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 |
---|---|---|
@@ -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"] |
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,3 @@ | ||
This crate is used by forcerelay. | ||
- Data structures like Block, Proposal etc. | ||
- Block and Transaction verification APIs. |
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,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, | ||
]); |
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,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 {} |
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 @@ | ||
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() | ||
} | ||
} |
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,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) | ||
} |
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,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; |
Oops, something went wrong.