Skip to content

Commit

Permalink
Merge pull request #196 from LeoHChen/add_bls_key_dir_option
Browse files Browse the repository at this point in the history
[hmcli] add --bls-pubkeys-dir option
  • Loading branch information
Daniel-VDM authored Apr 13, 2020
2 parents 1259204 + 5b58125 commit 2176f31
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
6 changes: 4 additions & 2 deletions cmd/subcommands/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
minSelfDelegation string
maxTotalDelegation string
stakingBlsPubKeys []string
blsPubKeyDir string
delegatorAddress oneAddress
validatorAddress oneAddress
stakingAmount string
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/common/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
57 changes: 37 additions & 20 deletions pkg/keys/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 2176f31

Please sign in to comment.