1use serde::{Deserialize, Serialize};
6use thiserror::Error;
7
8#[cfg(feature = "wallet")]
9use super::nut00::PreMintSecrets;
10use super::nut00::{BlindSignature, BlindedMessage, Proofs};
11use super::ProofsMethods;
12use crate::Amount;
13
14#[derive(Debug, Error)]
16pub enum Error {
17 #[error(transparent)]
19 DHKE(#[from] crate::dhke::Error),
20 #[error(transparent)]
22 Amount(#[from] crate::amount::Error),
23}
24
25#[cfg(feature = "wallet")]
27#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
28pub struct PreSwap {
29 pub pre_mint_secrets: PreMintSecrets,
31 pub swap_request: SwapRequest,
33 pub derived_secret_count: u32,
35 pub fee: Amount,
37}
38
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
41#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
42pub struct SwapRequest {
43 #[cfg_attr(feature = "swagger", schema(value_type = Vec<crate::Proof>))]
45 inputs: Proofs,
46 outputs: Vec<BlindedMessage>,
48}
49
50impl SwapRequest {
51 pub fn new(inputs: Proofs, outputs: Vec<BlindedMessage>) -> Self {
53 Self {
54 inputs: inputs.without_dleqs(),
55 outputs,
56 }
57 }
58
59 pub fn inputs(&self) -> &Proofs {
61 &self.inputs
62 }
63
64 pub fn outputs(&self) -> &Vec<BlindedMessage> {
66 &self.outputs
67 }
68
69 pub fn outputs_mut(&mut self) -> &mut Vec<BlindedMessage> {
71 &mut self.outputs
72 }
73
74 pub fn input_amount(&self) -> Result<Amount, Error> {
76 Ok(Amount::try_sum(
77 self.inputs.iter().map(|proof| proof.amount),
78 )?)
79 }
80
81 pub fn output_amount(&self) -> Result<Amount, Error> {
83 Ok(Amount::try_sum(
84 self.outputs.iter().map(|proof| proof.amount),
85 )?)
86 }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
91#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
92pub struct SwapResponse {
93 pub signatures: Vec<BlindSignature>,
95}
96
97impl SwapResponse {
98 pub fn new(promises: Vec<BlindSignature>) -> Self {
100 Self {
101 signatures: promises,
102 }
103 }
104
105 pub fn promises_amount(&self) -> Result<Amount, Error> {
107 Ok(Amount::try_sum(
108 self.signatures
109 .iter()
110 .map(|BlindSignature { amount, .. }| *amount),
111 )?)
112 }
113}