1
+ mod credential;
2
+
3
+ use serde_json:: { self , Value } ;
4
+ use credential:: * ;
5
+ use std:: { collections:: HashMap } ;
6
+
1
7
pub mod error;
2
8
/// Verification of Data Integrity Proofs requires the resolution of the `verificationMethod` specified in the proof.
3
9
/// The `verificationMethod` refers to a cryptographic key stored in some external source.
@@ -22,23 +28,37 @@ pub trait DIDResolver {
22
28
}
23
29
}
24
30
25
- /// Given the credential type and the credential subject information, create a unissued JSON-LD credential.
26
- /// In order to become a Verifiable Credential, a data integrity proof must be created for the credential and appended to the JSON-LD document.
27
- pub fn create_credential (
28
- _cred_type : & str ,
29
- _cred_subject : serde_json:: Value ,
30
- ) -> Result < serde_json:: Value , Box < dyn std:: error:: Error > > {
31
- unimplemented ! ( ) ;
32
- }
31
+ pub trait DocumentBuilder {
32
+ /// Given the credential type and the credential subject information, create a unissued JSON-LD credential.
33
+ /// In order to become a Verifiable Credential, a data integrity proof must be created for the credential and appended to the JSON-LD document.
34
+ /// this is the default implementation of the `create` method. The `create` method can be overridden to create a custom credential.
35
+ fn create_credential (
36
+ & self ,
37
+ cred_type : String ,
38
+ cred_subject : HashMap < String , Value > ,
39
+ property_set : HashMap < String , Value > ,
40
+ id : & str
41
+ ) -> Result < Credential , Box < dyn std:: error:: Error > > {
42
+ let vc = Credential :: new ( CONTEXT_CREDENTIALS ,
43
+ cred_type,
44
+ cred_subject,
45
+ property_set,
46
+ id
47
+ ) ;
48
+ Ok ( vc)
49
+ }
33
50
34
- /// Given the set of credentials, create a unsigned JSON-LD Presentation of those credentials.
35
- /// In order to become a Verifiable Presentation, a data integrity proof must be created for the presentation and appended to the JSON-LD document.
36
- pub fn create_presentation (
37
- _creds : Vec < serde_json:: Value > ,
38
- ) -> Result < serde_json:: Value , Box < dyn std:: error:: Error > > {
39
- unimplemented ! ( ) ;
51
+ /// Given the set of credentials, create a unsigned JSON-LD Presentation of those credentials.
52
+ /// In order to become a Verifiable Presentation, a data integrity proof must be created for the presentation and appended to the JSON-LD document.
53
+ fn create_presentation (
54
+ _creds : Vec < serde_json:: Value > ,
55
+ ) -> Result < serde_json:: Value , Box < dyn std:: error:: Error > > {
56
+ unimplemented ! ( ) ;
57
+ }
40
58
}
41
59
60
+
61
+ // Commented due to failing cargo check
42
62
// ed25519 cryptography key generation & DID Document creation
43
63
pub fn create_identity (
44
64
_mnemonic : & str ,
@@ -47,7 +67,8 @@ pub fn create_identity(
47
67
unimplemented ! ( ) ;
48
68
}
49
69
50
- /// Given a JSON-LD document, create a data integrity proof for the document.
70
+ /// Given a JSON-LD document, c
71
+ /// reate a data integrity proof for the document.
51
72
/// Currently, only `Ed25519Signature2018` data integrity proofs in the JSON-LD format can be created.
52
73
pub fn create_data_integrity_proof < S : signature:: Signature > (
53
74
_doc : serde_json:: Value ,
@@ -56,9 +77,9 @@ pub fn create_data_integrity_proof<S: signature::Signature>(
56
77
unimplemented ! ( ) ;
57
78
}
58
79
59
- /// Given a JSON-LD document and a DIDResolver, verify the data integrity proof for the document.
60
- /// This will by parsing the `verificationMethod` property of the data integrity proof and resolving it to a key that can be used to verify the proof.
61
- /// Currently only `Ed25519Signature2018` is supported for data integrity proof verification.
80
+ // // / Given a JSON-LD document and a DIDResolver, verify the data integrity proof for the document.
81
+ // // / This will by parsing the `verificationMethod` property of the data integrity proof and resolving it to a key that can be used to verify the proof.
82
+ // // / Currently only `Ed25519Signature2018` is supported for data integrity proof verification.
62
83
pub fn verify_data_integrity_proof < S : signature:: Signature > (
63
84
_doc : serde_json:: Value ,
64
85
_resolver : & impl DIDResolver ,
@@ -69,10 +90,102 @@ pub fn verify_data_integrity_proof<S: signature::Signature>(
69
90
70
91
/// Given a JSON-LD document and a DIDResolver, verify the data integrity proof for the Verifiable Presentation.
71
92
/// Then each claimed Verifiable Credential must be verified for validity and ownership of the credential by the subject.
72
- pub fn verify_presentation < S : signature:: Signature > (
73
- _doc : serde_json:: Value ,
74
- _resolver : & impl DIDResolver ,
75
- _verifier : & impl signature:: Verifier < S > ,
76
- ) -> Result < bool , Box < dyn std:: error:: Error > > {
93
+ pub fn create_presentation (
94
+ _creds : Vec < serde_json:: Value > ,
95
+ ) -> Result < serde_json:: Value , Box < dyn std:: error:: Error > > {
77
96
unimplemented ! ( ) ;
78
97
}
98
+
99
+ #[ cfg( test) ]
100
+ mod tests {
101
+ use crate :: DocumentBuilder ;
102
+ use std:: { collections:: HashMap , vec} ;
103
+ use assert_json_diff:: { assert_json_eq} ;
104
+ use crate :: serde_json:: json;
105
+
106
+ use serde_json:: Value ;
107
+ struct TestObj { }
108
+
109
+ impl TestObj {
110
+ pub fn new ( ) -> Self {
111
+ TestObj { }
112
+ }
113
+ }
114
+ impl DocumentBuilder for TestObj { }
115
+
116
+ #[ test]
117
+ fn test_create_credential ( ) -> Result < ( ) , String > {
118
+ let to = TestObj :: new ( ) ;
119
+ let mut kv_body: HashMap < String , Value > = HashMap :: new ( ) ;
120
+ let mut kv_subject: HashMap < String , Value > = HashMap :: new ( ) ;
121
+
122
+ let _expect = json ! ( {
123
+ "@context" : [
124
+ "https://www.w3.org/2018/credentials/v1" ,
125
+ "https://www.w3.org/2018/credentials/examples/v1"
126
+ ] ,
127
+ "@id" : "https://issuer.oidp.uscis.gov/credentials/83627465" ,
128
+ "type" : [ "VerifiableCredential" , "PermanentResidentCard" ] ,
129
+ "issuer" : "did:example:28394728934792387" ,
130
+ "identifier" : "83627465" ,
131
+ "name" : "Permanent Resident Card" ,
132
+ "description" : "Government of Example Permanent Resident Card." ,
133
+ "issuanceDate" : "2019-12-03T12:19:52Z" ,
134
+ "expirationDate" : "2029-12-03T12:19:52Z" ,
135
+ "credentialSubject" : {
136
+ "id" : "did:example:b34ca6cd37bbf23" ,
137
+ "type" : [ "PermanentResident" , "Person" ] ,
138
+ "givenName" : "JOHN" ,
139
+ "familyName" : "SMITH" ,
140
+ "gender" : "Male" ,
141
+ "image" : "data:image/png;base64,iVBORw0KGgo...kJggg==" ,
142
+ "residentSince" : "2015-01-01" ,
143
+ "lprCategory" : "C09" ,
144
+ "lprNumber" : "999-999-999" ,
145
+ "commuterClassification" : "C1" ,
146
+ "birthCountry" : "Bahamas" ,
147
+ "birthDate" : "1958-07-17"
148
+ } ,
149
+ } ) ;
150
+
151
+ let type_rs = serde_json:: to_value ( [ "VerifiableCredential" . to_string ( ) , "PermanentResidentCard" . to_string ( ) ] ) ;
152
+ if type_rs. is_ok ( ) {
153
+ kv_body. entry ( "type" . to_string ( ) ) . or_insert ( type_rs. unwrap ( ) ) ;
154
+ }
155
+
156
+ kv_body. entry ( "issuer" . to_string ( ) ) . or_insert ( Value :: String ( "did:example:28394728934792387" . to_string ( ) ) ) ;
157
+ kv_body. entry ( "identifier" . to_string ( ) ) . or_insert ( Value :: String ( "83627465" . to_string ( ) ) ) ;
158
+ kv_body. entry ( "name" . to_string ( ) ) . or_insert ( Value :: String ( "Permanent Resident Card" . to_string ( ) ) ) ;
159
+ kv_body. entry ( "description" . to_string ( ) ) . or_insert ( Value :: String ( "Government of Example Permanent Resident Card." . to_string ( ) ) ) ;
160
+ kv_body. entry ( "issuanceDate" . to_string ( ) ) . or_insert ( Value :: String ( "2019-12-03T12:19:52Z" . to_string ( ) ) ) ;
161
+ kv_body. entry ( "expirationDate" . to_string ( ) ) . or_insert ( Value :: String ( "2029-12-03T12:19:52Z" . to_string ( ) ) ) ;
162
+
163
+ kv_subject. entry ( "id" . to_string ( ) ) . or_insert ( Value :: String ( "did:example:b34ca6cd37bbf23" . to_string ( ) ) ) ;
164
+
165
+ let type_rs = serde_json:: to_value ( [ "PermanentResident" . to_string ( ) , "Person" . to_string ( ) ] ) ;
166
+ if type_rs. is_ok ( ) {
167
+ kv_subject. entry ( "type" . to_string ( ) ) . or_insert ( type_rs. unwrap ( ) ) ;
168
+ }
169
+
170
+ kv_subject. entry ( "givenName" . to_string ( ) ) . or_insert ( Value :: String ( "JOHN" . to_string ( ) ) ) ;
171
+ kv_subject. entry ( "familyName" . to_string ( ) ) . or_insert ( Value :: String ( "SMITH" . to_string ( ) ) ) ;
172
+ kv_subject. entry ( "gender" . to_string ( ) ) . or_insert ( Value :: String ( "Male" . to_string ( ) ) ) ;
173
+ kv_subject. entry ( "image" . to_string ( ) ) . or_insert ( Value :: String ( "data:image/png;base64,iVBORw0KGgo...kJggg==" . to_string ( ) ) ) ;
174
+ kv_subject. entry ( "residentSince" . to_string ( ) ) . or_insert ( Value :: String ( "2015-01-01" . to_string ( ) ) ) ;
175
+ kv_subject. entry ( "lprCategory" . to_string ( ) ) . or_insert ( Value :: String ( "C09" . to_string ( ) ) ) ;
176
+ kv_subject. entry ( "lprNumber" . to_string ( ) ) . or_insert ( Value :: String ( "999-999-999" . to_string ( ) ) ) ;
177
+ kv_subject. entry ( "commuterClassification" . to_string ( ) ) . or_insert ( Value :: String ( "C1" . to_string ( ) ) ) ;
178
+ kv_subject. entry ( "birthCountry" . to_string ( ) ) . or_insert ( Value :: String ( "Bahamas" . to_string ( ) ) ) ;
179
+ kv_subject. entry ( "birthDate" . to_string ( ) ) . or_insert ( Value :: String ( "1958-07-17" . to_string ( ) ) ) ;
180
+
181
+ let vc = to. create_credential (
182
+ crate :: CRED_TYPE_PERMANENT_RESIDENT_CARD . to_string ( ) ,
183
+ kv_subject,
184
+ kv_body,
185
+ "https://issuer.oidp.uscis.gov/credentials/83627465" ,
186
+ ) ;
187
+ assert ! ( vc. is_ok( ) ) ;
188
+ assert_json_eq ! ( _expect, vc. unwrap( ) ) ;
189
+ Ok ( ( ) )
190
+ }
191
+ }
0 commit comments