fi-verifiable-data 0.0.6

VC and VP wrapper for rust and WASM. fi-digital-signatures crate is used to sign and verify the data objects.
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#[cfg(not(feature = "wasm"))]
use std::borrow::{Borrow, BorrowMut};
use std::collections::HashMap;

#[cfg(not(feature = "wasm"))]
use crate::constants::FIELD_CASTING_ERROR;
#[cfg(feature = "wasm")]
use crate::proof::ProofType;
#[cfg(not(feature = "wasm"))]
use crate::proof::{FiProof, Proof};
use crate::{document::VerificationDocument, error::FiError};
#[cfg(not(feature = "wasm"))]
use chrono::DateTime;
use chrono::Utc;
#[cfg(feature = "wasm")]
use fi_digital_signatures::algorithms::Algorithm;
#[cfg(feature = "wasm")]
use js_sys::{Array, Object};
#[cfg(not(feature = "wasm"))]
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "wasm"))]
use serde_json::Value;
use wasm_bindgen::prelude::wasm_bindgen;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::JsValue;

#[cfg(not(feature = "wasm"))]
#[derive(Serialize, Deserialize)]
#[wasm_bindgen]
pub struct VC {
    #[serde(rename = "@context")]
    contexts: Vec<Value>,
    #[serde(rename = "type")]
    types: Vec<String>,
    id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    evidence: Option<Value>,
    issuer: Value,
    #[serde(rename = "validFrom")]
    valid_from: String,
    #[serde(rename = "validUntil", skip_serializing_if = "Option::is_none")]
    valid_until: Option<String>,
    #[serde(rename = "credentialSubject")]
    credential_subject: Value,
    #[serde(rename = "credentialStatus")]
    credential_status: Option<Value>,
    #[serde(rename = "credentialSchema", skip_serializing_if = "Option::is_none")]
    credential_schema: Option<Value>,
    #[serde(rename = "refreshService", skip_serializing_if = "Option::is_none")]
    refresh_service: Option<Value>,
    #[serde(rename = "termsOfUse", skip_serializing_if = "Option::is_none")]
    terms_of_use: Option<Value>,
    #[serde(rename = "proof", skip_serializing_if = "Option::is_none")]
    proof: Option<FiProof>,
    #[serde(skip_serializing, skip_deserializing)]
    optional_fields: HashMap<String, Box<Value>>,
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
pub struct VC(HashMap<String, Box<JsValue>>);

#[cfg(not(feature = "wasm"))]
impl VC {
    pub fn new(
        id: String,
        issuer: Value,
        name: Option<Value>,
        description: Option<Value>,
        valid_until: Option<DateTime<Utc>>,
    ) -> VC {
        let datetime = Utc::now().to_rfc3339();
        let mut vc = VC {
            contexts: Vec::new(),
            types: Vec::new(),
            credential_subject: Value::Null,
            evidence: None,
            id,
            name,
            description,
            issuer,
            valid_from: datetime,
            valid_until: match valid_until {
                Some(val) => Some(val.to_rfc3339()),
                None => None,
            },
            credential_status: None,
            optional_fields: HashMap::new(),
            credential_schema: None,
            proof: None,
            refresh_service: None,
            terms_of_use: None,
        };

        vc.types.push(String::from("VerifiableCredential"));

        vc
    }

    pub fn add_issuer(&mut self, issuer: Value) -> Result<(), FiError> {
        if self.issuer.is_array() {
            let arr = match self.issuer.as_array_mut() {
                Some(val) => val,
                None => return Err(FiError::new(FIELD_CASTING_ERROR)),
            };

            arr.push(issuer);

            self.issuer = match serde_json::to_value(arr) {
                Ok(val) => val,
                Err(error) => {
                    eprint!("{}", error);
                    return Err(FiError::new(FIELD_CASTING_ERROR));
                }
            };
        } else {
            return Err(FiError::new(FIELD_CASTING_ERROR));
        }

        return Ok(());
    }

    pub fn set_issuer(&mut self, issuer: Value) {
        self.issuer = issuer;
    }

    pub fn set_context(&mut self, contexts: Vec<Value>) {
        self.contexts = contexts;
    }

    pub fn add_context(&mut self, context: Value) {
        self.contexts.push(context)
    }

    pub fn set_types(&mut self, types: Vec<String>) {
        self.types = types;
    }

    pub fn set_credential_status(&mut self, credential_status: Option<Value>) {
        self.credential_status = credential_status;
    }

    pub fn set_credential_schemas(&mut self, credential_schema: Option<Value>) {
        self.credential_schema = credential_schema;
    }

    pub fn set_expire(&mut self, expire: Option<String>) {
        self.valid_until = expire;
    }

    pub fn set_terms_of_use(&mut self, terms_of_use: Option<Value>) {
        self.terms_of_use = terms_of_use;
    }

    pub fn set_refresh_service(&mut self, refresh_service: Option<Value>) {
        self.refresh_service = refresh_service;
    }

    pub fn set_evidence(&mut self, evidence: Option<Value>) {
        self.evidence = evidence;
    }

    pub fn get_proof(&self) -> &Option<FiProof> {
        self.proof.borrow()
    }

    pub fn get_proof_mut(&mut self) -> &mut Option<FiProof> {
        self.proof.borrow_mut()
    }

    pub fn sign(
        &mut self,
        doc: &mut VerificationDocument,
        mut proof: FiProof,
    ) -> Result<(), FiError> {
        let signable_values = match self.get_signable_content() {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        match proof.sign(doc, signable_values.to_string()) {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        self.proof = Some(proof);
        return Ok(());
    }

    pub fn verify(&mut self, doc: &mut VerificationDocument) -> Result<bool, FiError> {
        let signable_values = match self.get_signable_content() {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        let proof = match self.proof.as_mut() {
            None => return Err(FiError::new("Cannot get proof as a mutable reference")),
            Some(val) => val,
        };

        proof.verify(doc, signable_values.to_string())
    }

    pub fn to_object(&mut self) -> Result<Value, FiError> {
        let mut value = match serde_json::to_value(&self) {
            Err(error) => {
                eprintln!("{}", error);
                return Err(FiError::new("Cannot create value object from VC"));
            }
            Ok(val) => val,
        };

        let obj = value.as_object_mut().unwrap();

        self.optional_fields.iter_mut().for_each(move |(key, val)| {
            obj.insert(String::from(key), val.take());
        });

        return Ok(value);
    }

    pub fn get_signable_content(&mut self) -> Result<Value, FiError> {
        let mut val = match self.to_object() {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        val.as_object_mut().unwrap().remove("proof");

        return Ok(val);
    }

    pub fn add_field(&mut self, key: &str, val: Value) {
        self.optional_fields
            .insert(String::from(key), Box::new(val));
    }

    pub fn from(value: Value) -> Result<Self, FiError> {
        let mut map: HashMap<String, Box<Value>> = HashMap::new();

        match serde_ignored::deserialize(&value, |path| {
            let _path = path.to_string();
            let value_to_save = value[&_path].clone();
            map.insert(_path, Box::new(value_to_save));
        }) {
            Ok(val) => return Ok(val),
            Err(error) => return Err(FiError::new(error.to_string().as_str())),
        };
    }
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
impl VC {
    #[wasm_bindgen(constructor)]
    pub fn new(
        id: String,
        issuer: JsValue,
        name: JsValue,
        description: JsValue,
        valid_until: JsValue,
        contexts: Vec<String>,
    ) -> VC {
        let datetime = Utc::now().to_rfc3339();
        let mut vc: HashMap<String, Box<JsValue>> = HashMap::new();

        let mut types: Vec<JsValue> = Vec::new();
        types.push(JsValue::from_str("VerifiableCredential"));

        vc.insert(String::from("type"), Box::new(JsValue::from(types)));
        vc.insert(String::from("@context"), Box::new(JsValue::from(contexts)));
        vc.insert(String::from("credentialSubject"), Box::new(JsValue::NULL));
        vc.insert(String::from("evidence"), Box::new(JsValue::NULL));
        vc.insert(String::from("id"), Box::new(JsValue::from(id)));
        vc.insert(String::from("name"), Box::new(name));
        vc.insert(String::from("description"), Box::new(description));
        vc.insert(String::from("issuer"), Box::new(issuer));
        vc.insert(String::from("validFrom"), Box::new(JsValue::from(datetime)));
        vc.insert(String::from("validUntil"), Box::new(valid_until));
        vc.insert(String::from("credentialStatus"), Box::new(JsValue::NULL));
        vc.insert(String::from("credentialSchema"), Box::new(JsValue::NULL));
        vc.insert(String::from("proof"), Box::new(JsValue::null()));
        vc.insert(String::from("termsOfUse"), Box::new(JsValue::NULL));
        vc.insert(String::from("refreshService"), Box::new(JsValue::NULL));

        VC(vc)
    }

    #[wasm_bindgen(js_name = "addIsser")]
    pub fn add_issuer(&mut self, issuer: JsValue) -> Result<(), FiError> {
        if self.0["issuer"].is_array() {
            let arr: Array = js_sys::Array::from(&*self.0["issuer"]);

            arr.push(&issuer.clone());

            self.0
                .insert(String::from("issuer"), Box::new(JsValue::from(arr)));
        } else {
            let arr = js_sys::Array::new();
            arr.push(&self.0["issuer"].to_owned());
            arr.push(&issuer.clone());

            self.0
                .insert(String::from("issuer"), Box::new(JsValue::from(arr)));
        }

        return Ok(());
    }

    #[wasm_bindgen(js_name = "setIsser")]
    pub fn set_issuer(&mut self, issuer: JsValue) {
        self.0.insert(String::from("issuer"), Box::new(issuer));
    }

    #[wasm_bindgen(js_name = "setContext")]
    pub fn set_context(&mut self, contexts: JsValue) {
        self.0.insert(String::from("@context"), Box::new(contexts));
    }

    #[wasm_bindgen(js_name = "addContext")]
    pub fn add_context(&mut self, context: JsValue) -> Result<(), FiError> {
        if self.0["@context"].is_array() {
            let arr: Array = js_sys::Array::from(&*self.0["@context"]);

            arr.push(&context.clone());

            self.0
                .insert(String::from("@context"), Box::new(JsValue::from(arr)));
        } else {
            let arr = js_sys::Array::new();
            arr.push(&self.0["@context"].to_owned());
            arr.push(&context.clone());

            self.0
                .insert(String::from("@context"), Box::new(JsValue::from(arr)));
        }

        return Ok(());
    }

    #[wasm_bindgen(js_name = "addType")]
    pub fn add_type(&mut self, _type: JsValue) -> Result<(), FiError> {
        if self.0["type"].is_array() {
            let arr: Array = js_sys::Array::from(&*self.0["type"]);

            arr.push(&_type.clone());

            self.0
                .insert(String::from("type"), Box::new(JsValue::from(arr)));
        } else {
            let arr = js_sys::Array::new();
            arr.push(&self.0["type"].to_owned());
            arr.push(&_type.clone());

            self.0
                .insert(String::from("type"), Box::new(JsValue::from(arr)));
        }

        return Ok(());
    }

    #[wasm_bindgen(js_name = "setType")]
    pub fn set_type(&mut self, _type: JsValue) {
        self.0.insert(String::from("type"), Box::new(_type));
    }

    #[wasm_bindgen(js_name = "setCredentialStatus")]
    pub fn set_credential_status(&mut self, credential_status: JsValue) {
        self.0.insert(
            String::from("credentialStatus"),
            Box::new(credential_status),
        );
    }

    #[wasm_bindgen(js_name = "setCredentialSchemas")]
    pub fn set_credential_schemas(&mut self, credential_schema: JsValue) {
        self.0.insert(
            String::from("credentialSchema"),
            Box::new(credential_schema),
        );
    }

    #[wasm_bindgen(js_name = "setExpire")]
    pub fn set_expire(&mut self, expire: JsValue) {
        self.0.insert(String::from("expire"), Box::new(expire));
    }

    #[wasm_bindgen(js_name = "setTermsOfUse")]
    pub fn set_terms_of_use(&mut self, terms_of_use: JsValue) {
        self.0
            .insert(String::from("termsOfUse"), Box::new(terms_of_use));
    }

    #[wasm_bindgen(js_name = "setRefreshService")]
    pub fn set_refresh_service(&mut self, refresh_service: JsValue) {
        self.0
            .insert(String::from("refreshService"), Box::new(refresh_service));
    }

    #[wasm_bindgen(js_name = "setEvidence")]
    pub fn set_evidence(&mut self, evidence: JsValue) {
        self.0.insert(String::from("issuer"), Box::new(evidence));
    }

    #[wasm_bindgen(js_name = "getProof")]
    pub fn get_proof(&self) -> JsValue {
        *self.0["proof"].clone()
    }

    #[wasm_bindgen]
    pub fn sign(
        &mut self,
        alg: Algorithm,
        purpose: String,
        doc: &mut VerificationDocument,
        proof_type: ProofType,
    ) -> Result<(), FiError> {
        let signable_values = match self.get_signable_content() {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        let proof = match proof_type.sign(alg, purpose, doc, signable_values.to_string()) {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        self.0.insert(String::from("proof"), Box::new(proof));
        return Ok(());
    }

    #[wasm_bindgen]
    pub fn verify(
        &mut self,
        doc: &mut VerificationDocument,
        proof_type: ProofType,
    ) -> Result<bool, FiError> {
        let signable_values = match self.get_signable_content() {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        proof_type.verify(doc, signable_values.to_string(), *self.0["proof"].clone())
    }

    #[wasm_bindgen(js_name = "toObject")]
    pub fn to_object(&mut self) -> Result<Object, FiError> {
        let value = js_sys::Object::new();
        self.0.iter().for_each(|(key, val)| {
            _ = js_sys::Reflect::set(&value, &JsValue::from_str(key), val);
        });

        return Ok(value);
    }

    #[wasm_bindgen(js_name = "getSignableContent")]
    pub fn get_signable_content(&mut self) -> Result<String, FiError> {
        let val = match self.to_object() {
            Err(error) => {
                return Err(error);
            }
            Ok(val) => val,
        };

        match js_sys::Reflect::delete_property(&val, &JsValue::from_str("proof")) {
            Err(error) => return Err(FiError::new(error.as_string().unwrap().as_str())),
            Ok(_val) => {}
        };

        return Ok(val.to_string().into());
    }

    #[wasm_bindgen(js_name = "addField")]
    pub fn add_field(&mut self, key: &str, val: JsValue) {
        self.0.insert(String::from(key), Box::new(val));
    }

    #[wasm_bindgen]
    pub fn from(value: JsValue) -> Result<VC, FiError> {
        let keys = match js_sys::Reflect::own_keys(&value) {
            Ok(val) => val,
            Err(error) => return Err(FiError::new(error.as_string().unwrap().as_str())),
        };

        let mut new_value: HashMap<String, Box<JsValue>> = HashMap::new();
        keys.iter().for_each(|key| {
            let val = match js_sys::Reflect::get(&value, &key) {
                Err(error) => {
                    eprintln!("{}", error.as_string().unwrap());
                    return;
                }
                Ok(_val) => _val,
            };

            new_value.insert(key.as_string().unwrap(), Box::new(val));
        });

        Ok(VC(new_value))
    }
}