diff --git a/crypto/bls/attestation.go b/crypto/bls/attestation.go index 2c80b4ad..7da705d4 100644 --- a/crypto/bls/attestation.go +++ b/crypto/bls/attestation.go @@ -186,6 +186,24 @@ func GenRandomBlsKeys() (*KeyPair, error) { // SaveToFile saves the private key in an encrypted keystore file func (k *KeyPair) SaveToFile(path string, password string) error { + data, err := k.EncryptedString(path, password) + if err != nil { + return err + } + + dir := filepath.Dir(path) + if err := os.MkdirAll(dir, 0755); err != nil { + fmt.Println("Error creating directories:", err) + return err + } + err = os.WriteFile(path, data, 0644) + if err != nil { + return err + } + return nil +} + +func (k *KeyPair) EncryptedString(path string, password string) ([]byte, error) { sk32Bytes := k.PrivKey.Bytes() skBytes := make([]byte, 32) for i := 0; i < 32; i++ { @@ -199,7 +217,7 @@ func (k *KeyPair) SaveToFile(path string, password string) error { keystore.StandardScryptP, ) if err != nil { - return err + return nil, err } encryptedBLSStruct := encryptedBLSKeyJSONV3{ @@ -208,19 +226,9 @@ func (k *KeyPair) SaveToFile(path string, password string) error { } data, err := json.Marshal(encryptedBLSStruct) if err != nil { - return err - } - - dir := filepath.Dir(path) - if err := os.MkdirAll(dir, 0755); err != nil { - fmt.Println("Error creating directories:", err) - return err - } - err = os.WriteFile(path, data, 0644) - if err != nil { - return err + return nil, err } - return nil + return data, nil } func ReadPrivateKeyFromFile(path string, password string) (*KeyPair, error) {