Skip to content

Commit 62b7fbe

Browse files
authored
feat: add support for secp256k1 key (#83)
Fix #81
1 parent cf2864f commit 62b7fbe

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

pkg/app/debug.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"github.com/cometbft/cometbft/rpc/client/http"
77
"github.com/cosmos/cosmos-sdk/client"
88
"github.com/cosmos/cosmos-sdk/codec"
9-
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
109
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
1110
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
11+
"github.com/kilnfi/cosmos-validator-watcher/pkg/crypto"
1212
"github.com/urfave/cli/v2"
1313
)
1414

@@ -119,8 +119,7 @@ func DebugConsensusKeyRun(cCtx *cli.Context) error {
119119
}
120120

121121
val := resp.Validator
122-
pubkey := ed25519.PubKey{Key: val.ConsensusPubkey.Value[2:]}
123-
address := pubkey.Address().String()
122+
address := crypto.PubKeyAddress(val.ConsensusPubkey)
124123

125124
fmt.Println(address)
126125

pkg/app/run.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111

1212
"github.com/cometbft/cometbft/rpc/client/http"
1313
"github.com/cosmos/cosmos-sdk/client"
14-
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
1514
"github.com/cosmos/cosmos-sdk/types/query"
1615
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
1716
"github.com/fatih/color"
17+
"github.com/kilnfi/cosmos-validator-watcher/pkg/crypto"
1818
_ "github.com/kilnfi/cosmos-validator-watcher/pkg/crypto"
1919
"github.com/kilnfi/cosmos-validator-watcher/pkg/metrics"
2020
"github.com/kilnfi/cosmos-validator-watcher/pkg/rpc"
@@ -318,8 +318,7 @@ func createTrackedValidators(ctx context.Context, pool *rpc.Pool, validators []s
318318
val := watcher.ParseValidator(v)
319319

320320
for _, stakingVal := range stakingValidators {
321-
pubkey := ed25519.PubKey{Key: stakingVal.ConsensusPubkey.Value[2:]}
322-
address := pubkey.Address().String()
321+
address := crypto.PubKeyAddress(stakingVal.ConsensusPubkey)
323322
if address == val.Address {
324323
val.Moniker = stakingVal.Description.Moniker
325324
val.OperatorAddress = stakingVal.OperatorAddress

pkg/crypto/utils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package crypto
2+
3+
import (
4+
types1 "github.com/cosmos/cosmos-sdk/codec/types"
5+
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
6+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
7+
)
8+
9+
func PubKeyAddress(consensusPubkey *types1.Any) string {
10+
switch consensusPubkey.TypeUrl {
11+
case "/cosmos.crypto.ed25519.PubKey":
12+
key := ed25519.PubKey{Key: consensusPubkey.Value[2:]}
13+
return key.Address().String()
14+
15+
case "/cosmos.crypto.secp256k1.PubKey":
16+
key := secp256k1.PubKey{Key: consensusPubkey.Value[2:]}
17+
return key.Address().String()
18+
}
19+
20+
panic("unknown pubkey type: " + consensusPubkey.TypeUrl)
21+
}

pkg/watcher/validators.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"time"
88

99
"github.com/cosmos/cosmos-sdk/client"
10-
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
1110
"github.com/cosmos/cosmos-sdk/types/query"
1211
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
12+
"github.com/kilnfi/cosmos-validator-watcher/pkg/crypto"
1313
"github.com/kilnfi/cosmos-validator-watcher/pkg/metrics"
1414
"github.com/kilnfi/cosmos-validator-watcher/pkg/rpc"
1515
"github.com/rs/zerolog/log"
@@ -98,8 +98,7 @@ func (w *ValidatorsWatcher) handleValidators(chainID string, validators []stakin
9898
name := tracked.Name
9999

100100
for i, val := range validators {
101-
pubkey := ed25519.PubKey{Key: val.ConsensusPubkey.Value[2:]}
102-
address := pubkey.Address().String()
101+
address := crypto.PubKeyAddress(val.ConsensusPubkey)
103102

104103
if tracked.Address == address {
105104
var (

0 commit comments

Comments
 (0)