common.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //! Types
  2. use serde::{Deserialize, Serialize};
  3. use crate::error::Error;
  4. use crate::mint_url::MintUrl;
  5. use crate::nuts::nut00::ProofsMethods;
  6. use crate::nuts::{
  7. CurrencyUnit, MeltQuoteState, PaymentMethod, Proof, Proofs, PublicKey, SpendingConditions,
  8. State,
  9. };
  10. use crate::Amount;
  11. /// Melt response with proofs
  12. #[derive(Debug, Clone, Hash, PartialEq, Eq, Default, Serialize, Deserialize)]
  13. pub struct Melted {
  14. /// State of quote
  15. pub state: MeltQuoteState,
  16. /// Preimage of melt payment
  17. pub preimage: Option<String>,
  18. /// Melt change
  19. pub change: Option<Proofs>,
  20. /// Melt amount
  21. pub amount: Amount,
  22. /// Fee paid
  23. pub fee_paid: Amount,
  24. }
  25. impl Melted {
  26. /// Create new [`Melted`]
  27. pub fn from_proofs(
  28. state: MeltQuoteState,
  29. preimage: Option<String>,
  30. amount: Amount,
  31. proofs: Proofs,
  32. change_proofs: Option<Proofs>,
  33. ) -> Result<Self, Error> {
  34. let proofs_amount = proofs.total_amount()?;
  35. let change_amount = match &change_proofs {
  36. Some(change_proofs) => change_proofs.total_amount()?,
  37. None => Amount::ZERO,
  38. };
  39. let fee_paid = proofs_amount
  40. .checked_sub(amount + change_amount)
  41. .ok_or(Error::AmountOverflow)?;
  42. Ok(Self {
  43. state,
  44. preimage,
  45. change: change_proofs,
  46. amount,
  47. fee_paid,
  48. })
  49. }
  50. /// Total amount melted
  51. pub fn total_amount(&self) -> Amount {
  52. self.amount + self.fee_paid
  53. }
  54. }
  55. /// Prooinfo
  56. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  57. pub struct ProofInfo {
  58. /// Proof
  59. pub proof: Proof,
  60. /// y
  61. pub y: PublicKey,
  62. /// Mint Url
  63. pub mint_url: MintUrl,
  64. /// Proof State
  65. pub state: State,
  66. /// Proof Spending Conditions
  67. pub spending_condition: Option<SpendingConditions>,
  68. /// Unit
  69. pub unit: CurrencyUnit,
  70. }
  71. impl ProofInfo {
  72. /// Create new [`ProofInfo`]
  73. pub fn new(
  74. proof: Proof,
  75. mint_url: MintUrl,
  76. state: State,
  77. unit: CurrencyUnit,
  78. ) -> Result<Self, Error> {
  79. let y = proof.y()?;
  80. let spending_condition: Option<SpendingConditions> = (&proof.secret).try_into().ok();
  81. Ok(Self {
  82. proof,
  83. y,
  84. mint_url,
  85. state,
  86. spending_condition,
  87. unit,
  88. })
  89. }
  90. /// Check if [`Proof`] matches conditions
  91. pub fn matches_conditions(
  92. &self,
  93. mint_url: &Option<MintUrl>,
  94. unit: &Option<CurrencyUnit>,
  95. state: &Option<Vec<State>>,
  96. spending_conditions: &Option<Vec<SpendingConditions>>,
  97. ) -> bool {
  98. if let Some(mint_url) = mint_url {
  99. if mint_url.ne(&self.mint_url) {
  100. return false;
  101. }
  102. }
  103. if let Some(unit) = unit {
  104. if unit.ne(&self.unit) {
  105. return false;
  106. }
  107. }
  108. if let Some(state) = state {
  109. if !state.contains(&self.state) {
  110. return false;
  111. }
  112. }
  113. if let Some(spending_conditions) = spending_conditions {
  114. match &self.spending_condition {
  115. None => return false,
  116. Some(s) => {
  117. if !spending_conditions.contains(s) {
  118. return false;
  119. }
  120. }
  121. }
  122. }
  123. true
  124. }
  125. }
  126. /// Key used in hashmap of ln backends to identify what unit and payment method
  127. /// it is for
  128. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  129. pub struct PaymentProcessorKey {
  130. /// Unit of Payment backend
  131. pub unit: CurrencyUnit,
  132. /// Method of payment backend
  133. pub method: PaymentMethod,
  134. }
  135. impl PaymentProcessorKey {
  136. /// Create new [`LnKey`]
  137. pub fn new(unit: CurrencyUnit, method: PaymentMethod) -> Self {
  138. Self { unit, method }
  139. }
  140. }
  141. /// Secs wuotes are valid
  142. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize, Default)]
  143. pub struct QuoteTTL {
  144. /// Seconds mint quote is valid
  145. pub mint_ttl: u64,
  146. /// Seconds melt quote is valid
  147. pub melt_ttl: u64,
  148. }
  149. impl QuoteTTL {
  150. /// Create new [`QuoteTTL`]
  151. pub fn new(mint_ttl: u64, melt_ttl: u64) -> QuoteTTL {
  152. Self { mint_ttl, melt_ttl }
  153. }
  154. }
  155. #[cfg(test)]
  156. mod tests {
  157. use std::str::FromStr;
  158. use super::Melted;
  159. use crate::nuts::{Id, Proof, PublicKey};
  160. use crate::secret::Secret;
  161. use crate::Amount;
  162. #[test]
  163. fn test_melted() {
  164. let keyset_id = Id::from_str("00deadbeef123456").unwrap();
  165. let proof = Proof::new(
  166. Amount::from(64),
  167. keyset_id,
  168. Secret::generate(),
  169. PublicKey::from_hex(
  170. "02deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
  171. )
  172. .unwrap(),
  173. );
  174. let melted = Melted::from_proofs(
  175. super::MeltQuoteState::Paid,
  176. Some("preimage".to_string()),
  177. Amount::from(64),
  178. vec![proof.clone()],
  179. None,
  180. )
  181. .unwrap();
  182. assert_eq!(melted.amount, Amount::from(64));
  183. assert_eq!(melted.fee_paid, Amount::ZERO);
  184. assert_eq!(melted.total_amount(), Amount::from(64));
  185. }
  186. #[test]
  187. fn test_melted_with_change() {
  188. let keyset_id = Id::from_str("00deadbeef123456").unwrap();
  189. let proof = Proof::new(
  190. Amount::from(64),
  191. keyset_id,
  192. Secret::generate(),
  193. PublicKey::from_hex(
  194. "02deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
  195. )
  196. .unwrap(),
  197. );
  198. let change_proof = Proof::new(
  199. Amount::from(32),
  200. keyset_id,
  201. Secret::generate(),
  202. PublicKey::from_hex(
  203. "03deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
  204. )
  205. .unwrap(),
  206. );
  207. let melted = Melted::from_proofs(
  208. super::MeltQuoteState::Paid,
  209. Some("preimage".to_string()),
  210. Amount::from(31),
  211. vec![proof.clone()],
  212. Some(vec![change_proof.clone()]),
  213. )
  214. .unwrap();
  215. assert_eq!(melted.amount, Amount::from(31));
  216. assert_eq!(melted.fee_paid, Amount::from(1));
  217. assert_eq!(melted.total_amount(), Amount::from(32));
  218. }
  219. }
  220. /// Mint Fee Reserve
  221. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
  222. pub struct FeeReserve {
  223. /// Absolute expected min fee
  224. pub min_fee_reserve: Amount,
  225. /// Percentage expected fee
  226. pub percent_fee_reserve: f32,
  227. }