Skip to content

Commit

Permalink
refactor: write encrypt bls key to string
Browse files Browse the repository at this point in the history
  • Loading branch information
renlulu committed Aug 6, 2024
1 parent 67787e9 commit b5c83e0
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 b5c83e0

Please sign in to comment.