Skip to content

Commit

Permalink
parse vault public key
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jun 18, 2024
1 parent 6b698f7 commit 8b0a3d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion x/btcbridge/keeper/keeper_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (k Keeper) ProcessBitcoinWithdrawTransaction(ctx sdk.Context, msg *types.Ms
senderPubKey := uTx.MsgTx().TxIn[0].Witness[1]

// check if the first sender is one of the vault addresses
vault := types.SelectVaultByPubKey(param.Vaults, hex.EncodeToString(senderPubKey))
vault := types.SelectVaultByPubKey(k.cdc, param.Vaults, hex.EncodeToString(senderPubKey))
if vault == nil {
return types.ErrInvalidSenders
}
Expand Down
32 changes: 29 additions & 3 deletions x/btcbridge/types/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"
import (
"encoding/hex"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/segwit"

Check failure on line 7 in x/btcbridge/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

github.com/cosmos/[email protected]: replacement directory ../cosmos-sdk does not exist
sdk "github.com/cosmos/cosmos-sdk/types"
)

// NewParams creates a new Params instance
func NewParams(relayers []string) Params {
Expand Down Expand Up @@ -59,12 +65,32 @@ func SelectVaultByBitcoinAddress(vaults []*Vault, address string) *Vault {

// SelectVaultByPubKey returns the vault if the public key is found
// returns the vault if the public key is found
func SelectVaultByPubKey(vaults []*Vault, pubKey string) *Vault {
func SelectVaultByPubKey(cdc codec.BinaryCodec, vaults []*Vault, pubKey string) *Vault {
for _, v := range vaults {
if v.PubKey == pubKey {
if ParseVaultPubKey(cdc, v) == pubKey {
return v
}
}

return nil
}

// ParseVaultPubKey parses the given vault public key
// Note: return empty if any error occured
func ParseVaultPubKey(cdc codec.BinaryCodec, 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 = cdc.Unmarshal([]byte(pubKey), &pk)
if err != nil {
return ""
}

return hex.EncodeToString(pk.Bytes())
}

0 comments on commit 8b0a3d3

Please sign in to comment.