Skip to content

Commit 19cb601

Browse files
committed
Cleanup
1 parent 53553a1 commit 19cb601

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

keys.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"crypto/sha256"
2626
"errors"
2727
"fmt"
28-
"io/ioutil"
2928
"math"
29+
"os"
3030
"sort"
3131

3232
"filippo.io/edwards25519"
@@ -75,27 +75,34 @@ func ValidatePrivateKey(b []byte) (bool, error) {
7575
// check if the public key is on the ed25519 curve
7676
pub := ed25519.PrivateKey(b).Public().(ed25519.PublicKey)
7777
if !IsOnCurve(pub) {
78-
return false, errors.New("the corresponding public key is not on the ed25519 curve")
78+
return false, errors.New("the corresponding public key is NOT on the ed25519 curve")
7979
}
8080
return true, nil
8181
}
8282

8383
func PrivateKeyFromSolanaKeygenFile(file string) (PrivateKey, error) {
84-
content, err := ioutil.ReadFile(file)
84+
content, err := os.ReadFile(file)
8585
if err != nil {
8686
return nil, fmt.Errorf("read keygen file: %w", err)
8787
}
88+
return PrivateKeyFromSolanaKeygenFileBytes(content)
89+
}
8890

89-
var privateKeyBytes []byte
90-
err = json.Unmarshal(content, &privateKeyBytes)
91+
func PrivateKeyFromSolanaKeygenFileBytes(content []byte) (PrivateKey, error) {
92+
var values []byte
93+
err := json.Unmarshal(content, &values)
9194
if err != nil {
9295
return nil, fmt.Errorf("decode keygen file: %w", err)
9396
}
94-
95-
if _, err := ValidatePrivateKey(privateKeyBytes); err != nil {
97+
// check private key length, should be 64 bytes (TODO: are private keys always 64 bytes?)
98+
if len(values) != 64 {
99+
return nil, fmt.Errorf("invalid private key length %d", len(values))
100+
}
101+
prk := PrivateKey([]byte(values))
102+
if _, err := ValidatePrivateKey(prk); err != nil {
96103
return nil, fmt.Errorf("invalid private key: %w", err)
97104
}
98-
return PrivateKey(privateKeyBytes), nil
105+
return prk, nil
99106
}
100107

101108
func (k PrivateKey) String() string {
@@ -726,7 +733,7 @@ func GetAssociatedAuthority(programID PublicKey, marketAddr PublicKey) (PublicKe
726733
bumpSeed := uint8(0)
727734
endSeed := []byte{0, 0, 0, 0, 0, 0, 0}
728735
for bumpSeed < 100 {
729-
address, err = CreateProgramAddress([][]byte{marketAddr[:], []byte{byte(bumpSeed)}, endSeed}, programID)
736+
address, err = CreateProgramAddress([][]byte{marketAddr[:], {byte(bumpSeed)}, endSeed}, programID)
730737
if err == nil {
731738
return address, bumpSeed, nil
732739
}

0 commit comments

Comments
 (0)