Skip to content

Commit

Permalink
Added keyring backend to config file and fixed minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed May 8, 2021
1 parent 466932b commit c93fc19
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
4 changes: 3 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bufio"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -66,6 +67,7 @@ func StartCmd() *cobra.Command {
encoding = params.MakeEncodingConfig()
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
service = wireguard.NewWireGuard(wgtypes.NewIPPool(ipv4Pool, ipv6Pool))
reader = bufio.NewReader(cmd.InOrStdin())
)

std.RegisterInterfaces(encoding.InterfaceRegistry)
Expand All @@ -76,7 +78,7 @@ func StartCmd() *cobra.Command {
return err
}

kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendFile, home, nil)
kr, err := keyring.New(sdk.KeyringServiceName(), cfg.Keyring.Backend, home, reader)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gorilla/mux"
"github.com/sentinel-official/dvpn-node/lite"
"github.com/sentinel-official/dvpn-node/types"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/sentinel-official/dvpn-node/lite"
"github.com/sentinel-official/dvpn-node/types"
)

type Context struct {
Expand Down
25 changes: 0 additions & 25 deletions lite/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
rpcclient "github.com/tendermint/tendermint/rpc/client"

"github.com/sentinel-official/dvpn-node/types"
)

type Client struct {
Expand All @@ -41,29 +39,6 @@ func NewDefaultClient() *Client {
WithMemo("")
}

func NewClientFromConfig(home string, cfg *types.Config) (*Client, error) {
kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendFile, home, nil)
if err != nil {
return nil, err
}

info, err := kr.Key(cfg.Node.From)
if err != nil {
return nil, err
}

return NewClient().
WithGasAdjustment(cfg.Chain.GasAdjustment).
WithKeyring(kr).
WithNodeURI(cfg.Chain.RPCAddress).
WithFrom(cfg.Node.From).
WithFromAddress(info.GetAddress()).
WithFromName(cfg.Node.From).
WithGas(cfg.Chain.Gas).
WithChainID(cfg.Chain.ID).
WithGasPrices(cfg.Chain.GasPrices), nil
}

func (c *Client) Copy() *Client {
return &Client{
ctx: c.ctx,
Expand Down
10 changes: 6 additions & 4 deletions lite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
plantypes "github.com/sentinel-official/hub/x/plan/types"
subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types"
vpntypes "github.com/sentinel-official/hub/x/vpn/types"

"github.com/sentinel-official/dvpn-node/utils"
)

func (c *Client) QueryAccount(address sdk.AccAddress) (authtypes.AccountI, error) {
Expand All @@ -22,7 +24,7 @@ func (c *Client) QueryAccount(address sdk.AccAddress) (authtypes.AccountI, error
res, err := qc.Account(context.Background(),
&authtypes.QueryAccountRequest{Address: address.String()})
if err != nil {
return nil, err
return nil, utils.IsNotFoundError(err)
}

if err := c.ctx.InterfaceRegistry.UnpackAny(res.Account, &account); err != nil {
Expand All @@ -40,7 +42,7 @@ func (c *Client) QueryNode(address hub.NodeAddress) (*nodetypes.Node, error) {
res, err := qc.QueryNode(context.Background(),
nodetypes.NewQueryNodeRequest(address))
if err != nil {
return nil, err
return nil, utils.IsNotFoundError(err)
}

return &res.Node, nil
Expand All @@ -54,7 +56,7 @@ func (c *Client) QuerySubscription(id uint64) (*subscriptiontypes.Subscription,
res, err := qc.QuerySubscription(context.Background(),
subscriptiontypes.NewQuerySubscriptionRequest(id))
if err != nil {
return nil, err
return nil, utils.IsNotFoundError(err)
}

return &res.Subscription, nil
Expand All @@ -68,7 +70,7 @@ func (c *Client) QueryQuota(id uint64, address sdk.AccAddress) (*subscriptiontyp
res, err := qc.QueryQuota(context.Background(),
subscriptiontypes.NewQueryQuotaRequest(id, address))
if err != nil {
return nil, err
return nil, utils.IsNotFoundError(err)
}

return &res.Quota, nil
Expand Down
12 changes: 12 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ rpc_address = "{{ .Chain.RPCAddress }}"
enable = {{ .Handshake.Enable }}
peers = {{ .Handshake.Peers }}
[keyring]
backend = "{{ .Keyring.Backend }}"
[node]
from = "{{ .Node.From }}"
interval_sessions = {{ .Node.IntervalSessions }}
Expand Down Expand Up @@ -61,6 +64,9 @@ type Config struct {
Enable bool `json:"enable"`
Peers uint64 `json:"peers"`
}
Keyring struct {
Backend string `json:"backend"`
}
Node struct {
From string `json:"from"`
IntervalSessions int64 `json:"interval_sessions"`
Expand Down Expand Up @@ -88,6 +94,8 @@ func (c *Config) WithDefaultValues() *Config {
c.Handshake.Enable = true
c.Handshake.Peers = 8

c.Keyring.Backend = "file"

c.Node.From = ""
c.Node.IntervalSessions = 8 * time.Minute.Nanoseconds()
c.Node.IntervalStatus = 4 * time.Minute.Nanoseconds()
Expand Down Expand Up @@ -158,6 +166,10 @@ func (c *Config) Validate() error {
return fmt.Errorf("invalid handshake->peers; expected positive value")
}

if c.Keyring.Backend == "" {
return fmt.Errorf("invalid keyring->backend; expected non-empty value")
}

if c.Node.From == "" {
return fmt.Errorf("invalid node->from; expected non-empty value")
}
Expand Down
13 changes: 13 additions & 0 deletions utils/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import (
"strings"
)

func IsNotFoundError(v error) error {
if strings.Contains(v.Error(), "code = NotFound") {
return nil
}

return v
}

0 comments on commit c93fc19

Please sign in to comment.