From 7c6a04dc353cad1d8cad9172179b761b68c8a6e9 Mon Sep 17 00:00:00 2001 From: "xchdata.io" Date: Wed, 10 Jul 2024 12:19:37 +0200 Subject: [PATCH] Add serde serialisation for Streamables --- Cargo.lock | 2 ++ Cargo.toml | 1 + crates/chia-bls/Cargo.toml | 2 ++ crates/chia-bls/src/public_key.rs | 10 ++++++++++ crates/chia-bls/src/signature.rs | 10 ++++++++++ crates/chia-protocol/Cargo.toml | 2 ++ crates/chia-protocol/src/bytes.rs | 8 ++++++++ crates/chia-protocol/src/chia_protocol.rs | 2 ++ crates/chia-protocol/src/program.rs | 1 + crates/chia-protocol/src/wallet_protocol.rs | 1 + crates/chia_streamable_macro/src/lib.rs | 1 + 11 files changed, 40 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e4ede321e..6ee57ef54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -323,6 +323,7 @@ dependencies = [ "pyo3", "rand", "rstest", + "serde", "sha2", "thiserror", ] @@ -404,6 +405,7 @@ dependencies = [ "hex", "pyo3", "rstest", + "serde", "sha2", ] diff --git a/Cargo.toml b/Cargo.toml index 41d96bcef..88fc3a564 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -130,6 +130,7 @@ rcgen = "0.13.1" rsa = "0.9.5" time = "0.3.22" rusqlite = "0.31.0" +serde = { version = "1.0.198", features = ["derive"] } clap = "4.5.8" zstd = "0.13.2" blocking-threadpool = "1.0.1" diff --git a/crates/chia-bls/Cargo.toml b/crates/chia-bls/Cargo.toml index 8bb42ce23..323a3f3f2 100644 --- a/crates/chia-bls/Cargo.toml +++ b/crates/chia-bls/Cargo.toml @@ -14,6 +14,7 @@ workspace = true [features] py-bindings = ["dep:pyo3", "chia_py_streamable_macro", "chia-traits/py-bindings"] arbitrary = ["dep:arbitrary"] +serde = ["dep:serde"] [dependencies] chia-traits = { workspace = true } @@ -27,6 +28,7 @@ thiserror = { workspace = true } pyo3 = { workspace = true, features = ["multiple-pymethods"], optional = true } arbitrary = { workspace = true, optional = true } lru = { workspace = true } +serde = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] rand = { workspace = true } diff --git a/crates/chia-bls/src/public_key.rs b/crates/chia-bls/src/public_key.rs index 867e1203d..a780a39b0 100644 --- a/crates/chia-bls/src/public_key.rs +++ b/crates/chia-bls/src/public_key.rs @@ -26,6 +26,16 @@ impl<'a> arbitrary::Arbitrary<'a> for PublicKey { } } +#[cfg(feature = "serde")] +impl serde::Serialize for PublicKey { + fn serialize( + &self, + serializer: S, + ) -> std::result::Result { + serializer.serialize_str(&hex::encode(self.to_bytes())) + } +} + impl PublicKey { pub fn from_bytes_unchecked(bytes: &[u8; 48]) -> Result { // check if the element is canonical diff --git a/crates/chia-bls/src/signature.rs b/crates/chia-bls/src/signature.rs index 2031757a0..6f71cf3f3 100644 --- a/crates/chia-bls/src/signature.rs +++ b/crates/chia-bls/src/signature.rs @@ -28,6 +28,16 @@ impl<'a> arbitrary::Arbitrary<'a> for Signature { } } +#[cfg(feature = "serde")] +impl serde::Serialize for Signature { + fn serialize( + &self, + serializer: S, + ) -> std::result::Result { + serializer.serialize_str(&hex::encode(self.to_bytes())) + } +} + impl Signature { pub fn from_bytes_unchecked(buf: &[u8; 96]) -> Result { let p2 = unsafe { diff --git a/crates/chia-protocol/Cargo.toml b/crates/chia-protocol/Cargo.toml index 4fa62f1b6..764d5412d 100644 --- a/crates/chia-protocol/Cargo.toml +++ b/crates/chia-protocol/Cargo.toml @@ -14,6 +14,7 @@ workspace = true [features] py-bindings = ["dep:pyo3", "dep:chia_py_streamable_macro", "chia-traits/py-bindings", "chia-bls/py-bindings"] arbitrary = ["dep:arbitrary", "chia-bls/arbitrary"] +serde = ["dep:serde", "chia-bls/serde"] [dependencies] pyo3 = { workspace = true, features = ["multiple-pymethods", "num-bigint"], optional = true } @@ -27,6 +28,7 @@ clvm-traits = { workspace = true, features = ["derive"] } clvm-utils = { workspace = true } chia-bls = { workspace = true } arbitrary = { workspace = true, features = ["derive"], optional = true } +serde = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] rstest = { workspace = true } diff --git a/crates/chia-protocol/src/bytes.rs b/crates/chia-protocol/src/bytes.rs index d9dd02a37..869464035 100644 --- a/crates/chia-protocol/src/bytes.rs +++ b/crates/chia-protocol/src/bytes.rs @@ -20,6 +20,7 @@ use pyo3::types::PyBytes; #[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct Bytes(Vec); impl Bytes { @@ -371,6 +372,13 @@ impl Deref for BytesImpl { } } +#[cfg(feature = "serde")] +impl serde::Serialize for BytesImpl { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(&hex::encode(self.0)) + } +} + pub type Bytes32 = BytesImpl<32>; pub type Bytes48 = BytesImpl<48>; pub type Bytes96 = BytesImpl<96>; diff --git a/crates/chia-protocol/src/chia_protocol.rs b/crates/chia-protocol/src/chia_protocol.rs index 28c87264d..a70740c69 100644 --- a/crates/chia-protocol/src/chia_protocol.rs +++ b/crates/chia-protocol/src/chia_protocol.rs @@ -8,6 +8,7 @@ use chia_py_streamable_macro::{PyJsonDict, PyStreamable}; #[repr(u8)] #[cfg_attr(feature = "py-bindings", derive(PyJsonDict, PyStreamable))] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] #[derive(Streamable, Hash, Debug, Copy, Clone, Eq, PartialEq)] pub enum ProtocolMessageTypes { // Shared protocol (all services) @@ -151,6 +152,7 @@ pub trait ChiaProtocolMessage { #[repr(u8)] #[cfg_attr(feature = "py-bindings", derive(PyJsonDict, PyStreamable))] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] #[derive(Streamable, Hash, Debug, Copy, Clone, Eq, PartialEq)] pub enum NodeType { FullNode = 1, diff --git a/crates/chia-protocol/src/program.rs b/crates/chia-protocol/src/program.rs index 0a8a3a958..fa1be0479 100644 --- a/crates/chia-protocol/src/program.rs +++ b/crates/chia-protocol/src/program.rs @@ -16,6 +16,7 @@ use std::io::Cursor; use std::ops::Deref; #[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Program(Bytes); diff --git a/crates/chia-protocol/src/wallet_protocol.rs b/crates/chia-protocol/src/wallet_protocol.rs index 605efd8a5..ce298410c 100644 --- a/crates/chia-protocol/src/wallet_protocol.rs +++ b/crates/chia-protocol/src/wallet_protocol.rs @@ -292,6 +292,7 @@ use chia_py_streamable_macro::{PyJsonDict, PyStreamable}; #[cfg_attr(feature = "py-bindings", derive(PyJsonDict, PyStreamable))] #[derive(Streamable, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] pub enum RejectStateReason { Reorg = 0, ExceededSubscriptionLimit = 1, diff --git a/crates/chia_streamable_macro/src/lib.rs b/crates/chia_streamable_macro/src/lib.rs index 95b6ee491..87a2f9344 100644 --- a/crates/chia_streamable_macro/src/lib.rs +++ b/crates/chia_streamable_macro/src/lib.rs @@ -106,6 +106,7 @@ pub fn streamable(attr: TokenStream, item: TokenStream) -> TokenStream { )] #main_derives #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] + #[cfg_attr(feature = "serde", derive(serde::Serialize))] } } else { main_derives