signatory.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //! Signatory mod
  2. //!
  3. //! This module abstract all the key related operations, defining an interface for the necessary
  4. //! operations, to be implemented by the different signatory implementations.
  5. //!
  6. //! There is an in memory implementation, when the keys are stored in memory, in the same process,
  7. //! but it is isolated from the rest of the application, and they communicate through a channel with
  8. //! the defined API.
  9. use cashu::{BlindSignature, BlindedMessage, CurrencyUnit, Id, KeySet, MintKeySet, Proof};
  10. use cdk_common::error::Error;
  11. use cdk_common::mint::MintKeySetInfo;
  12. #[derive(Debug)]
  13. /// Type alias to make the keyset info API more useful, queryable by unit and Id
  14. pub enum KeysetIdentifier {
  15. /// Mint Keyset by unit
  16. Unit(CurrencyUnit),
  17. /// Mint Keyset by Id
  18. Id(Id),
  19. }
  20. impl From<Id> for KeysetIdentifier {
  21. fn from(id: Id) -> Self {
  22. Self::Id(id)
  23. }
  24. }
  25. impl From<CurrencyUnit> for KeysetIdentifier {
  26. fn from(unit: CurrencyUnit) -> Self {
  27. Self::Unit(unit)
  28. }
  29. }
  30. /// RotateKeyArguments
  31. ///
  32. /// This struct is used to pass the arguments to the rotate_keyset function
  33. #[derive(Debug, Clone)]
  34. pub struct RotateKeyArguments {
  35. pub unit: CurrencyUnit,
  36. pub derivation_path_index: Option<u32>,
  37. pub max_order: u8,
  38. pub input_fee_ppk: u64,
  39. }
  40. #[derive(Debug, Clone)]
  41. /// SignatoryKeySet
  42. ///
  43. /// This struct is used to represent a keyset and its info, pretty much all the information but the
  44. /// private key, that will never leave the signatory
  45. pub struct SignatoryKeySet {
  46. /// KeySet
  47. pub key: KeySet,
  48. /// MintSetInfo
  49. pub info: MintKeySetInfo,
  50. }
  51. impl From<&(MintKeySetInfo, MintKeySet)> for SignatoryKeySet {
  52. fn from((info, key): &(MintKeySetInfo, MintKeySet)) -> Self {
  53. Self {
  54. key: key.clone().into(),
  55. info: info.clone(),
  56. }
  57. }
  58. }
  59. #[async_trait::async_trait]
  60. /// Signatory trait
  61. pub trait Signatory {
  62. /// Blind sign a message.
  63. ///
  64. /// The message can be for a coin or an auth token.
  65. async fn blind_sign(&self, blinded_message: BlindedMessage) -> Result<BlindSignature, Error>;
  66. /// Verify [`Proof`] meets conditions and is signed
  67. async fn verify_proof(&self, proofs: Proof) -> Result<(), Error>;
  68. /// Retrieve the list of all mint keysets
  69. async fn keysets(&self) -> Result<Vec<SignatoryKeySet>, Error>;
  70. /// Add current keyset to inactive keysets
  71. /// Generate new keyset
  72. async fn rotate_keyset(&self, args: RotateKeyArguments) -> Result<MintKeySetInfo, Error>;
  73. }