/
querier.go
89 lines (76 loc) · 2.4 KB
/
querier.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
package keeper
import (
"context"
"github.com/golang/protobuf/ptypes/empty"
"github.com/HermitMatrixNetwork/HermitMatrixNetwork/x/registration/internal/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
type GrpcQuerier struct {
keeper Keeper
}
// todo: this needs proper tests and doc
func NewQuerier(keeper Keeper) GrpcQuerier {
return GrpcQuerier{keeper: keeper}
}
func (q GrpcQuerier) TxKey(c context.Context, _ *empty.Empty) (*types.Key, error) {
rsp, err := queryMasterKey(sdk.UnwrapSDKContext(c), q.keeper)
switch {
case err != nil:
return nil, err
case rsp == nil:
return nil, types.ErrNotFound
}
return &types.Key{
Key: rsp.IoMasterCertificate.Bytes,
}, nil
}
func (q GrpcQuerier) RegistrationKey(c context.Context, _ *empty.Empty) (*types.Key, error) {
rsp, err := queryMasterKey(sdk.UnwrapSDKContext(c), q.keeper)
switch {
case err != nil:
return nil, err
case rsp == nil:
return nil, types.ErrNotFound
}
return &types.Key{
Key: rsp.NodeExchMasterCertificate.Bytes,
}, nil
}
func (q GrpcQuerier) EncryptedSeed(c context.Context, req *types.QueryEncryptedSeedRequest) (*types.QueryEncryptedSeedResponse, error) {
if req.PubKey == nil {
return nil, sdkerrors.Wrap(types.ErrInvalid, "public key")
}
rsp, err := queryEncryptedSeed(sdk.UnwrapSDKContext(c), req.PubKey, q.keeper)
switch {
case err != nil:
return nil, err
case rsp == nil:
return nil, types.ErrNotFound
}
return &types.QueryEncryptedSeedResponse{EncryptedSeed: rsp}, nil
}
func queryMasterKey(ctx sdk.Context, keeper Keeper) (*types.GenesisState, error) {
ioKey := keeper.GetMasterCertificate(ctx, types.MasterIoKeyId)
nodeKey := keeper.GetMasterCertificate(ctx, types.MasterNodeKeyId)
if ioKey == nil || nodeKey == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, "Chain has not been initialized yet")
}
resp := &types.GenesisState{
Registration: nil,
NodeExchMasterCertificate: nodeKey,
IoMasterCertificate: ioKey,
}
//asBytes, err := keeper.cdc.Marshal(ioKey)
//if err != nil {
// return nil, err
//}
return resp, nil
}
func queryEncryptedSeed(ctx sdk.Context, pubkeyBytes []byte, keeper Keeper) ([]byte, error) {
seed := keeper.getRegistrationInfo(ctx, pubkeyBytes)
if seed == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, "Node has not been authenticated yet")
}
return seed.EncryptedSeed, nil
}