-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc_query.go
130 lines (109 loc) · 4.56 KB
/
grpc_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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/elesto-dao/elesto/v4/x/credential"
"github.com/elesto-dao/elesto/v4/x/did"
)
var _ credential.QueryServer = Keeper{}
// CredentialDefinition returns credential definition for an id
func (k Keeper) CredentialDefinition(
c context.Context,
req *credential.QueryCredentialDefinitionRequest,
) (*credential.QueryCredentialDefinitionResponse, error) {
if credential.IsEmpty(req.Id) {
return nil, status.Error(codes.InvalidArgument, "credential definition id must not be empty")
}
ctx := sdk.UnwrapSDKContext(c)
cd, found := k.GetCredentialDefinition(ctx, req.Id)
if !found {
return nil, status.Error(codes.NotFound, "credential definition not found")
}
return &credential.QueryCredentialDefinitionResponse{Definition: &cd}, nil
}
// CredentialDefinitionsByPublisher returns credential definitions for a particular publisher id
func (k Keeper) CredentialDefinitionsByPublisher(
c context.Context,
req *credential.QueryCredentialDefinitionsByPublisherRequest,
) (*credential.QueryCredentialDefinitionsByPublisherResponse, error) {
if !did.IsValidDID(req.Did) {
return nil, status.Error(codes.InvalidArgument, "publisher DID must be a valid DID")
}
ctx := sdk.UnwrapSDKContext(c)
cds, pr, err := k.GetCredentialDefinitionsWithFilter(ctx, req.Pagination, func(cd *credential.CredentialDefinition) bool {
return cd.PublisherId == req.Did
})
return &credential.QueryCredentialDefinitionsByPublisherResponse{Definitions: cds, Pagination: pr}, err
}
// CredentialDefinitions returns all credential definitions
func (k Keeper) CredentialDefinitions(
c context.Context,
req *credential.QueryCredentialDefinitionsRequest,
) (*credential.QueryCredentialDefinitionsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
cd, pr, err := k.GetCredentialDefinitions(ctx, req)
return &credential.QueryCredentialDefinitionsResponse{Definitions: cd, Pagination: pr}, err
}
// PublicCredential returns public credential for a particular id
func (k Keeper) PublicCredential(
c context.Context,
req *credential.QueryPublicCredentialRequest,
) (*credential.QueryPublicCredentialResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
pc, found := k.GetPublicCredential(ctx, req.Id)
if !found {
return nil, status.Error(codes.NotFound, "public verifiable credential not found")
}
return &credential.QueryPublicCredentialResponse{Credential: &pc}, nil
}
// PublicCredentialsByHolder returns public credentials for particular holder
func (k Keeper) PublicCredentialsByHolder(
c context.Context,
req *credential.QueryPublicCredentialsByHolderRequest,
) (*credential.QueryPublicCredentialsByHolderResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
pvcs, pr, err := k.GetPublicCredentialWithFilter(ctx, req.Pagination, func(vc *credential.PublicVerifiableCredential) bool {
wc, err := credential.NewWrappedCredential(vc)
if err != nil {
return false
}
subjectID, hasIt := wc.GetSubjectID()
if !hasIt {
return false
}
return subjectID == req.Did
})
return &credential.QueryPublicCredentialsByHolderResponse{Credential: pvcs, Pagination: pr}, err
}
// PublicCredentialsByIssuer returns public credentials for a particular issuer
func (k Keeper) PublicCredentialsByIssuer(
c context.Context,
req *credential.QueryPublicCredentialsByIssuerRequest,
) (*credential.QueryPublicCredentialsByIssuerResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
pvcs, pr, err := k.GetPublicCredentialWithFilter(ctx, req.Pagination, func(vc *credential.PublicVerifiableCredential) bool {
return vc.Issuer == req.Did
})
return &credential.QueryPublicCredentialsByIssuerResponse{Credential: pvcs, Pagination: pr}, err
}
// PublicCredentials returns all public credentials
func (k Keeper) PublicCredentials(
c context.Context,
req *credential.QueryPublicCredentialsRequest,
) (*credential.QueryPublicCredentialsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
pvcs, pr, err := k.GetPublicCredentialWithFilter(ctx, req.Pagination, func(vc *credential.PublicVerifiableCredential) bool {
return true
})
return &credential.QueryPublicCredentialsResponse{Credential: pvcs, Pagination: pr}, err
}
func (k Keeper) AllowedPublicCredentials(
c context.Context,
req *credential.QueryAllowedPublicCredentialsRequest,
) (*credential.QueryAllowedPublicCredentialsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
cds, pageRes, err := k.GetAllowedCredentialDefinitions(ctx, req.Pagination)
return &credential.QueryAllowedPublicCredentialsResponse{Credentials: cds, Pagination: pageRes}, err
}