Merge pull request #196 from LeoHChen/add_bls_key_dir_option

[hmcli] add --bls-pubkeys-dir option
pull/211/head v1.0.9
Daniel Van Der Maden 5 years ago committed by GitHub
commit 2176f31ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      cmd/subcommands/staking.go
  2. 2
      pkg/common/values.go
  3. 57
      pkg/keys/bls.go

@ -52,6 +52,7 @@ var (
minSelfDelegation string
maxTotalDelegation string
stakingBlsPubKeys []string
blsPubKeyDir string
delegatorAddress oneAddress
validatorAddress oneAddress
stakingAmount string
@ -326,7 +327,7 @@ Create a new validator"
blsPubKeys[i].FromLibBLSPublicKey(blsPubKey)
}
blsSigs, err := keys.VerifyBLSKeys(stakingBlsPubKeys)
blsSigs, err := keys.VerifyBLSKeys(stakingBlsPubKeys, blsPubKeyDir)
if err != nil {
return err
}
@ -427,6 +428,7 @@ Create a new validator"
&stakingBlsPubKeys, "bls-pubkeys",
[]string{}, "validator's list of public BLS key addresses",
)
subCmdNewValidator.Flags().StringVar(&blsPubKeyDir, "bls-pubkeys-dir", "", "directory to bls pubkeys storing pub.key, pub.pass files")
subCmdNewValidator.Flags().StringVar(&stakingAmount, "amount", "0.0", "staking amount")
subCmdNewValidator.Flags().StringVar(&gasPrice, "gas-price", "1", "gas price to pay")
subCmdNewValidator.Flags().StringVar(&gasLimit, "gas-limit", "", "gas limit")
@ -494,7 +496,7 @@ Create a new validator"
shardKey.FromLibBLSPublicKey(blsKey)
shardPubKeyAdd = &shardKey
sig, err := keys.VerifyBLS(strings.TrimPrefix(slotKeyToAdd, "0x"))
sig, err := keys.VerifyBLS(strings.TrimPrefix(slotKeyToAdd, "0x"), blsPubKeyDir)
if err != nil {
return err
}

@ -22,6 +22,8 @@ var (
DebugTransaction = false
ErrNotAbsPath = errors.New("keypath is not absolute path")
ErrBadKeyLength = errors.New("Invalid private key (wrong length)")
ErrFoundNoKey = errors.New("found no bls key file")
ErrFoundNoPass = errors.New("found no passphrase file")
)
func init() {

@ -157,10 +157,10 @@ func GetPublicBlsKey(privateKeyHex string) error {
}
func VerifyBLSKeys(blsPubKeys []string) ([]shard.BLSSignature, error) {
func VerifyBLSKeys(blsPubKeys []string, blsPubKeyDir string) ([]shard.BLSSignature, error) {
blsSigs := make([]shard.BLSSignature, len(blsPubKeys))
for i := 0; i < len(blsPubKeys); i++ {
sig, err := VerifyBLS(strings.TrimPrefix(blsPubKeys[i], "0x"))
sig, err := VerifyBLS(strings.TrimPrefix(blsPubKeys[i], "0x"), blsPubKeyDir)
if err != nil {
return nil, err
}
@ -169,30 +169,47 @@ func VerifyBLSKeys(blsPubKeys []string) ([]shard.BLSSignature, error) {
return blsSigs, nil
}
func VerifyBLS(blsPubKey string) (shard.BLSSignature, error) {
func VerifyBLS(blsPubKey string, blsPubKeyDir string) (shard.BLSSignature, error) {
var sig shard.BLSSignature
// look for key file in the current directory
// if not ask for the absolute path
cwd, _ := os.Getwd()
filePath := fmt.Sprintf("%s/%s.key", cwd, blsPubKey)
encryptedPrivateKeyBytes, err := ioutil.ReadFile(filePath)
if err != nil {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("For bls public key: %s\n", blsPubKey)
fmt.Println("Enter the absolute path to the encrypted bls private key file:")
filePath, _ := reader.ReadString('\n')
if !path.IsAbs(filePath) {
return sig, common.ErrNotAbsPath
var encryptedPrivateKeyBytes []byte
var pass []byte
var err error
// specified blsPubKeyDir
if len(blsPubKeyDir) != 0 {
filePath := fmt.Sprintf("%s/%s.key", blsPubKeyDir, blsPubKey)
encryptedPrivateKeyBytes, err = ioutil.ReadFile(filePath)
if err != nil {
return sig, common.ErrFoundNoKey
}
filePath = strings.TrimSpace(filePath)
passFile := fmt.Sprintf("%s/%s.pass", blsPubKeyDir, blsPubKey)
pass, err = ioutil.ReadFile(passFile)
if err != nil {
return sig, common.ErrFoundNoPass
}
} else {
// look for key file in the current directory
// if not ask for the absolute path
cwd, _ := os.Getwd()
filePath := fmt.Sprintf("%s/%s.key", cwd, blsPubKey)
encryptedPrivateKeyBytes, err = ioutil.ReadFile(filePath)
if err != nil {
return sig, err
reader := bufio.NewReader(os.Stdin)
fmt.Printf("For bls public key: %s\n", blsPubKey)
fmt.Println("Enter the absolute path to the encrypted bls private key file:")
filePath, _ := reader.ReadString('\n')
if !path.IsAbs(filePath) {
return sig, common.ErrNotAbsPath
}
filePath = strings.TrimSpace(filePath)
encryptedPrivateKeyBytes, err = ioutil.ReadFile(filePath)
if err != nil {
return sig, err
}
}
// ask passphrase for bls key twice
fmt.Println("Enter the bls passphrase:")
pass, _ = terminal.ReadPassword(int(os.Stdin.Fd()))
}
// ask passphrase for bls key twice
fmt.Println("Enter the bls passphrase:")
pass, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
decryptedPrivateKeyBytes, err := decrypt(encryptedPrivateKeyBytes, string(pass))
if err != nil {

Loading…
Cancel
Save