Cesar Rodas 1 mēnesi atpakaļ
vecāks
revīzija
b12c0346f8

+ 5 - 25
crates/cashu/src/nuts/nut02.rs

@@ -299,11 +299,10 @@ impl MintKeySet {
         secp: &Secp256k1<C>,
         xpriv: Xpriv,
         unit: CurrencyUnit,
-        max_order: u8,
+        units: &[Amount],
     ) -> Self {
         let mut map = BTreeMap::new();
-        for i in 0..max_order {
-            let amount = Amount::from(2_u64.pow(i as u32));
+        for (i, amount) in units.iter().enumerate() {
             let secret_key = xpriv
                 .derive_priv(
                     secp,
@@ -313,7 +312,7 @@ impl MintKeySet {
                 .private_key;
             let public_key = secret_key.public_key(secp);
             map.insert(
-                amount,
+                *amount,
                 MintKeyPair {
                     secret_key: secret_key.into(),
                     public_key: public_key.into(),
@@ -329,30 +328,11 @@ impl MintKeySet {
         }
     }
 
-    /// Generate new [`MintKeySet`] from seed
-    pub fn generate_from_seed<C: secp256k1::Signing>(
-        secp: &Secp256k1<C>,
-        seed: &[u8],
-        max_order: u8,
-        currency_unit: CurrencyUnit,
-        derivation_path: DerivationPath,
-    ) -> Self {
-        let xpriv = Xpriv::new_master(bitcoin::Network::Bitcoin, seed).expect("RNG busted");
-        Self::generate(
-            secp,
-            xpriv
-                .derive_priv(secp, &derivation_path)
-                .expect("RNG busted"),
-            currency_unit,
-            max_order,
-        )
-    }
-
     /// Generate new [`MintKeySet`] from xpriv
     pub fn generate_from_xpriv<C: secp256k1::Signing>(
         secp: &Secp256k1<C>,
         xpriv: Xpriv,
-        max_order: u8,
+        units: &[Amount],
         currency_unit: CurrencyUnit,
         derivation_path: DerivationPath,
     ) -> Self {
@@ -362,7 +342,7 @@ impl MintKeySet {
                 .derive_priv(secp, &derivation_path)
                 .expect("RNG busted"),
             currency_unit,
-            max_order,
+            units,
         )
     }
 }

+ 0 - 1
crates/cdk-signatory/proto/signatory.proto

@@ -89,7 +89,6 @@ message Proof {
   string keyset_id = 2;
   bytes secret = 3;
   bytes C = 4;
-  optional Witness witness = 5;
 }
 
 message ProofDLEQ {

+ 8 - 7
crates/cdk-signatory/src/common.rs

@@ -3,6 +3,7 @@ use std::sync::Arc;
 
 use bitcoin::bip32::{ChildNumber, DerivationPath, Xpriv};
 use bitcoin::secp256k1::{self, All, Secp256k1};
+use cashu::Amount;
 use cdk_common::database;
 use cdk_common::error::Error;
 use cdk_common::mint::MintKeySetInfo;
@@ -16,7 +17,7 @@ pub async fn init_keysets(
     xpriv: Xpriv,
     secp_ctx: &Secp256k1<All>,
     localstore: &Arc<dyn database::MintKeysDatabase<Err = database::Error> + Send + Sync>,
-    supported_units: &HashMap<CurrencyUnit, (u64, u8)>,
+    supported_units: &HashMap<CurrencyUnit, (u64, Vec<Amount>)>,
     custom_paths: &HashMap<CurrencyUnit, DerivationPath>,
 ) -> Result<(HashMap<Id, MintKeySet>, Vec<CurrencyUnit>), Error> {
     let mut active_keysets: HashMap<Id, MintKeySet> = HashMap::new();
@@ -55,10 +56,10 @@ pub async fn init_keysets(
                 .filter(|ks| ks.derivation_path_index.is_some())
                 .collect();
 
-            if let Some((input_fee_ppk, max_order)) = supported_units.get(&unit) {
+            if let Some((input_fee_ppk, units)) = supported_units.get(&unit) {
                 if !keysets.is_empty()
                     && &highest_index_keyset.input_fee_ppk == input_fee_ppk
-                    && &highest_index_keyset.max_order == max_order
+                    && &highest_index_keyset.max_order == units
                 {
                     tracing::debug!("Current highest index keyset matches expect fee and max order. Setting active");
                     let id = highest_index_keyset.id;
@@ -95,7 +96,7 @@ pub async fn init_keysets(
                         derivation_path,
                         Some(derivation_path_index),
                         unit.clone(),
-                        *max_order,
+                        units,
                         *input_fee_ppk,
                     );
 
@@ -120,7 +121,7 @@ pub fn create_new_keyset<C: secp256k1::Signing>(
     derivation_path: DerivationPath,
     derivation_path_index: Option<u32>,
     unit: CurrencyUnit,
-    max_order: u8,
+    amounts: &[Amount],
     input_fee_ppk: u64,
 ) -> (MintKeySet, MintKeySetInfo) {
     let keyset = MintKeySet::generate(
@@ -129,7 +130,7 @@ pub fn create_new_keyset<C: secp256k1::Signing>(
             .derive_priv(secp, &derivation_path)
             .expect("RNG busted"),
         unit,
-        max_order,
+        &amounts,
     );
     let keyset_info = MintKeySetInfo {
         id: keyset.id,
@@ -139,7 +140,7 @@ pub fn create_new_keyset<C: secp256k1::Signing>(
         valid_to: None,
         derivation_path,
         derivation_path_index,
-        max_order,
+        max_order: amounts.len() as u8,
         input_fee_ppk,
     };
     (keyset, keyset_info)

+ 6 - 48
crates/cdk-signatory/src/db_signatory.rs

@@ -3,7 +3,7 @@ use std::sync::Arc;
 
 use bitcoin::bip32::{DerivationPath, Xpriv};
 use bitcoin::secp256k1::{self, Secp256k1};
-use cashu::PublicKey;
+use cashu::{Amount, PublicKey};
 use cdk_common::dhke::{sign_message, verify_message};
 use cdk_common::mint::MintKeySetInfo;
 use cdk_common::nuts::{BlindSignature, BlindedMessage, CurrencyUnit, Id, MintKeySet, Proof};
@@ -40,7 +40,7 @@ impl DbSignatory {
             Arc<dyn database::MintAuthDatabase<Err = database::Error> + Send + Sync>,
         >,
         seed: &[u8],
-        supported_units: HashMap<CurrencyUnit, (u64, u8)>,
+        supported_units: HashMap<CurrencyUnit, (u64, Vec<Amount>)>,
         custom_paths: HashMap<CurrencyUnit, DerivationPath>,
     ) -> Result<Self, Error> {
         let secp_ctx = Secp256k1::new();
@@ -69,7 +69,7 @@ impl DbSignatory {
                 derivation_path,
                 Some(0),
                 CurrencyUnit::Auth,
-                1,
+                &[1.into()],
                 0,
             );
 
@@ -80,7 +80,7 @@ impl DbSignatory {
         }
 
         // Create new keysets for supported units that aren't covered by the current keysets
-        for (unit, (fee, max_order)) in supported_units {
+        for (unit, (fee, amounts)) in supported_units {
             if !active_keyset_units.contains(&unit) {
                 let derivation_path = match custom_paths.get(&unit) {
                     Some(path) => path.clone(),
@@ -95,7 +95,7 @@ impl DbSignatory {
                     derivation_path,
                     Some(0),
                     unit.clone(),
-                    max_order,
+                    &amounts,
                     fee,
                 );
 
@@ -299,48 +299,6 @@ mod test {
     use super::*;
 
     #[test]
-    fn mint_mod_generate_keyset_from_seed() {
-        let seed = "test_seed".as_bytes();
-        let keyset = MintKeySet::generate_from_seed(
-            &Secp256k1::new(),
-            seed,
-            2,
-            CurrencyUnit::Sat,
-            derivation_path_from_unit(CurrencyUnit::Sat, 0).unwrap(),
-        );
-
-        assert_eq!(keyset.unit, CurrencyUnit::Sat);
-        assert_eq!(keyset.keys.len(), 2);
-
-        let expected_amounts_and_pubkeys: HashSet<(Amount, PublicKey)> = vec![
-            (
-                Amount::from(1),
-                PublicKey::from_hex(
-                    "0257aed43bf2c1cdbe3e7ae2db2b27a723c6746fc7415e09748f6847916c09176e",
-                )
-                .unwrap(),
-            ),
-            (
-                Amount::from(2),
-                PublicKey::from_hex(
-                    "03ad95811e51adb6231613f9b54ba2ba31e4442c9db9d69f8df42c2b26fbfed26e",
-                )
-                .unwrap(),
-            ),
-        ]
-        .into_iter()
-        .collect();
-
-        let amounts_and_pubkeys: HashSet<(Amount, PublicKey)> = keyset
-            .keys
-            .iter()
-            .map(|(amount, pair)| (*amount, pair.public_key))
-            .collect();
-
-        assert_eq!(amounts_and_pubkeys, expected_amounts_and_pubkeys);
-    }
-
-    #[test]
     fn mint_mod_generate_keyset_from_xpriv() {
         let seed = "test_seed".as_bytes();
         let network = Network::Bitcoin;
@@ -348,7 +306,7 @@ mod test {
         let keyset = MintKeySet::generate_from_xpriv(
             &Secp256k1::new(),
             xpriv,
-            2,
+            &vec![1.into(), 2.into()],
             CurrencyUnit::Sat,
             derivation_path_from_unit(CurrencyUnit::Sat, 0).unwrap(),
         );

+ 10 - 5
crates/cdk-signatory/src/proto/convert.rs

@@ -392,7 +392,11 @@ impl From<crate::signatory::RotateKeyArguments> for RotationRequest {
     fn from(value: crate::signatory::RotateKeyArguments) -> Self {
         Self {
             unit: Some(value.unit.into()),
-            max_order: value.max_order.into(),
+            amounts: value
+                .units
+                .into_iter()
+                .map(|amount| amount.into())
+                .collect(),
             input_fee_ppk: value.input_fee_ppk,
         }
     }
@@ -407,10 +411,11 @@ impl TryInto<crate::signatory::RotateKeyArguments> for RotationRequest {
                 .unit
                 .ok_or(Status::invalid_argument("unit not set"))?
                 .try_into()?,
-            max_order: self
-                .max_order
-                .try_into()
-                .map_err(|_| Status::invalid_argument("Invalid max_order"))?,
+            units: self
+                .amounts
+                .into_iter()
+                .map(|amount| amount.into())
+                .collect(),
             input_fee_ppk: self.input_fee_ppk,
         })
     }

+ 4 - 4
crates/cdk-signatory/src/signatory.rs

@@ -7,7 +7,8 @@
 //! but it is isolated from the rest of the application, and they communicate through a channel with
 //! the defined API.
 use cashu::{
-    BlindSignature, BlindedMessage, CurrencyUnit, Id, KeySet, Keys, MintKeySet, Proof, PublicKey,
+    Amount, BlindSignature, BlindedMessage, CurrencyUnit, Id, KeySet, Keys, MintKeySet, Proof,
+    PublicKey,
 };
 use cdk_common::error::Error;
 use cdk_common::mint::MintKeySetInfo;
@@ -42,11 +43,10 @@ impl From<CurrencyUnit> for KeysetIdentifier {
 pub struct RotateKeyArguments {
     /// Unit
     pub unit: CurrencyUnit,
-    /// Max order
-    pub max_order: u8,
     /// Input fee
     pub input_fee_ppk: u64,
-    pub amounts: Vec<Amount>,
+    /// Supported units
+    pub units: Vec<Amount>,
 }
 
 #[derive(Debug, Clone)]

+ 0 - 33
crates/cdk/src/mint/mod.rs

@@ -539,39 +539,6 @@ impl Mint {
     }
 }
 
-/// Generate new [`MintKeySetInfo`] from path
-#[instrument(skip_all)]
-fn create_new_keyset<C: secp256k1::Signing>(
-    secp: &secp256k1::Secp256k1<C>,
-    xpriv: Xpriv,
-    derivation_path: DerivationPath,
-    derivation_path_index: Option<u32>,
-    unit: CurrencyUnit,
-    max_order: u8,
-    input_fee_ppk: u64,
-) -> (MintKeySet, MintKeySetInfo) {
-    let keyset = MintKeySet::generate(
-        secp,
-        xpriv
-            .derive_priv(secp, &derivation_path)
-            .expect("RNG busted"),
-        unit,
-        max_order,
-    );
-    let keyset_info = MintKeySetInfo {
-        id: keyset.id,
-        unit: keyset.unit.clone(),
-        active: true,
-        valid_from: unix_time(),
-        valid_to: None,
-        derivation_path,
-        derivation_path_index,
-        max_order,
-        input_fee_ppk,
-    };
-    (keyset, keyset_info)
-}
-
 #[cfg(test)]
 mod tests {
     use cdk_common::common::PaymentProcessorKey;