nut24.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //! Bolt12
  2. use serde::{Deserialize, Serialize};
  3. use thiserror::Error;
  4. use super::{CurrencyUnit, MeltOptions, PublicKey};
  5. #[cfg(feature = "mint")]
  6. use crate::quote_id::QuoteId;
  7. use crate::Amount;
  8. /// NUT18 Error
  9. #[derive(Debug, Error)]
  10. pub enum Error {
  11. /// Unknown Quote State
  12. #[error("Unknown quote state")]
  13. UnknownState,
  14. /// Amount overflow
  15. #[error("Amount Overflow")]
  16. AmountOverflow,
  17. /// Publickey not defined
  18. #[error("Publickey not defined")]
  19. PublickeyUndefined,
  20. }
  21. /// Mint quote request [NUT-24]
  22. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  23. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  24. pub struct MintQuoteBolt12Request {
  25. /// Amount
  26. pub amount: Option<Amount>,
  27. /// Unit wallet would like to pay with
  28. pub unit: CurrencyUnit,
  29. /// Memo to create the invoice with
  30. pub description: Option<String>,
  31. /// Pubkey
  32. pub pubkey: PublicKey,
  33. }
  34. /// Mint quote response [NUT-24]
  35. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  36. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  37. #[serde(bound = "Q: Serialize + for<'a> Deserialize<'a>")]
  38. pub struct MintQuoteBolt12Response<Q> {
  39. /// Quote Id
  40. pub quote: Q,
  41. /// Payment request to fulfil
  42. pub request: String,
  43. /// Amount
  44. pub amount: Option<Amount>,
  45. /// Unit wallet would like to pay with
  46. pub unit: CurrencyUnit,
  47. /// Unix timestamp until the quote is valid
  48. pub expiry: Option<u64>,
  49. /// Pubkey
  50. pub pubkey: PublicKey,
  51. /// Amount that has been paid
  52. pub amount_paid: Amount,
  53. /// Amount that has been issued
  54. pub amount_issued: Amount,
  55. }
  56. #[cfg(feature = "mint")]
  57. impl<Q: ToString> MintQuoteBolt12Response<Q> {
  58. /// Convert the MintQuote with a quote type Q to a String
  59. pub fn to_string_id(&self) -> MintQuoteBolt12Response<String> {
  60. MintQuoteBolt12Response {
  61. quote: self.quote.to_string(),
  62. request: self.request.clone(),
  63. amount: self.amount,
  64. unit: self.unit.clone(),
  65. expiry: self.expiry,
  66. pubkey: self.pubkey,
  67. amount_paid: self.amount_paid,
  68. amount_issued: self.amount_issued,
  69. }
  70. }
  71. }
  72. #[cfg(feature = "mint")]
  73. impl From<MintQuoteBolt12Response<QuoteId>> for MintQuoteBolt12Response<String> {
  74. fn from(value: MintQuoteBolt12Response<QuoteId>) -> Self {
  75. Self {
  76. quote: value.quote.to_string(),
  77. request: value.request,
  78. expiry: value.expiry,
  79. amount_paid: value.amount_paid,
  80. amount_issued: value.amount_issued,
  81. pubkey: value.pubkey,
  82. amount: value.amount,
  83. unit: value.unit,
  84. }
  85. }
  86. }
  87. /// Melt quote request [NUT-18]
  88. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  89. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  90. pub struct MeltQuoteBolt12Request {
  91. /// Bolt12 invoice to be paid
  92. pub request: String,
  93. /// Unit wallet would like to pay with
  94. pub unit: CurrencyUnit,
  95. /// Payment Options
  96. pub options: Option<MeltOptions>,
  97. }