-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
59 lines (48 loc) · 1.66 KB
/
client.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
package pubkeyclient
import (
"context"
"cosmossdk.io/depinject"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/pokt-network/poktroll/pkg/client"
"github.com/pokt-network/poktroll/pkg/crypto"
)
var _ crypto.PubKeyClient = (*pubKeyClient)(nil)
// pubKeyClient is an implementation of the PubKeyClient that uses an account
// querier to get the public key of an address.
type pubKeyClient struct {
// accountQuerier is the querier for the account module, it is used to get
// the public key corresponding to an address.
accountQuerier client.AccountQueryClient
}
// NewPubKeyClient creates a new PubKeyClient with the given dependencies.
// The querier is injected using depinject and has to be specific to the
// environment in which the pubKeyClient is initialized as on-chain and off-chain
// environments may have different queriers.
//
// Required dependencies:
// - client.AccountQueryClient
func NewPubKeyClient(deps depinject.Config) (crypto.PubKeyClient, error) {
pc := new(pubKeyClient)
if err := depinject.Inject(
deps,
&pc.accountQuerier,
); err != nil {
return nil, err
}
return pc, nil
}
// GetPubKeyFromAddress returns the public key of the given address.
// It retrieves the corresponding account by querying for it and returns
// the associated public key.
func (pc *pubKeyClient) GetPubKeyFromAddress(ctx context.Context, address string) (cryptotypes.PubKey, error) {
acc, err := pc.accountQuerier.GetAccount(ctx, address)
if err != nil {
return nil, err
}
// If the account's public key is nil, then return an error.
pubKey := acc.GetPubKey()
if pubKey == nil {
return nil, ErrPubKeyClientEmptyPubKey
}
return pubKey, nil
}