From 5b337a50291ffae9739b48b810cad11f0b56edec Mon Sep 17 00:00:00 2001 From: NathanBSC Date: Wed, 21 Jun 2023 14:33:30 +0800 Subject: [PATCH] feat: support password flag when handling bls keys --- cmd/geth/blsaccountcmd.go | 50 +++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/cmd/geth/blsaccountcmd.go b/cmd/geth/blsaccountcmd.go index 99c4453e07..cb95c1332a 100644 --- a/cmd/geth/blsaccountcmd.go +++ b/cmd/geth/blsaccountcmd.go @@ -46,6 +46,10 @@ var ( Name: "show-private-key", Usage: "Show the BLS12-381 private key you will encrypt into a keystore file", } + BLSAccountPasswordFileFlag = cli.StringFlag{ + Name: "blsaccountpassword", + Usage: "File path for the BLS account password, which contains the password to encrypt private key into keystore file for managing votes in fast_finality feature", + } ) var ( @@ -87,6 +91,7 @@ or import a BLS account. The BLS wallet dir should be "/bls/wallet".`, Category: "BLS ACCOUNT COMMANDS", Flags: []cli.Flag{ utils.DataDirFlag, + utils.BLSPasswordFileFlag, }, Description: ` geth bls wallet create @@ -128,6 +133,8 @@ Make sure you backup your BLS keys regularly.`, utils.DataDirFlag, privateKeyFlag, showPrivateKeyFlag, + utils.BLSPasswordFileFlag, + BLSAccountPasswordFileFlag, }, Description: ` geth bls account new @@ -147,6 +154,8 @@ You must remember this password to unlock your account in the future.`, Category: "BLS ACCOUNT COMMANDS", Flags: []cli.Flag{ utils.DataDirFlag, + utils.BLSPasswordFileFlag, + BLSAccountPasswordFileFlag, }, Description: ` geth bls account import @@ -163,6 +172,7 @@ If the BLS wallet not created yet, it will try to create BLS wallet first.`, Category: "BLS ACCOUNT COMMANDS", Flags: []cli.Flag{ utils.DataDirFlag, + utils.BLSPasswordFileFlag, }, Description: ` geth bls account list @@ -177,6 +187,7 @@ Print summary of existing BLS accounts in the current BLS wallet.`, Category: "BLS ACCOUNT COMMANDS", Flags: []cli.Flag{ utils.DataDirFlag, + utils.BLSPasswordFileFlag, }, Description: ` geth bls account delete @@ -209,7 +220,7 @@ func blsWalletCreate(ctx *cli.Context) error { utils.Fatalf("BLS wallet already exists in /bls/wallet.") } - password := utils.GetPassPhrase("Your new BLS wallet will be locked with a password. Please give a password. Do not forget this password.", true) + password := utils.GetPassPhraseWithList("Your new BLS wallet will be locked with a password. Please give a password. Do not forget this password.", true, 0, GetBLSPassword(ctx)) opts := []accounts.Option{} opts = append(opts, accounts.WithWalletDir(dir)) @@ -239,7 +250,7 @@ func openOrCreateBLSWallet(ctx *cli.Context, cfg *gethConfig) (*wallet.Wallet, e } if !dirExists { fmt.Println("BLS wallet not exists, creating BLS wallet...") - password := utils.GetPassPhrase("Your new BLS wallet will be locked with a password. Please give a password. Do not forget this password.", true) + password := utils.GetPassPhraseWithList("Your new BLS wallet will be locked with a password. Please give a password. Do not forget this password.", true, 0, GetBLSPassword(ctx)) opts := []accounts.Option{} opts = append(opts, accounts.WithWalletDir(walletDir)) @@ -259,7 +270,7 @@ func openOrCreateBLSWallet(ctx *cli.Context, cfg *gethConfig) (*wallet.Wallet, e return w, nil } - walletPassword := utils.GetPassPhrase("Enter the password for your BLS wallet.", false) + walletPassword := utils.GetPassPhraseWithList("Enter the password for your BLS wallet.", false, 0, GetBLSPassword(ctx)) w, err = wallet.OpenWallet(context.Background(), &wallet.Config{ WalletDir: walletDir, WalletPassword: walletPassword, @@ -299,7 +310,7 @@ func blsAccountCreate(ctx *cli.Context) error { if err := os.MkdirAll(keystoreDir, 0755); err != nil { utils.Fatalf("Could not access keystore dir: %v.", err) } - accountPassword := utils.GetPassPhrase("Your new BLS account will be encrypted with a password. Please give a password. Do not forget this password.", true) + accountPassword := utils.GetPassPhraseWithList("Your new BLS account will be encrypted with a password. Please give a password. Do not forget this password.", true, 0, GetBLSAccountPassword(ctx)) if err := core.ValidatePasswordFormat(accountPassword); err != nil { utils.Fatalf("Password invalid: %v.", err) } @@ -411,7 +422,8 @@ func blsAccountImport(ctx *cli.Context) error { utils.Fatalf("The BLS keymanager cannot import keystores") } - password := utils.GetPassPhrase("Enter the password for your imported account.", false) + password := utils.GetPassPhraseWithList("Enter the password for your imported account.", false, 0, GetBLSAccountPassword(ctx)) + fmt.Println("Importing BLS account, this may take a while...") statuses, err := accounts.ImportAccounts(context.Background(), &accounts.ImportAccountsConfig{ Importer: k, @@ -447,7 +459,7 @@ func blsAccountList(ctx *cli.Context) error { utils.Fatalf("BLS wallet not exists.") } - walletPassword := utils.GetPassPhrase("Enter the password for your BLS wallet.", false) + walletPassword := utils.GetPassPhraseWithList("Enter the password for your BLS wallet.", false, 0, GetBLSPassword(ctx)) w, err := wallet.OpenWallet(context.Background(), &wallet.Config{ WalletDir: walletDir, WalletPassword: walletPassword, @@ -526,7 +538,7 @@ func blsAccountDelete(ctx *cli.Context) error { utils.Fatalf("BLS wallet not exists.") } - walletPassword := utils.GetPassPhrase("Enter the password for your BLS wallet.", false) + walletPassword := utils.GetPassPhraseWithList("Enter the password for your BLS wallet.", false, 0, GetBLSPassword(ctx)) w, err := wallet.OpenWallet(context.Background(), &wallet.Config{ WalletDir: walletDir, WalletPassword: walletPassword, @@ -587,3 +599,27 @@ func blsAccountDelete(ctx *cli.Context) error { return nil } + +func GetBLSPassword(ctx *cli.Context) []string { + path := ctx.GlobalString(utils.BLSPasswordFileFlag.Name) + if path == "" { + return nil + } + text, err := ioutil.ReadFile(path) + if err != nil { + utils.Fatalf("Failed to read wallet password file: %v", err) + } + return []string{string(text)} +} + +func GetBLSAccountPassword(ctx *cli.Context) []string { + path := ctx.String(BLSAccountPasswordFileFlag.Name) + if path == "" { + return nil + } + text, err := ioutil.ReadFile(path) + if err != nil { + utils.Fatalf("Failed to read account password file: %v", err) + } + return []string{string(text)} +}