Skip to content

Commit

Permalink
feat: add support for secp256k1 key (#83)
Browse files Browse the repository at this point in the history
Fix #81
  • Loading branch information
MattKetmo authored Oct 11, 2024
1 parent cf2864f commit 62b7fbe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
5 changes: 2 additions & 3 deletions pkg/app/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pkg/crypto/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 2 additions & 3 deletions pkg/watcher/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 62b7fbe

Please sign in to comment.