-
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.
feat(abi): introduce macro impl_codec for encoding ABIs
- Loading branch information
Showing
5 changed files
with
69 additions
and
47 deletions.
There are no files selected for viewing
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,46 @@ | ||
//! Serialization and Deserialization for Zink ABI. | ||
use crate::{Abi, Error, Result}; | ||
|
||
/// Implement encoding and decoding for Zink ABI. | ||
macro_rules! impl_codec { | ||
($ty:ident) => { | ||
impl $ty { | ||
/// Convert self to bytes. | ||
pub fn to_bytes(&self) -> Result<Vec<u8>> { | ||
postcard::to_stdvec(self).map_err(Into::into) | ||
} | ||
|
||
/// Convert self to hex string. | ||
pub fn to_hex(&self) -> Result<String> { | ||
Ok(hex::encode(self.to_bytes()?)) | ||
} | ||
|
||
/// Convert bytes to self. | ||
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self> { | ||
postcard::from_bytes(bytes.as_ref()).map_err(Into::into) | ||
} | ||
|
||
/// Convert hex string to self. | ||
pub fn from_hex(hex: impl AsRef<str>) -> Result<Self> { | ||
Self::from_bytes(&hex::decode(hex.as_ref().trim_start_matches("0x"))?) | ||
} | ||
} | ||
|
||
impl ToString for $ty { | ||
fn to_string(&self) -> String { | ||
self.to_hex().unwrap_or_default() | ||
} | ||
} | ||
|
||
impl core::str::FromStr for $ty { | ||
type Err = Error; | ||
|
||
fn from_str(hex: &str) -> Result<Self> { | ||
Self::from_hex(hex) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_codec!(Abi); |
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,17 @@ | ||
//! Zink ABI utils | ||
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() | ||
} | ||
|
||
/// Get function selector from function signature. | ||
pub fn selector(input: &[u8]) -> [u8; 4] { | ||
let mut selector = [0u8; 4]; | ||
selector.copy_from_slice(&keccak256(input)[..4]); | ||
|
||
selector | ||
} |
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