-
Notifications
You must be signed in to change notification settings - Fork 12
/
tx.go
362 lines (312 loc) · 9.05 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
package cli
import (
"encoding/hex"
"errors"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/allinbits/cosmos-cash/x/did/types"
vcstypes "github.com/allinbits/cosmos-cash/x/verifiable-credential/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
// this line is used by starport scaffolding # 1
cmd.AddCommand(
NewCreateDidDocumentCmd(),
NewAddVerificationCmd(),
NewAddServiceCmd(),
NewRevokeVerificationCmd(),
NewDeleteServiceCmd(),
NewUpdateDidDocumentCmd(),
NewAddVerificationRelationshipCmd(),
)
return cmd
}
// NewCreateDidDocumentCmd defines the command to create a new IBC light client.
func NewCreateDidDocumentCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-did [id]",
Short: "create decentralized did (did) document",
Example: "creates a did document for users",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// did
did := types.DID(clientCtx.ChainID, args[0])
// verification
signer := clientCtx.GetFromAddress()
// pubkey
info, err := clientCtx.Keyring.KeyByAddress(signer)
if err != nil {
return err
}
pubKey := info.GetPubKey()
// verification method id
vmID := fmt.Sprint(did, "#", sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), pubKey.Address().Bytes()))
auth := types.NewVerification(
types.NewVerificationMethod(
vmID,
did,
hex.EncodeToString(pubKey.Bytes()),
types.DIDVerificationMaterialPublicKeyHex,
),
[]string{types.Authentication},
nil,
)
// create the message
msg := types.NewMsgCreateDidDocument(
did,
[]*types.Verification{auth},
[]*types.Service{},
signer.String(),
)
// validate
if err := msg.ValidateBasic(); err != nil {
return err
}
// execute
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewAddVerificationCmd define the command to add a verification message
func NewAddVerificationCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-verification-method [id] [pubkey]",
Short: "add an verification method to a decentralized did (did) document",
Example: "adds an verification method for a did document",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// signer address
signer := clientCtx.GetFromAddress()
// public key
pubKey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, args[1])
if err != nil {
return err
}
// document did
did := types.DID(clientCtx.ChainID, args[0])
// verification method id
vmID := fmt.Sprint(did, "#", sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), pubKey.Address().Bytes()))
verification := types.NewVerification(
types.NewVerificationMethod(
vmID,
did,
hex.EncodeToString(pubKey.Bytes()),
types.DIDVerificationMaterialPublicKeyHex,
),
[]string{types.Authentication},
nil,
)
// add verification
msg := types.NewMsgAddVerification(
did,
verification,
signer.String(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewAddServiceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-service [id] [service_id] [type] [endpoint]",
Short: "add a service to a decentralized did (did) document",
Example: "adds a service to a did document",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
if !vcstypes.IsValidCredentialType(args[2]) {
return errors.New("invalid credential type")
}
// tx signer
signer := clientCtx.GetFromAddress()
// service parameters
serviceID, serviceType, endpoint := args[1], args[2], args[3]
// document did
did := types.DID(clientCtx.ChainID, args[0])
service := types.NewService(
serviceID,
serviceType,
endpoint,
)
msg := types.NewMsgAddService(
did,
service,
signer.String(),
)
// validate
if err := msg.ValidateBasic(); err != nil {
return err
}
// broadcast
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewRevokeVerificationCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "revoke-verification-method [id] [verification-method-id]",
Short: "revoke a verification method from a decentralized did (did) document",
Example: "revoke a verification method for a did document",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// document did
did := types.DID(clientCtx.ChainID, args[0])
// signer
signer := clientCtx.GetFromAddress()
// verification method id
vmID := types.DID(clientCtx.ChainID, args[1])
// build the message
msg := types.NewMsgRevokeVerification(
did,
vmID,
signer.String(),
)
// validate
if err := msg.ValidateBasic(); err != nil {
return err
}
// broadcast
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewDeleteServiceCmd deletes a service from a DID Document
func NewDeleteServiceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-service [id] [service-id]",
Short: "deletes a service from a decentralized did (did) document",
Example: "delete a service for a did document",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// document did
did := types.DID(clientCtx.ChainID, args[0])
// signer
signer := clientCtx.GetFromAddress()
// service id
sID := args[1]
msg := types.NewMsgDeleteService(
did,
sID,
signer.String(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewUpdateDidDocumentCmd adds a controller to a did document
func NewUpdateDidDocumentCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update-did-document [id] [id-key]",
Short: "updates a decentralized identifier (did) document to contain a controller",
Example: "update-did-document vasp cosmos1kslgpxklq75aj96cz3qwsczr95vdtrd3p0fslp",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// document did
did := types.DID(clientCtx.ChainID, args[0])
// did key to use as the controller
didKey := types.DIDKey(args[1])
// signer
signer := clientCtx.GetFromAddress()
msg := types.NewMsgUpdateDidDocument(
did,
[]string{
didKey,
},
signer.String(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewAddVerificationRelationshipCmd adds a verification relationship to a verification method
func NewAddVerificationRelationshipCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-verification-relationship [id] [method-id] [relationship]",
Short: "adds a verification relationship to a key on a decentralized identifier (did) document",
Example: "add-verification-relationship vasp vasp#6f1e0700-6c86-41b6-9e05-ae3cf839cdd0 ",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// document did
did := types.DID(clientCtx.ChainID, args[0])
// method id
methodID := types.DID(clientCtx.ChainID, args[1])
// relationship types
relationship := args[2]
// signer
signer := clientCtx.GetFromAddress()
msg := types.NewMsgSetVerificationRelationships(
did,
methodID,
[]string{
relationship,
},
signer.String(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}