dubp_documents_parser/compact_text/
memberships.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 memberships
19pub fn parse_compact_memberships(
20    currency: &str,
21    membership_type: MembershipType,
22    array_memberships: &[&str],
23) -> Result<Vec<MembershipDocumentV10>, ParseCompactDocError> {
24    array_memberships
25        .iter()
26        .map(|membership| {
27            let membership_datas: Vec<&str> = membership.split(':').collect();
28            if membership_datas.len() == 5 {
29                let membership_doc_builder = MembershipDocumentV10Builder {
30                    currency,
31                    issuer: ed25519::PublicKey::from_base58(membership_datas[0])
32                        .map_err(ParseCompactDocError::Issuer)?,
33                    blockstamp: Blockstamp::from_str(membership_datas[2])
34                        .map_err(ParseCompactDocError::Blockstamp)?,
35                    membership: membership_type,
36                    identity_username: membership_datas[4],
37                    identity_blockstamp: Blockstamp::from_str(membership_datas[3])
38                        .map_err(ParseCompactDocError::Blockstamp)?,
39                };
40                let membership_sig = ed25519::Signature::from_base64(membership_datas[1])
41                    .map_err(ParseCompactDocError::Sig)?;
42                Ok(membership_doc_builder.build_with_signature(svec![membership_sig]))
43            } else {
44                Err(ParseCompactDocError::WrongFormat)
45            }
46        })
47        .collect()
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use unwrap::unwrap;
54
55    #[test]
56    fn test_parse_compact_memberships() -> Result<(), ParseCompactDocError> {
57        let compact_memberships_strs = &[
58            "5bG4wxsFDpG3n7vtgDyv8jCC9h5pWjJdDxDDSE21RgZJ:462+UqY616pj2WU1M9/xLQIppfuT2CLruoPSGT8Frm1iKepp1fQ3iNk3b/Z6EaFJ3cFD4Eu2jMmgwsbcnVQXBg==:123123-000004E70532AEC7EFC90C63C3FF996D2C070915DFAEB37E24E149E94A48730E:123123-000004E70532AEC7EFC90C63C3FF996D2C070915DFAEB37E24E149E94A48730E:toto",
59            "GFShJBGAnXXNvWuWv2sBTc2jxPfuJLgB6sxunEj69i31:PPuXwwmL/Voc4Q+6NNKV31cfwlK07SC10m+u91RovPLj4Dn7F+452BucruiFZ190L8aB66RbiHByebE5kVD/DQ==:246246-0000060288862F19C36CD79AD8BAE142B0667EDECCD9E10826E345C358002F6F:246246-0000060288862F19C36CD79AD8BAE142B0667EDECCD9E10826E345C358002F6F:titi",
60        ];
61
62        let memberships =
63            parse_compact_memberships("test", MembershipType::In(), compact_memberships_strs)?;
64
65        assert_eq!(
66            vec![
67                MembershipDocumentV10Builder {
68                    currency: "test",
69                    issuer: unwrap!(ed25519::PublicKey::from_base58("5bG4wxsFDpG3n7vtgDyv8jCC9h5pWjJdDxDDSE21RgZJ")),
70                    blockstamp: unwrap!(Blockstamp::from_str("123123-000004E70532AEC7EFC90C63C3FF996D2C070915DFAEB37E24E149E94A48730E")),
71                    membership: MembershipType::In(),
72                    identity_username: "toto",
73                    identity_blockstamp: unwrap!(Blockstamp::from_str("123123-000004E70532AEC7EFC90C63C3FF996D2C070915DFAEB37E24E149E94A48730E")),
74                }.build_with_signature(svec![unwrap!(ed25519::Signature::from_base64("462+UqY616pj2WU1M9/xLQIppfuT2CLruoPSGT8Frm1iKepp1fQ3iNk3b/Z6EaFJ3cFD4Eu2jMmgwsbcnVQXBg=="))]),
75                MembershipDocumentV10Builder {
76                    currency: "test",
77                    issuer: unwrap!(ed25519::PublicKey::from_base58("GFShJBGAnXXNvWuWv2sBTc2jxPfuJLgB6sxunEj69i31")),
78                    blockstamp: unwrap!(Blockstamp::from_str("246246-0000060288862F19C36CD79AD8BAE142B0667EDECCD9E10826E345C358002F6F")),
79                    membership: MembershipType::In(),
80                    identity_username: "titi",
81                    identity_blockstamp: unwrap!(Blockstamp::from_str("246246-0000060288862F19C36CD79AD8BAE142B0667EDECCD9E10826E345C358002F6F")),
82                }.build_with_signature(svec![unwrap!(ed25519::Signature::from_base64("PPuXwwmL/Voc4Q+6NNKV31cfwlK07SC10m+u91RovPLj4Dn7F+452BucruiFZ190L8aB66RbiHByebE5kVD/DQ=="))]),
83            ],
84            memberships,
85        );
86
87        Ok(())
88    }
89}