1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2023 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{
    transaction::{DbcTransaction, Output},
    DbcId, DerivationIndex, DerivedKey, FeeOutput, Input, PublicAddress, Spend,
};
use crate::{Dbc, DbcSecrets, Error, Hash, Result, SignedSpend, Token};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};

pub type InputSrcTx = DbcTransaction;

/// A builder to create a DBC transaction from
/// inputs and outputs.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default)]
pub struct TransactionBuilder {
    inputs: Vec<Input>,
    outputs: Vec<Output>,
    fee: FeeOutput,
    input_details: BTreeMap<DbcId, (DerivedKey, InputSrcTx)>,
    output_details: BTreeMap<DbcId, (PublicAddress, DerivationIndex)>,
}

impl TransactionBuilder {
    /// Add an input given a the Input, the input's derived_key and the input's src transaction
    pub fn add_input(
        mut self,
        input: Input,
        derived_key: DerivedKey,
        input_src_tx: InputSrcTx,
    ) -> Self {
        self.input_details
            .insert(input.dbc_id(), (derived_key, input_src_tx));
        self.inputs.push(input);
        self
    }

    /// Add an input given an iterator over the Input, the input's derived_key and the input's src transaction
    pub fn add_inputs(
        mut self,
        inputs: impl IntoIterator<Item = (Input, DerivedKey, InputSrcTx)>,
    ) -> Self {
        for (input, derived_key, input_src_tx) in inputs.into_iter() {
            self = self.add_input(input, derived_key, input_src_tx);
        }
        self
    }

    /// Add an input given a Dbc and its DerivedKey.
    pub fn add_input_dbc(mut self, dbc: &Dbc, derived_key: &DerivedKey) -> Result<Self> {
        let input_src_tx = dbc.src_tx.clone();
        let input = Input {
            dbc_id: dbc.id(),
            token: dbc.token()?,
        };
        self = self.add_input(input, derived_key.clone(), input_src_tx);
        Ok(self)
    }

    /// Add an input given a list of Dbcs and associated DerivedKeys.
    pub fn add_input_dbcs(mut self, dbcs: &[(Dbc, DerivedKey)]) -> Result<Self> {
        for (dbc, derived_key) in dbcs.iter() {
            self = self.add_input_dbc(dbc, derived_key)?;
        }
        Ok(self)
    }

    /// Add an output given the token, the PublicAddress and the DerivationIndex
    pub fn add_output(
        mut self,
        token: Token,
        public_address: PublicAddress,
        derivation_index: DerivationIndex,
    ) -> Self {
        let dbc_id = public_address.new_dbc_id(&derivation_index);

        self.output_details
            .insert(dbc_id, (public_address, derivation_index));
        let output = Output::new(dbc_id, token.as_nano());
        self.outputs.push(output);

        self
    }

    /// Add a list of outputs given the tokens, the PublicAddress and the DerivationIndex
    pub fn add_outputs(
        mut self,
        outputs: impl IntoIterator<Item = (Token, PublicAddress, DerivationIndex)>,
    ) -> Self {
        for (token, public_address, derivation_index) in outputs.into_iter() {
            self = self.add_output(token, public_address, derivation_index);
        }
        self
    }

    /// Sets the given fee output.
    pub fn set_fee_output(mut self, output: FeeOutput) -> Self {
        self.fee = output;
        self
    }

    /// Get a list of input ids.
    pub fn input_ids(&self) -> Vec<DbcId> {
        self.inputs.iter().map(|i| i.dbc_id()).collect()
    }

    /// Get sum of input Tokens.
    pub fn inputs_tokens_sum(&self) -> Token {
        let amount = self.inputs.iter().map(|i| i.token.as_nano()).sum();
        Token::from_nano(amount)
    }

    /// Get sum of output Tokens.
    pub fn outputs_tokens_sum(&self) -> Token {
        let amount = self
            .outputs
            .iter()
            .map(|o| o.token.as_nano())
            .chain(std::iter::once(self.fee.token.as_nano()))
            .sum();
        Token::from_nano(amount)
    }

    /// Get inputs.
    pub fn inputs(&self) -> &Vec<Input> {
        &self.inputs
    }

    /// Get outputs.
    pub fn outputs(&self) -> &Vec<Output> {
        &self.outputs
    }

    /// Build the DbcTransaction by signing the inputs. Return a DbcBuilder.
    pub fn build(self, reason: Hash) -> Result<DbcBuilder> {
        let spent_tx = DbcTransaction {
            inputs: self.inputs.clone(),
            outputs: self.outputs.clone(),
            fee: self.fee.clone(),
        };
        let signed_spends: BTreeSet<_> = self
            .inputs
            .iter()
            .flat_map(|input| {
                let (derived_key, input_src_tx) = self.input_details.get(&input.dbc_id)?;
                let spend = Spend {
                    dbc_id: input.dbc_id(),
                    spent_tx: spent_tx.clone(),
                    reason,
                    token: input.token,
                    dbc_creation_tx: input_src_tx.clone(),
                };
                let derived_key_sig = derived_key.sign(&spend.to_bytes());
                Some(SignedSpend {
                    spend,
                    derived_key_sig,
                })
            })
            .collect();

        Ok(DbcBuilder::new(
            spent_tx,
            self.output_details,
            signed_spends,
        ))
    }
}

/// A Builder for aggregating SignedSpends and generating the final Dbc outputs.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DbcBuilder {
    pub spent_tx: DbcTransaction,
    pub output_details: BTreeMap<DbcId, (PublicAddress, DerivationIndex)>,
    pub signed_spends: BTreeSet<SignedSpend>,
}

impl DbcBuilder {
    /// Create a new DbcBuilder.
    pub fn new(
        spent_tx: DbcTransaction,
        output_details: BTreeMap<DbcId, (PublicAddress, DerivationIndex)>,
        signed_spends: BTreeSet<SignedSpend>,
    ) -> Self {
        Self {
            spent_tx,
            output_details,
            signed_spends,
        }
    }

    /// Return the signed spends. They each already contain the
    /// spent_tx, so the inclusion of it in the result is just for convenience.
    pub fn signed_spends(&self) -> Vec<&SignedSpend> {
        self.signed_spends.iter().collect()
    }

    /// Build the output Dbcs, verifying the transaction and SignedSpends.
    ///
    /// See TransactionVerifier::verify() for a description of
    /// verifier requirements.
    pub fn build(self) -> Result<Vec<(Dbc, Token)>> {
        // Verify the tx, along with signed spends.
        // Note that we do this just once for entire tx, not once per output Dbc.
        self.spent_tx
            .verify_against_inputs_spent(&self.signed_spends)?;

        // Build output Dbcs.
        self.build_output_dbcs()
    }

    /// Build the output Dbcs (no verification over Tx or SignedSpend is performed).
    pub fn build_without_verifying(self) -> Result<Vec<(Dbc, Token)>> {
        self.build_output_dbcs()
    }

    // Private helper to build output Dbcs.
    fn build_output_dbcs(self) -> Result<Vec<(Dbc, Token)>> {
        self.spent_tx
            .outputs
            .iter()
            .map(|output| {
                let (public_address, derivation_index) = self
                    .output_details
                    .get(&output.dbc_id)
                    .ok_or(Error::DbcIdNotFound)?;

                Ok((
                    Dbc {
                        id: public_address.new_dbc_id(derivation_index),
                        src_tx: self.spent_tx.clone(),
                        secrets: DbcSecrets::from((public_address, derivation_index)),
                        signed_spends: self.signed_spends.clone(),
                    },
                    output.token,
                ))
            })
            .collect()
    }
}