Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interop-node: derive address from either feeder's private key or aws kms #14

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/settlusd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package config

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"

evmtypes "github.com/settlus/chain/evmos/types"
"math/big"
)

const (
Expand Down
5 changes: 2 additions & 3 deletions tools/interop-node/client/settlus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/settlus/chain/app"
"github.com/settlus/chain/evmos/encoding"

"github.com/settlus/chain/tools/interop-node/config"
"github.com/settlus/chain/tools/interop-node/signer"
)
Expand Down Expand Up @@ -63,7 +62,7 @@ type SettlusClient struct {
}

// NewSettlusClient creates a new SettlusClient instance
func NewSettlusClient(config *config.Config, ctx context.Context, logger cometlog.Logger) (*SettlusClient, error) {
func NewSettlusClient(config *config.Config, ctx context.Context, s signer.Signer, logger cometlog.Logger) (*SettlusClient, error) {
rpcClient, gRpcClient, err := getSettlusRpcs(config.Settlus.RpcUrl, config.Settlus.GrpcUrl, config.Settlus.Insecure)
if err != nil {
return nil, fmt.Errorf("failed to create settlus rpc clients: %w", err)
Expand All @@ -89,7 +88,7 @@ func NewSettlusClient(config *config.Config, ctx context.Context, logger cometlo
chainId: config.Settlus.ChainId,
gasLimit: config.Settlus.GasLimit,
fees: fees,
signer: signer.NewSigner(ctx, config),
signer: s,
accountnumber: account.GetAccountNumber(),
sequence: account.GetSequence(),
logger: logger,
Expand Down
5 changes: 2 additions & 3 deletions tools/interop-node/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ for oracle feeder.
},
Feeder: cfg.FeederConfig{
Topics: "block",
Address: "settlus1uad3rkzpcgrvytqnd5d77lrhxgv83qad782h92",
SignerMode: "local",
Key: "0123456789abcdef",
SignerMode: cfg.Local,
Key: "",
ValidatorAddress: "settlusvaloper1x0foobar",
},
Chains: []cfg.ChainConfig{
Expand Down
16 changes: 12 additions & 4 deletions tools/interop-node/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func (sc *SettlusConfig) Validate() error {

type FeederConfig struct {
Topics string `yaml:"topics"`
Address string `yaml:"address"`
SignerMode string `yaml:"signer_mode"`
jacobhjkim marked this conversation as resolved.
Show resolved Hide resolved
Key string `yaml:"key"`
Address string `yaml:"address"` // derived from private key or aws kms key id, no need to set manually
Key string `yaml:"key"` // aws kms key id or private key
ValidatorAddress string `yaml:"validator_address"`
}

Expand All @@ -138,8 +138,16 @@ func (fc *FeederConfig) Validate() error {
return fmt.Errorf("invalid signer_mode, must be one of: %s, %s", AwsKms, Local)
}

if fc.Address == "" {
return fmt.Errorf("address must not be empty")
if fc.Key == "" {
return fmt.Errorf("key must not be empty")
}

if fc.SignerMode == AwsKms && len(fc.Key) != 36 {
return fmt.Errorf("invalid aws kms key id: %s", fc.Key)
}

if fc.SignerMode == Local && len(fc.Key) != 64 {
return fmt.Errorf("invalid private key: %s", fc.Key)
}

return nil
Expand Down
1 change: 0 additions & 1 deletion tools/interop-node/feeder/block_feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/settlus/chain/tools/interop-node/client"
"github.com/settlus/chain/tools/interop-node/config"
"github.com/settlus/chain/tools/interop-node/subscriber"

oracletypes "github.com/settlus/chain/x/oracle/types"
)

Expand Down
16 changes: 12 additions & 4 deletions tools/interop-node/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/tendermint/tendermint/libs/log"

"github.com/settlus/chain/tools/interop-node/client"
"github.com/settlus/chain/tools/interop-node/config"
cfg "github.com/settlus/chain/tools/interop-node/config"
"github.com/settlus/chain/tools/interop-node/feeder"
"github.com/settlus/chain/tools/interop-node/signer"
"github.com/settlus/chain/tools/interop-node/subscriber"
"github.com/settlus/chain/tools/interop-node/types"
"github.com/settlus/chain/x/interop"
Expand All @@ -25,7 +26,7 @@ type Server struct {
interop.UnimplementedInteropServer

ctx context.Context
config *config.Config
config *cfg.Config
logger log.Logger

sc *client.SettlusClient
Expand All @@ -36,13 +37,20 @@ type Server struct {

// NewServer creates a new interop server
func NewServer(
config *config.Config,
config *cfg.Config,
ctx context.Context,
logger log.Logger,
) (*Server, error) {
logger = logger.With("server", "interop-node")

sc, err := client.NewSettlusClient(config, ctx, logger)
s := signer.NewSigner(ctx, config)
address, err := types.GetAddressFromPubKey(s.PubKey())
if err != nil {
return nil, fmt.Errorf("failed to get address from pubkey: %w", err)
}
config.Feeder.Address = address

sc, err := client.NewSettlusClient(config, ctx, s, logger)
if err != nil {
return nil, fmt.Errorf("failed to create settlus client: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion tools/interop-node/signer/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/x509/pkix"
"encoding/asn1"
"fmt"
"math/big"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -43,7 +44,7 @@ func NewKmsSigner(ctx context.Context, key string) Signer {
}

if err := signer.loadPubKey(); err != nil {
panic(err)
panic(fmt.Errorf("failed to load public key during kms signer initialization: %w", err))
}

return signer
Expand Down
13 changes: 13 additions & 0 deletions tools/interop-node/types/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package types

import (
"fmt"
"strings"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common/hexutil"
)

Expand Down Expand Up @@ -30,3 +33,13 @@ func ValidateHexString(s string) bool {
_, err := hexutil.Decode(s)
return err == nil
}

// GetAddressFromPubKey returns the address of a public key
func GetAddressFromPubKey(pubKey cryptotypes.PubKey) (string, error) {
acc := authtypes.NewBaseAccount(pubKey.Address().Bytes(), pubKey, 0, 0)
if err := acc.Validate(); err != nil {
return "", fmt.Errorf("failed to validate account: %w", err)
}

return acc.GetAddress().String(), nil
}
40 changes: 40 additions & 0 deletions tools/interop-node/types/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"bytes"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"

settlusconfig "github.com/settlus/chain/cmd/settlusd/config"
"github.com/settlus/chain/evmos/crypto/ethsecp256k1"
"github.com/settlus/chain/tools/interop-node/types"
)

Expand Down Expand Up @@ -96,3 +101,38 @@ func Test_ValidateHexString(t *testing.T) {
})
}
}

func Test_GetAddressFromPubKey(t *testing.T) {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(settlusconfig.Bech32Prefix, settlusconfig.Bech32PrefixAccPub)

tests := []struct {
name string
pubKey string
want string
wantErr bool
}{
{
name: "valid public key",
pubKey: "023A67CE381ACA142344D9458BDAA8BC960CF852AB9D674765ABA8A70475804611",
want: "settlus1mnd2teke7w0heukka3cctuqkq3kzzazrygtv4e",
wantErr: false,
},
{
name: "invalid public key",
pubKey: "foo",
want: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if actual, err := types.GetAddressFromPubKey(&ethsecp256k1.PubKey{Key: common.FromHex(tt.pubKey)}); err != nil && !tt.wantErr {
t.Errorf("error = %v", err)
} else if actual != tt.want {
t.Errorf("actual = %v, want %v", actual, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const (
// MainnetChainID defines the Evmos EIP155 chain ID for mainnet
MainnetChainID = "settlus_5371-1"
// TestnetChainID defines the Evmos EIP155 chain ID for testnet
TestnetChainID = "settlus_1001-1"
TestnetChainID = "settlus_5372-1"
// BaseDenom defines the Evmos mainnet denomination
BaseDenom = "asetl"
)