-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
388 lines (354 loc) · 12.6 KB
/
tx.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package cli
import (
"encoding/base64"
"fmt"
"os"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/noandrea/rl2020"
"github.com/spf13/cobra"
"github.com/elesto-dao/elesto/v2/x/credential"
"github.com/elesto-dao/elesto/v2/x/did"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: credential.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", credential.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
// this line is used by starport scaffolding # 1
cmd.AddCommand(
NewPublishCredentialDefinitionCmd(),
NewIssuePublicCredentialCmd(),
NewCreateRevocationListCmd(),
NewUpdateRevocationListCmd(),
)
return cmd
}
func exTx(cmd ...string) string {
return fmt.Sprintln(version.AppName, "tx", credential.ModuleName, strings.Join(cmd, " "))
}
// NewIssuePublicCredentialCmd defines the command to publish credentials
func NewIssuePublicCredentialCmd() *cobra.Command {
var (
command = "issue-public-credential"
signOnly bool
credentialFileOut string
)
cmd := &cobra.Command{
Use: fmt.Sprintln(command, "credential-definition-id", "credential_file"),
Short: "issue a public, on-chain, credential",
Example: exTx(command, "example-definition-id", "credential.json"),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
var (
cID, credentialFile = args[0], args[1]
definitionDID = did.NewChainDID(clientCtx.ChainID, cID)
signer = clientCtx.GetFromAddress()
pwc *credential.WrappedCredential
)
// initialize the definition
if pwc, err = credential.NewWrappedPublicCredentialFromFile(credentialFile); err != nil {
println("error building credential definition", err)
return err
}
// get the issuer did
vmID := pwc.GetIssuerDID().NewVerificationMethodID(signer.String())
if err = sign(pwc, clientCtx.Keyring, signer, vmID); err != nil {
println("error signing the credential:", err)
return err
}
// write to the output file
if !credential.IsEmpty(credentialFileOut) {
pwcB, err := pwc.GetBytes()
if err != nil {
fmt.Printf("error reading the credential bytes: %v", err)
return err
}
if err = os.WriteFile(credentialFileOut, pwcB, 0600); err != nil {
fmt.Printf("error writing the credential to %v: %v", credentialFileOut, err)
return err
}
fmt.Sprintln("credential exported to", credentialFileOut)
if signOnly {
return nil
}
}
// create the message
msg := credential.NewMsgIssuePublicVerifiableCredentialRequest(
pwc.PublicVerifiableCredential,
definitionDID,
signer,
)
// execute
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
// add flags
cmd.Flags().StringVar(&credentialFileOut, "export", "", "export the signed credential to a json file")
cmd.Flags().BoolVar(&signOnly, "sign-only", false, "only sign the credential, do not broadcast (requires --export)")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewCreateRevocationListCmd update a revocation list
func NewCreateRevocationListCmd() *cobra.Command {
var (
command = "create-revocation-list"
issuerDIDstr string
definitionID string
revocationListSize int
revocationIndexes []int
)
cmd := &cobra.Command{
Use: fmt.Sprintln(command, "revocation-credential-id"),
Short: "create a revocation list credential",
Example: exTx(command, "https://revocations.id/list/001"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
var (
cID = args[0]
signer = clientCtx.GetFromAddress()
definitionDID = did.NewChainDID(clientCtx.ChainID, definitionID)
pwc *credential.WrappedCredential
rl rl2020.RevocationList2020
issuerDID = did.NewChainDID(clientCtx.ChainID, signer.String())
)
// REVOCATION LIST CREATION
// create the revocation list
if rl, err = rl2020.NewRevocationList(cID, revocationListSize); err != nil {
err = fmt.Errorf("revocation list corrupted: %w", err)
return err
}
// update the revocation list
if err = rl.Revoke(revocationIndexes...); err != nil {
err = fmt.Errorf("credential revocations failed: %w", err)
return err
}
// check if credential exits
if _, err = queryPublicCredential(credential.NewQueryClient(clientCtx), cID); err == nil {
err = fmt.Errorf("revocation list credential %s exists", cID)
return err
}
// if issuer is set use the provided one
if issuerDIDstr != "" {
issuerDID = did.DID(issuerDIDstr)
}
// PUBLIC CREDENTIAL
// create the credential
if pwc, err = credential.NewWrappedCredential(
credential.NewPublicVerifiableCredential(
cID,
credential.WithIssuerDID(issuerDID),
credential.WithContext("https://w3id.org/vc-revocation-list-2020/v1"),
credential.WithType(fmt.Sprint(rl2020.TypeRevocationList2020, "Credential")),
credential.WithIssuanceDate(time.Now()),
),
); err != nil {
err = fmt.Errorf("error composing credential: %w", err)
return err
}
// set the credential subject
if err = pwc.SetSubject(rl); err != nil {
err = fmt.Errorf("encoding of credentials failed: %w", err)
return err
}
// sign the updated credential
vmID := pwc.GetIssuerDID().NewVerificationMethodID(signer.String())
if err = sign(pwc, clientCtx.Keyring, signer, vmID); err != nil {
println("error signing the credential:", err)
return err
}
// publish the new credential
msg := credential.NewMsgIssuePublicVerifiableCredentialRequest(
pwc.PublicVerifiableCredential,
definitionDID,
signer,
)
// execute
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
// add flags
cmd.Flags().IntVar(&revocationListSize, "size", 16, "the size of the revocation list, in KB")
cmd.Flags().StringVar(&issuerDIDstr, "issuer", "", "the issuer DID. If not set the signer key will be used as issuer")
cmd.Flags().StringVar(&definitionID, "definition-id", "revocation-list-2020", "the RevocationList2020 definition ID")
cmd.Flags().IntSliceVarP(&revocationIndexes, "revoke", "r", []int{}, "index of credentials to be revoked")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewUpdateRevocationListCmd update a revocation list
func NewUpdateRevocationListCmd() *cobra.Command {
var (
command = "update-revocation-list"
definitionID string
revocationIndexes []int
resetIndexes []int
)
cmd := &cobra.Command{
Use: fmt.Sprintln(command, "credentialID"),
Short: "update a revocation list",
Example: exTx(command, "https://revocations.id/list/001"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
var (
cID = args[0]
signer = clientCtx.GetFromAddress()
definitionDID = did.NewChainDID(clientCtx.ChainID, definitionID)
pwc *credential.WrappedCredential
rl rl2020.RevocationList2020
)
// query the credential
if pwc, err = queryPublicCredential(credential.NewQueryClient(clientCtx), cID); err != nil {
err = fmt.Errorf("revocation list credential not available: %w", err)
return err
}
// check the credential type, it must be a revocation list
if !pwc.HasType(rl2020.TypeRevocationList2020Credential) {
err = fmt.Errorf("expecting credential type %v, found: %v", rl2020.TypeRevocationList2020, pwc.Type)
return err
}
// parse the revocation list
if rl, err = rl2020.NewRevocationListFromJSON(pwc.PublicVerifiableCredential.CredentialSubject); err != nil {
err = fmt.Errorf("revocation list corrupted: %w", err)
return err
}
// update the revocation list
if err = rl.Revoke(revocationIndexes...); err != nil {
err = fmt.Errorf("credential revocations failed: %w", err)
return err
}
if err = rl.Reset(resetIndexes...); err != nil {
err = fmt.Errorf("credential resets failed: %w", err)
return err
}
// update the credential
if err = pwc.SetSubject(rl); err != nil {
err = fmt.Errorf("encoding of credentials failed: %w", err)
return err
}
// sign the updated credential
vmID := pwc.GetIssuerDID().NewVerificationMethodID(signer.String())
if err = sign(pwc, clientCtx.Keyring, signer, vmID); err != nil {
println("error signing the credential:", err)
return err
}
// publish the new credential
msg := credential.NewMsgIssuePublicVerifiableCredentialRequest(
pwc.PublicVerifiableCredential,
definitionDID,
signer,
)
// execute
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
// add flags
cmd.Flags().StringVar(&definitionID, "definition-id", "revocation-list-2020", "the RevocationList2020 definition ID")
cmd.Flags().IntSliceVarP(&revocationIndexes, "revoke", "r", []int{}, "index of credentials to be revoked")
cmd.Flags().IntSliceVarP(&resetIndexes, "reset", "t", []int{}, "index of credentials to be reset")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewPublishCredentialDefinitionCmd defines the command to publish credential definitions
func NewPublishCredentialDefinitionCmd() *cobra.Command {
var (
command = "publish-credential-definition"
isPublic bool
inactive bool
publisherID string
descr string
expirationDays int
)
cmd := &cobra.Command{
Use: fmt.Sprintln(command, "id", "name", "schemaFile", "contextFile"),
Short: "publish a credential definition",
Example: exTx(command, "example-definition-id", "example-credential", "schema.json", "vocab.json"),
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
cdID, name, schemaFile, vocabFIle := args[0], args[1], args[2], args[3]
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// verification
signer := clientCtx.GetFromAddress()
publisherDID := did.NewKeyDID(signer.String())
if !credential.IsEmpty(publisherID) {
publisherDID = did.DID(publisherID)
}
// initialize the definition
def, err := credential.NewCredentialDefinitionFromFile(cdID, publisherDID, name, descr, isPublic, !inactive, schemaFile, vocabFIle)
if err != nil {
println("error building credential definition", err)
return err
}
// create the message
msg := credential.NewMsgPublishCredentialDefinitionRequest(
def,
signer.String(),
)
// execute
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
// add flags to set did relationships
cmd.Flags().StringVar(&descr, "description", "", "a human-readable description about the credential usage")
cmd.Flags().StringVarP(&publisherID, "publisher", "p", "", "the publisher DID. If not set, the DID key of the signer account will be used instead")
cmd.Flags().BoolVar(&isPublic, "public", false, "if is set, the credential is a public one and can be issued on chain")
cmd.Flags().BoolVar(&inactive, "inactive", false, "if is set, the credential definition will be flagged as inactive, client may refuse to issue credentials based on an inactive definition")
cmd.Flags().IntVar(&expirationDays, "expiration", 365, "number of days that the definition can be ")
// add flags to set did relationships
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// Sign signs a credential with a provided private key
func sign(
wc *credential.WrappedCredential,
keyring keyring.Keyring,
address sdk.Address,
verificationMethodID string,
) error {
tm := time.Now()
// reset the proof
wc.Proof = nil
// TODO: this could be expensive review this signing method
// TODO: we can hash this an make this less expensive
data, err := wc.GetBytes()
if err != nil {
return fmt.Errorf("error serializing the credential: %w", err)
}
signature, pubKey, err := keyring.SignByAddress(address, data)
if err != nil {
return err
}
wc.Proof = credential.NewProof(
pubKey.Type(),
tm.Format(time.RFC3339),
// TODO: define proof purposes
did.AssertionMethod,
verificationMethodID,
base64.StdEncoding.EncodeToString(signature),
)
return nil
}