dubp_documents_parser/stringified_object/
transaction.rs

1//  Copyright (C) 2020  Éloïs SANCHEZ.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16use crate::*;
17
18impl FromStringObject for TransactionDocumentV10 {
19    fn from_string_object(
20        stringified: &TransactionDocumentV10Stringified,
21    ) -> Result<Self, TextParseError> {
22        let issuers = stringified
23            .issuers
24            .iter()
25            .map(|s| ed25519::PublicKey::from_base58(s))
26            .collect::<Result<SmallVec<[ed25519::PublicKey; 1]>, BaseConversionError>>()
27            .map_err(|e| TextParseError::BaseConversionError {
28                field: "issuers",
29                error: e,
30            })?;
31
32        let inputs = stringified
33            .inputs
34            .iter()
35            .map(|s| tx_input_v10_from_str(s))
36            .collect::<Result<Vec<TransactionInputV10>, TextParseError>>()?;
37
38        let unlocks = stringified
39            .unlocks
40            .iter()
41            .map(|s| tx_unlock_v10_from_str(s))
42            .collect::<Result<Vec<TransactionInputUnlocksV10>, TextParseError>>()?;
43
44        let outputs = stringified
45            .outputs
46            .iter()
47            .map(|s| tx_output_v10_from_str(s))
48            .collect::<Result<SmallVec<[TransactionOutputV10; 2]>, TextParseError>>()?;
49
50        let signatures = stringified
51            .signatures
52            .iter()
53            .map(|s| ed25519::Signature::from_base64(s))
54            .collect::<Result<SmallVec<[ed25519::Signature; 1]>, BaseConversionError>>()
55            .map_err(|e| TextParseError::BaseConversionError {
56                field: "signatures",
57                error: e,
58            })?;
59
60        Ok(TransactionDocumentV10Builder {
61            currency: &stringified.currency,
62            blockstamp: Blockstamp::from_str(&stringified.blockstamp)
63                .map_err(TextParseError::BlockstampParseError)?,
64            locktime: stringified.locktime,
65            issuers,
66            inputs: &inputs[..],
67            unlocks: &unlocks[..],
68            outputs,
69            comment: &stringified.comment,
70            hash: if let Some(ref hash) = stringified.hash {
71                Some(
72                    Hash::from_hex(hash).map_err(|e| TextParseError::BaseConversionError {
73                        field: "hash",
74                        error: e,
75                    })?,
76                )
77            } else {
78                None
79            },
80        }
81        .build_with_signature(signatures))
82    }
83}