From c05abc71abf70fc88b1cdbf94f62167f96427201 Mon Sep 17 00:00:00 2001 From: sufay Date: Tue, 18 Jun 2024 11:48:11 +0800 Subject: [PATCH] parse vault public key --- x/btcbridge/types/params.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/x/btcbridge/types/params.go b/x/btcbridge/types/params.go index f6d8dc7..55a0e79 100644 --- a/x/btcbridge/types/params.go +++ b/x/btcbridge/types/params.go @@ -1,6 +1,12 @@ package types -import sdk "github.com/cosmos/cosmos-sdk/types" +import ( + "encoding/hex" + "encoding/json" + + "github.com/cosmos/cosmos-sdk/crypto/keys/segwit" + sdk "github.com/cosmos/cosmos-sdk/types" +) // NewParams creates a new Params instance func NewParams(relayers []string) Params { @@ -61,10 +67,30 @@ func SelectVaultByBitcoinAddress(vaults []*Vault, address string) *Vault { // returns the vault if the public key is found func SelectVaultByPubKey(vaults []*Vault, pubKey string) *Vault { for _, v := range vaults { - if v.PubKey == pubKey { + if ParseVaultPubKey(v) == pubKey { return v } } return nil } + +// ParseVaultPubKey parses the given vault public key +// Note: return empty if any error occured +func ParseVaultPubKey(vault *Vault) string { + pubKey := vault.PubKey + + _, err := hex.DecodeString(pubKey) + if err == nil { + // pub key is hex encoded + return pubKey + } + + var pk segwit.PubKey + err = json.Unmarshal([]byte(pubKey), &pk) + if err != nil { + return "" + } + + return hex.EncodeToString(pk.Bytes()) +}