Skip to content

Commit

Permalink
add: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wnjoon committed Jan 10, 2025
1 parent 648cc94 commit 5e1f5c1
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 683 deletions.
79 changes: 12 additions & 67 deletions app/signer/private.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package signer

import (
"fmt"
"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"
Expand All @@ -17,83 +15,30 @@ type PrivSigner struct {

func InitPrivSigner(nodeDir string) (*PrivSigner, error) {
nodeCfg := cmtconfig.DefaultConfig()
blsCfg := privval.DefaultBlsConfig()

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

blsCfg := privval.DefaultBlsConfig()
blsCfg.SetRoot(nodeCfg.RootDir)
blsKeyFile := filepath.Join(nodeDir, blsCfg.BlsKeyFile())
err = cmtos.EnsureDir(filepath.Dir(blsKeyFile), 0777)
if err != nil {
return nil, err
}
blsPasswordFile := filepath.Join(nodeDir, blsCfg.BlsPasswordFile())
err = cmtos.EnsureDir(filepath.Dir(blsPasswordFile), 0777)
if err != nil {

if err := privval.IsValidFilePath(pvKeyFile, pvStateFile, blsKeyFile, blsPasswordFile); err != nil {
return nil, err
}

if !cmtos.FileExists(blsKeyFile) {
return nil, fmt.Errorf("BLS key file does not exist: %v", blsKeyFile)
}
cometPV := cmtprivval.LoadFilePV(pvKeyFile, pvStateFile)
blsPV := privval.LoadBlsPV(blsKeyFile, blsPasswordFile)

if !cmtos.FileExists(blsPasswordFile) {
return nil, fmt.Errorf("BLS password file does not exist: %v", blsPasswordFile)
}

blsPv := privval.LoadBlsPV(blsKeyFile, blsPasswordFile)
cmtPv := cmtprivval.LoadFilePV(pvKeyFile, pvStateFile)
wrappedPvKey := privval.WrappedFilePVKey{
CometPVKey: cmtPv.Key,
BlsPVKey: blsPv.Key,
}
wrappedPV := &privval.WrappedFilePV{
Key: wrappedPvKey,
LastSignState: cmtPv.LastSignState,
Key: privval.WrappedFilePVKey{
CometPVKey: cometPV.Key,
BlsPVKey: blsPV.Key,
DelegatorAddress: privval.LoadDeleagatorAddres(blsKeyFile),
},
LastSignState: cometPV.LastSignState,
}

return &PrivSigner{
WrappedPV: wrappedPV,
}, nil
}

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

blsCfg := privval.DefaultBlsConfig()
blsCfg.SetRoot(nodeCfg.RootDir)
blsKeyFile := filepath.Join(nodeDir, blsCfg.BlsKeyFile())
err = cmtos.EnsureDir(filepath.Dir(blsKeyFile), 0777)
if err != nil {
return nil, err
}
blsPasswordFile := filepath.Join(nodeDir, blsCfg.BlsPasswordFile())
err = cmtos.EnsureDir(filepath.Dir(blsPasswordFile), 0777)
if err != nil {
return nil, err
}

wrappedPV := privval.LoadOrGenWrappedFilePV(pvKeyFile, pvStateFile, blsKeyFile, blsPasswordFile)

return &PrivSigner{
WrappedPV: wrappedPV,
}, nil
}
52 changes: 6 additions & 46 deletions cmd/babylond/cmd/create_bls_key.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package cmd

import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strings"

cmtconfig "github.com/cometbft/cometbft/config"
cmtos "github.com/cometbft/cometbft/libs/os"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
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/crypto/erc2335"
"github.com/babylonlabs-io/babylon/privval"
cmtprivval "github.com/cometbft/cometbft/privval"
)

func CreateBlsKeyCmd() *cobra.Command {
Expand Down Expand Up @@ -62,47 +54,15 @@ $ babylond create-bls-key %s1f5tnl46mk4dfp4nx3n2vnrvyw2h2ydz6ykhk3r --home ./
}

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

blsCfg := privval.DefaultBlsConfig()
blsKeyPath := filepath.Join(home, blsCfg.BlsKeyFile())
blsPasswordFile := filepath.Join(home, blsCfg.BlsPasswordFile())

var blsPassword string
var err error
if !cmtos.FileExists(blsPasswordFile) {
log.Printf("BLS password file don't exists in file: %v", blsPasswordFile)
inBuf := bufio.NewReader(os.Stdin)
blsPassword, err = input.GetString("Enter your bls password", inBuf)
if err != nil {
return err
}
err = erc2335.SavePasswordToFile(blsPassword, blsPasswordFile)
if err != nil {
return err
}
} else {
blsPassword, err = erc2335.LoadPaswordFromFile(blsPasswordFile)
if err != nil {
return err
}
}
blsPv := privval.NewBlsPV(bls12381.GenPrivKey(), blsKeyPath, blsPasswordFile)
blsPv.Key.Save(blsPassword)
keyPath := filepath.Join(home, blsCfg.BlsKeyFile())
passwordPath := filepath.Join(home, blsCfg.BlsPasswordFile())

wrappedPv := privval.WrappedFilePV{
Key: privval.WrappedFilePVKey{
CometPVKey: cmtPv.Key,
BlsPVKey: blsPv.Key,
},
LastSignState: cmtPv.LastSignState,
}
password := privval.GetBlsPassword()

blsPv := privval.NewBlsPV(bls12381.GenPrivKey(), keyPath, passwordPath)
blsPv.Key.Save(password, addr.String())

wrappedPv.SetAccAddress(addr)
log.Printf("Saved delegator address: %s", addr.String())
log.Printf("Saved delegator address in wrapperPv: %s", wrappedPv.Key.DelegatorAddress)
return nil
}
199 changes: 0 additions & 199 deletions cmd/babylond/cmd/init.go

This file was deleted.

Loading

0 comments on commit 5e1f5c1

Please sign in to comment.