diff --git a/pkg/app/debug.go b/pkg/app/debug.go index 7632973..9c7c69b 100644 --- a/pkg/app/debug.go +++ b/pkg/app/debug.go @@ -6,9 +6,9 @@ import ( "github.com/cometbft/cometbft/rpc/client/http" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" bank "github.com/cosmos/cosmos-sdk/x/bank/types" staking "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/kilnfi/cosmos-validator-watcher/pkg/crypto" "github.com/urfave/cli/v2" ) @@ -119,8 +119,7 @@ func DebugConsensusKeyRun(cCtx *cli.Context) error { } val := resp.Validator - pubkey := ed25519.PubKey{Key: val.ConsensusPubkey.Value[2:]} - address := pubkey.Address().String() + address := crypto.PubKeyAddress(val.ConsensusPubkey) fmt.Println(address) diff --git a/pkg/app/run.go b/pkg/app/run.go index d0fd715..fa83168 100644 --- a/pkg/app/run.go +++ b/pkg/app/run.go @@ -11,10 +11,10 @@ import ( "github.com/cometbft/cometbft/rpc/client/http" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/types/query" staking "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/fatih/color" + "github.com/kilnfi/cosmos-validator-watcher/pkg/crypto" _ "github.com/kilnfi/cosmos-validator-watcher/pkg/crypto" "github.com/kilnfi/cosmos-validator-watcher/pkg/metrics" "github.com/kilnfi/cosmos-validator-watcher/pkg/rpc" @@ -318,8 +318,7 @@ func createTrackedValidators(ctx context.Context, pool *rpc.Pool, validators []s val := watcher.ParseValidator(v) for _, stakingVal := range stakingValidators { - pubkey := ed25519.PubKey{Key: stakingVal.ConsensusPubkey.Value[2:]} - address := pubkey.Address().String() + address := crypto.PubKeyAddress(stakingVal.ConsensusPubkey) if address == val.Address { val.Moniker = stakingVal.Description.Moniker val.OperatorAddress = stakingVal.OperatorAddress diff --git a/pkg/crypto/utils.go b/pkg/crypto/utils.go new file mode 100644 index 0000000..05930c8 --- /dev/null +++ b/pkg/crypto/utils.go @@ -0,0 +1,21 @@ +package crypto + +import ( + types1 "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" +) + +func PubKeyAddress(consensusPubkey *types1.Any) string { + switch consensusPubkey.TypeUrl { + case "/cosmos.crypto.ed25519.PubKey": + key := ed25519.PubKey{Key: consensusPubkey.Value[2:]} + return key.Address().String() + + case "/cosmos.crypto.secp256k1.PubKey": + key := secp256k1.PubKey{Key: consensusPubkey.Value[2:]} + return key.Address().String() + } + + panic("unknown pubkey type: " + consensusPubkey.TypeUrl) +} diff --git a/pkg/watcher/validators.go b/pkg/watcher/validators.go index c63b51f..e6ce835 100644 --- a/pkg/watcher/validators.go +++ b/pkg/watcher/validators.go @@ -7,9 +7,9 @@ import ( "time" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/types/query" staking "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/kilnfi/cosmos-validator-watcher/pkg/crypto" "github.com/kilnfi/cosmos-validator-watcher/pkg/metrics" "github.com/kilnfi/cosmos-validator-watcher/pkg/rpc" "github.com/rs/zerolog/log" @@ -98,8 +98,7 @@ func (w *ValidatorsWatcher) handleValidators(chainID string, validators []stakin name := tracked.Name for i, val := range validators { - pubkey := ed25519.PubKey{Key: val.ConsensusPubkey.Value[2:]} - address := pubkey.Address().String() + address := crypto.PubKeyAddress(val.ConsensusPubkey) if tracked.Address == address { var (