Skip to content

Commit

Permalink
refactor: encrypt bls key to json string (#316)
Browse files Browse the repository at this point in the history
Co-authored-by: Madhur Shrimal <[email protected]>
  • Loading branch information
renlulu and shrimalmadhur authored Aug 6, 2024
1 parent f322db0 commit dc23135
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions crypto/bls/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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{
Expand All @@ -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) {
Expand Down

0 comments on commit dc23135

Please sign in to comment.