Skip to content

Instantly share code, notes, and snippets.

@Jeongseup
Created November 2, 2023 15:06
Show Gist options
  • Save Jeongseup/c79912c8bd1303965ba892b1c56de6ad to your computer and use it in GitHub Desktop.
Save Jeongseup/c79912c8bd1303965ba892b1c56de6ad to your computer and use it in GitHub Desktop.
Cosmos SDK Study - How to parse each validators pubkey to their address
// 1. git clone https://github.com/cosmos/cosmos-sdk.git
// 2. add a new debug_test.go file
// 3. copy this code in your test file
// run test code
// reference: https://github.com/cosmos/cosmos-sdk/blob/main/client/debug/main.go
package debug
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"strings"
"testing"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
)
/*
Example:
$ %s debug pubkey '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}'
`, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
pk, err := getPubKeyFromString(clientCtx, args[0])
if err != nil {
return err
}
cmd.Println("Address:", pk.Address())
cmd.Println("PubKey Hex:", hex.EncodeToString(pk.Bytes()))
return nil
},
*/
func TestXxx(t *testing.T) {
// clientCtx := client.GetClientContextFromCmd(&cobra.Command{})
str := `{"@type":"/cosmos.crypto.ed25519.PubKey","key":"uEUR1gpesU4bnSWL2TOXOf3org2mCYhQHMYkiCJyMD4="}`
encodingConfig := simapp.MakeTestEncodingConfig()
var pk cryptotypes.PubKey
err := encodingConfig.Marshaler.UnmarshalInterfaceJSON([]byte(str), &pk)
if err != nil {
t.Log("err", err)
}
decoded, _ := base64.StdEncoding.DecodeString("uEUR1gpesU4bnSWL2TOXOf3org2mCYhQHMYkiCJyMD4=")
t.Logf("protobuf encoded key: %X", decoded)
// String returns Hex representation of a pubkey with it's type
// func (pubKey *PubKey) String() string {
// return fmt.Sprintf("PubKeyEd25519{%X}", pubKey.Key)
t.Log("key to address:", ToString(SumTruncated(decoded)))
/*
if len(pubKey.Key) != PubKeySize {
panic("pubkey is incorrect size")
}
// For ADR-28 compatible address we would need to
// return address.Hash(proto.MessageName(pubKey), pubKey.Key)
return crypto.Address(tmhash.SumTruncated(pubKey.Key))
*/
t.Log("pubkey string:", pk.String())
t.Log("pubkey bytes:", pk.Bytes())
t.Log("Address:", pk.Address())
t.Log("PubKey Hex:", hex.EncodeToString(pk.Bytes()))
}
/*
// in SDK except in a tendermint validator context.
func (pubKey *PubKey) Address() crypto.Address {
if len(pubKey.Key) != PubKeySize {
panic("pubkey is incorrect size")
}
// For ADR-28 compatible address we would need to
// return address.Hash(proto.MessageName(pubKey), pubKey.Key)
return crypto.Address(tmhash.SumTruncated(pubKey.Key))
}
*/
/*
"address": "0ABBA36C54DD0CA6A790AEF96A01D4392E36345F",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "uEUR1gpesU4bnSWL2TOXOf3org2mCYhQHMYkiCJyMD4="
},
*/
// SumTruncated returns the first 20 bytes of SHA256 of the bz.
func SumTruncated(bz []byte) []byte {
hash := sha256.Sum256(bz)
return hash[:TruncatedSize]
}
const TruncatedSize = 20
func ToString(bz []byte) string {
return strings.ToUpper(hex.EncodeToString(bz))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment