-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.go
71 lines (61 loc) · 2.33 KB
/
account.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
package sdk
import (
"context"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
accounttypes "github.com/cosmos/cosmos-sdk/x/auth/types"
grpc "github.com/cosmos/gogoproto/grpc"
grpcoptions "google.golang.org/grpc"
)
var queryCodec *codec.ProtoCodec
// init initializes the codec for the account module
func init() {
reg := cdctypes.NewInterfaceRegistry()
accounttypes.RegisterInterfaces(reg)
cryptocodec.RegisterInterfaces(reg)
queryCodec = codec.NewProtoCodec(reg)
}
// AccountClient is used to interact with the account module.
//
// For example, it can be used to get the public key corresponding to an address.
type AccountClient struct {
PoktNodeAccountFetcher
}
// GetPubKeyFromAddress returns the public key of the account with the given address.
// It queries the account module using the gRPC query client.
func (ac *AccountClient) GetPubKeyFromAddress(
ctx context.Context,
address string,
) (pubKey cryptotypes.PubKey, err error) {
req := &accounttypes.QueryAccountRequest{Address: address}
res, err := ac.PoktNodeAccountFetcher.Account(ctx, req)
if err != nil {
return nil, err
}
var fetchedAccount types.AccountI
if err = queryCodec.UnpackAny(res.Account, &fetchedAccount); err != nil {
return nil, err
}
return fetchedAccount.GetPubKey(), nil
}
// NewPoktNodeAccountFetcher returns the default implementation of the PoktNodeAccountFetcher interfce.
// It connects to a POKT full node, through the account module's query client, to get account data.
func NewPoktNodeAccountFetcher(grpcConn grpc.ClientConn) PoktNodeAccountFetcher {
return accounttypes.NewQueryClient(grpcConn)
}
// PoktNodeAccountFetcher is used by the AccountClient to fetch accounts using
// poktroll request/response types.
//
// Most users can rely on the default implementation provided by NewPoktNodeAccountFetcher function.
// A custom implementation of this interface can be used to gain more granular
// control over the interactions of the AccountClient with the POKT full node.
type PoktNodeAccountFetcher interface {
Account(
context.Context,
*accounttypes.QueryAccountRequest,
...grpcoptions.CallOption,
) (*accounttypes.QueryAccountResponse, error)
}