forked from iotaledger/identity.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.rs
84 lines (71 loc) · 3.09 KB
/
common.rs
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
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
// This file contains helper functions for the examples.
#![allow(dead_code)]
use identity::core::json;
use identity::core::Url;
use identity::core::{FromJson, Timestamp, ToJson};
use identity::credential::Credential;
use identity::credential::CredentialBuilder;
use identity::credential::Subject;
use identity::did::MethodScope;
use identity::iota::{
ClientMap, CredentialValidation, CredentialValidator, IotaVerificationMethod, Receipt, TangleRef,
};
use identity::prelude::*;
/// Helper that takes two DID Documents (identities) for issuer and subject, and
/// creates an unsigned credential with claims about subject by issuer.
pub fn issue_degree(issuer: &IotaDocument, subject: &IotaDocument) -> Result<Credential> {
// Create VC "subject" field containing subject ID and claims about it.
let subject: Subject = Subject::from_json_value(json!({
"id": subject.id().as_str(),
"name": "Alice",
"degree": {
"type": "BachelorDegree",
"name": "Bachelor of Science and Arts",
},
"GPA": "4.0",
}))?;
// Build credential using subject above and issuer.
let credential: Credential = CredentialBuilder::default()
.id(Url::parse("https://example.edu/credentials/3732")?)
.issuer(Url::parse(issuer.id().as_str())?)
.type_("UniversityDegreeCredential")
.subject(subject)
.build()?;
Ok(credential)
}
/// Convenience function for checking that a verifiable credential is valid and not revoked.
pub async fn check_credential(client: &ClientMap, credential: &Credential) -> Result<CredentialValidation> {
// Convert the Verifiable Credential to JSON to potentially "exchange" with a verifier
let credential_json = credential.to_json()?;
// Create a `CredentialValidator` instance to fetch and validate all
// associated DID Documents from the Tangle.
let validator: CredentialValidator<ClientMap> = CredentialValidator::new(client);
// Perform the validation operation.
let validation: CredentialValidation = validator.check(&credential_json).await?;
Ok(validation)
}
/// Convenience function for adding a new `VerificationMethod` with tag #newKey to a DID document
/// and performing an integration chain update, publishing it to the Tangle.
///
/// See "manipulate_did" for further explanation.
pub async fn add_new_key(
client: &ClientMap,
doc: &IotaDocument,
key: &KeyPair,
receipt: &Receipt,
) -> Result<(IotaDocument, KeyPair, Receipt)> {
let mut updated_doc = doc.clone();
// Add #newKey to the document
let new_key: KeyPair = KeyPair::new_ed25519()?;
let method: IotaVerificationMethod = IotaVerificationMethod::from_did(updated_doc.did().clone(), &new_key, "newKey")?;
assert!(updated_doc.insert_method(MethodScope::VerificationMethod, method));
// Prepare the update
updated_doc.set_previous_message_id(*receipt.message_id());
updated_doc.set_updated(Timestamp::now_utc());
updated_doc.sign(key.private())?;
// Publish the update to the Tangle
let update_receipt: Receipt = client.publish_document(&updated_doc).await?;
Ok((updated_doc, new_key, update_receipt))
}