1use serde::{Deserialize, Serialize};
6use thiserror::Error;
7
8#[cfg(feature = "wallet")]
9use super::nut00::PreMintSecrets;
10use super::nut00::{BlindSignature, BlindedMessage, Proofs};
11use crate::Amount;
12
13#[derive(Debug, Error)]
15pub enum Error {
16 #[error(transparent)]
18 DHKE(#[from] crate::dhke::Error),
19 #[error(transparent)]
21 Amount(#[from] crate::amount::Error),
22}
23
24#[cfg(feature = "wallet")]
26#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
27pub struct PreSwap {
28 pub pre_mint_secrets: PreMintSecrets,
30 pub swap_request: SwapRequest,
32 pub derived_secret_count: u32,
34 pub fee: Amount,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
41pub struct SwapRequest {
42 #[cfg_attr(feature = "swagger", schema(value_type = Vec<Proof>))]
44 pub inputs: Proofs,
45 pub outputs: Vec<BlindedMessage>,
47}
48
49impl SwapRequest {
50 pub fn new(inputs: Proofs, outputs: Vec<BlindedMessage>) -> Self {
52 Self { inputs, outputs }
53 }
54
55 pub fn input_amount(&self) -> Result<Amount, Error> {
57 Ok(Amount::try_sum(
58 self.inputs.iter().map(|proof| proof.amount),
59 )?)
60 }
61
62 pub fn output_amount(&self) -> Result<Amount, Error> {
64 Ok(Amount::try_sum(
65 self.outputs.iter().map(|proof| proof.amount),
66 )?)
67 }
68}
69
70#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
72#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
73pub struct SwapResponse {
74 pub signatures: Vec<BlindSignature>,
76}
77
78impl SwapResponse {
79 pub fn new(promises: Vec<BlindSignature>) -> SwapResponse {
81 SwapResponse {
82 signatures: promises,
83 }
84 }
85
86 pub fn promises_amount(&self) -> Result<Amount, Error> {
88 Ok(Amount::try_sum(
89 self.signatures
90 .iter()
91 .map(|BlindSignature { amount, .. }| *amount),
92 )?)
93 }
94}