@@ -6,8 +6,10 @@ import {
6
6
HcsDidMessage ,
7
7
HcsDidUpdateDidOwnerEvent ,
8
8
} from ".." ;
9
+ import { IpfsDidDocumentDownloader } from "../utils/ipfs" ;
9
10
import { DidDocumentJsonProperties } from "./did-document-json-properties" ;
10
11
import { DidSyntax } from "./did-syntax" ;
12
+ import { HcsDidCreateDidDocumentEvent } from "./hcs/did/event/document/hcs-did-create-did-document-event" ;
11
13
import { HcsDidEventTargetName } from "./hcs/did/event/hcs-did-event-target-name" ;
12
14
import { HcsDidUpdateServiceEvent } from "./hcs/did/event/service/hcs-did-update-service-event" ;
13
15
import { HcsDidCreateVerificationMethodEvent } from "./hcs/did/event/verification-method/hcs-did-create-verification-method-event" ;
@@ -24,6 +26,7 @@ export class DidDocument {
24
26
private updated : Timestamp = null ;
25
27
private versionId : string = null ;
26
28
private deactivated : boolean = false ;
29
+ private downloader : IpfsDidDocumentDownloader = new IpfsDidDocumentDownloader ( ) ;
27
30
28
31
private controller : any ;
29
32
private services : Map < string , any > = new Map ( ) ;
@@ -37,11 +40,9 @@ export class DidDocument {
37
40
capabilityDelegation : [ ] ,
38
41
} ;
39
42
40
- constructor ( did : string , messages : HcsDidMessage [ ] ) {
43
+ constructor ( did : string ) {
41
44
this . id = did ;
42
45
this . context = DidSyntax . DID_DOCUMENT_CONTEXT ;
43
-
44
- this . processMessages ( messages ) ;
45
46
}
46
47
47
48
public hasOwner ( ) {
@@ -72,14 +73,49 @@ export class DidDocument {
72
73
return this . deactivated ;
73
74
}
74
75
76
+ public async processMessages ( messages : HcsDidMessage [ ] ) : Promise < void > {
77
+ for ( const msg of messages ) {
78
+ if (
79
+ ! this . controller &&
80
+ msg . getOperation ( ) === DidMethodOperation . CREATE &&
81
+ msg . getEvent ( ) . targetName !== HcsDidEventTargetName . DID_OWNER &&
82
+ msg . getEvent ( ) . targetName !== HcsDidEventTargetName . DID_DOCUMENT
83
+ ) {
84
+ console . warn ( "DID document owner is not registered. Event will be ignored..." ) ;
85
+ return ;
86
+ }
87
+
88
+ switch ( msg . getOperation ( ) ) {
89
+ case DidMethodOperation . CREATE :
90
+ await this . processCreateMessage ( msg ) ;
91
+ return ;
92
+ case DidMethodOperation . UPDATE :
93
+ await this . processUpdateMessage ( msg ) ;
94
+ return ;
95
+ case DidMethodOperation . REVOKE :
96
+ await this . processRevokeMessage ( msg ) ;
97
+ return ;
98
+ case DidMethodOperation . DELETE :
99
+ await this . processDeleteMessage ( msg ) ;
100
+ return ;
101
+ default :
102
+ console . warn ( `Operation ${ msg . getOperation ( ) } is not supported. Event will be ignored...` ) ;
103
+ }
104
+ }
105
+ }
106
+
107
+ public setIpfsDownloader ( downloader : IpfsDidDocumentDownloader ) {
108
+ this . downloader = downloader ;
109
+ }
110
+
75
111
public toJsonTree ( ) : any {
76
112
let rootObject = { } ;
77
113
78
114
rootObject [ DidDocumentJsonProperties . CONTEXT ] = this . context ;
79
115
rootObject [ DidDocumentJsonProperties . ID ] = this . id ;
80
116
81
- if ( this . controller && this . id !== this . controller . controller ) {
82
- rootObject [ DidDocumentJsonProperties . CONTROLLER ] = this . controller . controller ;
117
+ if ( this . controller && this . id !== this . controller && this . id !== this . controller . controller ) {
118
+ rootObject [ DidDocumentJsonProperties . CONTROLLER ] = this . controller . controller ?? this . controller ;
83
119
}
84
120
85
121
rootObject [ DidDocumentJsonProperties . VERIFICATION_METHOD ] = Array . from ( this . verificationMethods . values ( ) ) ;
@@ -148,40 +184,40 @@ export class DidDocument {
148
184
this . versionId = timestamp . toDate ( ) . getTime ( ) . toString ( ) ;
149
185
}
150
186
151
- private processMessages ( messages : HcsDidMessage [ ] ) : void {
152
- messages . forEach ( ( msg ) => {
153
- if (
154
- ! this . controller &&
155
- msg . getOperation ( ) === DidMethodOperation . CREATE &&
156
- msg . getEvent ( ) . targetName !== HcsDidEventTargetName . DID_OWNER
157
- ) {
158
- console . warn ( "DID document owner is not registered. Event will be ignored..." ) ;
159
- return ;
160
- }
161
-
162
- switch ( msg . getOperation ( ) ) {
163
- case DidMethodOperation . CREATE :
164
- this . processCreateMessage ( msg ) ;
165
- return ;
166
- case DidMethodOperation . UPDATE :
167
- this . processUpdateMessage ( msg ) ;
168
- return ;
169
- case DidMethodOperation . REVOKE :
170
- this . processRevokeMessage ( msg ) ;
171
- return ;
172
- case DidMethodOperation . DELETE :
173
- this . processDeleteMessage ( msg ) ;
174
- return ;
175
- default :
176
- console . warn ( `Operation ${ msg . getOperation ( ) } is not supported. Event will be ignored...` ) ;
177
- }
178
- } ) ;
179
- }
180
-
181
- private processCreateMessage ( message : HcsDidMessage ) : void {
187
+ private async processCreateMessage ( message : HcsDidMessage ) : Promise < void > {
182
188
const event = message . getEvent ( ) ;
183
189
184
190
switch ( event . targetName ) {
191
+ case HcsDidEventTargetName . DID_DOCUMENT :
192
+ const doc = await this . downloader . downloadDocument ( event as HcsDidCreateDidDocumentEvent ) ;
193
+ if ( doc [ DidDocumentJsonProperties . ID ] !== this . id ) {
194
+ throw new Error ( "Document ID does not match did" ) ;
195
+ }
196
+ this . controller = doc [ DidDocumentJsonProperties . CONTROLLER ] ;
197
+
198
+ this . services = new Map (
199
+ ( doc [ DidDocumentJsonProperties . SERVICE ] ?? [ ] ) . map ( ( service ) => [ service . id , service ] )
200
+ ) ;
201
+ this . verificationMethods = new Map (
202
+ ( doc [ DidDocumentJsonProperties . VERIFICATION_METHOD ] ?? [ ] ) . map ( ( verificationMethod ) => [
203
+ verificationMethod . id ,
204
+ verificationMethod ,
205
+ ] )
206
+ ) ;
207
+
208
+ this . verificationRelationships [ DidDocumentJsonProperties . ASSERTION_METHOD ] =
209
+ doc [ DidDocumentJsonProperties . ASSERTION_METHOD ] ?? [ ] ;
210
+ this . verificationRelationships [ DidDocumentJsonProperties . AUTHENTICATION ] =
211
+ doc [ DidDocumentJsonProperties . AUTHENTICATION ] ?? [ ] ;
212
+ this . verificationRelationships [ DidDocumentJsonProperties . KEY_AGREEMENT ] =
213
+ doc [ DidDocumentJsonProperties . KEY_AGREEMENT ] ?? [ ] ;
214
+ this . verificationRelationships [ DidDocumentJsonProperties . CAPABILITY_INVOCATION ] =
215
+ doc [ DidDocumentJsonProperties . CAPABILITY_INVOCATION ] ?? [ ] ;
216
+ this . verificationRelationships [ DidDocumentJsonProperties . CAPABILITY_DELEGATION ] =
217
+ doc [ DidDocumentJsonProperties . CAPABILITY_DELEGATION ] ?? [ ] ;
218
+
219
+ return ;
220
+
185
221
case HcsDidEventTargetName . DID_OWNER :
186
222
if ( this . controller ) {
187
223
console . warn ( `DID owner is already registered: ${ this . controller } . Event will be ignored...` ) ;
@@ -244,7 +280,7 @@ export class DidDocument {
244
280
}
245
281
}
246
282
247
- private processUpdateMessage ( message : HcsDidMessage ) : void {
283
+ private async processUpdateMessage ( message : HcsDidMessage ) : Promise < void > {
248
284
const event = message . getEvent ( ) ;
249
285
250
286
switch ( event . targetName ) {
@@ -303,7 +339,7 @@ export class DidDocument {
303
339
}
304
340
}
305
341
306
- private processRevokeMessage ( message : HcsDidMessage ) : void {
342
+ private async processRevokeMessage ( message : HcsDidMessage ) : Promise < void > {
307
343
const event = message . getEvent ( ) ;
308
344
309
345
switch ( event . targetName ) {
@@ -369,7 +405,7 @@ export class DidDocument {
369
405
}
370
406
}
371
407
372
- private processDeleteMessage ( message : HcsDidMessage ) : void {
408
+ private async processDeleteMessage ( message : HcsDidMessage ) : Promise < void > {
373
409
const event = message . getEvent ( ) ;
374
410
375
411
switch ( event . targetName ) {
0 commit comments