-
Notifications
You must be signed in to change notification settings - Fork 14
/
merge.go
225 lines (201 loc) · 7.12 KB
/
merge.go
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
/*
* Nuts node
* Copyright (C) 2021 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package doc
import (
"errors"
"sort"
"strings"
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/did"
)
// ErrDiffID is returned when a merge is done on documents that do not share the same ID
var ErrDiffID = errors.New("no matching IDs")
// MergeDocuments merges two DID Documents that share the same ID
func MergeDocuments(docA did.Document, docB did.Document) (*did.Document, error) {
result := &did.Document{}
docs := []did.Document{docA, docB}
if !docA.ID.Equals(docB.ID) {
return nil, ErrDiffID
}
mergeBasics(docs, result)
mergeKeys(docs, result)
mergeControllers(docs, result)
mergeServices(docs, result)
// for consistent results
sort.Slice(result.Context, contextSort(result))
sort.Slice(result.Service, serviceSort(result))
sort.Slice(result.VerificationMethod, verificationMethodSort(result))
sort.Slice(result.KeyAgreement, keyAgreementSort(result))
sort.Slice(result.AssertionMethod, assertionSort(result))
sort.Slice(result.Authentication, authenticationSort(result))
sort.Slice(result.CapabilityInvocation, capabilityInvocationSort(result))
sort.Slice(result.CapabilityDelegation, capabilityDelegationSort(result))
return result, nil
}
func mergeBasics(docs []did.Document, result *did.Document) {
// ID
result.ID = docs[0].ID
// context
contexts := map[string]ssi.URI{}
for _, doc := range docs {
for _, context := range doc.Context {
contexts[context.String()] = context
}
}
for _, context := range contexts {
result.Context = append(result.Context, context)
}
}
// mergeKeys merges keys based upon their ID. The ID is derived from the public key.
func mergeKeys(docs []did.Document, result *did.Document) {
// VerificationMethod holds the actual keys
verificationMethods := map[string]*did.VerificationMethod{}
// different VerificationRelationShips
authentications := map[string]did.VerificationRelationship{}
assertions := map[string]did.VerificationRelationship{}
capabilityInvocations := map[string]did.VerificationRelationship{}
capabilityDelegations := map[string]did.VerificationRelationship{}
keyAgreements := map[string]did.VerificationRelationship{}
for _, doc := range docs {
for _, verificationMethod := range doc.VerificationMethod {
verificationMethods[verificationMethod.ID.String()] = verificationMethod
}
for _, authentication := range doc.Authentication {
authentications[authentication.ID.String()] = authentication
}
for _, assertion := range doc.AssertionMethod {
assertions[assertion.ID.String()] = assertion
}
for _, capabilityInvocation := range doc.CapabilityInvocation {
capabilityInvocations[capabilityInvocation.ID.String()] = capabilityInvocation
}
for _, capabilityDelegation := range doc.CapabilityDelegation {
capabilityDelegations[capabilityDelegation.ID.String()] = capabilityDelegation
}
for _, keyAgreement := range doc.KeyAgreement {
keyAgreements[keyAgreement.ID.String()] = keyAgreement
}
}
// verificationMethods from set
for _, verificationMethod := range verificationMethods {
result.VerificationMethod = append(result.VerificationMethod, verificationMethod)
}
// for consistent results
sort.Slice(result.VerificationMethod, func(i, j int) bool {
is := result.VerificationMethod[i].ID.String()
js := result.VerificationMethod[j].ID.String()
return strings.Compare(is, js) == -1
})
// same for all relationships
for _, relation := range authentications {
result.Authentication = append(result.Authentication, relation)
}
for _, relation := range assertions {
result.AssertionMethod = append(result.AssertionMethod, relation)
}
for _, relation := range capabilityInvocations {
result.CapabilityInvocation = append(result.CapabilityInvocation, relation)
}
for _, relation := range capabilityDelegations {
result.CapabilityDelegation = append(result.CapabilityDelegation, relation)
}
for _, relation := range keyAgreements {
result.KeyAgreement = append(result.KeyAgreement, relation)
}
}
func mergeControllers(docs []did.Document, result *did.Document) {
controllers := map[string]did.DID{}
for _, doc := range docs {
for _, controller := range doc.Controller {
controllers[controller.String()] = controller
}
}
for _, controller := range controllers {
result.Controller = append(result.Controller, controller)
}
}
// mergeServices merges services based upon their ID. The ID is derived from the contents. Two services sharing the same ID have the same contents.
func mergeServices(docs []did.Document, result *did.Document) {
services := map[string]did.Service{}
for _, doc := range docs {
for _, service := range doc.Service {
services[service.ID.String()] = service
}
}
for _, service := range services {
result.Service = append(result.Service, service)
}
}
type less func(i, j int) bool
func verificationMethodSort(result *did.Document) less {
return func(i, j int) bool {
is := result.VerificationMethod[i].ID.String()
js := result.VerificationMethod[j].ID.String()
return strings.Compare(is, js) == -1
}
}
func keyAgreementSort(result *did.Document) less {
return func(i, j int) bool {
is := result.KeyAgreement[i].ID.String()
js := result.KeyAgreement[j].ID.String()
return strings.Compare(is, js) == -1
}
}
func assertionSort(result *did.Document) less {
return func(i, j int) bool {
is := result.AssertionMethod[i].ID.String()
js := result.AssertionMethod[j].ID.String()
return strings.Compare(is, js) == -1
}
}
func authenticationSort(result *did.Document) less {
return func(i, j int) bool {
is := result.Authentication[i].ID.String()
js := result.Authentication[j].ID.String()
return strings.Compare(is, js) == -1
}
}
func capabilityInvocationSort(result *did.Document) less {
return func(i, j int) bool {
is := result.CapabilityInvocation[i].ID.String()
js := result.CapabilityInvocation[j].ID.String()
return strings.Compare(is, js) == -1
}
}
func capabilityDelegationSort(result *did.Document) less {
return func(i, j int) bool {
is := result.CapabilityDelegation[i].ID.String()
js := result.CapabilityDelegation[j].ID.String()
return strings.Compare(is, js) == -1
}
}
func serviceSort(result *did.Document) less {
return func(i, j int) bool {
is := result.Service[i].ID.String()
js := result.Service[j].ID.String()
return strings.Compare(is, js) == -1
}
}
func contextSort(result *did.Document) less {
return func(i, j int) bool {
is := result.Context[i].String()
js := result.Context[j].String()
return strings.Compare(is, js) == -1
}
}