From 46fad3c5e7e27568d4a9e27ffbedd32c46fee191 Mon Sep 17 00:00:00 2001 From: Pravdyvy Date: Tue, 25 Nov 2025 09:40:46 +0200 Subject: [PATCH] fix: comments fix 1 --- .../privacy_preserving_transaction.rs | 4 +-- .../program_deployment_transaction.rs | 2 +- nssa/src/encoding/public_transaction.rs | 4 +-- nssa/src/signature/public_key.rs | 26 ++++++++++++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/nssa/src/encoding/privacy_preserving_transaction.rs b/nssa/src/encoding/privacy_preserving_transaction.rs index 8647a32..fcb6c94 100644 --- a/nssa/src/encoding/privacy_preserving_transaction.rs +++ b/nssa/src/encoding/privacy_preserving_transaction.rs @@ -5,7 +5,7 @@ use crate::{ impl Message { pub fn to_bytes(&self) -> Vec { - borsh::to_vec(&self).unwrap() + borsh::to_vec(&self).expect("Autoderived borsh serialization failure") } pub fn from_bytes(bytes: &[u8]) -> Result { @@ -15,7 +15,7 @@ impl Message { impl PrivacyPreservingTransaction { pub fn to_bytes(&self) -> Vec { - borsh::to_vec(&self).unwrap() + borsh::to_vec(&self).expect("Autoderived borsh serialization failure") } pub fn from_bytes(bytes: &[u8]) -> Result { diff --git a/nssa/src/encoding/program_deployment_transaction.rs b/nssa/src/encoding/program_deployment_transaction.rs index 0cf00e0..ee66863 100644 --- a/nssa/src/encoding/program_deployment_transaction.rs +++ b/nssa/src/encoding/program_deployment_transaction.rs @@ -2,7 +2,7 @@ use crate::{ProgramDeploymentTransaction, error::NssaError}; impl ProgramDeploymentTransaction { pub fn to_bytes(&self) -> Vec { - borsh::to_vec(&self).unwrap() + borsh::to_vec(&self).expect("Autoderived borsh serialization failure") } pub fn from_bytes(bytes: &[u8]) -> Result { diff --git a/nssa/src/encoding/public_transaction.rs b/nssa/src/encoding/public_transaction.rs index 5e6838c..ea0988c 100644 --- a/nssa/src/encoding/public_transaction.rs +++ b/nssa/src/encoding/public_transaction.rs @@ -2,13 +2,13 @@ use crate::{PublicTransaction, error::NssaError, public_transaction::Message}; impl Message { pub(crate) fn to_bytes(&self) -> Vec { - borsh::to_vec(&self).unwrap() + borsh::to_vec(&self).expect("Autoderived borsh serialization failure") } } impl PublicTransaction { pub fn to_bytes(&self) -> Vec { - borsh::to_vec(&self).unwrap() + borsh::to_vec(&self).expect("Autoderived borsh serialization failure") } pub fn from_bytes(bytes: &[u8]) -> Result { diff --git a/nssa/src/signature/public_key.rs b/nssa/src/signature/public_key.rs index d7f8d2e..4e78f3b 100644 --- a/nssa/src/signature/public_key.rs +++ b/nssa/src/signature/public_key.rs @@ -5,9 +5,23 @@ use crate::{PrivateKey, error::NssaError}; use sha2::{Digest, Sha256}; -#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize)] pub struct PublicKey([u8; 32]); +impl BorshDeserialize for PublicKey { + fn deserialize_reader(reader: &mut R) -> std::io::Result { + let mut buf = [0u8; 32]; + reader.read_exact(&mut buf)?; + + Self::try_new(buf).map_err(|_| { + std::io::Error::new( + std::io::ErrorKind::InvalidData, + "Invalid public key: not a valid point", + ) + }) + } +} + impl PublicKey { pub fn new_from_private_key(key: &PrivateKey) -> Self { let value = { @@ -94,4 +108,14 @@ mod test { ); } } + + #[test] + fn test_correct_ser_deser_roundtrip() { + let pub_key = PublicKey::try_new([42; 32]).unwrap(); + + let pub_key_borsh_ser = borsh::to_vec(&pub_key).unwrap(); + let pub_key_new: PublicKey = borsh::from_slice(&pub_key_borsh_ser).unwrap(); + + assert_eq!(pub_key, pub_key_new); + } }