cashu improve: amount type in Keys map

This commit is contained in:
thesimplekid
2023-09-10 09:47:29 +01:00
parent 85d0db95ce
commit e47762029e
4 changed files with 23 additions and 14 deletions

View File

@@ -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<String, Arc<PublicKey>>) -> Self {
let keys = keys
.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();
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<KeysSdk> 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)

View File

@@ -100,7 +100,7 @@ impl Mint {
fn blind_sign(&self, blinded_message: &BlindedMessage) -> Result<BlindedSignature, Error> {
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);
};

View File

@@ -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<u64, PublicKey>);
pub struct Keys(BTreeMap<Amount, PublicKey>);
impl Keys {
pub fn new(keys: BTreeMap<u64, PublicKey>) -> Self {
pub fn new(keys: BTreeMap<Amount, PublicKey>) -> Self {
Self(keys)
}
pub fn keys(&self) -> BTreeMap<u64, PublicKey> {
pub fn keys(&self) -> BTreeMap<Amount, PublicKey> {
self.0.clone()
}
pub fn amount_key(&self, amount: Amount) -> Option<PublicKey> {
self.0.get(&amount.to_sat()).cloned()
self.0.get(&amount).cloned()
}
/// As serialized hashmap
pub fn as_hashmap(&self) -> HashMap<u64, String> {
pub fn as_hashmap(&self) -> HashMap<Amount, String> {
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<Item = (&u64, &PublicKey)> {
pub fn iter(&self) -> impl Iterator<Item = (&Amount, &PublicKey)> {
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<u64, KeyPair>);
pub struct Keys(pub BTreeMap<Amount, KeyPair>);
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct KeyPair {

View File

@@ -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();