diff --git a/bindings/cashu-ffi/src/nuts/nut01/keys.rs b/bindings/cashu-ffi/src/nuts/nut01/keys.rs index 2ab1a29d..2aae9930 100644 --- a/bindings/cashu-ffi/src/nuts/nut01/keys.rs +++ b/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 cashu::nuts::nut01::Keys as KeysSdk; +use cashu::Amount as AmountSdk; pub struct Keys { inner: KeysSdk, @@ -18,7 +19,12 @@ impl Keys { pub fn new(keys: HashMap>) -> Self { let keys = keys .into_iter() - .map(|(amount, pk)| (amount.parse::().unwrap(), pk.as_ref().into())) + .map(|(amount, pk)| { + ( + AmountSdk::from_sat(amount.parse::().unwrap()), + pk.as_ref().into(), + ) + }) .collect(); Self { @@ -30,7 +36,7 @@ impl Keys { self.inner .keys() .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() } @@ -44,7 +50,7 @@ impl Keys { self.inner .as_hashmap() .into_iter() - .map(|(amount, pk)| (amount.to_string(), pk)) + .map(|(amount, pk)| (amount.to_sat().to_string(), pk)) .collect() } } @@ -60,7 +66,7 @@ impl From for Keys { let keys = keys .keys() .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(); Keys::new(keys) diff --git a/crates/cashu-sdk/src/mint.rs b/crates/cashu-sdk/src/mint.rs index f66475cd..246c7443 100644 --- a/crates/cashu-sdk/src/mint.rs +++ b/crates/cashu-sdk/src/mint.rs @@ -100,7 +100,7 @@ impl Mint { fn blind_sign(&self, blinded_message: &BlindedMessage) -> Result { 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 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); }; diff --git a/crates/cashu/src/nuts/nut01.rs b/crates/cashu/src/nuts/nut01.rs index 4964e57b..d07b0b27 100644 --- a/crates/cashu/src/nuts/nut01.rs +++ b/crates/cashu/src/nuts/nut01.rs @@ -80,23 +80,23 @@ impl SecretKey { /// Mint Keys [NUT-01] // TODO: CHange this to Amount type #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] -pub struct Keys(BTreeMap); +pub struct Keys(BTreeMap); impl Keys { - pub fn new(keys: BTreeMap) -> Self { + pub fn new(keys: BTreeMap) -> Self { Self(keys) } - pub fn keys(&self) -> BTreeMap { + pub fn keys(&self) -> BTreeMap { self.0.clone() } pub fn amount_key(&self, amount: Amount) -> Option { - self.0.get(&amount.to_sat()).cloned() + self.0.get(&amount).cloned() } /// As serialized hashmap - pub fn as_hashmap(&self) -> HashMap { + pub fn as_hashmap(&self) -> HashMap { self.0 .iter() .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 - pub fn iter(&self) -> impl Iterator { + pub fn iter(&self) -> impl Iterator { self.0.iter() } } @@ -125,11 +125,13 @@ pub mod mint { use serde::Serialize; + use crate::Amount; + use super::PublicKey; use super::SecretKey; #[derive(Debug, Clone, PartialEq, Eq, Serialize)] - pub struct Keys(pub BTreeMap); + pub struct Keys(pub BTreeMap); #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct KeyPair { diff --git a/crates/cashu/src/nuts/nut02.rs b/crates/cashu/src/nuts/nut02.rs index 86a200dc..5fbbf846 100644 --- a/crates/cashu/src/nuts/nut02.rs +++ b/crates/cashu/src/nuts/nut02.rs @@ -170,6 +170,7 @@ pub mod mint { use super::Id; use crate::nuts::nut01::mint::{KeyPair, Keys}; + use crate::Amount; #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct KeySet { @@ -198,7 +199,7 @@ pub mod mint { engine.input(derivation_path.into().as_bytes()); 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 let mut e = engine.clone();