signatory.rs 4.9 KB

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