Browse Source

`cashu` improve: amount type in `Keys` map

thesimplekid 1 year ago
parent
commit
e47762029e

+ 10 - 4
bindings/cashu-ffi/src/nuts/nut01/keys.rs

@@ -2,6 +2,7 @@ use std::{collections::HashMap, ops::Deref, sync::Arc};
 
 
 use crate::{Amount, PublicKey};
 use crate::{Amount, PublicKey};
 use cashu::nuts::nut01::Keys as KeysSdk;
 use cashu::nuts::nut01::Keys as KeysSdk;
+use cashu::Amount as AmountSdk;
 
 
 pub struct Keys {
 pub struct Keys {
     inner: KeysSdk,
     inner: KeysSdk,
@@ -18,7 +19,12 @@ impl Keys {
     pub fn new(keys: HashMap<String, Arc<PublicKey>>) -> Self {
     pub fn new(keys: HashMap<String, Arc<PublicKey>>) -> Self {
         let keys = keys
         let keys = keys
             .into_iter()
             .into_iter()
-            .map(|(amount, pk)| (amount.parse::<u64>().unwrap(), pk.as_ref().into()))
+            .map(|(amount, pk)| {
+                (
+                    AmountSdk::from_sat(amount.parse::<u64>().unwrap()),
+                    pk.as_ref().into(),
+                )
+            })
             .collect();
             .collect();
 
 
         Self {
         Self {
@@ -30,7 +36,7 @@ impl Keys {
         self.inner
         self.inner
             .keys()
             .keys()
             .into_iter()
             .into_iter()
-            .map(|(amount, pk)| (amount.to_string(), Arc::new(pk.into())))
+            .map(|(amount, pk)| (amount.to_sat().to_string(), Arc::new(pk.into())))
             .collect()
             .collect()
     }
     }
 
 
@@ -44,7 +50,7 @@ impl Keys {
         self.inner
         self.inner
             .as_hashmap()
             .as_hashmap()
             .into_iter()
             .into_iter()
-            .map(|(amount, pk)| (amount.to_string(), pk))
+            .map(|(amount, pk)| (amount.to_sat().to_string(), pk))
             .collect()
             .collect()
     }
     }
 }
 }
@@ -60,7 +66,7 @@ impl From<KeysSdk> for Keys {
         let keys = keys
         let keys = keys
             .keys()
             .keys()
             .into_iter()
             .into_iter()
-            .map(|(amount, pk)| (amount.to_string(), Arc::new(pk.into())))
+            .map(|(amount, pk)| (amount.to_sat().to_string(), Arc::new(pk.into())))
             .collect();
             .collect();
 
 
         Keys::new(keys)
         Keys::new(keys)

+ 2 - 2
crates/cashu-sdk/src/mint.rs

@@ -100,7 +100,7 @@ impl Mint {
     fn blind_sign(&self, blinded_message: &BlindedMessage) -> Result<BlindedSignature, Error> {
     fn blind_sign(&self, blinded_message: &BlindedMessage) -> Result<BlindedSignature, Error> {
         let BlindedMessage { amount, b } = blinded_message;
         let BlindedMessage { amount, b } = blinded_message;
 
 
-        let Some(key_pair) = self.active_keyset.keys.0.get(&amount.to_sat()) else {
+        let Some(key_pair) = self.active_keyset.keys.0.get(&amount) else {
             // No key for amount
             // No key for amount
             return Err(Error::AmountKey);
             return Err(Error::AmountKey);
         };
         };
@@ -187,7 +187,7 @@ impl Mint {
             },
             },
         );
         );
 
 
-        let Some(keypair) = keyset.keys.0.get(&proof.amount.to_sat()) else {
+        let Some(keypair) = keyset.keys.0.get(&proof.amount) else {
             return Err(Error::AmountKey);
             return Err(Error::AmountKey);
         };
         };
 
 

+ 9 - 7
crates/cashu/src/nuts/nut01.rs

@@ -80,23 +80,23 @@ impl SecretKey {
 /// Mint Keys [NUT-01]
 /// Mint Keys [NUT-01]
 // TODO: CHange this to Amount type
 // TODO: CHange this to Amount type
 #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
 #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
-pub struct Keys(BTreeMap<u64, PublicKey>);
+pub struct Keys(BTreeMap<Amount, PublicKey>);
 
 
 impl Keys {
 impl Keys {
-    pub fn new(keys: BTreeMap<u64, PublicKey>) -> Self {
+    pub fn new(keys: BTreeMap<Amount, PublicKey>) -> Self {
         Self(keys)
         Self(keys)
     }
     }
 
 
-    pub fn keys(&self) -> BTreeMap<u64, PublicKey> {
+    pub fn keys(&self) -> BTreeMap<Amount, PublicKey> {
         self.0.clone()
         self.0.clone()
     }
     }
 
 
     pub fn amount_key(&self, amount: Amount) -> Option<PublicKey> {
     pub fn amount_key(&self, amount: Amount) -> Option<PublicKey> {
-        self.0.get(&amount.to_sat()).cloned()
+        self.0.get(&amount).cloned()
     }
     }
 
 
     /// As serialized hashmap
     /// As serialized hashmap
-    pub fn as_hashmap(&self) -> HashMap<u64, String> {
+    pub fn as_hashmap(&self) -> HashMap<Amount, String> {
         self.0
         self.0
             .iter()
             .iter()
             .map(|(k, v)| (k.to_owned(), hex::encode(v.0.to_sec1_bytes())))
             .map(|(k, v)| (k.to_owned(), hex::encode(v.0.to_sec1_bytes())))
@@ -104,7 +104,7 @@ impl Keys {
     }
     }
 
 
     /// Iterate through the (`Amount`, `PublicKey`) entries in the Map
     /// Iterate through the (`Amount`, `PublicKey`) entries in the Map
-    pub fn iter(&self) -> impl Iterator<Item = (&u64, &PublicKey)> {
+    pub fn iter(&self) -> impl Iterator<Item = (&Amount, &PublicKey)> {
         self.0.iter()
         self.0.iter()
     }
     }
 }
 }
@@ -125,11 +125,13 @@ pub mod mint {
 
 
     use serde::Serialize;
     use serde::Serialize;
 
 
+    use crate::Amount;
+
     use super::PublicKey;
     use super::PublicKey;
     use super::SecretKey;
     use super::SecretKey;
 
 
     #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
-    pub struct Keys(pub BTreeMap<u64, KeyPair>);
+    pub struct Keys(pub BTreeMap<Amount, KeyPair>);
 
 
     #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     pub struct KeyPair {
     pub struct KeyPair {

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

@@ -170,6 +170,7 @@ pub mod mint {
     use super::Id;
     use super::Id;
 
 
     use crate::nuts::nut01::mint::{KeyPair, Keys};
     use crate::nuts::nut01::mint::{KeyPair, Keys};
+    use crate::Amount;
 
 
     #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
     pub struct KeySet {
     pub struct KeySet {
@@ -198,7 +199,7 @@ pub mod mint {
             engine.input(derivation_path.into().as_bytes());
             engine.input(derivation_path.into().as_bytes());
 
 
             for i in 0..max_order {
             for i in 0..max_order {
-                let amount = 2_u64.pow(i as u32);
+                let amount = Amount::from_sat(2_u64.pow(i as u32));
 
 
                 // Reuse midstate
                 // Reuse midstate
                 let mut e = engine.clone();
                 let mut e = engine.clone();