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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::{
    account::Identifier,
    account::{LedgerError, SpendingCounter},
    accounting::account::SpendingCounterIncreasing,
    chaintypes::HeaderId,
    key::EitherEd25519SecretKey,
    testing::builders::make_witness_with_lane,
    testing::data::LeaderPair,
    tokens::name::TokenName,
    transaction::{Input, Output, TransactionAuthData, Witness},
    utxo::Entry,
    value::Value,
};
use chain_addr::{Address, AddressReadable, Discrimination, Kind, KindType};
use chain_crypto::{
    testing::TestCryptoGen, AsymmetricKey, Ed25519, Ed25519Extended, KeyPair, PublicKey,
};
use rand_core::RngCore;
use std::{
    collections::HashMap,
    fmt::{self, Debug},
};
use thiserror::Error;

///
/// Struct is responsible for adding some code which makes converting into transaction input/output easily.
/// Also it held all needed information (private key, public key) which can construct witness for transaction.
///
#[derive(Clone)]
pub struct AddressData {
    pub private_key: EitherEd25519SecretKey,
    pub spending_counter: SpendingCounterIncreasing,
    pub address: Address,
}

impl Debug for AddressData {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("AddressData")
            .field("public_key", &self.public_key())
            .field("spending_counter", &self.spending_counter)
            .field("address", &self.address)
            .finish()
    }
}

impl PartialEq for AddressData {
    fn eq(&self, other: &Self) -> bool {
        self.address == other.address
    }
}

impl AddressData {
    pub fn new(
        private_key: EitherEd25519SecretKey,
        spending_counter: SpendingCounterIncreasing,
        address: Address,
    ) -> Self {
        AddressData {
            private_key,
            address,
            spending_counter,
        }
    }

    pub fn from_discrimination_and_kind_type(
        discrimination: Discrimination,
        kind: KindType,
    ) -> Self {
        match kind {
            KindType::Account => AddressData::account(discrimination),
            KindType::Single => AddressData::utxo(discrimination),
            KindType::Group => AddressData::delegation(discrimination),
            _ => panic!("not implemented yet"),
        }
    }

    pub fn from_leader_pair(leader_pair: LeaderPair, discrimination: Discrimination) -> Self {
        Self::new(
            EitherEd25519SecretKey::Normal(leader_pair.key()),
            Default::default(),
            Address(discrimination, Kind::Account(leader_pair.key().to_public())),
        )
    }

    pub fn utxo(discrimination: Discrimination) -> Self {
        let (sk, pk) = AddressData::generate_key_pair::<Ed25519Extended>().into_keys();
        let sk = EitherEd25519SecretKey::Extended(sk);
        let user_address = Address(discrimination, Kind::Single(pk));
        AddressData::new(sk, Default::default(), user_address)
    }

    pub fn account(discrimination: Discrimination) -> Self {
        let (sk, pk) = AddressData::generate_key_pair::<Ed25519Extended>().into_keys();
        let sk = EitherEd25519SecretKey::Extended(sk);
        let user_address = Address(discrimination, Kind::Account(pk));
        AddressData::new(sk, Default::default(), user_address)
    }

    pub fn account_with_spending_counter(
        discrimination: Discrimination,
        spending_counter: u32,
    ) -> Self {
        let (sk, pk) = AddressData::generate_key_pair::<Ed25519Extended>().into_keys();
        let sk = EitherEd25519SecretKey::Extended(sk);
        let user_address = Address(discrimination, Kind::Account(pk));
        AddressData::new(
            sk,
            SpendingCounterIncreasing::new_from_counter(spending_counter.into()),
            user_address,
        )
    }

    pub fn delegation(discrimination: Discrimination) -> Self {
        let (single_sk, single_pk) =
            AddressData::generate_key_pair::<Ed25519Extended>().into_keys();
        let (_delegation_sk, delegation_pk) =
            AddressData::generate_key_pair::<Ed25519Extended>().into_keys();

        let user_address = Address(discrimination, Kind::Group(single_pk, delegation_pk));
        let single_sk = EitherEd25519SecretKey::Extended(single_sk);
        AddressData::new(single_sk, Default::default(), user_address)
    }

    pub fn delegation_from(
        primary_address: &AddressData,
        delegation_address: &AddressData,
    ) -> Self {
        let single_sk = primary_address.private_key.clone();
        let single_pk = primary_address.public_key();
        let user_address = Address(
            primary_address.discrimination(),
            Kind::Group(single_pk, delegation_address.public_key()),
        );
        AddressData::new(single_sk, Default::default(), user_address)
    }

    pub fn delegation_for(address: &AddressData) -> Self {
        AddressData::delegation_from(&AddressData::delegation(address.discrimination()), address)
    }

    pub fn make_input(&self, value: Value, utxo: Option<Entry<Address>>) -> Input {
        match self.address.kind() {
            Kind::Account { .. } => Input::from_account_public_key(self.public_key(), value),
            Kind::Single { .. } | Kind::Group { .. } => {
                Input::from_utxo_entry(utxo.unwrap_or_else(|| {
                    panic!(
                        "invalid state, utxo should be Some if Kind not Account {:?}",
                        &self.address
                    )
                }))
            }
            Kind::Multisig { .. } => unimplemented!(),
            Kind::Script { .. } => unimplemented!(),
        }
    }

    pub fn delegation_id(&self) -> Identifier {
        Identifier::from(self.delegation_key())
    }

    pub fn to_id(&self) -> Identifier {
        Identifier::from(self.public_key())
    }

    pub fn make_output(&self, value: Value) -> Output<Address> {
        Output::from_address(self.address.clone(), value)
    }

    pub fn public_key(&self) -> PublicKey<Ed25519> {
        match self.kind() {
            Kind::Account(key) => key,
            Kind::Group(key, _) => key,
            Kind::Single(key) => key,
            Kind::Multisig(_) => panic!("not yet implemented"),
            Kind::Script(_) => panic!("No public key for a script address"),
        }
    }

    pub fn delegation_key(&self) -> PublicKey<Ed25519> {
        match self.kind() {
            Kind::Group(_, delegation_key) => delegation_key,
            Kind::Account(public_key) => public_key,
            _ => panic!("wrong kind of address to to get delegation key"),
        }
    }

    pub fn confirm_transaction(&mut self) -> Result<(), Error> {
        self.confirm_transaction_at_lane(0)
    }

    pub fn confirm_transaction_at_lane(&mut self, lane: usize) -> Result<(), Error> {
        let sc = self.spending_counter_at_lane(lane)?;
        self.spending_counter
            .next_verify(sc)
            .map_err(|e| LedgerError::SpendingCounterError(e).into())
    }

    pub fn spending_counter_at_lane(&self, lane: usize) -> Result<SpendingCounter, Error> {
        if lane >= SpendingCounterIncreasing::LANES {
            return Err(Error::WrongLaneForSpendingCounter(lane));
        }
        self.spending_counter
            .get_valid_counters()
            .into_iter()
            .find(|x| x.lane() == lane)
            .ok_or(Error::CannotFindSpendingCounter(lane))
    }

    pub fn spending_counter(&self) -> &SpendingCounterIncreasing {
        &self.spending_counter
    }

    pub fn spending_counter_mut(&mut self) -> &mut SpendingCounterIncreasing {
        &mut self.spending_counter
    }

    pub fn private_key(&self) -> EitherEd25519SecretKey {
        self.private_key.clone()
    }

    pub fn kind(&self) -> Kind {
        self.address.kind().clone()
    }

    pub fn discrimination(&self) -> Discrimination {
        self.address.discrimination()
    }

    pub fn to_bech32_str(&self) -> String {
        let prefix = match self.discrimination() {
            Discrimination::Production => "ta",
            Discrimination::Test => "ca",
        };
        AddressReadable::from_address(prefix, &self.address).to_string()
    }

    pub fn generate_key_pair<A: AsymmetricKey>() -> KeyPair<A> {
        TestCryptoGen(0).keypair::<A>(rand_core::OsRng.next_u32())
    }

    pub fn delegation_for_account(
        other: AddressData,
        delegation_public_key: PublicKey<Ed25519>,
    ) -> Self {
        let user_address = Address(
            other.address.discrimination(),
            Kind::Group(other.public_key(), delegation_public_key),
        );
        AddressData::new(other.private_key, other.spending_counter, user_address)
    }

    pub fn make_witness(
        &mut self,
        block0_hash: &HeaderId,
        tad: TransactionAuthData<'_>,
    ) -> Witness {
        self.make_witness_with_lane(block0_hash, 0, tad)
    }

    pub fn make_witness_with_lane(
        &mut self,
        block0_hash: &HeaderId,
        lane: usize,
        tad: TransactionAuthData<'_>,
    ) -> Witness {
        let witness = make_witness_with_lane(block0_hash, self, lane, &tad.hash());
        self.confirm_transaction_at_lane(lane).unwrap();
        witness
    }

    pub fn address(&self) -> Address {
        self.address.clone()
    }
}

impl From<AddressData> for Address {
    fn from(data: AddressData) -> Self {
        data.address
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct AddressDataValue {
    pub address_data: AddressData,
    pub value: Value,
    pub tokens: HashMap<TokenName, Value>,
}

impl AddressDataValue {
    pub fn new(address_data: AddressData, value: Value) -> Self {
        AddressDataValue {
            address_data,
            value,
            tokens: Default::default(),
        }
    }

    pub fn new_with_tokens(
        address_data: AddressData,
        value: Value,
        tokens: HashMap<TokenName, Value>,
    ) -> Self {
        AddressDataValue {
            address_data,
            value,
            tokens,
        }
    }

    pub fn utxo(discrimination: Discrimination, value: Value) -> Self {
        AddressDataValue::new(AddressData::utxo(discrimination), value)
    }

    pub fn account(discrimination: Discrimination, value: Value) -> Self {
        AddressDataValue::new(AddressData::account(discrimination), value)
    }

    pub fn account_with_spending_counter(
        discrimination: Discrimination,
        spending_counter: u32,
        value: Value,
    ) -> Self {
        let address_data =
            AddressData::account_with_spending_counter(discrimination, spending_counter);
        Self::new(address_data, value)
    }

    pub fn delegation(discrimination: Discrimination, value: Value) -> Self {
        AddressDataValue::new(AddressData::delegation(discrimination), value)
    }

    pub fn account_with_tokens(
        discrimination: Discrimination,
        value: Value,
        tokens: HashMap<TokenName, Value>,
    ) -> Self {
        let mut base = Self::account(discrimination, value);
        base.tokens = tokens;
        base
    }

    pub fn from_discrimination_and_kind_type(
        discrimination: Discrimination,
        kind: KindType,
        value: Value,
    ) -> Self {
        AddressDataValue::new(
            AddressData::from_discrimination_and_kind_type(discrimination, kind),
            value,
        )
    }

    pub fn to_id(&self) -> Identifier {
        self.address_data.to_id()
    }

    pub fn public_key(&self) -> PublicKey<Ed25519> {
        self.address_data.public_key()
    }

    pub fn private_key(&self) -> EitherEd25519SecretKey {
        self.address_data.private_key.clone()
    }

    pub fn make_input(&self, utxo: Option<Entry<Address>>) -> Input {
        self.make_input_with_value(utxo, self.value)
    }

    pub fn make_input_with_value(&self, utxo: Option<Entry<Address>>, value: Value) -> Input {
        self.address_data.make_input(value, utxo)
    }

    pub fn make_output(&self) -> Output<Address> {
        self.make_output_with_value(self.value)
    }

    pub fn make_output_with_value(&self, value: Value) -> Output<Address> {
        self.address_data.make_output(value)
    }

    pub fn confirm_transaction(&mut self) -> Result<(), Error> {
        self.address_data.confirm_transaction()
    }

    pub fn confirm_transaction_at_lane(&mut self, lane: usize) -> Result<(), Error> {
        self.address_data.confirm_transaction_at_lane(lane)
    }

    pub fn make_witness(
        &mut self,
        block0_hash: &HeaderId,
        tad: TransactionAuthData<'_>,
    ) -> Witness {
        self.make_witness_with_lane(block0_hash, 0, tad)
    }

    pub fn make_witness_with_lane(
        &mut self,
        block0_hash: &HeaderId,
        lane: usize,
        tad: TransactionAuthData<'_>,
    ) -> Witness {
        self.address_data
            .make_witness_with_lane(block0_hash, lane, tad)
    }

    pub fn kind(&self) -> Kind {
        self.address_data.kind()
    }

    pub fn is_utxo(&self) -> bool {
        matches!(self.kind(), Kind::Single { .. } | Kind::Group { .. })
    }

    pub fn address(&self) -> Address {
        self.address_data.address.clone()
    }

    pub fn address_data(&self) -> AddressData {
        self.address_data.clone()
    }
}

impl From<AddressDataValue> for AddressData {
    fn from(value: AddressDataValue) -> Self {
        value.address_data
    }
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("wrong lane for spending counter: {0}")]
    WrongLaneForSpendingCounter(usize),
    #[error("cannot find spending counter for lane: {0}")]
    CannotFindSpendingCounter(usize),
    #[error(transparent)]
    AccountLedger(#[from] crate::accounting::account::LedgerError),
}