diff --git a/cmd/settlusd/config/config.go b/cmd/settlusd/config/config.go index c4e52495..dae6f42f 100644 --- a/cmd/settlusd/config/config.go +++ b/cmd/settlusd/config/config.go @@ -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 ( diff --git a/tools/interop-node/client/settlus.go b/tools/interop-node/client/settlus.go index 3ecefe08..7317da2d 100644 --- a/tools/interop-node/client/settlus.go +++ b/tools/interop-node/client/settlus.go @@ -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" ) @@ -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) @@ -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, diff --git a/tools/interop-node/cmd/config.go b/tools/interop-node/cmd/config.go index 370c5a5e..d2307ea0 100644 --- a/tools/interop-node/cmd/config.go +++ b/tools/interop-node/cmd/config.go @@ -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{ diff --git a/tools/interop-node/config/config.go b/tools/interop-node/config/config.go index 57cda138..b7f0d8e3 100644 --- a/tools/interop-node/config/config.go +++ b/tools/interop-node/config/config.go @@ -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"` - 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"` } @@ -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 diff --git a/tools/interop-node/feeder/block_feeder.go b/tools/interop-node/feeder/block_feeder.go index 59932601..f741bd25 100644 --- a/tools/interop-node/feeder/block_feeder.go +++ b/tools/interop-node/feeder/block_feeder.go @@ -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" ) diff --git a/tools/interop-node/server/server.go b/tools/interop-node/server/server.go index 887a01ac..8e2077b8 100644 --- a/tools/interop-node/server/server.go +++ b/tools/interop-node/server/server.go @@ -10,9 +10,11 @@ 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" ) @@ -25,7 +27,7 @@ type Server struct { interop.UnimplementedInteropServer ctx context.Context - config *config.Config + config *cfg.Config logger log.Logger sc *client.SettlusClient @@ -36,13 +38,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) } diff --git a/tools/interop-node/signer/kms.go b/tools/interop-node/signer/kms.go index d141e254..9000b809 100644 --- a/tools/interop-node/signer/kms.go +++ b/tools/interop-node/signer/kms.go @@ -4,6 +4,7 @@ import ( "context" "crypto/x509/pkix" "encoding/asn1" + "fmt" "math/big" "github.com/aws/aws-sdk-go-v2/aws" @@ -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 diff --git a/tools/interop-node/types/util.go b/tools/interop-node/types/util.go index 53fdf1e0..3ea56feb 100644 --- a/tools/interop-node/types/util.go +++ b/tools/interop-node/types/util.go @@ -1,5 +1,12 @@ package types +import ( + "fmt" + + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +) + func PadBytes(pad int, b []byte) []byte { if len(b) == pad { return b @@ -13,3 +20,13 @@ func PadBytes(pad int, b []byte) []byte { copy(padded[pad-len(b):], b) return padded } + +// 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 +} diff --git a/tools/interop-node/types/util_test.go b/tools/interop-node/types/util_test.go index 45197f6c..481495e1 100644 --- a/tools/interop-node/types/util_test.go +++ b/tools/interop-node/types/util_test.go @@ -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" ) @@ -51,3 +56,38 @@ func Test_PadBytes(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(ðsecp256k1.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) + } + }) + } +} diff --git a/utils/utils.go b/utils/utils.go index eb84cd35..44237acd 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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" )