+90
src/types.rs
+90
src/types.rs
···
7
7
use std::collections::HashMap;
8
8
use std::fmt::{Debug, Display};
9
9
10
+
const DID_DOC_JSONLD_CTX: &[&str] = &[
11
+
"https://www.w3.org/ns/did/v1",
12
+
"https://w3id.org/security/multikey/v1",
13
+
"https://w3id.org/security/suites/secp256k1-2019/v1",
14
+
];
15
+
10
16
#[derive(Debug, JsonSchema, Deserialize, Serialize)]
11
17
#[serde(tag = "type")]
12
18
pub enum PlcOperationType {
···
83
89
String::json_schema(generator)
84
90
}
85
91
}
92
+
93
+
#[derive(Debug, JsonSchema, Serialize)]
94
+
#[serde(rename_all = "camelCase")]
95
+
pub struct DidDocument {
96
+
// I hate json-ld.
97
+
#[serde(rename = "@context")]
98
+
context: &'static [&'static str],
99
+
100
+
pub id: String,
101
+
pub also_known_as: Vec<String>,
102
+
pub verification_method: Vec<DidVerificationMethod>,
103
+
pub service: Vec<DidService>,
104
+
}
105
+
106
+
impl DidDocument {
107
+
pub fn from_create(did: &str, op: PlcCreate) -> Self {
108
+
DidDocument {
109
+
context: DID_DOC_JSONLD_CTX,
110
+
id: did.to_string(),
111
+
also_known_as: vec![op.handle],
112
+
verification_method: vec![DidVerificationMethod {
113
+
id: format!("{did}:#atproto"),
114
+
ty: "Multikey".to_string(),
115
+
controller: did.to_string(),
116
+
public_key_multibase: op.signing_key[8..].to_string(),
117
+
}],
118
+
service: vec![DidService {
119
+
id: "#atproto_pds".to_string(),
120
+
ty: "AtprotoPersonalDataServer".to_string(),
121
+
service_endpoint: op.service,
122
+
}],
123
+
}
124
+
}
125
+
126
+
pub fn from_plc_op(did: &str, op: PlcOperation) -> Self {
127
+
let verification_method = op
128
+
.verification_methods
129
+
.into_iter()
130
+
.map(|(service, did_key)| DidVerificationMethod {
131
+
id: format!("{did}#{service}"),
132
+
ty: "Multikey".to_string(),
133
+
controller: did.to_string(),
134
+
public_key_multibase: did_key[8..].to_string(),
135
+
})
136
+
.collect();
137
+
138
+
let service = op
139
+
.services
140
+
.into_iter()
141
+
.map(|(id, service)| DidService {
142
+
id: format!("#{id}"),
143
+
ty: service.ty,
144
+
service_endpoint: service.endpoint,
145
+
})
146
+
.collect();
147
+
148
+
DidDocument {
149
+
context: DID_DOC_JSONLD_CTX,
150
+
id: did.to_string(),
151
+
also_known_as: op.also_known_as,
152
+
verification_method,
153
+
service,
154
+
}
155
+
}
156
+
}
157
+
158
+
#[derive(Debug, JsonSchema, Serialize)]
159
+
#[serde(rename_all = "camelCase")]
160
+
pub struct DidVerificationMethod {
161
+
pub id: String,
162
+
#[serde(rename = "type")]
163
+
pub ty: String,
164
+
pub controller: String,
165
+
pub public_key_multibase: String,
166
+
}
167
+
168
+
#[derive(Debug, JsonSchema, Serialize)]
169
+
#[serde(rename_all = "camelCase")]
170
+
pub struct DidService {
171
+
pub id: String,
172
+
#[serde(rename = "type")]
173
+
pub ty: String,
174
+
pub service_endpoint: String,
175
+
}