1use std::str::FromStr;
2
3use crate::{
4 group::{codes::GroupCode, parsers::parse_group},
5 primitives::{codes::PrimitiveCode, parsers::parse_primitive},
6};
7
8use super::{error::Error, group::Group};
9
10#[derive(PartialEq, Debug)]
11pub enum Value {
12 Primitive(PrimitiveCode, Vec<u8>),
13 Group(GroupCode, Group),
14}
15
16pub fn parse_value(stream: &[u8]) -> nom::IResult<&[u8], Value> {
17 const GROUP_SELECTOR: &[u8] = "-".as_bytes();
18 match stream.get(..1).ok_or(Error::EmptyCodeError).unwrap() {
19 GROUP_SELECTOR => {
20 let code = GroupCode::from_str(std::str::from_utf8(stream).unwrap()).unwrap();
21 let (rest, group) = parse_group(stream)?;
22 Ok((rest, Value::Group(code, group)))
23 }
24 _ => {
25 let (rest, value) = parse_primitive::<PrimitiveCode>(stream)?;
26 Ok((rest, Value::Primitive(value.0, value.1)))
27 }
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use crate::{
34 group::{codes::GroupCode, Group},
35 primitives::codes::{
36 attached_signature_code::{AttachedSignatureCode, Index},
37 basic::Basic,
38 self_addressing::SelfAddressing,
39 self_signing::SelfSigning,
40 },
41 primitives::IdentifierCode,
42 value::{parse_value, Value},
43 };
44
45 #[test]
46 fn test_parse_controller_signatures() {
47 let val = parse_value("-AABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".as_bytes());
48 let expected_val = Value::Group(
49 GroupCode::IndexedControllerSignatures(1),
50 Group::IndexedControllerSignatures(vec![(
51 AttachedSignatureCode {
52 index: Index::BothSame(0),
53 code: SelfSigning::Ed25519Sha512,
54 },
55 vec![0u8; 64],
56 )]),
57 );
58 assert_eq!(val, Ok(("".as_bytes(), expected_val)));
59
60 let val = parse_value("-AACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0AACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".as_bytes());
61 let expected_val = Value::Group(
62 GroupCode::IndexedControllerSignatures(2),
63 Group::IndexedControllerSignatures(vec![
64 (
65 AttachedSignatureCode {
66 index: Index::BothSame(0),
67 code: SelfSigning::Ed25519Sha512,
68 },
69 vec![0u8; 64],
70 ),
71 (
72 AttachedSignatureCode {
73 index: Index::Dual(0, 2),
74 code: SelfSigning::Ed448,
75 },
76 vec![0u8; 114],
77 ),
78 ]),
79 );
80 assert_eq!(val, Ok(("".as_bytes(), expected_val)));
81
82 let val = parse_value("-AACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0AACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAextra data".as_bytes());
83 let expected_val = Value::Group(
84 GroupCode::IndexedControllerSignatures(2),
85 Group::IndexedControllerSignatures(vec![
86 (
87 AttachedSignatureCode {
88 index: Index::BothSame(0),
89 code: SelfSigning::Ed25519Sha512,
90 },
91 vec![0u8; 64],
92 ),
93 (
94 AttachedSignatureCode {
95 index: Index::Dual(0, 2),
96 code: SelfSigning::Ed448,
97 },
98 vec![0u8; 114],
99 ),
100 ]),
101 );
102 assert_eq!(val, Ok(("extra data".as_bytes(), expected_val)));
103
104 assert!(parse_value("-AABAA0Q7bqPvenjWXo_YIikMBKOg-pghLKwBi1Plm0PEqdv67L1_c6dq9bll7OFnoLp0a74Nw1cBGdjIPcu-yAllHAw".as_bytes()).is_ok());
105 }
106
107 #[test]
108 fn test_parse_groups() {
109 let attached_str = "-GAC0AAAAAAAAAAAAAAAAAAAAAABEJtQndkvwnMpVGE5oVVbLWSCm-jLviGw1AOOkzBvNwsS0AAAAAAAAAAAAAAAAAAAAAABEJtQndkvwnMpVGE5oVVbLWSCm-jLviGw1AOOkzBvNwsS";
110 let (_rest, attached_sn_dig) = parse_value(attached_str.as_bytes()).unwrap();
111 let expected_value = Value::Group(
112 GroupCode::SealSourceCouples(2),
113 Group::SourceSealCouples(vec![
114 (
115 1,
116 (
117 SelfAddressing::Blake3_256,
118 vec![
119 155, 80, 157, 217, 47, 194, 115, 41, 84, 97, 57, 161, 85, 91, 45, 100,
120 130, 155, 232, 203, 190, 33, 176, 212, 3, 142, 147, 48, 111, 55, 11,
121 18,
122 ],
123 ),
124 ),
125 (
126 1,
127 (
128 SelfAddressing::Blake3_256,
129 vec![
130 155, 80, 157, 217, 47, 194, 115, 41, 84, 97, 57, 161, 85, 91, 45, 100,
131 130, 155, 232, 203, 190, 33, 176, 212, 3, 142, 147, 48, 111, 55, 11,
132 18,
133 ],
134 ),
135 ),
136 ]),
137 );
138 assert_eq!(attached_sn_dig, expected_value);
139
140 let attached_str = "-FABEKC8085pwSwzLwUGzh-HrEoFDwZnCJq27bVp5atdMT9o0AAAAAAAAAAAAAAAAAAAAAAAEKC8085pwSwzLwUGzh-HrEoFDwZnCJq27bVp5atdMT9o-AABAABB5IVZOhEfcH4TBQgOCyMgyQrJujtBBjT8K_zTPk0-FLMtTZuBgXV7jnLw6fDe6FWtzshh2HGCL_H_j4i1b9kF";
141 let (_rest, value) = parse_value(attached_str.as_bytes()).unwrap();
142 let expected_value = Value::Group(
143 GroupCode::TransferableIndexedSigGroups(1),
144 Group::TransIndexedSigGroups(vec![(
145 (
146 IdentifierCode::SelfAddressing(SelfAddressing::Blake3_256),
147 vec![
148 160, 188, 211, 206, 105, 193, 44, 51, 47, 5, 6, 206, 31, 135, 172, 74, 5,
149 15, 6, 103, 8, 154, 182, 237, 181, 105, 229, 171, 93, 49, 63, 104,
150 ],
151 ),
152 0,
153 (
154 SelfAddressing::Blake3_256,
155 vec![
156 160, 188, 211, 206, 105, 193, 44, 51, 47, 5, 6, 206, 31, 135, 172, 74, 5,
157 15, 6, 103, 8, 154, 182, 237, 181, 105, 229, 171, 93, 49, 63, 104,
158 ],
159 ),
160 vec![(
161 AttachedSignatureCode {
162 code: SelfSigning::Ed25519Sha512,
163 index: Index::BothSame(0),
164 },
165 vec![
166 65, 228, 133, 89, 58, 17, 31, 112, 126, 19, 5, 8, 14, 11, 35, 32, 201, 10,
167 201, 186, 59, 65, 6, 52, 252, 43, 252, 211, 62, 77, 62, 20, 179, 45, 77,
168 155, 129, 129, 117, 123, 142, 114, 240, 233, 240, 222, 232, 85, 173, 206,
169 200, 97, 216, 113, 130, 47, 241, 255, 143, 136, 181, 111, 217, 5,
170 ],
171 )],
172 )]),
173 );
174
175 assert_eq!(value, expected_value);
176
177 let attached_str = "-CABBMrwi0a-Zblpqe5Hg7w7iz9JCKnMgWKu_W9w4aNUL64y0BB6cL0DtDVDW26lgjbQu0_D_Pd_6ovBZj6fU-Qjmm7epVs51jEOOwXKbmG4yUvCSN-DQSYSc7HXZRp8CfAw9DQL";
178 let (_rest, value) = parse_value(attached_str.as_bytes()).unwrap();
179 let expected_value = Value::Group(
180 GroupCode::NontransferableReceiptCouples(1),
181 Group::NontransReceiptCouples(vec![(
182 (
183 Basic::Ed25519Nontrans,
184 vec![
185 202, 240, 139, 70, 190, 101, 185, 105, 169, 238, 71, 131, 188, 59, 139, 63,
186 73, 8, 169, 204, 129, 98, 174, 253, 111, 112, 225, 163, 84, 47, 174, 50,
187 ],
188 ),
189 (
190 SelfSigning::Ed25519Sha512,
191 vec![
192 122, 112, 189, 3, 180, 53, 67, 91, 110, 165, 130, 54, 208, 187, 79, 195,
193 252, 247, 127, 234, 139, 193, 102, 62, 159, 83, 228, 35, 154, 110, 222,
194 165, 91, 57, 214, 49, 14, 59, 5, 202, 110, 97, 184, 201, 75, 194, 72, 223,
195 131, 65, 38, 18, 115, 177, 215, 101, 26, 124, 9, 240, 48, 244, 52, 11,
196 ],
197 ),
198 )]),
199 );
200
201 assert_eq!(value, expected_value);
202
203 let cesr_attachment = "-AABAAB6P97kZ3al3V3z3VstRtHRPeOrotuqZZUgBl2yHzgpGyOjAXYGinVqWLAMhdmQ089FTSAzqSTBmJzI8RvIezsJ";
204 let (_rest, value) = parse_value(cesr_attachment.as_bytes()).unwrap();
205 let expected_value = Value::Group(
206 GroupCode::IndexedControllerSignatures(1),
207 Group::IndexedControllerSignatures(vec![(
208 AttachedSignatureCode {
209 code: SelfSigning::Ed25519Sha512,
210 index: Index::BothSame(0),
211 },
212 vec![
213 122, 63, 222, 228, 103, 118, 165, 221, 93, 243, 221, 91, 45, 70, 209, 209, 61,
214 227, 171, 162, 219, 170, 101, 149, 32, 6, 93, 178, 31, 56, 41, 27, 35, 163, 1,
215 118, 6, 138, 117, 106, 88, 176, 12, 133, 217, 144, 211, 207, 69, 77, 32, 51,
216 169, 36, 193, 152, 156, 200, 241, 27, 200, 123, 59, 9,
217 ],
218 )]),
219 );
220
221 assert_eq!(value, expected_value);
222
223 }
229}