mod.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use std::ops::Deref;
  2. use std::str::FromStr;
  3. use std::sync::Arc;
  4. use cashu::nuts::nut03::RequestMintResponse as RequestMintResponseSdk;
  5. use cashu::nuts::{SplitRequest as SplitRequestSdk, SplitResponse as SplitResponseSdk};
  6. use cashu::Bolt11Invoice;
  7. use crate::error::Result;
  8. use crate::{Amount, BlindedMessage, BlindedSignature, Proof};
  9. pub struct RequestMintResponse {
  10. inner: RequestMintResponseSdk,
  11. }
  12. impl RequestMintResponse {
  13. pub fn new(invoice: String, hash: String) -> Result<Self> {
  14. let pr = Bolt11Invoice::from_str(&invoice)?;
  15. Ok(Self {
  16. inner: RequestMintResponseSdk { pr, hash },
  17. })
  18. }
  19. pub fn invoice(&self) -> String {
  20. self.inner.pr.to_string()
  21. }
  22. pub fn hash(&self) -> String {
  23. self.inner.hash.to_string()
  24. }
  25. }
  26. impl From<cashu::nuts::nut03::RequestMintResponse> for RequestMintResponse {
  27. fn from(mint_response: cashu::nuts::nut03::RequestMintResponse) -> RequestMintResponse {
  28. RequestMintResponse {
  29. inner: mint_response,
  30. }
  31. }
  32. }
  33. pub struct SplitRequest {
  34. inner: SplitRequestSdk,
  35. }
  36. impl Deref for SplitRequest {
  37. type Target = SplitRequestSdk;
  38. fn deref(&self) -> &Self::Target {
  39. &self.inner
  40. }
  41. }
  42. impl SplitRequest {
  43. pub fn new(proofs: Vec<Arc<Proof>>, outputs: Vec<Arc<BlindedMessage>>) -> Self {
  44. let proofs = proofs.into_iter().map(|p| p.as_ref().into()).collect();
  45. let outputs = outputs.into_iter().map(|o| o.as_ref().into()).collect();
  46. Self {
  47. inner: SplitRequestSdk::new(proofs, outputs),
  48. }
  49. }
  50. pub fn proofs(&self) -> Vec<Arc<Proof>> {
  51. self.inner
  52. .inputs
  53. .clone()
  54. .into_iter()
  55. .map(|p| Arc::new(p.into()))
  56. .collect()
  57. }
  58. pub fn outputs(&self) -> Vec<Arc<BlindedMessage>> {
  59. self.inner
  60. .outputs
  61. .clone()
  62. .into_iter()
  63. .map(|o| Arc::new(o.into()))
  64. .collect()
  65. }
  66. pub fn proofs_amount(&self) -> Arc<Amount> {
  67. Arc::new(self.inner.input_amount().into())
  68. }
  69. pub fn output_amount(&self) -> Arc<Amount> {
  70. Arc::new(self.inner.output_amount().into())
  71. }
  72. }
  73. pub struct SplitResponse {
  74. inner: SplitResponseSdk,
  75. }
  76. impl SplitResponse {
  77. pub fn new(promises: Vec<Arc<BlindedSignature>>) -> Self {
  78. let promises = promises.into_iter().map(|p| p.as_ref().into()).collect();
  79. Self {
  80. inner: SplitResponseSdk::new(promises),
  81. }
  82. }
  83. pub fn promises(&self) -> Vec<Arc<BlindedSignature>> {
  84. self.inner
  85. .promises
  86. .clone()
  87. .unwrap_or_default()
  88. .into_iter()
  89. .map(|p| Arc::new(p.into()))
  90. .collect()
  91. }
  92. pub fn promises_amount(&self) -> Option<Arc<Amount>> {
  93. self.inner.promises_amount().map(|a| Arc::new(a.into()))
  94. }
  95. }
  96. impl From<cashu::nuts::SplitResponse> for SplitResponse {
  97. fn from(inner: cashu::nuts::SplitResponse) -> SplitResponse {
  98. SplitResponse { inner }
  99. }
  100. }