-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types: JSON representation types (#300)
* add `cuprate_types::json` * docs * `Option` -> flattened enums + prefix structs * output enum * docs * todo!() epee impl * cuprate-rpc-types: add comments * cuprate-rpc-types: common `TxEntry` fields into prefix struct * remove epee * docs * add `hex` module * `From` serai types * cleanup * proofs * tx from impls * fix tx timelock * add block value tests * add ringct types * add tx_v1, tx_rct_3 test * clsag bulletproofs tx test * clsag bulletproofs plus tx test * docs * fix hex bytes * typo * docs
- Loading branch information
1 parent
a003e05
commit 80bfe0a
Showing
14 changed files
with
2,004 additions
and
26 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 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
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,74 @@ | ||
//! Hexadecimal serde wrappers for arrays. | ||
//! | ||
//! This module provides transparent wrapper types for | ||
//! arrays that (de)serialize from hexadecimal input/output. | ||
|
||
#[cfg(feature = "epee")] | ||
use cuprate_epee_encoding::{error, macros::bytes, EpeeValue, Marker}; | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Wrapper type for a byte array that (de)serializes from/to hexadecimal strings. | ||
/// | ||
/// # Deserialization | ||
/// This struct has a custom deserialization that only applies to certain | ||
/// `N` lengths because [`hex::FromHex`] does not implement for a generic `N`: | ||
/// <https://docs.rs/hex/0.4.3/src/hex/lib.rs.html#220-230> | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
#[cfg_attr(feature = "serde", derive(Serialize))] | ||
#[cfg_attr(feature = "serde", serde(transparent))] | ||
#[repr(transparent)] | ||
pub struct HexBytes<const N: usize>( | ||
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] pub [u8; N], | ||
); | ||
|
||
impl<'de, const N: usize> Deserialize<'de> for HexBytes<N> | ||
where | ||
[u8; N]: hex::FromHex, | ||
<[u8; N] as hex::FromHex>::Error: std::fmt::Display, | ||
{ | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
Ok(Self(hex::serde::deserialize(deserializer)?)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "epee")] | ||
impl<const N: usize> EpeeValue for HexBytes<N> { | ||
const MARKER: Marker = <[u8; N] as EpeeValue>::MARKER; | ||
|
||
fn read<B: bytes::Buf>(r: &mut B, marker: &Marker) -> error::Result<Self> { | ||
Ok(Self(<[u8; N] as EpeeValue>::read(r, marker)?)) | ||
} | ||
|
||
fn write<B: bytes::BufMut>(self, w: &mut B) -> error::Result<()> { | ||
<[u8; N] as EpeeValue>::write(self.0, w) | ||
} | ||
} | ||
|
||
// Default is not implemented for arrays >32, so we must do it manually. | ||
impl<const N: usize> Default for HexBytes<N> { | ||
fn default() -> Self { | ||
Self([0; N]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn hex_bytes_32() { | ||
let hash = [1; 32]; | ||
let hex_bytes = HexBytes::<32>(hash); | ||
let expected_json = r#""0101010101010101010101010101010101010101010101010101010101010101""#; | ||
|
||
let to_string = serde_json::to_string(&hex_bytes).unwrap(); | ||
assert_eq!(to_string, expected_json); | ||
|
||
let from_str = serde_json::from_str::<HexBytes<32>>(expected_json).unwrap(); | ||
assert_eq!(hex_bytes, from_str); | ||
} | ||
} |
Oops, something went wrong.