dpp/identity/v0/conversion/
json.rs1use crate::identity::conversion::json::IdentityJsonConversionMethodsV0;
2use crate::identity::conversion::platform_value::IdentityPlatformValueConversionMethodsV0;
3use crate::identity::{identity_public_key, IdentityV0, IDENTIFIER_FIELDS_RAW_OBJECT};
4use crate::ProtocolError;
5use platform_value::{ReplacementType, Value};
6use serde_json::Value as JsonValue;
7use std::convert::TryInto;
8
9impl IdentityJsonConversionMethodsV0 for IdentityV0 {
10 fn to_json_object(&self) -> Result<JsonValue, ProtocolError> {
11 self.to_cleaned_object()?
12 .try_into_validating_json()
13 .map_err(ProtocolError::ValueError)
14 }
15
16 fn to_json(&self) -> Result<JsonValue, ProtocolError> {
17 self.to_cleaned_object()?
18 .try_into()
19 .map_err(ProtocolError::ValueError)
20 }
21
22 fn from_json(json_object: JsonValue) -> Result<Self, ProtocolError> {
24 let mut platform_value: Value = json_object.into();
25
26 platform_value
27 .replace_at_paths(IDENTIFIER_FIELDS_RAW_OBJECT, ReplacementType::Identifier)?;
28
29 if let Some(public_keys_array) = platform_value.get_optional_array_mut_ref("publicKeys")? {
30 for public_key in public_keys_array.iter_mut() {
31 public_key.replace_at_paths(
32 identity_public_key::BINARY_DATA_FIELDS,
33 ReplacementType::BinaryBytes,
34 )?;
35 }
36 }
37
38 let identity: Self = platform_value::from_value(platform_value)?;
39
40 Ok(identity)
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use crate::identity::identity_public_key::v0::IdentityPublicKeyV0;
48 use crate::identity::{IdentityPublicKey, KeyType, Purpose, SecurityLevel};
49 use platform_value::{BinaryData, Identifier};
50 use std::collections::BTreeMap;
51
52 fn sample_identity_v0() -> IdentityV0 {
53 let mut keys: BTreeMap<u32, IdentityPublicKey> = BTreeMap::new();
54 keys.insert(
55 0,
56 IdentityPublicKey::V0(IdentityPublicKeyV0 {
57 id: 0,
58 purpose: Purpose::AUTHENTICATION,
59 security_level: SecurityLevel::MASTER,
60 contract_bounds: None,
61 key_type: KeyType::ECDSA_SECP256K1,
62 read_only: false,
63 data: BinaryData::new(vec![0x33; 33]),
64 disabled_at: None,
65 }),
66 );
67 IdentityV0 {
68 id: Identifier::from([9u8; 32]),
69 public_keys: keys,
70 balance: 42,
71 revision: 1,
72 }
73 }
74
75 #[test]
76 fn to_json_contains_expected_top_level_fields() {
77 let id = sample_identity_v0();
78 let json = id.to_json().expect("to_json");
79 let obj = json.as_object().expect("object");
80 assert!(obj.contains_key("id"));
81 assert!(obj.contains_key("publicKeys"));
82 assert!(obj.contains_key("balance"));
83 assert!(obj.contains_key("revision"));
84 }
85
86 #[test]
98 fn to_json_then_from_json_round_trips_v0() {
99 let id = sample_identity_v0();
100 let json = id.to_json().unwrap();
101 let back = IdentityV0::from_json(json).expect("v0 round-trip should succeed");
102 assert_eq!(id, back);
103 }
104
105 #[test]
106 fn to_json_object_encodes_identifier_as_bytes_array() {
107 let id = sample_identity_v0();
110 let json = id.to_json_object().expect("to_json_object");
111 let obj = json.as_object().expect("object");
112 let id_field =
113 obj.get("id").expect("id").as_array().expect(
114 "to_json_object should render the identifier as a JSON array of byte values",
115 );
116 assert_eq!(id_field.len(), 32);
117 }
118
119 #[test]
120 fn from_json_fails_on_garbage_input() {
121 let json = serde_json::json!({ "id": "not-a-valid-identifier" });
122 let result = IdentityV0::from_json(json);
123 assert!(result.is_err());
124 }
125
126 #[test]
133 fn from_json_fixture_fails_missing_format_version_v0_frozen() {
134 use crate::tests::fixtures::identity_fixture_json;
135 let json = identity_fixture_json();
136 let result = IdentityV0::from_json(json);
137 match result {
138 Err(e) => {
139 let msg = format!("{:?}", e);
140 assert!(
141 msg.contains("$formatVersion") || msg.contains("formatVersion"),
142 "expected missing-formatVersion error, got {msg}"
143 );
144 }
145 Ok(_) => panic!("expected from_json on legacy fixture to fail"),
146 }
147 }
148
149 #[test]
150 fn from_json_errors_when_public_keys_field_is_not_array() {
151 let json = serde_json::json!({
153 "id": "3bufpwQjL5qsvuP4fmCKgXJrKG852DDMYfi9J6XKqPAT",
154 "publicKeys": "oops",
155 "balance": 0,
156 "revision": 0,
157 });
158 let result = IdentityV0::from_json(json);
159 assert!(result.is_err());
160 }
161}