signatory.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 cdk_common::error::Error;
  10. use cdk_common::mint::MintKeySetInfo;
  11. use cdk_common::{
  12. BlindSignature, BlindedMessage, CurrencyUnit, Id, KeySet, Keys, MintKeySet, Proof, PublicKey,
  13. };
  14. #[derive(Debug)]
  15. /// Type alias to make the keyset info API more useful, queryable by unit and Id
  16. pub enum KeysetIdentifier {
  17. /// Mint Keyset by unit
  18. Unit(CurrencyUnit),
  19. /// Mint Keyset by Id
  20. Id(Id),
  21. }
  22. impl From<Id> for KeysetIdentifier {
  23. fn from(id: Id) -> Self {
  24. Self::Id(id)
  25. }
  26. }
  27. impl From<CurrencyUnit> for KeysetIdentifier {
  28. fn from(unit: CurrencyUnit) -> Self {
  29. Self::Unit(unit)
  30. }
  31. }
  32. /// RotateKeyArguments
  33. ///
  34. /// This struct is used to pass the arguments to the rotate_keyset function
  35. ///
  36. /// TODO: Change argument to accept a vector of Amount instead of max_order.
  37. #[derive(Debug, Clone)]
  38. pub struct RotateKeyArguments {
  39. /// Unit
  40. pub unit: CurrencyUnit,
  41. /// Max order
  42. pub max_order: u8,
  43. /// Input fee
  44. pub input_fee_ppk: u64,
  45. }
  46. #[derive(Debug, Clone)]
  47. /// Signatory keysets
  48. pub struct SignatoryKeysets {
  49. /// The public key
  50. pub pubkey: PublicKey,
  51. /// The list of keysets
  52. pub keysets: Vec<SignatoryKeySet>,
  53. }
  54. #[derive(Debug, Clone)]
  55. /// SignatoryKeySet
  56. ///
  57. /// This struct is used to represent a keyset and its info, pretty much all the information but the
  58. /// private key, that will never leave the signatory
  59. pub struct SignatoryKeySet {
  60. /// The keyset Id
  61. pub id: Id,
  62. /// The Currency Unit
  63. pub unit: CurrencyUnit,
  64. /// Whether to set it as active or not
  65. pub active: bool,
  66. /// The list of public keys
  67. pub keys: Keys,
  68. /// Information about the fee per public key
  69. pub input_fee_ppk: u64,
  70. }
  71. impl From<&SignatoryKeySet> for KeySet {
  72. fn from(val: &SignatoryKeySet) -> Self {
  73. val.to_owned().into()
  74. }
  75. }
  76. impl From<SignatoryKeySet> for KeySet {
  77. fn from(val: SignatoryKeySet) -> Self {
  78. KeySet {
  79. id: val.id,
  80. unit: val.unit,
  81. keys: val.keys,
  82. }
  83. }
  84. }
  85. impl From<&SignatoryKeySet> for MintKeySetInfo {
  86. fn from(val: &SignatoryKeySet) -> Self {
  87. val.to_owned().into()
  88. }
  89. }
  90. impl From<SignatoryKeySet> for MintKeySetInfo {
  91. fn from(val: SignatoryKeySet) -> Self {
  92. MintKeySetInfo {
  93. id: val.id,
  94. unit: val.unit,
  95. active: val.active,
  96. input_fee_ppk: val.input_fee_ppk,
  97. derivation_path: Default::default(),
  98. derivation_path_index: Default::default(),
  99. max_order: 0,
  100. valid_to: None,
  101. valid_from: 0,
  102. }
  103. }
  104. }
  105. impl From<&(MintKeySetInfo, MintKeySet)> for SignatoryKeySet {
  106. fn from((info, key): &(MintKeySetInfo, MintKeySet)) -> Self {
  107. Self {
  108. id: info.id,
  109. unit: key.unit.clone(),
  110. active: info.active,
  111. input_fee_ppk: info.input_fee_ppk,
  112. keys: key.keys.clone().into(),
  113. }
  114. }
  115. }
  116. #[async_trait::async_trait]
  117. /// Signatory trait
  118. pub trait Signatory {
  119. /// The Signatory implementation name. This may be exposed, so being as discreet as possible is
  120. /// advised.
  121. fn name(&self) -> String;
  122. /// Blind sign a message.
  123. ///
  124. /// The message can be for a coin or an auth token.
  125. async fn blind_sign(
  126. &self,
  127. blinded_messages: Vec<BlindedMessage>,
  128. ) -> Result<Vec<BlindSignature>, Error>;
  129. /// Verify [`Proof`] meets conditions and is signed
  130. async fn verify_proofs(&self, proofs: Vec<Proof>) -> Result<(), Error>;
  131. /// Retrieve the list of all mint keysets
  132. async fn keysets(&self) -> Result<SignatoryKeysets, Error>;
  133. /// Add current keyset to inactive keysets
  134. /// Generate new keyset
  135. async fn rotate_keyset(&self, args: RotateKeyArguments) -> Result<SignatoryKeySet, Error>;
  136. }