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
//  Copyright (C) 2020  Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::*;

impl FromStringObject for TransactionDocumentV10 {
    fn from_string_object(
        stringified: &TransactionDocumentV10Stringified,
    ) -> Result<Self, TextParseError> {
        let issuers = stringified
            .issuers
            .iter()
            .map(|s| ed25519::PublicKey::from_base58(s))
            .collect::<Result<SmallVec<[ed25519::PublicKey; 1]>, BaseConversionError>>()
            .map_err(|e| TextParseError::BaseConversionError {
                field: "issuers",
                error: e,
            })?;

        let inputs = stringified
            .inputs
            .iter()
            .map(|s| tx_input_v10_from_str(s))
            .collect::<Result<Vec<TransactionInputV10>, TextParseError>>()?;

        let unlocks = stringified
            .unlocks
            .iter()
            .map(|s| tx_unlock_v10_from_str(s))
            .collect::<Result<Vec<TransactionInputUnlocksV10>, TextParseError>>()?;

        let outputs = stringified
            .outputs
            .iter()
            .map(|s| tx_output_v10_from_str(s))
            .collect::<Result<SmallVec<[TransactionOutputV10; 2]>, TextParseError>>()?;

        let signatures = stringified
            .signatures
            .iter()
            .map(|s| ed25519::Signature::from_base64(s))
            .collect::<Result<SmallVec<[ed25519::Signature; 1]>, BaseConversionError>>()
            .map_err(|e| TextParseError::BaseConversionError {
                field: "signatures",
                error: e,
            })?;

        Ok(TransactionDocumentV10Builder {
            currency: &stringified.currency,
            blockstamp: Blockstamp::from_str(&stringified.blockstamp)
                .map_err(TextParseError::BlockstampParseError)?,
            locktime: stringified.locktime,
            issuers,
            inputs: &inputs[..],
            unlocks: &unlocks[..],
            outputs,
            comment: &stringified.comment,
            hash: if let Some(ref hash) = stringified.hash {
                Some(
                    Hash::from_hex(hash).map_err(|e| TextParseError::BaseConversionError {
                        field: "hash",
                        error: e,
                    })?,
                )
            } else {
                None
            },
        }
        .build_with_signature(signatures))
    }
}