Skip to content

Commit

Permalink
improve participant field
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Dec 31, 2024
1 parent afa021e commit 24ac038
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 202 deletions.
206 changes: 103 additions & 103 deletions api/side/btcbridge/btcbridge.pulsar.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions proto/side/btcbridge/btcbridge.proto
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ message DKGParticipant {
string moniker = 1;
// the operator address of the corresponding validator
string operator_address = 2;
// the consensus address of the corresponding validator
string consensus_address = 3;
// the consensus public key of the corresponding validator
string consensus_pubkey = 3;
}

enum DKGRequestStatus {
Expand Down
15 changes: 13 additions & 2 deletions x/btcbridge/keeper/tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"bytes"
"encoding/base64"
"time"

"github.com/btcsuite/btcd/btcutil/psbt"
Expand Down Expand Up @@ -169,13 +170,23 @@ func (k Keeper) IterateDKGCompletionRequests(ctx sdk.Context, id uint64, cb func
// InitiateDKG initiates the DKG request by the specified params
func (k Keeper) InitiateDKG(ctx sdk.Context, participants []*types.DKGParticipant, threshold uint32, vaultTypes []types.AssetType, enableTransfer bool, targetUtxoNum uint32) (*types.DKGRequest, error) {
for _, p := range participants {
consAddress, _ := sdk.ConsAddressFromHex(p.ConsensusAddress)
valAddr, _ := sdk.ValAddressFromBech32(p.OperatorAddress)

validator, err := k.stakingKeeper.GetValidatorByConsAddr(ctx, consAddress)
validator, err := k.stakingKeeper.GetValidator(ctx, valAddr)
if err != nil {
return nil, errorsmod.Wrap(types.ErrInvalidDKGParams, "non validator")
}

pubKey, err := validator.ConsPubKey()
if err != nil {
return nil, err
}

pubKeyBytes, _ := base64.StdEncoding.DecodeString(p.ConsensusPubkey)
if !bytes.Equal(pubKeyBytes, pubKey.Bytes()) {
errorsmod.Wrap(types.ErrInvalidDKGParams, "incorrect consensus public key")
}

if validator.Status != stakingtypes.Bonded {
return nil, errorsmod.Wrap(types.ErrInvalidDKGParams, "validator not bonded")
}
Expand Down
181 changes: 91 additions & 90 deletions x/btcbridge/types/btcbridge.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions x/btcbridge/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ type BankKeeper interface {

// StakingKeeper defines the expected staking keeper used to retrieve validator (noalias)
type StakingKeeper interface {
GetValidator(ctx context.Context, addr sdk.ValAddress) (stakingtypes.Validator, error)
GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) (stakingtypes.Validator, error)
}
11 changes: 7 additions & 4 deletions x/btcbridge/types/msg_initiate_dkg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package types

import (
"encoding/base64"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -29,15 +32,15 @@ func (m *MsgInitiateDKG) ValidateBasic() error {
return errorsmod.Wrap(err, "invalid operator address")
}

if _, err := sdk.ConsAddressFromHex(p.ConsensusAddress); err != nil {
return errorsmod.Wrap(err, "invalid consensus address")
if pubKey, err := base64.StdEncoding.DecodeString(p.ConsensusPubkey); err != nil || len(pubKey) != ed25519.PubKeySize {
return errorsmod.Wrap(err, "invalid consensus public key")
}

if participants[p.ConsensusAddress] {
if participants[p.ConsensusPubkey] {
return errorsmod.Wrap(ErrInvalidDKGParams, "duplicate participant")
}

participants[p.ConsensusAddress] = true
participants[p.ConsensusPubkey] = true
}

if len(m.VaultTypes) == 0 {
Expand Down
15 changes: 14 additions & 1 deletion x/btcbridge/types/tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@ package types

import (
"crypto/ed25519"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"reflect"

"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/tmhash"
)

// MustGetConsensusAddr gets the hex-encoded consensus address from the given consensus public key
// Panic if any error occurs
func MustGetConsensusAddr(consPubKey string) string {
pubKey, err := base64.StdEncoding.DecodeString(consPubKey)
if err != nil {
panic(err)
}

return hex.EncodeToString(tmhash.SumTruncated(pubKey))
}

// ParticipantExists returns true if the given address is a participant, false otherwise
func ParticipantExists(participants []*DKGParticipant, consAddress string) bool {
for _, p := range participants {
if p.ConsensusAddress == consAddress {
if MustGetConsensusAddr(p.ConsensusPubkey) == consAddress {
return true
}
}
Expand Down

0 comments on commit 24ac038

Please sign in to comment.