Skip to content

Commit

Permalink
feat: modify logic in migrate to verify before saving keys to file.
Browse files Browse the repository at this point in the history
  • Loading branch information
wnjoon committed Jan 21, 2025
1 parent 4e41051 commit d055b84
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
10 changes: 4 additions & 6 deletions cmd/babylond/cmd/create_bls_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"

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

Expand All @@ -29,7 +28,7 @@ $ babylond create-bls-key --home ./
RunE: func(cmd *cobra.Command, args []string) error {
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
password, _ := cmd.Flags().GetString(flagBlsPassword)
createBlsKey(bls12381.GenPrivKey(), homeDir, password)
createBlsKeyAndSave(homeDir, password)
return nil
},
}
Expand All @@ -39,11 +38,10 @@ $ babylond create-bls-key --home ./
return cmd
}

func createBlsKey(privKey bls12381.PrivateKey, homeDir, password string) *privval.BlsPV {
// createBlsKeyAndSave creates a pair of BLS keys and saves them to files
func createBlsKeyAndSave(homeDir, password string) {
if password == "" {
password = privval.NewBlsPassword()
}
pv := privval.NewBlsPV(privKey, privval.DefaultBlsKeyFile(homeDir), privval.DefaultBlsPasswordFile(homeDir))
pv.Key.Save(password)
return pv
privval.GenBlsPV(privval.DefaultBlsKeyFile(homeDir), privval.DefaultBlsPasswordFile(homeDir), password)
}
3 changes: 1 addition & 2 deletions cmd/babylond/cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/babylonlabs-io/babylon/crypto/bls12381"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -23,7 +22,7 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {

homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
password, _ := cmd.Flags().GetString(flagBlsPassword)
createBlsKey(bls12381.GenPrivKey(), homeDir, password)
createBlsKeyAndSave(homeDir, password)
return nil
},
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/babylond/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/babylonlabs-io/babylon/app"
"github.com/babylonlabs-io/babylon/crypto/bls12381"
"github.com/babylonlabs-io/babylon/privval"
cmtcfg "github.com/cometbft/cometbft/config"
cmtcrypto "github.com/cometbft/cometbft/crypto"
cmtjson "github.com/cometbft/cometbft/libs/json"
Expand Down Expand Up @@ -77,10 +78,14 @@ 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 = privval.NewBlsPassword()
}

cmtPv := cmtprivval.NewFilePV(prevCmtPrivKey, cmtcfg.PrivValidatorKeyFile(), cmtcfg.PrivValidatorStateFile())
cmtPv.Key.Save()
blsPv := createBlsKey(prevBlsPrivKey, homeDir, password)
blsPv := privval.NewBlsPV(prevBlsPrivKey, privval.DefaultBlsKeyFile(homeDir), privval.DefaultBlsPasswordFile(homeDir))

// before saving keys to files, verify that the migrated keys match
if err := verifyAfterMigration(
prevCmtPrivKey,
cmtPv.Key.PrivKey,
Expand All @@ -89,6 +94,10 @@ func migrate(homeDir, password string) error {
); err != nil {
return fmt.Errorf("failed to verify after migration: %w", err)
}

// save key to files after verification
cmtPv.Save()
blsPv.Key.Save(password)
return nil
}

Expand Down
22 changes: 22 additions & 0 deletions cmd/babylond/cmd/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"testing"

"github.com/babylonlabs-io/babylon/crypto/bls12381"
"github.com/babylonlabs-io/babylon/privval"
"github.com/cometbft/cometbft/crypto/ed25519"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtprivval "github.com/cometbft/cometbft/privval"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -61,6 +63,10 @@ func TestMigrate(t *testing.T) {
err := os.MkdirAll(configDir, 0755)
require.NoError(t, err)

dataDir := filepath.Join(tempDir, "data")
err = os.MkdirAll(dataDir, 0755)
require.NoError(t, err)

pvKeyFile := filepath.Join(configDir, "priv_validator_key.json")
pvKey := PrevWrappedFilePV{
PrivKey: ed25519.GenPrivKey(),
Expand All @@ -78,9 +84,25 @@ func TestMigrate(t *testing.T) {

// Check if new files are created
newPvKeyFile := filepath.Join(configDir, "priv_validator_key.json")
newPvStateFile := filepath.Join(dataDir, "priv_validator_state.json")
newBlsKeyFile := filepath.Join(configDir, "bls_key.json")
newBlsPasswordFile := filepath.Join(configDir, "bls_password.txt")
require.FileExists(t, newPvKeyFile)
require.FileExists(t, newPvStateFile)
require.FileExists(t, newBlsKeyFile)
require.FileExists(t, newBlsPasswordFile)

t.Run("verify after migration", func(t *testing.T) {
newCmtPv := cmtprivval.LoadFilePV(newPvKeyFile, newPvStateFile)
newBlsPv := privval.LoadBlsPV(newBlsKeyFile, newBlsPasswordFile)
err := verifyAfterMigration(
pvKey.PrivKey,
newCmtPv.Key.PrivKey,
pvKey.BlsPrivKey,
newBlsPv.Key.PrivKey,
)
require.NoError(t, err)
})
})
}

Expand Down

0 comments on commit d055b84

Please sign in to comment.