Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove dependency cometpv remote signer #11

Open
wants to merge 10 commits into
base: feat/bls-keystore-improvement
Choose a base branch
from
4 changes: 2 additions & 2 deletions app/signer/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ func (k *BlsKey) SignMsgWithBls(msg []byte) (bls12381.Signature, error) {
return bls12381.Sign(k.PrivKey, msg), nil
}

// GetBlsPubkey returns the public key of the BLS, implementing the BlsSigner interface
func (k *BlsKey) GetBlsPubkey() (bls12381.PublicKey, error) {
// BlsPubKey returns the public key of the BLS, implementing the BlsSigner interface
func (k *BlsKey) BlsPubKey() (bls12381.PublicKey, error) {
if k.PrivKey == nil {
return nil, checkpointingtypes.ErrBlsPrivKeyDoesNotExist
}
Expand Down
24 changes: 17 additions & 7 deletions cmd/babylond/cmd/create_bls_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,31 @@ $ babylond create-bls-key --home ./

RunE: func(cmd *cobra.Command, args []string) error {
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
password, _ := cmd.Flags().GetString(flagBlsPassword)
createBlsKeyAndSave(homeDir, password)
appsigner.GenBls(appsigner.DefaultBlsKeyFile(homeDir), appsigner.DefaultBlsPasswordFile(homeDir), blsPassword(cmd))
return nil
},
}

cmd.Flags().String(flags.FlagHome, app.DefaultNodeHome, "The node home directory")
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If a flag is set, the non-empty password should be provided. If a flag is not set, the password will be read from the prompt.")
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If the flag is not set, the password will be read from the prompt.")
cmd.Flags().Bool(flagNoBlsPassword, false, "The BLS key will use an empty password if the flag is set.")
return cmd
}

// createBlsKeyAndSave creates a pair of BLS keys and saves them to files
func createBlsKeyAndSave(homeDir, password string) {
// blsPassword returns the password for the BLS key.
// If the noBlsPassword flag is set, the function returns an empty string.
// If the blsPassword flag is set but no argument, the function returns "flag needs an argument: --bls-password" error.
// If the blsPassword flag is set with non-empty string, the function returns the value of the flag.
// If the blsPassword flag is set with empty string, the function requires the user to enter a password.
// If the blsPassword flag is not set and the noBlsPassword flag is not set, the function requires the user to enter a password.
func blsPassword(cmd *cobra.Command) string {
noBlsPassword, _ := cmd.Flags().GetBool(flagNoBlsPassword)
if noBlsPassword {
return ""
}
password, _ := cmd.Flags().GetString(flagBlsPassword)
if password == "" {
password = appsigner.NewBlsPassword()
return appsigner.NewBlsPassword()
}
appsigner.GenBls(appsigner.DefaultBlsKeyFile(homeDir), appsigner.DefaultBlsPasswordFile(homeDir), password)
return password
}
1 change: 1 addition & 0 deletions cmd/babylond/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
flagFinalitySigTimeout = "finality-sig-timeout"
flagJailDuration = "jail-duration"
flagBlsPassword = "bls-password"
flagNoBlsPassword = "no-bls-password"
)

type GenesisCLIArgs struct {
Expand Down
6 changes: 4 additions & 2 deletions cmd/babylond/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

appsigner "github.com/babylonlabs-io/babylon/app/signer"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -22,17 +23,18 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
generate the BLS key and encrypt it into an erc2335 structure.`,
Args: cosmosInitCmd.Args,
RunE: func(cmd *cobra.Command, args []string) error {
// run cosmos init first
if err := cosmosInitCmd.RunE(cmd, args); err != nil {
return fmt.Errorf("failed to run init command: %w", err)
}

homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
password, _ := cmd.Flags().GetString(flagBlsPassword)
createBlsKeyAndSave(homeDir, password)
appsigner.GenBls(appsigner.DefaultBlsKeyFile(homeDir), appsigner.DefaultBlsPasswordFile(homeDir), blsPassword(cmd))
return nil
},
}
cmd.Flags().AddFlagSet(cosmosInitCmd.Flags())
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If the flag is not set, the password will be read from the prompt.")
cmd.Flags().Bool(flagNoBlsPassword, false, "The BLS key will use an empty password if the flag is set.")
return cmd
}
10 changes: 3 additions & 7 deletions cmd/babylond/cmd/migrate_bls_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ $ babylond migrate-bls-key --home ./

RunE: func(cmd *cobra.Command, args []string) error {
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
password, _ := cmd.Flags().GetString(flagBlsPassword)
return migrate(homeDir, password)
return migrate(homeDir, blsPassword(cmd))
},
}

cmd.Flags().String(flags.FlagHome, app.DefaultNodeHome, "The node home directory")
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If a flag is set, the non-empty password should be provided. If a flag is not set, the password will be read from the prompt.")
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If the flag is not set, the password will be read from the prompt.")
cmd.Flags().Bool(flagNoBlsPassword, false, "The BLS key will use an empty password if the flag is set.")
return cmd
}

Expand Down Expand Up @@ -81,10 +81,6 @@ func migrate(homeDir, password string) error {
return fmt.Errorf("priv_validator_key.json of previous version does not contain both the comet and bls keys")
}

if password == "" {
password = appsigner.NewBlsPassword()
}

cmtKeyFilePath := cmtcfg.PrivValidatorKeyFile()
cmtStateFilePath := cmtcfg.PrivValidatorStateFile()
blsKeyFilePath := appsigner.DefaultBlsKeyFile(homeDir)
Expand Down
22 changes: 10 additions & 12 deletions test/e2e/initialization/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package initialization

import (
"fmt"

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

type ChainMeta struct {
Expand All @@ -12,16 +10,16 @@ type ChainMeta struct {
}

type Node struct {
Name string `json:"name"`
ConfigDir string `json:"configDir"`
Mnemonic string `json:"mnemonic"`
PublicAddress string `json:"publicAddress"`
WalletName string `json:"walletName"`
PublicKey []byte `json:"publicKey"`
PrivateKey []byte `json:"privateKey"`
PeerId string `json:"peerId"`
IsValidator bool `json:"isValidator"`
CometPrivKey cmtcrypto.PrivKey `json:"cometPrivKey"`
Name string `json:"name"`
ConfigDir string `json:"configDir"`
Mnemonic string `json:"mnemonic"`
PublicAddress string `json:"publicAddress"`
WalletName string `json:"walletName"`
PublicKey []byte `json:"publicKey"`
PrivateKey []byte `json:"privateKey"`
PeerId string `json:"peerId"`
IsValidator bool `json:"isValidator"`
CometPrivKey []byte `json:"cometPrivKey"`
}

type Chain struct {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/initialization/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (n *internalNode) export() *Node {
PrivateKey: n.privateKey.Bytes(),
PeerId: n.peerId,
IsValidator: n.isValidator,
CometPrivKey: n.consensusKey.Comet.PrivKey,
CometPrivKey: n.consensusKey.Comet.PrivKey.Bytes(),
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/replay/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
cs "github.com/cometbft/cometbft/consensus"
cmtcrypto "github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/ed25519"
cometlog "github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/mempool"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand Down Expand Up @@ -278,7 +279,7 @@ func NewBabylonAppDriver(
FinalizedBlocks: []FinalizedBlock{},
LastState: state.Copy(),
DelegatorAddress: signerValAddress,
CometPrivKey: chain.Nodes[0].CometPrivKey,
CometPrivKey: ed25519.PrivKey(chain.Nodes[0].CometPrivKey),
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/checkpointing/keeper/bls_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (k Keeper) SignBLS(epochNum uint64, blockHash types.BlockHash) (bls12381.Si

// GetValidatorAddress returns the validator address of the signer
func (k Keeper) GetValidatorAddress(ctx context.Context) (sdk.ValAddress, error) {
blsPubKey, err := k.blsSigner.GetBlsPubkey()
blsPubKey, err := k.blsSigner.BlsPubKey()
if err != nil {
return nil, fmt.Errorf("failed to get BLS public key: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/checkpointing/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// BlsSigner is an interface for signing BLS messages
type BlsSigner interface {
SignMsgWithBls(msg []byte) (bls12381.Signature, error)
GetBlsPubkey() (bls12381.PublicKey, error)
BlsPubKey() (bls12381.PublicKey, error)
}

type BlockHash []byte
Expand Down
Loading