-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdid.go
160 lines (121 loc) · 2.88 KB
/
did.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
package did
import (
"strings"
"github.com/reiver/go-did/core/v1/syntax"
)
// DID represents a decentralized-identifier (DID).
//
// DIDs are defined here:
// https://www.w3.org/TR/did-core/#did-syntax
type DID string
func constructDID(methodName string, identifier string) ([]byte, error) {
if !syntax.IsMethodName(methodName) {
return nil, errBadDIDMethodName
}
if !syntax.IsMethodSpecificID(identifier) {
return nil, errBadDIDIdentifier
}
var p []byte
{
var buffer [256]byte
p = buffer[0:0]
p = append(p, "did:"...)
p = append(p, methodName...)
p = append(p, ':')
p = append(p, identifier...)
}
return p, nil
}
// ConstructDID creates a did.DID based on a method-name and identifier.
//
// ConstructDID also validates both method-name and identifier, and returns an error if either (or both) of them fail validation.
func ConstructDID(methodName string, identifier string) (DID, error) {
var empty DID
p, err := constructDID(methodName, identifier)
if nil != err {
return empty, err
}
return DID(string(p)), nil
}
// MustConstructDID is similar to [ConstructDID] except it panic()s if there is an error.
func MustConstructDID(methodName string, identifier string) DID {
decentralizedID, err := ConstructDID(methodName, identifier)
if nil != err {
panic(err)
}
return decentralizedID
}
func (receiver DID) DecompileDID() (methodName string, identifier string, err error) {
var str string = string(receiver)
{
if !strings.HasPrefix(str, didprefix) {
return "", "", errMissingDIDPrefix
}
str = str[len(didprefix):]
}
var index int
{
index = strings.Index(str, ":")
if index < 0 {
return "", "", errMissingDIDIdentifier
}
}
{
methodName = str[:index]
if !syntax.IsMethodName(methodName) {
return "", "", errBadDIDMethodName
}
str = str[1+index:]
}
{
identifier = str
if !syntax.IsMethodSpecificID(str) {
return "", "", errBadDIDIdentifier
}
}
return methodName, identifier, nil
}
func (receiver DID) MarshalText() ([]byte, error) {
err := receiver.Validate()
if nil != err {
return nil, err
}
var str string = string(receiver)
return []byte(str), nil
}
func (receiver DID) MethodName() string {
methodName, _, err := receiver.DecompileDID()
if nil != err {
return ""
}
return methodName
}
func (receiver DID) Identifier() string {
_, identifier, err := receiver.DecompileDID()
if nil != err {
return ""
}
return identifier
}
func (receiver DID) String() string {
_, _, err := receiver.DecompileDID()
if nil != err {
return ""
}
return string(receiver)
}
// Validator returns an error did the did.DID is not valid.
func (receiver DID) Validate() error {
_, _, err := receiver.DecompileDID()
return err
}
func (receiver *DID) UnmarshalText(text []byte) error {
if nil == receiver {
return errNilReceiver
}
if nil == text {
return errNilBytes
}
*receiver = DID(string(text))
return nil
}