-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce Digest32 newtype and use it token, box, tx ids;
- Loading branch information
Showing
10 changed files
with
144 additions
and
102 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
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 |
---|---|---|
@@ -1,38 +1,76 @@ | ||
use std::convert::TryInto; | ||
use crate::Base16DecodedBytes; | ||
use crate::Base16EncodedBytes; | ||
#[cfg(test)] | ||
use proptest_derive::Arbitrary; | ||
#[cfg(feature = "with-serde")] | ||
use serde::{Deserialize, Serialize}; | ||
use sigma_ser::{ | ||
serializer::{SerializationError, SigmaSerializable}, | ||
vlq_encode, | ||
}; | ||
use std::{ | ||
convert::{TryFrom, TryInto}, | ||
io, | ||
}; | ||
use thiserror::Error; | ||
|
||
/// Digest size 32 bytes | ||
pub const DIGEST32_SIZE: usize = 32; | ||
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr( | ||
feature = "with-serde", | ||
serde(into = "Base16EncodedBytes", try_from = "Base16DecodedBytes") | ||
)] | ||
#[derive(PartialEq, Eq, Hash, Debug, Clone)] | ||
#[cfg_attr(test, derive(Arbitrary))] | ||
pub struct Digest32(pub Box<[u8; Digest32::SIZE]>); | ||
|
||
/// Errors when decoding Digest32 from Base16 encoded string | ||
#[derive(Error, Debug)] | ||
pub enum Digest32DecodeError { | ||
/// Error on decoding from Base16 | ||
#[error("Base16 decoding error: {0}")] | ||
Base16DecodeError(base16::DecodeError), | ||
/// Invalid size of the decoded byte array | ||
#[error("Invalid byte array size")] | ||
InvalidSize, | ||
impl Digest32 { | ||
/// Digest size 32 bytes | ||
pub const SIZE: usize = 32; | ||
|
||
/// All zeros | ||
pub fn zero() -> Digest32 { | ||
Digest32(Box::new([0u8; Digest32::SIZE])) | ||
} | ||
} | ||
|
||
impl From<base16::DecodeError> for Digest32DecodeError { | ||
fn from(e: base16::DecodeError) -> Self { | ||
Digest32DecodeError::Base16DecodeError(e) | ||
impl From<[u8; Digest32::SIZE]> for Digest32 { | ||
fn from(bytes: [u8; Digest32::SIZE]) -> Self { | ||
Digest32(Box::new(bytes)) | ||
} | ||
} | ||
|
||
impl From<std::array::TryFromSliceError> for Digest32DecodeError { | ||
fn from(_: std::array::TryFromSliceError) -> Self { | ||
Digest32DecodeError::InvalidSize | ||
impl Into<Base16EncodedBytes> for Digest32 { | ||
fn into(self) -> Base16EncodedBytes { | ||
Base16EncodedBytes::new(self.0.as_ref()) | ||
} | ||
} | ||
|
||
pub fn decode_base16(value: String) -> Result<[u8; DIGEST32_SIZE], Digest32DecodeError> { | ||
let bytes = base16::decode(&value)?; | ||
let arr: [u8; DIGEST32_SIZE] = bytes.as_slice().try_into()?; | ||
Ok(arr) | ||
impl TryFrom<Base16DecodedBytes> for Digest32 { | ||
type Error = Digest32Error; | ||
fn try_from(bytes: Base16DecodedBytes) -> Result<Self, Self::Error> { | ||
let arr: [u8; Digest32::SIZE] = bytes.0.as_slice().try_into()?; | ||
Ok(Digest32(Box::new(arr))) | ||
} | ||
} | ||
|
||
pub fn encode_base16(value: &[u8; DIGEST32_SIZE]) -> String { | ||
base16::encode_lower(value) | ||
impl SigmaSerializable for Digest32 { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: &mut W) -> Result<(), io::Error> { | ||
w.write_all(self.0.as_ref())?; | ||
Ok(()) | ||
} | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: &mut R) -> Result<Self, SerializationError> { | ||
let mut bytes = [0; Digest32::SIZE]; | ||
r.read_exact(&mut bytes)?; | ||
Ok(Self(bytes.into())) | ||
} | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
#[error("Invalid byte array size ({0})")] | ||
pub struct Digest32Error(std::array::TryFromSliceError); | ||
|
||
impl From<std::array::TryFromSliceError> for Digest32Error { | ||
fn from(err: std::array::TryFromSliceError) -> Self { | ||
Digest32Error(err) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,43 @@ | ||
//! Transitioning type for Base16 encoded bytes in JSON serialization | ||
|
||
use core::fmt; | ||
use serde::{Deserialize, Serialize}; | ||
use std::convert::TryFrom; | ||
|
||
/// Transitioning type for Base16 encoded bytes | ||
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "with-serde", serde(into = "String", try_from = "String"))] | ||
#[cfg_attr(feature = "with-serde", serde(into = "String"))] | ||
#[derive(PartialEq, Eq, Debug, Clone)] | ||
pub struct Base16Bytes(pub Vec<u8>); | ||
pub struct Base16EncodedBytes(String); | ||
|
||
impl Into<String> for Base16Bytes { | ||
impl Base16EncodedBytes { | ||
/// Create from byte array ref (&[u8]) | ||
pub fn new<T: ?Sized + AsRef<[u8]>>(input: &T) -> Base16EncodedBytes { | ||
Base16EncodedBytes(base16::encode_lower(input)) | ||
} | ||
} | ||
|
||
impl Into<String> for Base16EncodedBytes { | ||
fn into(self) -> String { | ||
base16::encode_lower(&self.0) | ||
self.0 | ||
} | ||
} | ||
|
||
impl TryFrom<String> for Base16Bytes { | ||
impl fmt::Display for Base16EncodedBytes { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("TokenId").field(&self.0).finish() | ||
} | ||
} | ||
|
||
/// Transitioning type for Base16 decoded bytes | ||
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "with-serde", serde(try_from = "String"))] | ||
#[derive(PartialEq, Eq, Debug, Clone)] | ||
pub struct Base16DecodedBytes(pub Vec<u8>); | ||
|
||
impl TryFrom<String> for Base16DecodedBytes { | ||
type Error = base16::DecodeError; | ||
fn try_from(str: String) -> Result<Self, Self::Error> { | ||
Ok(Base16Bytes(base16::decode(&str)?)) | ||
Ok(Base16DecodedBytes(base16::decode(&str)?)) | ||
} | ||
} |
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.