Pārlūkot izejas kodu

Do not read keys from the DB

Cesar Rodas 2 nedēļas atpakaļ
vecāks
revīzija
a59c534260

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

@@ -40,7 +40,7 @@ message Keys {
 
 message RotateKeyArguments {
     CurrencyUnit unit = 1;
-    uint32 derivation_path_index = 2;
+    optional uint32 derivation_path_index = 2;
     uint32 max_order = 3;
     uint64 input_fee_ppk = 4;
 }

+ 20 - 2
crates/cdk-signatory/src/memory.rs

@@ -310,9 +310,27 @@ impl Signatory for Memory {
     /// Generate new keyset
     #[tracing::instrument(skip(self))]
     async fn rotate_keyset(&self, args: RotateKeyArguments) -> Result<MintKeySetInfo, Error> {
+        let path_index = if let Some(path_index) = args.derivation_path_index {
+            path_index
+        } else {
+            let current_keyset_id = self
+                .localstore
+                .get_active_keyset_id(&args.unit)
+                .await?
+                .ok_or(Error::UnsupportedUnit)?;
+
+            let keyset_info = self
+                .localstore
+                .get_keyset_info(&current_keyset_id)
+                .await?
+                .ok_or(Error::UnknownKeySet)?;
+
+            keyset_info.derivation_path_index.unwrap_or(1) + 1
+        };
+
         let derivation_path = match self.custom_paths.get(&args.unit) {
             Some(path) => path.clone(),
-            None => derivation_path_from_unit(args.unit.clone(), args.derivation_path_index)
+            None => derivation_path_from_unit(args.unit.clone(), path_index)
                 .ok_or(Error::UnsupportedUnit)?,
         };
 
@@ -320,7 +338,7 @@ impl Signatory for Memory {
             &self.secp_ctx,
             self.xpriv,
             derivation_path,
-            Some(args.derivation_path_index),
+            Some(path_index),
             args.unit.clone(),
             args.max_order,
             args.input_fee_ppk,

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

@@ -31,10 +31,13 @@ impl From<CurrencyUnit> for KeysetIdentifier {
     }
 }
 
+/// RotateKeyArguments
+///
+/// This struct is used to pass the arguments to the rotate_keyset function
 #[derive(Debug, Clone)]
 pub struct RotateKeyArguments {
     pub unit: CurrencyUnit,
-    pub derivation_path_index: u32,
+    pub derivation_path_index: Option<u32>,
     pub max_order: u8,
     pub input_fee_ppk: u64,
 }

+ 14 - 42
crates/cdk/src/mint/keysets/mod.rs

@@ -1,5 +1,3 @@
-use std::collections::HashSet;
-
 use cdk_signatory::signatory::RotateKeyArguments;
 use tracing::instrument;
 
@@ -45,23 +43,17 @@ impl Mint {
     /// Return a list of all supported keysets
     #[instrument(skip_all)]
     pub async fn keysets(&self) -> Result<KeysetResponse, Error> {
-        let keysets = self.localstore.get_keyset_infos().await?;
-        let active_keysets: HashSet<Id> = self
-            .localstore
-            .get_active_keysets()
+        let keysets = self
+            .signatory
+            .keysets()
             .await?
-            .values()
-            .cloned()
-            .collect();
-
-        let keysets = keysets
             .into_iter()
-            .filter(|k| k.unit != CurrencyUnit::Auth)
+            .filter(|k| k.key.unit != CurrencyUnit::Auth)
             .map(|k| KeySetInfo {
-                id: k.id,
-                unit: k.unit,
-                active: active_keysets.contains(&k.id),
-                input_fee_ppk: k.input_fee_ppk,
+                id: k.key.id,
+                unit: k.key.unit,
+                active: k.info.active,
+                input_fee_ppk: k.info.input_fee_ppk,
             })
             .collect();
 
@@ -93,7 +85,7 @@ impl Mint {
         self.signatory
             .rotate_keyset(RotateKeyArguments {
                 unit,
-                derivation_path_index,
+                derivation_path_index: Some(derivation_path_index),
                 max_order,
                 input_fee_ppk,
             })
@@ -108,33 +100,13 @@ impl Mint {
         max_order: u8,
         input_fee_ppk: u64,
     ) -> Result<MintKeySetInfo, Error> {
-        let current_keyset_id = self
-            .localstore
-            .get_active_keyset_id(&unit)
-            .await?
-            .ok_or(Error::UnsupportedUnit)?;
-
-        let keyset_info = self
-            .localstore
-            .get_keyset_info(&current_keyset_id)
-            .await?
-            .ok_or(Error::UnknownKeySet)?;
-
-        tracing::debug!(
-            "Current active keyset {} path index {:?}",
-            keyset_info.id,
-            keyset_info.derivation_path_index
-        );
-
-        let keyset_info = self
-            .rotate_keyset(
+        self.signatory
+            .rotate_keyset(RotateKeyArguments {
                 unit,
-                keyset_info.derivation_path_index.unwrap_or(1) + 1,
                 max_order,
+                derivation_path_index: None,
                 input_fee_ppk,
-            )
-            .await?;
-
-        Ok(keyset_info)
+            })
+            .await
     }
 }

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

@@ -286,6 +286,7 @@ impl Mint {
             if let std::collections::hash_map::Entry::Vacant(e) =
                 fee_per_keyset.entry(proof.keyset_id)
             {
+                // TODO: Get this from signatory
                 let mint_keyset_info = self
                     .localstore
                     .get_keyset_info(&proof.keyset_id)

+ 1 - 0
crates/cdk/src/mint/verification.rs

@@ -114,6 +114,7 @@ impl Mint {
         let inputs_keyset_ids: HashSet<Id> = inputs.iter().map(|p| p.keyset_id).collect();
 
         for id in &inputs_keyset_ids {
+            // TODO: Should ping the signatory instead of DB
             match self.localstore.get_keyset_info(id).await? {
                 Some(keyset) => {
                     keyset_units.insert(keyset.unit);