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
use crate::interfaces::Value;
use chain_impl_mockchain::transaction::{Input, InputEnum, UtxoPointer};
use serde::{Deserialize, Serialize};

const INPUT_PTR_SIZE: usize = 32;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TransactionInput {
    pub input: TransactionInputType,
    pub value: Value,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionInputType {
    Account([u8; INPUT_PTR_SIZE]),
    Utxo([u8; INPUT_PTR_SIZE], u8),
}

impl From<TransactionInput> for Input {
    fn from(i: TransactionInput) -> Input {
        match i.input {
            TransactionInputType::Account(aid) => {
                Input::from_enum(InputEnum::AccountInput(aid.into(), i.value.into()))
            }
            TransactionInputType::Utxo(txptr, txid) => {
                Input::from_enum(InputEnum::UtxoInput(UtxoPointer {
                    output_index: txid,
                    transaction_id: txptr.into(),
                    value: i.value.into(),
                }))
            }
        }
    }
}

impl From<Input> for TransactionInput {
    fn from(i: Input) -> TransactionInput {
        match i.to_enum() {
            InputEnum::AccountInput(ai, value) => TransactionInput {
                input: TransactionInputType::Account(ai.into()),
                value: value.into(),
            },
            InputEnum::UtxoInput(utxoptr) => TransactionInput {
                input: TransactionInputType::Utxo(
                    utxoptr.transaction_id.into(),
                    utxoptr.output_index,
                ),
                value: utxoptr.value.into(),
            },
        }
    }
}