[blsgen] LoadFromKey with passphrase more robustly (#2675)

pull/2678/head
Edgar Aroutiounian 5 years ago committed by GitHub
parent 76dbf7c88b
commit dfceae3691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      internal/blsgen/lib.go
  2. 6
      internal/blsgen/lib_test.go

@ -58,7 +58,9 @@ func GenBLSKeyWithPassPhrase(passphrase string) (*ffi_bls.SecretKey, string, err
}
// WritePriKeyWithPassPhrase writes encrypted key with passphrase.
func WritePriKeyWithPassPhrase(privateKey *ffi_bls.SecretKey, passphrase string) (string, error) {
func WritePriKeyWithPassPhrase(
privateKey *ffi_bls.SecretKey, passphrase string,
) (string, error) {
publickKey := privateKey.GetPublicKey()
fileName := publickKey.SerializeToHexStr() + ".key"
privateKeyHex := privateKey.SerializeToHexStr()
@ -68,8 +70,10 @@ func WritePriKeyWithPassPhrase(privateKey *ffi_bls.SecretKey, passphrase string)
return "", err
}
// Write to file.
err = WriteToFile(fileName, encryptedPrivateKeyStr)
return fileName, err
if err := WriteToFile(fileName, encryptedPrivateKeyStr); err != nil {
return fileName, err
}
return fileName, nil
}
// WriteToFile will print any string of text to a file safely by
@ -91,7 +95,7 @@ func WriteToFile(filename string, data string) error {
func LoadBLSKeyWithPassPhrase(fileName, passphrase string) (*ffi_bls.SecretKey, error) {
encryptedPrivateKeyBytes, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "attemped to load from %s", fileName)
}
for len(passphrase) > 0 && passphrase[len(passphrase)-1] == '\n' {
passphrase = passphrase[:len(passphrase)-1]
@ -102,7 +106,11 @@ func LoadBLSKeyWithPassPhrase(fileName, passphrase string) (*ffi_bls.SecretKey,
}
priKey := &ffi_bls.SecretKey{}
priKey.DeserializeHexStr(string(decryptedBytes))
if err := priKey.DeserializeHexStr(string(decryptedBytes)); err != nil {
return nil, errors.Wrapf(
err, "could not deserialize byte content of %s as BLS secret key", fileName,
)
}
return priKey, nil
}

@ -18,7 +18,11 @@ func TestUpdateStakingList(t *testing.T) {
}
if !privateKey.IsEqual(anotherPriKey) {
t.Error("Error when generating bls key.")
t.Errorf("Error when generating bls key \n%s\n%s\n%s",
fileName,
privateKey.SerializeToHexStr(),
anotherPriKey.SerializeToHexStr(),
)
}
// Clean up the testing file.

Loading…
Cancel
Save