-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
222 lines (196 loc) · 5.75 KB
/
query.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
package cli
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
"github.com/elesto-dao/elesto/v2/x/credential"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(_ string) *cobra.Command {
// Group did queries under a subcommand
cmd := &cobra.Command{
Use: credential.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", credential.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
// this line is used by starport scaffolding # 1
cmd.AddCommand(
NewQueryCredentialDefinitionsCmd(),
NewQueryCredentialDefinitionCmd(),
NewQueryPublicCredentialCmd(),
NewQueryPublicCredentialsByIssuerCmd(),
NewQueryCredentialStatusCmd(),
NewQueryPublicCredentialStatusCmd(),
NewMakeCredentialFromSchemaCmd(),
)
return cmd
}
func exQuery(cmd ...string) string {
return fmt.Sprintln(version.AppName, "query", credential.ModuleName, strings.Join(cmd, " "))
}
func use(cmd string, params ...string) string {
return fmt.Sprintln(cmd, strings.Join(params, " "))
}
func NewQueryCredentialDefinitionsCmd() *cobra.Command {
var (
command = "credential-definitions"
)
cmd := &cobra.Command{
Use: use(command),
Short: "query a credential definitions",
Example: exQuery(command, "credential-definitions"),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := credential.NewQueryClient(clientCtx)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
params := &credential.QueryCredentialDefinitionsRequest{
Pagination: pageReq,
}
result, err := queryClient.CredentialDefinitions(
context.Background(),
params,
)
if err != nil {
return err
}
return clientCtx.PrintProto(result)
},
}
flags.AddPaginationFlagsToCmd(cmd, command)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func NewQueryCredentialDefinitionCmd() *cobra.Command {
var (
command = "credential-definition"
)
cmd := &cobra.Command{
Use: use(command, "credentialDefinitionID"),
Short: "query a credential definition by its id",
Example: exQuery(command, "did:cosmos:elesto:cd-1"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := credential.NewQueryClient(clientCtx)
result, err := queryClient.CredentialDefinition(
context.Background(),
&credential.QueryCredentialDefinitionRequest{
Id: args[0],
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(result)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func NewQueryPublicCredentialCmd() *cobra.Command {
var (
command = "public-credential"
printNative bool
)
cmd := &cobra.Command{
Use: use(command, "credentialID"),
Short: "fetch a public credential by id",
Example: exQuery(command, "example-credential-id"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
var (
queryClient = credential.NewQueryClient(clientCtx)
credentialID = args[0]
wc *credential.WrappedCredential
)
// query the credential
if wc, err = queryPublicCredential(queryClient, credentialID); err != nil {
return err
}
if printNative {
return clientCtx.PrintProto(wc.PublicVerifiableCredential)
}
wcB, err := wc.GetBytes()
if err != nil {
return err
}
return clientCtx.PrintBytes(wcB)
},
}
cmd.Flags().BoolVar(&printNative, "native", false, "if set the credential will be printed in the raw format, that is how it is stored on chain")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func NewQueryPublicCredentialsByIssuerCmd() *cobra.Command {
var (
command = "public-credentials-by-issuer"
printNative bool
)
cmd := &cobra.Command{
Use: use(command, "issuerDID"),
Short: "list public credentials issued by a issuer ",
Example: exQuery(command, "did:cosmos:key:cosmos1sl48sj2jjed7enrv3lzzplr9wc2f5js5tzjph8"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
var (
queryClient = credential.NewQueryClient(clientCtx)
result *credential.QueryPublicCredentialsByIssuerResponse
pwcs []*credential.WrappedCredential
pwcsJSON []byte
)
// query credentials
if result, err = queryClient.PublicCredentialsByIssuer(
context.Background(),
&credential.QueryPublicCredentialsByIssuerRequest{
Did: args[0],
},
); err != nil {
return err
}
//
if printNative {
return clientCtx.PrintProto(result)
}
// process credentials
for _, pvc := range result.Credential {
pwc, errWC := credential.NewWrappedCredential(pvc)
if errWC != nil {
fmt.Printf("warning, cannot process credential %v: %v, for further inspection run the 'public-credential' command with the '--native' flag", pvc.Id, err)
continue
}
pwcs = append(pwcs, pwc)
}
if pwcsJSON, err = json.Marshal(pwcs); err != nil {
return err
}
return clientCtx.PrintBytes(pwcsJSON)
},
}
cmd.Flags().BoolVar(&printNative, "native", false, "if set the credential will be printed in the raw format, that is how it is stored on chain")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}