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: BLS Key Separation and ERC2335 Implementation #396

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Improvements

- [#396](https://github.com/babylonlabs-io/babylon/pull/396) BLS Key Separation and ERC2335 Implementation
- [#391](https://github.com/babylonlabs-io/babylon/pull/391) Fix e2e `TestBTCRewardsDistribution` flunky
check of rewards

Expand Down
30 changes: 19 additions & 11 deletions app/signer/private.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package signer

import (
"path/filepath"

cmtconfig "github.com/cometbft/cometbft/config"
cmtos "github.com/cometbft/cometbft/libs/os"

"github.com/babylonlabs-io/babylon/privval"
cmtprivval "github.com/cometbft/cometbft/privval"
)

type PrivSigner struct {
Expand All @@ -15,17 +13,27 @@ type PrivSigner struct {

func InitPrivSigner(nodeDir string) (*PrivSigner, error) {
nodeCfg := cmtconfig.DefaultConfig()
pvKeyFile := filepath.Join(nodeDir, nodeCfg.PrivValidatorKeyFile())
err := cmtos.EnsureDir(filepath.Dir(pvKeyFile), 0777)
if err != nil {
nodeCfg.SetRoot(nodeDir)

pvKeyFile := nodeCfg.PrivValidatorKeyFile()
pvStateFile := nodeCfg.PrivValidatorStateFile()
blsKeyFile := privval.DefaultBlsKeyFile(nodeDir)
blsPasswordFile := privval.DefaultBlsPasswordFile(nodeDir)

if err := privval.EnsureDirs(pvKeyFile, pvStateFile, blsKeyFile, blsPasswordFile); err != nil {
return nil, err
}
pvStateFile := filepath.Join(nodeDir, nodeCfg.PrivValidatorStateFile())
err = cmtos.EnsureDir(filepath.Dir(pvStateFile), 0777)
if err != nil {
return nil, err

cometPV := cmtprivval.LoadFilePV(pvKeyFile, pvStateFile)
blsPV := privval.LoadBlsPV(blsKeyFile, blsPasswordFile)

wrappedPV := &privval.WrappedFilePV{
Key: privval.WrappedFilePVKey{
CometPVKey: cometPV.Key,
BlsPVKey: blsPV.Key,
},
LastSignState: cometPV.LastSignState,
gitferry marked this conversation as resolved.
Show resolved Hide resolved
}
wrappedPV := privval.LoadOrGenWrappedFilePV(pvKeyFile, pvStateFile)

return &PrivSigner{
WrappedPV: wrappedPV,
Expand Down
4 changes: 2 additions & 2 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ func SetupWithBitcoinConf(t *testing.T, isCheckTx bool, btcConf bbn.SupportedBtc

ps, err := signer.SetupTestPrivSigner()
require.NoError(t, err)
valPubKey := ps.WrappedPV.Key.PubKey
valPubKey := ps.WrappedPV.Key.CometPVKey.PubKey
// generate genesis account
acc := authtypes.NewBaseAccount(valPubKey.Address().Bytes(), &cosmosed.PubKey{Key: valPubKey.Bytes()}, 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(appparams.DefaultBondDenom, math.NewInt(100000000000000))),
}
ps.WrappedPV.Key.DelegatorAddress = acc.GetAddress().String()
ps.WrappedPV.Key.BlsPVKey.DelegatorAddress = acc.GetAddress().String()
// create validator set with single validator
genesisKey, err := signer.GenesisKeyFromPrivSigner(ps)
require.NoError(t, err)
Expand Down
49 changes: 18 additions & 31 deletions cmd/babylond/cmd/create_bls_key.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package cmd

import (
"errors"
"fmt"
"path/filepath"
"strings"

cmtconfig "github.com/cometbft/cometbft/config"
cmtos "github.com/cometbft/cometbft/libs/os"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/babylonlabs-io/babylon/app"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/crypto/bls12381"
"github.com/babylonlabs-io/babylon/privval"
)

const (
FlagPassword = "bls-password"
)

func CreateBlsKeyCmd() *cobra.Command {
bech32PrefixAccAddr := appparams.Bech32PrefixAccAddr

Expand Down Expand Up @@ -47,38 +46,26 @@ $ babylond create-bls-key %s1f5tnl46mk4dfp4nx3n2vnrvyw2h2ydz6ykhk3r --home ./
return err
}

return CreateBlsKey(homeDir, addr)
var password string
password, _ = cmd.Flags().GetString(FlagPassword)
if password == "" {
password = privval.NewBlsPassword()
}
return CreateBlsKey(homeDir, password, addr)
},
}

cmd.Flags().String(flags.FlagHome, app.DefaultNodeHome, "The node home directory")

cmd.Flags().String(FlagPassword, "", "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.")
return cmd
}

func CreateBlsKey(home string, addr sdk.AccAddress) error {
nodeCfg := cmtconfig.DefaultConfig()
keyPath := filepath.Join(home, nodeCfg.PrivValidatorKeyFile())
statePath := filepath.Join(home, nodeCfg.PrivValidatorStateFile())

pv, err := LoadWrappedFilePV(keyPath, statePath)
if err != nil {
return err
}

wrappedPV := privval.NewWrappedFilePV(pv.GetValPrivKey(), bls12381.GenPrivKey(), keyPath, statePath)
wrappedPV.SetAccAddress(addr)

func CreateBlsKey(home, password string, addr sdk.AccAddress) error {
privval.GenBlsPV(
privval.DefaultBlsKeyFile(home),
privval.DefaultBlsPasswordFile(home),
password,
addr.String(),
)
return nil
}

// LoadWrappedFilePV loads the wrapped file private key from the file path.
func LoadWrappedFilePV(keyPath, statePath string) (*privval.WrappedFilePV, error) {
if !cmtos.FileExists(keyPath) {
return nil, errors.New("validator key file does not exist")
}
if !cmtos.FileExists(statePath) {
return nil, errors.New("validator state file does not exist")
}
return privval.LoadWrappedFilePV(keyPath, statePath), nil
}
28 changes: 24 additions & 4 deletions cmd/babylond/cmd/genhelpers/bls_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package genhelpers_test
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

Expand All @@ -20,6 +21,7 @@ import (
cmtconfig "github.com/cometbft/cometbft/config"
tmjson "github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/libs/tempfile"
cmtprivval "github.com/cometbft/cometbft/privval"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
Expand Down Expand Up @@ -147,13 +149,24 @@ func Test_CmdAddBlsWithGentx(t *testing.T) {
v := testNetwork.Validators[i]
// build and create genesis BLS key
genBlsCmd := genhelpers.CmdCreateBls()
nodeCfg := cmtconfig.DefaultConfig()
homeDir := filepath.Join(v.Dir, "simd")

nodeCfg := cmtconfig.DefaultConfig()
nodeCfg.SetRoot(homeDir)

keyPath := nodeCfg.PrivValidatorKeyFile()
statePath := nodeCfg.PrivValidatorStateFile()
filePV := privval.GenWrappedFilePV(keyPath, statePath)
filePV.SetAccAddress(v.Address)
blsKeyFile := privval.DefaultBlsKeyFile(homeDir)
blsPasswordFile := privval.DefaultBlsPasswordFile(homeDir)

err := privval.EnsureDirs(keyPath, statePath, blsKeyFile, blsPasswordFile)
require.NoError(t, err)

filePV := cmtprivval.GenFilePV(keyPath, statePath)
filePV.Key.Save()
filePV.LastSignState.Save()
privval.GenBlsPV(blsKeyFile, blsPasswordFile, "password", v.Address.String())

_, err = cli.ExecTestCLICmd(v.ClientCtx, genBlsCmd, []string{fmt.Sprintf("--%s=%s", flags.FlagHome, homeDir)})
require.NoError(t, err)
genKeyFileName := filepath.Join(filepath.Dir(keyPath), fmt.Sprintf("gen-bls-%s.json", v.ValAddress))
Expand All @@ -175,6 +188,13 @@ func Test_CmdAddBlsWithGentx(t *testing.T) {
require.NotEmpty(t, checkpointingGenState.GenesisKeys)
gks := checkpointingGenState.GetGenesisKeys()
require.Equal(t, genKey, gks[i])
filePV.Clean(keyPath, statePath)
Clean(keyPath, statePath, blsKeyFile, blsPasswordFile)
}
}

// Clean removes PVKey file and PVState file
func Clean(paths ...string) {
for _, path := range paths {
_ = os.RemoveAll(filepath.Dir(path))
}
}
35 changes: 28 additions & 7 deletions cmd/babylond/cmd/genhelpers/bls_create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package genhelpers

import (
"errors"
"fmt"
"path/filepath"
"strings"

Expand All @@ -12,6 +12,7 @@ import (

"github.com/babylonlabs-io/babylon/app"
"github.com/babylonlabs-io/babylon/privval"
cmtprivval "github.com/cometbft/cometbft/privval"
)

// CmdCreateBls CLI command to create BLS file with proof of possession.
Expand All @@ -35,15 +36,35 @@ $ babylond genbls --home ./
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)

nodeCfg := cmtconfig.DefaultConfig()
keyPath := filepath.Join(homeDir, nodeCfg.PrivValidatorKeyFile())
statePath := filepath.Join(homeDir, nodeCfg.PrivValidatorStateFile())
if !cmtos.FileExists(keyPath) {
return errors.New("validator key file does not exist")
nodeCfg.SetRoot(homeDir)
cmtPvKeyFile := nodeCfg.PrivValidatorKeyFile()
cmtPvStateFile := nodeCfg.PrivValidatorStateFile()
blsKeyFile := privval.DefaultBlsKeyFile(homeDir)
blsPasswordFile := privval.DefaultBlsPasswordFile(homeDir)

if err := func(paths ...string) error {
for _, path := range paths {
if !cmtos.FileExists(path) {
return fmt.Errorf("file does not exist in %s", path)
}
}
return nil
}(cmtPvKeyFile, cmtPvStateFile, blsKeyFile, blsPasswordFile); err != nil {
return err
}

wrappedPV := privval.LoadWrappedFilePV(keyPath, statePath)
cmtPV := cmtprivval.LoadFilePV(cmtPvKeyFile, cmtPvStateFile)
blsPV := privval.LoadBlsPV(blsKeyFile, blsPasswordFile)

wrappedPV := &privval.WrappedFilePV{
Key: privval.WrappedFilePVKey{
CometPVKey: cmtPV.Key,
BlsPVKey: blsPV.Key,
},
LastSignState: cmtPV.LastSignState,
}

outputFileName, err := wrappedPV.ExportGenBls(filepath.Dir(keyPath))
outputFileName, err := wrappedPV.ExportGenBls(filepath.Dir(cmtPvKeyFile))
if err != nil {
return err
}
Expand Down
25 changes: 19 additions & 6 deletions cmd/babylond/cmd/genhelpers/bls_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/babylonlabs-io/babylon/privval"
"github.com/babylonlabs-io/babylon/testutil/signer"
"github.com/babylonlabs-io/babylon/x/checkpointing/types"
cmtprivval "github.com/cometbft/cometbft/privval"
)

func Test_CmdCreateBls(t *testing.T) {
Expand Down Expand Up @@ -70,21 +71,33 @@ func Test_CmdCreateBls(t *testing.T) {

// create BLS keys
nodeCfg := cmtconfig.DefaultConfig()
keyPath := filepath.Join(home, nodeCfg.PrivValidatorKeyFile())
statePath := filepath.Join(home, nodeCfg.PrivValidatorStateFile())
filePV := privval.GenWrappedFilePV(keyPath, statePath)
defer filePV.Clean(keyPath, statePath)
filePV.SetAccAddress(addr)
nodeCfg.SetRoot(home)

keyPath := nodeCfg.PrivValidatorKeyFile()
statePath := nodeCfg.PrivValidatorStateFile()
blsKeyFile := privval.DefaultBlsKeyFile(home)
blsPasswordFile := privval.DefaultBlsPasswordFile(home)

err = privval.EnsureDirs(keyPath, statePath, blsKeyFile, blsPasswordFile)
require.NoError(t, err)

filePV := cmtprivval.GenFilePV(keyPath, statePath)
filePV.Key.Save()

blsPV := privval.GenBlsPV(blsKeyFile, blsPasswordFile, "password", addr.String())
defer Clean(keyPath, statePath, blsKeyFile, blsPasswordFile)

// execute the gen-bls cmd
err = genBlsCmd.ExecuteContext(ctx)
require.NoError(t, err)
outputFilePath := filepath.Join(filepath.Dir(keyPath), fmt.Sprintf("gen-bls-%s.json", sdk.ValAddress(addr).String()))
require.NoError(t, err)
genKey, err := types.LoadGenesisKeyFromFile(outputFilePath)

require.NoError(t, err)
require.Equal(t, sdk.ValAddress(addr).String(), genKey.ValidatorAddress)
require.True(t, filePV.Key.BlsPubKey.Equal(*genKey.BlsKey.Pubkey))
require.Equal(t, filePV.Key.PubKey.Bytes(), genKey.ValPubkey.Bytes())
require.True(t, blsPV.Key.PubKey.Equal(*genKey.BlsKey.Pubkey))

require.True(t, genKey.BlsKey.Pop.IsValid(*genKey.BlsKey.Pubkey, genKey.ValPubkey))
}
Loading