From fd955f22cb15a0b08ef8b7165e83b85e3ab68789 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 3 Sep 2023 14:06:27 +0100 Subject: [PATCH] fix: public key serialization This removes deserialixation from the secret key and related types as it is not needed and it's broken. It would be best to fix this at some point. --- bindings/cashu-ffi/src/cashu.udl | 2 -- .../cashu-ffi/src/nuts/nut01/public_key.rs | 2 +- .../cashu-ffi/src/nuts/nut01/secret_key.rs | 8 +---- bindings/cashu-sdk-ffi/src/cashu_sdk.udl | 2 -- crates/cashu/Cargo.toml | 2 +- crates/cashu/src/error.rs | 8 +++++ crates/cashu/src/nuts/nut00.rs | 2 +- crates/cashu/src/nuts/nut01.rs | 34 ++++++++----------- crates/cashu/src/nuts/nut02.rs | 2 +- crates/cashu/src/nuts/nut06.rs | 2 +- crates/cashu/src/serde_utils.rs | 24 ++++++------- 11 files changed, 40 insertions(+), 48 deletions(-) diff --git a/bindings/cashu-ffi/src/cashu.udl b/bindings/cashu-ffi/src/cashu.udl index b6742c07..662d6684 100644 --- a/bindings/cashu-ffi/src/cashu.udl +++ b/bindings/cashu-ffi/src/cashu.udl @@ -32,8 +32,6 @@ interface PublicKey { interface SecretKey { - [Throws=CashuError, Name=from_hex] - constructor(string hex); [Throws=CashuError] string to_hex(); }; diff --git a/bindings/cashu-ffi/src/nuts/nut01/public_key.rs b/bindings/cashu-ffi/src/nuts/nut01/public_key.rs index dd44e0bd..01cdd3c3 100644 --- a/bindings/cashu-ffi/src/nuts/nut01/public_key.rs +++ b/bindings/cashu-ffi/src/nuts/nut01/public_key.rs @@ -35,6 +35,6 @@ impl PublicKey { } pub fn to_hex(&self) -> Result { - Ok(self.inner.to_hex()?) + Ok(self.inner.to_hex()) } } diff --git a/bindings/cashu-ffi/src/nuts/nut01/secret_key.rs b/bindings/cashu-ffi/src/nuts/nut01/secret_key.rs index cd27d249..f85b4b53 100644 --- a/bindings/cashu-ffi/src/nuts/nut01/secret_key.rs +++ b/bindings/cashu-ffi/src/nuts/nut01/secret_key.rs @@ -28,13 +28,7 @@ impl From for SecretKeySdk { } impl SecretKey { - pub fn from_hex(hex: String) -> Result { - Ok(Self { - inner: SecretKeySdk::from_hex(hex)?, - }) - } - pub fn to_hex(&self) -> Result { - Ok(self.inner.to_hex()?) + Ok(self.inner.to_hex()) } } diff --git a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl index c3f998cc..69e62ba1 100644 --- a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl +++ b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl @@ -35,8 +35,6 @@ interface PublicKey { interface SecretKey { - [Throws=CashuError, Name=from_hex] - constructor(string hex); [Throws=CashuError] string to_hex(); }; diff --git a/crates/cashu/Cargo.toml b/crates/cashu/Cargo.toml index 11b45bb0..6d61afa2 100644 --- a/crates/cashu/Cargo.toml +++ b/crates/cashu/Cargo.toml @@ -20,7 +20,7 @@ base64 = "0.21.0" bitcoin = { version = "0.30.0", features=["serde", "rand", "no-std"] } bitcoin_hashes = "0.12.0" hex = "0.4.3" -k256 = { version = "0.13.1", features=["arithmetic"] } +k256 = { version = "0.13.1", features=["arithmetic", "serde", "schnorr"] } lightning-invoice = { version = "0.24.0", features=["serde"] } rand = "0.8.5" getrandom = { version = "0.2", features = ["js"] } diff --git a/crates/cashu/src/error.rs b/crates/cashu/src/error.rs index 0eb4af81..0b912310 100644 --- a/crates/cashu/src/error.rs +++ b/crates/cashu/src/error.rs @@ -15,6 +15,7 @@ pub enum Error { CustomError(String), /// From hex error HexError(hex::FromHexError), + EllipticCurve(k256::elliptic_curve::Error), AmountKey, Amount, TokenSpent, @@ -36,6 +37,7 @@ impl fmt::Display for Error { Error::TokenSpent => write!(f, "Token Spent"), Error::TokenNotVerifed => write!(f, "Token Not Verified"), Error::InvoiceAmountUndefined => write!(f, "Invoice without amount"), + Error::EllipticCurve(err) => write!(f, "{}", err.to_string()), } } } @@ -72,6 +74,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: k256::elliptic_curve::Error) -> Error { + Error::EllipticCurve(err) + } +} + #[cfg(feature = "wallet")] pub mod wallet { use std::error::Error as StdError; diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index 1a6f740b..8da8032e 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/crates/cashu/src/nuts/nut00.rs @@ -39,7 +39,7 @@ pub mod wallet { use super::MintProofs; /// Blinded Messages [NUT-00] - #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] + #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)] pub struct BlindedMessages { /// Blinded messages pub blinded_messages: Vec, diff --git a/crates/cashu/src/nuts/nut01.rs b/crates/cashu/src/nuts/nut01.rs index fae3f9a9..484a23f9 100644 --- a/crates/cashu/src/nuts/nut01.rs +++ b/crates/cashu/src/nuts/nut01.rs @@ -32,20 +32,18 @@ impl From for PublicKey { } impl PublicKey { - // HACK: Fix the from_hex and to_hex - // just leaving this hack for now as this pr is big enough pub fn from_hex(hex: String) -> Result { let hex = hex::decode(hex)?; - - Ok(PublicKey(k256::PublicKey::from_sec1_bytes(&hex).unwrap())) + Ok(PublicKey(k256::PublicKey::from_sec1_bytes(&hex)?)) } - pub fn to_hex(&self) -> Result { - Ok(serde_json::to_string(&self)?) + pub fn to_hex(&self) -> String { + let bytes = self.0.to_sec1_bytes(); + hex::encode(bytes) } } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] #[serde(transparent)] pub struct SecretKey(#[serde(with = "crate::serde_utils::serde_secret_key")] k256::SecretKey); @@ -61,14 +59,11 @@ impl From for SecretKey { } } -// REVIEW: Guessing this is broken as well since its the same as pubkey impl SecretKey { - pub fn from_hex(hex: String) -> Result { - Ok(serde_json::from_str(&hex)?) - } + pub fn to_hex(&self) -> String { + let bytes = self.0.to_bytes(); - pub fn to_hex(&self) -> Result { - Ok(serde_json::to_string(&self)?) + hex::encode(bytes) } pub fn public_key(&self) -> PublicKey { @@ -120,12 +115,11 @@ pub mod mint { use super::PublicKey; use super::SecretKey; - use serde::Deserialize; - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Keys(pub BTreeMap); - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct KeyPair { pub public_key: PublicKey, pub secret_key: SecretKey, @@ -148,9 +142,9 @@ mod tests { #[test] fn pubkey() { - let pubkey = PublicKey::from_hex( - "02c020067db727d586bc3183aecf97fcb800c3f4cc4759f69c626c9db5d8f5b5d4".to_string(), - ) - .unwrap(); + let pubkey_str = "02c020067db727d586bc3183aecf97fcb800c3f4cc4759f69c626c9db5d8f5b5d4"; + let pubkey = PublicKey::from_hex(pubkey_str.to_string()).unwrap(); + + assert_eq!(pubkey_str, pubkey.to_hex()) } } diff --git a/crates/cashu/src/nuts/nut02.rs b/crates/cashu/src/nuts/nut02.rs index 0b8a870c..281038bc 100644 --- a/crates/cashu/src/nuts/nut02.rs +++ b/crates/cashu/src/nuts/nut02.rs @@ -66,7 +66,7 @@ pub mod mint { use crate::nuts::nut01::mint::{KeyPair, Keys}; - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct KeySet { pub id: String, pub keys: Keys, diff --git a/crates/cashu/src/nuts/nut06.rs b/crates/cashu/src/nuts/nut06.rs index 40a60f3c..49c9de56 100644 --- a/crates/cashu/src/nuts/nut06.rs +++ b/crates/cashu/src/nuts/nut06.rs @@ -11,7 +11,7 @@ use crate::nuts::nut00::wallet::BlindedMessages; use super::nut00::BlindedSignature; #[cfg(feature = "wallet")] -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct SplitPayload { pub blinded_messages: BlindedMessages, pub split_payload: SplitRequest, diff --git a/crates/cashu/src/serde_utils.rs b/crates/cashu/src/serde_utils.rs index a7be0da3..ae0dcf40 100644 --- a/crates/cashu/src/serde_utils.rs +++ b/crates/cashu/src/serde_utils.rs @@ -103,22 +103,22 @@ pub mod serde_public_key { pub mod serde_secret_key { use k256::SecretKey; - use serde::Deserialize; - pub fn serialize(pubkey: &SecretKey, serializer: S) -> Result + pub fn serialize(seckey: &SecretKey, serializer: S) -> Result where S: serde::Serializer, { - let encoded = hex::encode(pubkey.to_bytes()); + let encoded = hex::encode(seckey.to_bytes()); serializer.serialize_str(&encoded) } - - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let encoded = String::deserialize(deserializer)?; - let decoded = hex::decode(encoded).map_err(serde::de::Error::custom)?; - SecretKey::from_slice(&decoded).map_err(serde::de::Error::custom) - } + /* + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let encoded = String::deserialize(deserializer)?; + let decoded = hex::decode(encoded).map_err(serde::de::Error::custom)?; + SecretKey::from_slice(&decoded).map_err(serde::de::Error::custom) + } + */ }