-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
121 lines (93 loc) · 2.86 KB
/
document.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
package doc
import (
"encoding/json"
"fmt"
"slices"
"github.com/bahner/go-ma/did"
cbor "github.com/fxamacker/cbor/v2"
"github.com/ipfs/go-cid"
log "github.com/sirupsen/logrus"
)
type Document struct {
Context []string `cbor:"@context,toarray" json:"@context"`
ID string `cbor:"id" json:"id"`
Controller []string `cbor:"controller,toarray" json:"controller"`
VerificationMethod []VerificationMethod `cbor:"verificationMethod,toarray" json:"verificationMethod"`
AssertionMethod string `cbor:"assertionMethod" json:"assertionMethod"`
KeyAgreement string `cbor:"keyAgreement" json:"keyAgreement"`
Proof Proof `cbor:"proof" json:"proof"`
}
func New(identifier string, controller string) (*Document, error) {
log.Debugf("doc/new: identifier: %s", identifier)
log.Debugf("doc/new: controller: %s", controller)
err := did.Validate(identifier)
if err != nil {
return nil, fmt.Errorf("doc/new: invalid identifier: %w", err)
}
ctrller := []string{identifier}
doc := Document{
Context: DID_CONTEXT,
ID: identifier,
Controller: ctrller,
}
doc.AddController(controller)
log.Infof("doc/new: created new document for %s", identifier)
return &doc, nil
}
func (d *Document) MarshalToCBOR() ([]byte, error) {
bytes, err := cbor.Marshal(d)
if err != nil {
return nil, fmt.Errorf("doc/string: failed to marshal document to CBOR: %w", err)
}
return bytes, nil
}
// Simple string representation of the document
// JSON or empty string on error
func (d *Document) String() string {
bytes, err := json.Marshal(d)
if err != nil {
return ""
}
return string(bytes)
}
// GetOrCreate document from cache or create a new one
// identifier is a did string, eg. "did:ma:k51qzi5uqu5dj9807pbuod1pplf0vxh8m4lfy3ewl9qbm2s8dsf9ugdf9gedhr#foo"
func GetOrCreate(identifier string, controller string) (*Document, cid.Cid, error) {
doc, c, err := Fetch(identifier, true) // Accept cached document
if err == nil {
doc.AddController(controller)
log.Debugf("doc/GetOrCreate: found previously published document for: %s", identifier)
return doc, c, nil
}
doc, err = New(identifier, controller)
if err != nil {
return nil, cid.Cid{}, fmt.Errorf("doc/GetOrCreate: %w", err)
}
return doc, cid.Cid{}, nil
}
func (d *Document) Equal(other *Document) bool {
if d.ID != other.ID {
return false
}
if d.KeyAgreement != other.KeyAgreement {
return false
}
if d.AssertionMethod != other.AssertionMethod {
return false
}
if !compareSlices(d.Context, other.Context) {
return false
}
if !compareSlices(d.Controller, other.Controller) {
return false
}
if d.Proof != other.Proof {
return false
}
return true
}
func compareSlices(a []string, b []string) bool {
slices.Sort(a)
slices.Sort(b)
return slices.Compare(a, b) == 0
}