Skip to main content

dubp_documents_parser/compact_text/
certifications.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
18/// Parse array of compact certification into vector of `CompactCertificationDocument`
19pub fn parse_compact_certifications(
20    str_certs: &[&str],
21) -> Result<Vec<TextDocumentFormat<CertificationDocumentV10>>, ParseCompactDocError> {
22    let mut certifications: Vec<TextDocumentFormat<CertificationDocumentV10>> = Vec::new();
23    for certification in str_certs {
24        let certifications_datas: Vec<&str> = certification.split(':').collect();
25        if certifications_datas.len() == 4 {
26            certifications.push(TextDocumentFormat::Compact(
27                CompactCertificationDocumentV10 {
28                    issuer: ed25519::PublicKey::from_base58(certifications_datas[0])
29                        .map_err(ParseCompactDocError::Issuer)?,
30                    target: ed25519::PublicKey::from_base58(certifications_datas[1])
31                        .map_err(ParseCompactDocError::Target)?,
32                    block_number: BlockNumber(
33                        certifications_datas[2]
34                            .parse()
35                            .map_err(ParseCompactDocError::BlockNumber)?,
36                    ),
37                    signature: ed25519::Signature::from_base64(certifications_datas[3])
38                        .map_err(ParseCompactDocError::Sig)?,
39                },
40            ));
41        }
42    }
43    Ok(certifications)
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use unwrap::unwrap;
50
51    #[test]
52    fn test_parse_compact_certifications() -> Result<(), ParseCompactDocError> {
53        let compact_certs_strs = &[
54            "5bG4wxsFDpG3n7vtgDyv8jCC9h5pWjJdDxDDSE21RgZJ:3Rt4qTmHoLPn5z2FWGAXwQp9AqVokgo85f5N47D2Fosu:352364:462+UqY616pj2WU1M9/xLQIppfuT2CLruoPSGT8Frm1iKepp1fQ3iNk3b/Z6EaFJ3cFD4Eu2jMmgwsbcnVQXBg==",
55            "GFShJBGAnXXNvWuWv2sBTc2jxPfuJLgB6sxunEj69i31:Aav6dYSbWiGZVSeunqYswZRcFkrobpk1NWw3PCjpD8Mz:346647:PPuXwwmL/Voc4Q+6NNKV31cfwlK07SC10m+u91RovPLj4Dn7F+452BucruiFZ190L8aB66RbiHByebE5kVD/DQ==",
56        ];
57
58        let compact_certs = parse_compact_certifications(compact_certs_strs)?;
59
60        assert_eq!(
61            vec![
62                TextDocumentFormat::Compact(CompactCertificationDocumentV10 {
63                    issuer: unwrap!(ed25519::PublicKey::from_base58("5bG4wxsFDpG3n7vtgDyv8jCC9h5pWjJdDxDDSE21RgZJ")),
64                    target: unwrap!(ed25519::PublicKey::from_base58("3Rt4qTmHoLPn5z2FWGAXwQp9AqVokgo85f5N47D2Fosu")),
65                    block_number: BlockNumber(352_364),
66                    signature: unwrap!(ed25519::Signature::from_base64("462+UqY616pj2WU1M9/xLQIppfuT2CLruoPSGT8Frm1iKepp1fQ3iNk3b/Z6EaFJ3cFD4Eu2jMmgwsbcnVQXBg==")),
67                }),
68                TextDocumentFormat::Compact(CompactCertificationDocumentV10 {
69                    issuer: unwrap!(ed25519::PublicKey::from_base58("GFShJBGAnXXNvWuWv2sBTc2jxPfuJLgB6sxunEj69i31")),
70                    target: unwrap!(ed25519::PublicKey::from_base58("Aav6dYSbWiGZVSeunqYswZRcFkrobpk1NWw3PCjpD8Mz")),
71                    block_number: BlockNumber(346_647),
72                    signature: unwrap!(ed25519::Signature::from_base64("PPuXwwmL/Voc4Q+6NNKV31cfwlK07SC10m+u91RovPLj4Dn7F+452BucruiFZ190L8aB66RbiHByebE5kVD/DQ==")),
73                }),
74            ],
75            compact_certs,
76        );
77
78        Ok(())
79    }
80}