Skip to content

Commit

Permalink
mintd: sort pks by amount in /v1/keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Oct 14, 2024
1 parent 260f262 commit 185353f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
22 changes: 13 additions & 9 deletions crates/cdk/src/nuts/nut01/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ pub enum Error {
},
}

/// Mint Keys [NUT-01]
/// Mint public keys per amount.
///
/// This is a variation of [MintKeys] that only exposes the public keys.
///
/// See [NUT-01]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Keys(BTreeMap<String, PublicKey>);
pub struct Keys(BTreeMap<Amount, PublicKey>);

impl From<MintKeys> for Keys {
fn from(keys: MintKeys) -> Self {
Self(
keys.0
.iter()
.map(|(amount, keypair)| (amount.to_string(), keypair.public_key))
.into_iter()
.map(|(amount, keypair)| (amount, keypair.public_key))
.collect(),
)
}
Expand All @@ -55,25 +59,25 @@ impl From<MintKeys> for Keys {
impl Keys {
/// Create new [`Keys`]
#[inline]
pub fn new(keys: BTreeMap<String, PublicKey>) -> Self {
pub fn new(keys: BTreeMap<Amount, PublicKey>) -> Self {
Self(keys)
}

/// Get [`Keys`]
#[inline]
pub fn keys(&self) -> &BTreeMap<String, PublicKey> {
pub fn keys(&self) -> &BTreeMap<Amount, PublicKey> {
&self.0
}

/// Get [`PublicKey`] for [`Amount`]
#[inline]
pub fn amount_key(&self, amount: Amount) -> Option<PublicKey> {
self.0.get(&amount.to_string()).copied()
self.0.get(&amount).copied()
}

/// Iterate through the (`Amount`, `PublicKey`) entries in the Map
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (&String, &PublicKey)> {
pub fn iter(&self) -> impl Iterator<Item = (&Amount, &PublicKey)> {
self.0.iter()
}
}
Expand All @@ -87,7 +91,7 @@ pub struct KeysResponse {
pub keysets: Vec<KeySet>,
}

/// Mint keys
/// Mint key pairs per amount
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MintKeys(BTreeMap<Amount, MintKeyPair>);

Expand Down
5 changes: 3 additions & 2 deletions crates/cdk/src/nuts/nut02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ impl From<&Keys> for Id {
5 - prefix it with a keyset ID version byte
*/

let mut keys: Vec<(&String, &super::PublicKey)> = map.iter().collect();
let mut keys: Vec<(&Amount, &super::PublicKey)> = map.iter()
.collect();

keys.sort_by_key(|(k, _v)| u64::from_str(k).unwrap());
keys.sort_by_key(|(&amt, _v)| u64::from(amt));

let pubkeys_concat: Vec<u8> = keys
.iter()
Expand Down

0 comments on commit 185353f

Please sign in to comment.