signatory.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 amounts: Vec<u64>,
  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. /// Amounts supported by the keyset
  69. pub amounts: Vec<u64>,
  70. /// Information about the fee per public key
  71. pub input_fee_ppk: u64,
  72. /// Final expiry of the keyset (unix timestamp in the future)
  73. pub final_expiry: Option<u64>,
  74. }
  75. impl From<&SignatoryKeySet> for KeySet {
  76. fn from(val: &SignatoryKeySet) -> Self {
  77. val.to_owned().into()
  78. }
  79. }
  80. impl From<SignatoryKeySet> for KeySet {
  81. fn from(val: SignatoryKeySet) -> Self {
  82. KeySet {
  83. id: val.id,
  84. unit: val.unit,
  85. keys: val.keys,
  86. final_expiry: val.final_expiry,
  87. }
  88. }
  89. }
  90. impl From<&SignatoryKeySet> for MintKeySetInfo {
  91. fn from(val: &SignatoryKeySet) -> Self {
  92. val.to_owned().into()
  93. }
  94. }
  95. impl From<SignatoryKeySet> for MintKeySetInfo {
  96. fn from(val: SignatoryKeySet) -> Self {
  97. MintKeySetInfo {
  98. id: val.id,
  99. unit: val.unit,
  100. active: val.active,
  101. input_fee_ppk: val.input_fee_ppk,
  102. derivation_path: Default::default(),
  103. derivation_path_index: Default::default(),
  104. max_order: 0,
  105. amounts: val.amounts,
  106. final_expiry: val.final_expiry,
  107. valid_from: 0,
  108. }
  109. }
  110. }
  111. impl From<&(MintKeySetInfo, MintKeySet)> for SignatoryKeySet {
  112. fn from((info, key): &(MintKeySetInfo, MintKeySet)) -> Self {
  113. Self {
  114. id: info.id,
  115. unit: key.unit.clone(),
  116. active: info.active,
  117. input_fee_ppk: info.input_fee_ppk,
  118. amounts: info.amounts.clone(),
  119. keys: key.keys.clone().into(),
  120. final_expiry: key.final_expiry,
  121. }
  122. }
  123. }
  124. #[async_trait::async_trait]
  125. /// Signatory trait
  126. pub trait Signatory {
  127. /// The Signatory implementation name. This may be exposed, so being as discrete as possible is
  128. /// advised.
  129. fn name(&self) -> String;
  130. /// Blind sign a message.
  131. ///
  132. /// The message can be for a coin or an auth token.
  133. async fn blind_sign(
  134. &self,
  135. blinded_messages: Vec<BlindedMessage>,
  136. ) -> Result<Vec<BlindSignature>, Error>;
  137. /// Verify [`Proof`] meets conditions and is signed
  138. async fn verify_proofs(&self, proofs: Vec<Proof>) -> Result<(), Error>;
  139. /// Retrieve the list of all mint keysets
  140. async fn keysets(&self) -> Result<SignatoryKeysets, Error>;
  141. /// Add current keyset to inactive keysets
  142. /// Generate new keyset
  143. async fn rotate_keyset(&self, args: RotateKeyArguments) -> Result<SignatoryKeySet, Error>;
  144. }