Quellcode durchsuchen

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.
thesimplekid vor 1 Jahr
Ursprung
Commit
fd955f22cb

+ 0 - 2
bindings/cashu-ffi/src/cashu.udl

@@ -32,8 +32,6 @@ interface PublicKey {
 
 
 
 
 interface SecretKey {
 interface SecretKey {
-    [Throws=CashuError, Name=from_hex]
-    constructor(string hex);
     [Throws=CashuError]
     [Throws=CashuError]
     string to_hex();
     string to_hex();
 };
 };

+ 1 - 1
bindings/cashu-ffi/src/nuts/nut01/public_key.rs

@@ -35,6 +35,6 @@ impl PublicKey {
     }
     }
 
 
     pub fn to_hex(&self) -> Result<String> {
     pub fn to_hex(&self) -> Result<String> {
-        Ok(self.inner.to_hex()?)
+        Ok(self.inner.to_hex())
     }
     }
 }
 }

+ 1 - 7
bindings/cashu-ffi/src/nuts/nut01/secret_key.rs

@@ -28,13 +28,7 @@ impl From<SecretKey> for SecretKeySdk {
 }
 }
 
 
 impl SecretKey {
 impl SecretKey {
-    pub fn from_hex(hex: String) -> Result<Self> {
-        Ok(Self {
-            inner: SecretKeySdk::from_hex(hex)?,
-        })
-    }
-
     pub fn to_hex(&self) -> Result<String> {
     pub fn to_hex(&self) -> Result<String> {
-        Ok(self.inner.to_hex()?)
+        Ok(self.inner.to_hex())
     }
     }
 }
 }

+ 0 - 2
bindings/cashu-sdk-ffi/src/cashu_sdk.udl

@@ -35,8 +35,6 @@ interface PublicKey {
 
 
 
 
 interface SecretKey {
 interface SecretKey {
-    [Throws=CashuError, Name=from_hex]
-    constructor(string hex);
     [Throws=CashuError]
     [Throws=CashuError]
     string to_hex();
     string to_hex();
 };
 };

+ 1 - 1
crates/cashu/Cargo.toml

@@ -20,7 +20,7 @@ base64 = "0.21.0"
 bitcoin = { version = "0.30.0", features=["serde",  "rand", "no-std"] }
 bitcoin = { version = "0.30.0", features=["serde",  "rand", "no-std"] }
 bitcoin_hashes = "0.12.0"
 bitcoin_hashes = "0.12.0"
 hex = "0.4.3"
 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"] }
 lightning-invoice = { version = "0.24.0", features=["serde"] }
 rand = "0.8.5"
 rand = "0.8.5"
 getrandom = { version = "0.2", features = ["js"] }
 getrandom = { version = "0.2", features = ["js"] }

+ 8 - 0
crates/cashu/src/error.rs

@@ -15,6 +15,7 @@ pub enum Error {
     CustomError(String),
     CustomError(String),
     /// From hex error
     /// From hex error
     HexError(hex::FromHexError),
     HexError(hex::FromHexError),
+    EllipticCurve(k256::elliptic_curve::Error),
     AmountKey,
     AmountKey,
     Amount,
     Amount,
     TokenSpent,
     TokenSpent,
@@ -36,6 +37,7 @@ impl fmt::Display for Error {
             Error::TokenSpent => write!(f, "Token Spent"),
             Error::TokenSpent => write!(f, "Token Spent"),
             Error::TokenNotVerifed => write!(f, "Token Not Verified"),
             Error::TokenNotVerifed => write!(f, "Token Not Verified"),
             Error::InvoiceAmountUndefined => write!(f, "Invoice without amount"),
             Error::InvoiceAmountUndefined => write!(f, "Invoice without amount"),
+            Error::EllipticCurve(err) => write!(f, "{}", err.to_string()),
         }
         }
     }
     }
 }
 }
@@ -72,6 +74,12 @@ impl From<hex::FromHexError> for Error {
     }
     }
 }
 }
 
 
+impl From<k256::elliptic_curve::Error> for Error {
+    fn from(err: k256::elliptic_curve::Error) -> Error {
+        Error::EllipticCurve(err)
+    }
+}
+
 #[cfg(feature = "wallet")]
 #[cfg(feature = "wallet")]
 pub mod wallet {
 pub mod wallet {
     use std::error::Error as StdError;
     use std::error::Error as StdError;

+ 1 - 1
crates/cashu/src/nuts/nut00.rs

@@ -39,7 +39,7 @@ pub mod wallet {
     use super::MintProofs;
     use super::MintProofs;
 
 
     /// Blinded Messages [NUT-00]
     /// Blinded Messages [NUT-00]
-    #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
+    #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
     pub struct BlindedMessages {
     pub struct BlindedMessages {
         /// Blinded messages
         /// Blinded messages
         pub blinded_messages: Vec<BlindedMessage>,
         pub blinded_messages: Vec<BlindedMessage>,

+ 14 - 20
crates/cashu/src/nuts/nut01.rs

@@ -32,20 +32,18 @@ impl From<k256::PublicKey> for PublicKey {
 }
 }
 
 
 impl 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<Self, Error> {
     pub fn from_hex(hex: String) -> Result<Self, Error> {
         let hex = hex::decode(hex)?;
         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<String, Error> {
-        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)]
 #[serde(transparent)]
 pub struct SecretKey(#[serde(with = "crate::serde_utils::serde_secret_key")] k256::SecretKey);
 pub struct SecretKey(#[serde(with = "crate::serde_utils::serde_secret_key")] k256::SecretKey);
 
 
@@ -61,14 +59,11 @@ impl From<k256::SecretKey> for SecretKey {
     }
     }
 }
 }
 
 
-// REVIEW: Guessing this is broken as well since its the same as pubkey
 impl SecretKey {
 impl SecretKey {
-    pub fn from_hex(hex: String) -> Result<Self, Error> {
-        Ok(serde_json::from_str(&hex)?)
-    }
+    pub fn to_hex(&self) -> String {
+        let bytes = self.0.to_bytes();
 
 
-    pub fn to_hex(&self) -> Result<String, Error> {
-        Ok(serde_json::to_string(&self)?)
+        hex::encode(bytes)
     }
     }
 
 
     pub fn public_key(&self) -> PublicKey {
     pub fn public_key(&self) -> PublicKey {
@@ -120,12 +115,11 @@ pub mod mint {
 
 
     use super::PublicKey;
     use super::PublicKey;
     use super::SecretKey;
     use super::SecretKey;
-    use serde::Deserialize;
 
 
-    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+    #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     pub struct Keys(pub BTreeMap<u64, KeyPair>);
     pub struct Keys(pub BTreeMap<u64, KeyPair>);
 
 
-    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+    #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     pub struct KeyPair {
     pub struct KeyPair {
         pub public_key: PublicKey,
         pub public_key: PublicKey,
         pub secret_key: SecretKey,
         pub secret_key: SecretKey,
@@ -148,9 +142,9 @@ mod tests {
 
 
     #[test]
     #[test]
     fn pubkey() {
     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())
     }
     }
 }
 }

+ 1 - 1
crates/cashu/src/nuts/nut02.rs

@@ -66,7 +66,7 @@ pub mod mint {
 
 
     use crate::nuts::nut01::mint::{KeyPair, Keys};
     use crate::nuts::nut01::mint::{KeyPair, Keys};
 
 
-    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+    #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     pub struct KeySet {
     pub struct KeySet {
         pub id: String,
         pub id: String,
         pub keys: Keys,
         pub keys: Keys,

+ 1 - 1
crates/cashu/src/nuts/nut06.rs

@@ -11,7 +11,7 @@ use crate::nuts::nut00::wallet::BlindedMessages;
 use super::nut00::BlindedSignature;
 use super::nut00::BlindedSignature;
 
 
 #[cfg(feature = "wallet")]
 #[cfg(feature = "wallet")]
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
 pub struct SplitPayload {
 pub struct SplitPayload {
     pub blinded_messages: BlindedMessages,
     pub blinded_messages: BlindedMessages,
     pub split_payload: SplitRequest,
     pub split_payload: SplitRequest,

+ 12 - 12
crates/cashu/src/serde_utils.rs

@@ -103,22 +103,22 @@ pub mod serde_public_key {
 
 
 pub mod serde_secret_key {
 pub mod serde_secret_key {
     use k256::SecretKey;
     use k256::SecretKey;
-    use serde::Deserialize;
 
 
-    pub fn serialize<S>(pubkey: &SecretKey, serializer: S) -> Result<S::Ok, S::Error>
+    pub fn serialize<S>(seckey: &SecretKey, serializer: S) -> Result<S::Ok, S::Error>
     where
     where
         S: serde::Serializer,
         S: serde::Serializer,
     {
     {
-        let encoded = hex::encode(pubkey.to_bytes());
+        let encoded = hex::encode(seckey.to_bytes());
         serializer.serialize_str(&encoded)
         serializer.serialize_str(&encoded)
     }
     }
-
-    pub fn deserialize<'de, D>(deserializer: D) -> Result<SecretKey, D::Error>
-    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<SecretKey, D::Error>
+        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)
+        }
+    */
 }
 }