-
Notifications
You must be signed in to change notification settings - Fork 170
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: checkpointing/add create-bls-key cli #162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/babylonchain/babylon/app" | ||
appparams "github.com/babylonchain/babylon/app/params" | ||
"github.com/babylonchain/babylon/crypto/bls12381" | ||
"github.com/babylonchain/babylon/privval" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/spf13/cobra" | ||
tmconfig "github.com/tendermint/tendermint/config" | ||
tmos "github.com/tendermint/tendermint/libs/os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func createBlsKeyCmd() *cobra.Command { | ||
bech32PrefixAccAddr := appparams.Bech32PrefixAccAddr | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create-bls-key [account-address]", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Create a pair of BLS keys for a validator", | ||
Long: strings.TrimSpace( | ||
fmt.Sprintf(`create-bls will create a pair of BLS keys that are used to | ||
send BLS signatures for checkpointing. | ||
|
||
BLS keys are stored along with other validator keys in priv_validator_key.json, | ||
which should exist before running the command (via babylond init or babylond testnet). | ||
|
||
Example: | ||
$ babylond create-bls-key %s1f5tnl46mk4dfp4nx3n2vnrvyw2h2ydz6ykhk3r --home ./ | ||
`, | ||
bech32PrefixAccAddr, | ||
), | ||
), | ||
|
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
homeDir, _ := cmd.Flags().GetString(flags.FlagHome) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error is ignored here, but not for the account address. Does that mean this is optional, but the other isn't? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe look at https://stackoverflow.com/questions/38105859/make-a-cobra-command-flag-required to mark them as required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other one is an arg instead of a flag. |
||
|
||
addr, err := sdk.AccAddressFromBech32(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return CreateBlsKey(homeDir, addr) | ||
}, | ||
} | ||
|
||
cmd.Flags().String(flags.FlagHome, app.DefaultNodeHome, "The node home directory") | ||
|
||
return cmd | ||
} | ||
|
||
func CreateBlsKey(home string, addr sdk.AccAddress) error { | ||
nodeCfg := tmconfig.DefaultConfig() | ||
keyPath := filepath.Join(home, nodeCfg.PrivValidatorKeyFile()) | ||
statePath := filepath.Join(home, nodeCfg.PrivValidatorStateFile()) | ||
if !tmos.FileExists(keyPath) { | ||
return errors.New("validator key file does not exist") | ||
} | ||
pv := privval.LoadWrappedFilePV(keyPath, statePath) | ||
wrappedPV := privval.NewWrappedFilePV(pv.GetValPrivKey(), bls12381.GenPrivKey(), keyPath, statePath) | ||
wrappedPV.SetAccAddress(addr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it automatically saves itself? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, when we set the account address, we should save the file. |
||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[]
brackets mean this flag is optional, and the example should contain--
to show it's a flag, otherwise I would think the way to call this iscreate-bls-key
orcrate-bls-key bbn1f5tnl46mk4dfp4nx3n2vnrvyw2h2ydz6ykhk3r
.If it's not an optional parameter than maybe it should be an
arg
instead of a flag.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right. It should an arg instead of a flag. Thanks!