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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::scheme::{on_tx_input_and_witnesses, on_tx_output};
use crate::states::States;
use crate::transaction::{AccountSecretKey, AccountWitnessBuilder};
use chain_crypto::{Ed25519, Ed25519Extended, PublicKey, SecretKey};
use chain_impl_mockchain::accounting::account::{spending, SpendingCounterIncreasing};
use chain_impl_mockchain::{
    account::SpendingCounter,
    fragment::{Fragment, FragmentId},
    transaction::{Input, InputEnum},
    value::Value,
};
pub use hdkeygen::account::AccountId;
use hdkeygen::account::{Account, Seed};
use thiserror::Error;

pub struct Wallet {
    account: EitherAccount,
    state: States<FragmentId, State>,
}

#[derive(Debug, Default)]
pub struct State {
    value: Value,
    counters: SpendingCounterIncreasing,
}

pub struct WalletBuildTx<'a> {
    wallet: &'a mut Wallet,
    needed_input: Value,
    next_value: Value,
    current_counter: SpendingCounter,
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("not enough funds, needed {needed:?}, available {current:?}")]
    NotEnoughFunds { current: Value, needed: Value },
    #[error("invalid lane for spending counter")]
    InvalidLane,
    #[error("spending counter does not match current state")]
    NonMonotonicSpendingCounter,
    #[error(transparent)]
    SpendingCounters(#[from] spending::Error),
}

pub enum EitherAccount {
    Seed(Account<Ed25519>),
    Extended(Account<Ed25519Extended>),
}

impl EitherAccount {
    pub fn new_from_seed(seed: Seed) -> Self {
        EitherAccount::Seed(Account::from_seed(seed))
    }

    pub fn new_from_key(key: SecretKey<Ed25519Extended>) -> Self {
        EitherAccount::Extended(Account::from_secret_key(key))
    }

    pub fn account_id(&self) -> AccountId {
        match self {
            EitherAccount::Extended(account) => account.account_id(),
            EitherAccount::Seed(account) => account.account_id(),
        }
    }

    pub fn secret_key(&self) -> AccountSecretKey {
        match &self {
            EitherAccount::Seed(account) => AccountSecretKey::Ed25519(account.secret().clone()),
            EitherAccount::Extended(account) => {
                AccountSecretKey::Ed25519Extended(account.secret().clone())
            }
        }
    }
}

impl Wallet {
    pub fn new_from_seed(seed: Seed) -> Self {
        Wallet {
            account: EitherAccount::new_from_seed(seed),
            state: States::new(FragmentId::zero_hash(), Default::default()),
        }
    }

    pub fn new_from_key(key: SecretKey<Ed25519Extended>) -> Self {
        Wallet {
            account: EitherAccount::new_from_key(key),
            state: States::new(FragmentId::zero_hash(), Default::default()),
        }
    }

    pub fn account_id(&self) -> AccountId {
        self.account.account_id()
    }

    pub fn secret_key(&self) -> AccountSecretKey {
        self.account.secret_key()
    }

    /// set the state counter so we can sync with the blockchain and the
    /// local state
    ///
    /// TODO: some handling to provide information if needed:
    ///
    /// - [ ] check the counter is not regressing?
    /// - [ ] check that there is continuity?
    ///
    /// TODO: change to a constructor/initializator?, or just make it so it resets the state
    ///
    pub fn set_state(
        &mut self,
        value: Value,
        counters: [SpendingCounter; SpendingCounterIncreasing::LANES],
    ) -> Result<(), Error> {
        let counters = SpendingCounterIncreasing::new_from_counters(counters)?;

        self.state = States::new(FragmentId::zero_hash(), State { value, counters });

        Ok(())
    }

    pub fn spending_counter(&self) -> [SpendingCounter; SpendingCounterIncreasing::LANES] {
        self.state
            .last_state()
            .state()
            .counters
            .get_valid_counters()
    }

    pub fn value(&self) -> Value {
        self.state.last_state().state().value
    }

    /// confirm a pending transaction
    ///
    /// to only do once it is confirmed a transaction is on chain
    /// and is far enough in the blockchain history to be confirmed
    /// as immutable
    ///
    pub fn confirm(&mut self, fragment_id: &FragmentId) {
        self.state.confirm(fragment_id);
    }

    /// get all the pending transactions of the wallet
    ///
    /// If empty it means there's no pending transactions waiting confirmation
    ///
    pub fn pending_transactions(&self) -> impl Iterator<Item = &FragmentId> {
        self.state.unconfirmed_states().map(|(k, _)| k)
    }

    /// get the confirmed value of the wallet
    pub fn confirmed_value(&self) -> Value {
        self.state.confirmed_state().state().value
    }

    /// get the unconfirmed value of the wallet
    ///
    /// if `None`, it means there is no unconfirmed state of the wallet
    /// and the value can be known from `confirmed_value`.
    ///
    /// The returned value is the value we expect to see at some point on
    /// chain once all transactions are on chain confirmed.
    pub fn unconfirmed_value(&self) -> Option<Value> {
        let s = self.state.last_state();

        if s.is_confirmed() {
            None
        } else {
            Some(s.state().value)
        }
    }

    pub fn new_transaction(
        &mut self,
        needed_input: Value,
        lane: u8,
    ) -> Result<WalletBuildTx, Error> {
        let state = self.state.last_state().state();

        let current_counter = *state
            .counters
            .get_valid_counters()
            .get(lane as usize)
            .ok_or(Error::InvalidLane)?;

        let next_value =
            state
                .value
                .checked_sub(needed_input)
                .map_err(|_| Error::NotEnoughFunds {
                    current: state.value,
                    needed: needed_input,
                })?;

        Ok(WalletBuildTx {
            wallet: self,
            needed_input,
            current_counter,
            next_value,
        })
    }

    pub fn check_fragment(
        &mut self,
        fragment_id: &FragmentId,
        fragment: &Fragment,
    ) -> Result<bool, Error> {
        if self.state.contains(fragment_id) {
            return Ok(true);
        }

        let state = self.state.last_state().state();

        let mut new_value = state.value;

        let mut increment_counter = None;
        let mut at_least_one_output = false;

        match fragment {
            Fragment::Initial(_config_params) => {}
            Fragment::UpdateProposal(_update_proposal) => {}
            Fragment::UpdateVote(_signed_update) => {}
            Fragment::OldUtxoDeclaration(_utxos) => {}
            _ => {
                on_tx_input_and_witnesses(fragment, |(input, witness)| {
                    if let InputEnum::AccountInput(id, input_value) = input.to_enum() {
                        if self.account_id().as_ref() == id.as_ref() {
                            new_value = new_value.checked_sub(input_value).expect("value overflow");

                            match witness {
                                chain_impl_mockchain::transaction::Witness::Account(
                                    spending,
                                    _,
                                ) => increment_counter = Some(spending),
                                _ => unreachable!("wrong witness type in account input"),
                            }
                        }
                    }
                });
                on_tx_output(fragment, |(_, output)| {
                    if output
                        .address
                        .public_key()
                        .map(|pk| *pk == Into::<PublicKey<Ed25519>>::into(self.account_id()))
                        .unwrap_or(false)
                    {
                        new_value = new_value.checked_add(output.value).unwrap();
                        at_least_one_output = true;
                    }
                })
            }
        };

        let counters = if let Some(counter) = increment_counter {
            let mut new = state.counters.clone();
            new.next_verify(counter)
                .map_err(|_| Error::NonMonotonicSpendingCounter)?;
            new
        } else {
            state.counters.clone()
        };

        let new_state = State {
            counters,
            value: new_value,
        };

        self.state.push(*fragment_id, new_state);

        Ok(at_least_one_output || increment_counter.is_some())
    }
}

impl<'a> WalletBuildTx<'a> {
    pub fn input(&self) -> Input {
        Input::from_account_public_key(self.wallet.account_id().into(), self.needed_input)
    }

    pub fn witness_builder(&self) -> AccountWitnessBuilder {
        AccountWitnessBuilder(self.current_counter)
    }

    pub fn add_fragment_id(self, fragment_id: FragmentId) {
        let mut counters = self.wallet.state.last_state().state().counters.clone();

        // the counter comes from the current state, so this shouldn't panic
        counters.next_verify(self.current_counter).unwrap();

        self.wallet.state.push(
            fragment_id,
            State {
                value: self.next_value,
                counters,
            },
        );
    }
}