This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
sidetree.go
223 lines (182 loc) · 5.09 KB
/
sidetree.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package sidetree
import (
"encoding/json"
"fmt"
"net/http"
"github.com/trustbloc/sidetree-core-go/pkg/commitment"
"github.com/trustbloc/sidetree-core-go/pkg/document"
"github.com/trustbloc/sidetree-core-go/pkg/util/pubkey"
"github.com/trustbloc/sidetree-core-go/pkg/versions/1_0/client"
"github.com/hyperledger/aries-framework-go/pkg/common/model"
diddoc "github.com/hyperledger/aries-framework-go/pkg/doc/did"
"github.com/hyperledger/aries-framework-go/pkg/doc/jose/jwk"
"github.com/hyperledger/aries-framework-go/pkg/doc/util/jwkkid"
"github.com/hyperledger/aries-framework-go/pkg/doc/util/kmsdidkey"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/pkg/vdr/fingerprint"
"github.com/hyperledger/aries-framework-go/test/bdd/pkg/util"
)
const docTemplate = `{
"publicKey": [
{
"id": "%s",
"type": "%s",
"purposes": ["authentication"],
"publicKeyJwk": %s
}%s
],
"service": [
{
"id": "hub",
"type": "%s",
"serviceEndpoint": %s,
"recipientKeys" : [ "%s" ]
}
]
}`
const (
sha2_256 = 18 // multihash
defaultKeyType = "JsonWebKey2020"
)
type didResolution struct {
Context interface{} `json:"@context"`
DIDDocument json.RawMessage `json:"didDocument"`
ResolverMetadata json.RawMessage `json:"resolverMetadata"`
MethodMetadata json.RawMessage `json:"methodMetadata"`
}
// CreateDIDParams defines parameters for CreateDID().
type CreateDIDParams struct {
URL string
KeyID string
JWK *jwk.JWK
UpdateJWK *jwk.JWK
RecoveryJWK *jwk.JWK
EncryptionKey []byte
KeyType string
EncKeyType kms.KeyType
ServiceEndpoint model.Endpoint
ServiceType string
}
// CreateDID in sidetree.
func CreateDID(params *CreateDIDParams) (*diddoc.Doc, error) {
opaqueDoc, err := getOpaqueDocument(params)
if err != nil {
return nil, err
}
req, err := getCreateRequest(opaqueDoc, params.UpdateJWK, params.RecoveryJWK)
if err != nil {
return nil, err
}
var result didResolution
err = util.SendHTTP(http.MethodPost, params.URL, req, &result)
if err != nil {
return nil, err
}
doc, err := diddoc.ParseDocument(result.DIDDocument)
if err != nil {
return nil, fmt.Errorf("failed to parse public DID document: %w", err)
}
return doc, nil
}
//nolint:funlen,gocyclo
func getOpaqueDocument(params *CreateDIDParams) ([]byte, error) {
opsPubKey, err := getPubKey(params.JWK)
if err != nil {
return nil, err
}
keyBytes, err := params.JWK.PublicKeyBytes()
if err != nil {
return nil, err
}
didKey, _ := fingerprint.CreateDIDKey(keyBytes)
ka := ""
keyType := params.KeyType
if keyType == "" {
keyType = defaultKeyType
}
serviceType := params.ServiceType
if serviceType == "" {
serviceType = vdr.DIDCommServiceType
}
if serviceType == vdr.DIDCommV2ServiceType {
didKey, err = kmsdidkey.BuildDIDKeyByKeyType(params.EncryptionKey, params.EncKeyType)
if err != nil {
return nil, err
}
var (
encJWK *jwk.JWK
encJWKMarshalled []byte
)
encJWK, err = jwkkid.BuildJWK(params.EncryptionKey, params.EncKeyType)
if err != nil {
return nil, err
}
encJWKMarshalled, err = encJWK.MarshalJSON()
if err != nil {
return nil, err
}
ka = `,{
"id": "%s",
"type": "%s",
"purposes": ["keyAgreement"],
"publicKeyJwk": %s
}`
kaType := "X25519KeyAgreementKey2019"
switch params.EncKeyType {
case kms.NISTP256ECDHKWType, kms.NISTP384ECDHKWType, kms.NISTP521ECDHKWType:
kaType = "JsonWebKey2020"
}
ka = fmt.Sprintf(ka, "key-2", kaType, encJWKMarshalled)
}
mEndPoint, err := params.ServiceEndpoint.MarshalJSON()
if err != nil {
return nil, err
}
data := fmt.Sprintf(docTemplate, params.KeyID, keyType, opsPubKey, ka, serviceType, mEndPoint, didKey)
doc, err := document.FromBytes([]byte(data))
if err != nil {
return nil, err
}
return doc.Bytes()
}
func getPubKey(j *jwk.JWK) (string, error) {
publicKey, err := pubkey.GetPublicKeyJWK(j.Key)
if err != nil {
return "", err
}
opsPubKeyBytes, err := json.Marshal(publicKey)
if err != nil {
return "", err
}
return string(opsPubKeyBytes), nil
}
func getCreateRequest(doc []byte, updateJWK, recoveryJWK *jwk.JWK) ([]byte, error) {
pubKeyUpdate, err := pubkey.GetPublicKeyJWK(updateJWK.Key)
if err != nil {
return nil, err
}
updateCommitment, err := commitment.GetCommitment(pubKeyUpdate, sha2_256)
if err != nil {
return nil, err
}
pubKeyRecovery, err := pubkey.GetPublicKeyJWK(recoveryJWK.Key)
if err != nil {
return nil, err
}
recoveryCommitment, err := commitment.GetCommitment(pubKeyRecovery, sha2_256)
if err != nil {
return nil, err
}
// for testing purposes we are going to use same commitment key for update and recovery
return client.NewCreateRequest(&client.CreateRequestInfo{
OpaqueDocument: string(doc),
UpdateCommitment: updateCommitment,
RecoveryCommitment: recoveryCommitment,
MultihashCode: sha2_256,
})
}