-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
185 additions
and
116 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,79 @@ | ||
//! EVM ABI implementation | ||
//! Zink ABI implementation | ||
//! | ||
//! https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
pub use self::result::{Error, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
//! Currently just a wrapper of solidity ABI. | ||
mod codec; | ||
mod result; | ||
pub mod util; | ||
pub mod result; | ||
pub mod selector; | ||
|
||
use core::ops::{Deref, DerefMut}; | ||
|
||
/// Function ABI. | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Abi { | ||
/// Function name. | ||
pub name: String, | ||
/// Function inputs. | ||
pub inputs: Vec<String>, | ||
#[derive(Debug, Clone)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct Abi(evm_abi::Abi); | ||
|
||
impl Deref for Abi { | ||
type Target = evm_abi::Abi; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for Abi { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
#[cfg(feature = "bytes")] | ||
impl Abi { | ||
/// Get function signature. | ||
pub fn signature(&self) -> String { | ||
self.name.clone() + "(" + &self.inputs.join(",") + ")" | ||
/// Convert [`Abi`] to bytes. | ||
pub fn to_bytes(&self) -> postcard::Result<Vec<u8>> { | ||
postcard::to_stdvec(self).map_err(Into::into) | ||
} | ||
|
||
/// Convert bytes to [`Abi`]. | ||
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> postcard::Result<Self> { | ||
postcard::from_bytes(bytes.as_ref()).map_err(Into::into) | ||
} | ||
} | ||
|
||
#[cfg(feature = "hex")] | ||
mod hex_impl { | ||
use crate::{result::Result, Abi}; | ||
|
||
impl Abi { | ||
/// Convert [`Abi`] to hex string. | ||
pub fn to_hex(&self) -> Result<String> { | ||
Ok("0x".to_string() + &hex::encode(self.to_bytes()?)) | ||
} | ||
|
||
/// Convert hex string to [`Abi`]. | ||
pub fn from_hex(hex: impl AsRef<str>) -> Result<Self> { | ||
Self::from_bytes(hex::decode(hex.as_ref().trim_start_matches("0x"))?) | ||
.map_err(Into::into) | ||
} | ||
} | ||
|
||
impl ToString for Abi { | ||
fn to_string(&self) -> String { | ||
self.to_hex().unwrap_or_default() | ||
} | ||
} | ||
|
||
/// Get function selector. | ||
pub fn selector(&self) -> [u8; 4] { | ||
let sig = self.signature(); | ||
util::selector(sig.as_bytes()) | ||
impl core::str::FromStr for Abi { | ||
type Err = crate::result::Error; | ||
|
||
fn from_str(hex: &str) -> Result<Self> { | ||
Self::from_hex(hex) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "syn")] | ||
impl From<&syn::Signature> for Abi { | ||
fn from(sig: &syn::Signature) -> Self { | ||
Self(evm_abi::Abi::from(sig)) | ||
} | ||
} |
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,4 +1,5 @@ | ||
//! Abi results | ||
#![cfg(feature = "hex")] | ||
|
||
/// ABI error | ||
#[derive(Debug, thiserror::Error)] | ||
|
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,39 @@ | ||
//! Zink ABI utils | ||
#![cfg(feature = "selector")] | ||
|
||
use crate::Abi; | ||
use sha3::{Digest, Keccak256}; | ||
|
||
/// Generate a keccak hash of the input (sha3) | ||
pub fn keccak256(input: &[u8]) -> [u8; 32] { | ||
let mut hasher = Keccak256::new(); | ||
hasher.update(input); | ||
hasher.finalize().into() | ||
} | ||
|
||
/// Parse selector from bytes. | ||
pub fn parse(bytes: &[u8]) -> [u8; 4] { | ||
let mut selector = [0u8; 4]; | ||
selector.copy_from_slice(&keccak256(bytes)[..4]); | ||
selector | ||
} | ||
|
||
impl Abi { | ||
/// Get function signature. | ||
pub fn signature(&self) -> String { | ||
self.name.clone() | ||
+ "(" | ||
+ &self | ||
.inputs | ||
.iter() | ||
.map(|i| i.ty.as_ref()) | ||
.collect::<Vec<_>>() | ||
.join(",") | ||
+ ")" | ||
} | ||
|
||
/// Get function selector. | ||
pub fn selector(&self) -> [u8; 4] { | ||
parse(self.signature().as_bytes()) | ||
} | ||
} |
This file was deleted.
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
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 @@ | ||
|
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.