1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
extern crate thiserror;

use async_trait::async_trait;
use didkit::{
    DIDMethod, DIDResolver, Document, DocumentMetadata, ResolutionInputMetadata,
    ResolutionMetadata,
};
use operation::{PLCOperation, Service, SignedOperation, SignedPLCOperation, UnsignedPLCOperation};
use util::op_from_json;

mod audit;
mod error;
mod keypair;
mod multicodec;
mod op_builder;
pub mod operation;
mod util;

pub const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
pub const DEFAULT_HOST: &str = "https://plc.directory";

pub use audit::{AuditLog, DIDAuditLogs};
pub use error::PLCError;
pub use keypair::{BlessedAlgorithm, Keypair};
pub use op_builder::OperationBuilder;

pub struct PLCOperationResult {
    pub did: String,
    pub status: u16,
    pub body: String,
}

/// did:plc Method
///
/// [Specification](https://web.plc.directory/spec/v0.1/did-plc)
pub struct DIDPLC {
    host: String,
    client: reqwest::Client,
}

impl DIDPLC {
    pub fn new(host: &str) -> Self {
        let client = reqwest::Client::builder()
            .user_agent(USER_AGENT)
            .build()
            .unwrap();

        Self {
            host: host.to_string(),
            client,
        }
    }

    pub async fn execute_op(&self, did: &str, op: &SignedPLCOperation) -> Result<PLCOperationResult, PLCError> {
        let res = self
            .client
            .post(format!("{}/{}", self.host, did))
            .header(reqwest::header::CONTENT_TYPE, "application/json")
            .body(op.to_json())
            .send()
            .await?;

        let status = res.status().as_u16();
        let body: String = res.text().await?;
        Ok(PLCOperationResult {
            did: did.to_string(),
            status: status,
            body,
        })
    }

    pub async fn get_log(&self, did: &str) -> Result<Vec<PLCOperation>, PLCError> {
        let res = self
            .client
            .get(format!("{}/{}/log", self.host, did))
            .send()
            .await?;

        let body: String = res.text().await?;
        let mut operations: Vec<PLCOperation> = vec![];
        let json: Vec<serde_json::Value> =
            serde_json::from_str(&body).map_err(|e| PLCError::Other(e.into()))?;

        for op in json {
            operations.push(
                op_from_json(
                    serde_json::to_string(&op)
                        .map_err(|e| PLCError::Other(e.into()))?
                        .as_str(),
                )
                .map_err(|e| PLCError::Other(e.into()))?,
            );
        }

        Ok(operations)
    }

    pub async fn get_audit_log(&self, did: &str) -> Result<DIDAuditLogs, PLCError> {
        let res = self
            .client
            .get(format!("{}/{}/log/audit", self.host, did))
            .send()
            .await?;

        if !res.status().is_success() {
            return Err(PLCError::Http(
                res.status().as_u16(),
                res.text().await.unwrap_or_default(),
            ));
        }

        let body: String = res.text().await?;

        Ok(DIDAuditLogs::from_json(&body).map_err(|e| PLCError::Other(e.into()))?)
    }

    pub async fn get_last_log(&self, did: &str) -> Result<PLCOperation, PLCError> {
        let res = self
            .client
            .get(format!("{}/{}/log/last", self.host, did))
            .send()
            .await?;

        let body: String = res.text().await?;
        let op: serde_json::Value =
            serde_json::from_str(&body).map_err(|e| PLCError::Other(e.into()))?;

        Ok(op_from_json(
            serde_json::to_string(&op)
                .map_err(|e| PLCError::Other(e.into()))?
                .as_str(),
        )?)
    }

    pub async fn get_current_state(&self, did: &str) -> Result<PLCOperation, PLCError> {
        let res = self
            .client
            .get(format!("{}/{}/data", self.host, did))
            .send()
            .await?;

        let body: String = res.text().await?;

        Ok(PLCOperation::UnsignedPLC(serde_json::from_str::<UnsignedPLCOperation>(&body)
            .map_err(|e| PLCError::Other(e.into()))?
        ))
    }
}

impl Default for DIDPLC {
    fn default() -> Self {
        Self::new(DEFAULT_HOST)
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl DIDMethod for DIDPLC {
    fn name(&self) -> &'static str {
        "did:plc"
    }

    fn to_resolver(&self) -> &dyn DIDResolver {
        self
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl DIDResolver for DIDPLC {
    async fn resolve(
        &self,
        did: &str,
        _input_metadata: &ResolutionInputMetadata,
    ) -> (
        ResolutionMetadata,
        Option<Document>,
        Option<DocumentMetadata>,
    ) {
        let res = match self
            .client
            .get(format!("{}/{}", self.host, did))
            .send()
            .await
        {
            Ok(res) => res,
            Err(err) => {
                return (
                    ResolutionMetadata::from_error(&format!("Failed to get URL: {:?}", err)),
                    None,
                    None,
                )
            }
        };

        match res.status().as_u16() {
            200 => {
                let text = match res.text().await {
                    Ok(json) => json,
                    Err(err) => {
                        return (
                            ResolutionMetadata::from_error(&format!(
                                "Failed to parse JSON response: {:?}",
                                err
                            )),
                            None,
                            None,
                        )
                    }
                };

                match Document::from_json(text.as_str()) {
                    Ok(document) => (ResolutionMetadata::default(), Some(document), None),
                    Err(err) => (
                        ResolutionMetadata::from_error(&format!(
                            "Unable to parse DID document: {:?}",
                            err
                        )),
                        None,
                        None,
                    ),
                }
            }
            404 => (
                ResolutionMetadata::from_error(&format!("DID not found: {}", did)),
                None,
                None,
            ),
            _ => (
                ResolutionMetadata::from_error(&format!("Failed to resolve DID: {}", res.status())),
                None,
                None,
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use operation::PLCOperationType;

    use super::*;

    const PLC_HOST: &str = "https://plc.directory"; // "http://localhost:2894";

    #[actix_rt::test]
    async fn test_didplc_resolve() {
        let didplc = DIDPLC::default();
        let did = "did:plc:ui5pgpumwvufhfnnz52c4lyl";
        let (res_metadata, document, _) = didplc
            .resolve(did, &ResolutionInputMetadata::default())
            .await;

        assert!(res_metadata.error.is_none());
        assert!(document.is_some());
    }

    #[actix_rt::test]
    async fn test_didplc_get_log() {
        let didplc = DIDPLC::default();
        let did = "did:plc:ui5pgpumwvufhfnnz52c4lyl";
        let log = didplc.get_log(did).await;

        assert!(log.is_ok());
        assert!(log.unwrap().len() > 0);
    }

    #[actix_rt::test]
    async fn test_didplc_get_audit_log() {
        let didplc = DIDPLC::default();
        let did = "did:plc:ui5pgpumwvufhfnnz52c4lyl";
        let log = didplc.get_audit_log(did).await;

        assert!(log.is_ok());
        assert!(log.unwrap().len() > 0);
    }

    #[actix_rt::test]
    async fn test_didplc_get_last_log() {
        let didplc = DIDPLC::default();
        let did = "did:plc:ui5pgpumwvufhfnnz52c4lyl";
        let log = didplc.get_last_log(did).await;

        assert!(log.is_ok());
    }

    #[actix_rt::test]
    async fn test_didplc_get_current_state() {
        let didplc = DIDPLC::default();
        let did = "did:plc:ui5pgpumwvufhfnnz52c4lyl";
        let log = didplc.get_current_state(did).await;

        assert!(log.is_ok());
    }

    #[actix_rt::test]
    async fn test_didplc_operations() {
        let didplc = DIDPLC::new(PLC_HOST);
        let recovery_key = Keypair::generate(BlessedAlgorithm::P256);
        let signing_key = Keypair::generate(BlessedAlgorithm::P256);
        let verification_key = Keypair::generate(BlessedAlgorithm::P256);

        let create_op = OperationBuilder::new(&didplc)
            .with_key(&signing_key)
            .with_validation_key(&verification_key)
            .add_rotation_key(&recovery_key)
            .add_rotation_key(&signing_key)
            .with_handle("example.test".to_owned())
            .with_pds("example.test".to_owned())
            .build(PLCOperationType::Operation)
            .await;

        assert!(create_op.is_ok(), "Failed to build create op: {:?}", create_op.err());
        let create_op = create_op.unwrap();
        let did = &create_op.to_did().expect("Failed to turn op to DID");

        let create_res = didplc.execute_op(did, &create_op).await;

        assert!(create_res.is_ok(), "Failed to execute create op: {:?}", create_res.err());
        let create_res = create_res.unwrap();

        assert!(create_res.status == 200, "Failed to execute create op: status = {}, body = {:?}", create_res.status, create_res.body);
        assert!(&create_res.did == did, "Failed to execute create op: did = {}, expected = {}", create_res.did, did);

        let update_op = OperationBuilder::for_did(&didplc, did.clone())
            .with_key(&signing_key)
            .with_validation_key(&verification_key)
            .add_rotation_key(&recovery_key)
            .add_rotation_key(&signing_key)
            .with_handle("touma.example.test".to_owned())
            .with_pds("example.test".to_owned())
            .build(PLCOperationType::Operation)
            .await;

        assert!(update_op.is_ok(), "Failed to build update op: {:?}", update_op.err());
        let update_op = update_op.unwrap();
        let update_res = didplc.execute_op(did, &update_op).await;
        assert!(update_res.is_ok(), "Failed to execute update op: {:?}", update_res.err());

        let update_res = update_res.unwrap();
        assert!(update_res.status == 200, "Failed to execute update op: status = {}, body = {:?}, json = {}", update_res.status, update_res.body, update_op.to_json());
        assert!(&update_res.did == did, "Failed to execute update op: did = {}, expected = {}", update_res.did, did);

        let deactivate_op = OperationBuilder::for_did(&didplc, did.clone())
            .with_key(&signing_key)
            .with_validation_key(&verification_key)
            .add_rotation_key(&recovery_key)
            .add_rotation_key(&signing_key)
            .with_handle("touma.example.test".to_owned())
            .with_pds("example.test".to_owned())
            .build(PLCOperationType::Tombstone)
            .await;
        assert!(deactivate_op.is_ok(), "Failed to build deactivate op: {:?}", deactivate_op.err());
        let deactivate_op = deactivate_op.unwrap();
        let deactivate_res = didplc.execute_op(did, &deactivate_op).await;
        assert!(deactivate_res.is_ok(), "Failed to execute deactivate op: {:?}, json = {}", deactivate_res.err(), deactivate_op.to_json());

        let deactivate_res = deactivate_res.unwrap();
        assert!(deactivate_res.status == 200, "Failed to execute deactivate op: status = {}, body = {:?}", deactivate_res.status, deactivate_res.body);
        assert!(&deactivate_res.did == did, "Failed to execute deactivate op: did = {}, expected = {}", deactivate_res.did, did);

        let recover_op = OperationBuilder::for_did(&didplc, did.clone())
            .with_key(&recovery_key)
            .with_validation_key(&verification_key)
            .add_rotation_key(&recovery_key)
            .add_rotation_key(&signing_key)
            .with_handle("touma.example.test".to_owned())
            .with_pds("example.test".to_owned())
            .build(PLCOperationType::Operation)
            .await;
        assert!(recover_op.is_ok(), "Failed to build recover op: {:?}", recover_op.err());
        let recover_op = recover_op.unwrap();
        let recover_res = didplc.execute_op(did, &recover_op).await;
        assert!(recover_res.is_ok(), "Failed to execute recover op: {:?}, json = {}", recover_res.err(), recover_op.to_json());

        let recover_res = recover_res.unwrap();
        assert!(recover_res.status == 200, "Failed to execute recover op: status = {}, body = {:?}", recover_res.status, recover_res.body);
        assert!(&recover_res.did == did, "Failed to execute recover op: did = {}, expected = {}", recover_res.did, did);
    }
}