@@ -25,8 +25,8 @@ import (
25
25
"crypto/sha256"
26
26
"errors"
27
27
"fmt"
28
- "io/ioutil"
29
28
"math"
29
+ "os"
30
30
"sort"
31
31
32
32
"filippo.io/edwards25519"
@@ -75,27 +75,34 @@ func ValidatePrivateKey(b []byte) (bool, error) {
75
75
// check if the public key is on the ed25519 curve
76
76
pub := ed25519 .PrivateKey (b ).Public ().(ed25519.PublicKey )
77
77
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" )
79
79
}
80
80
return true , nil
81
81
}
82
82
83
83
func PrivateKeyFromSolanaKeygenFile (file string ) (PrivateKey , error ) {
84
- content , err := ioutil .ReadFile (file )
84
+ content , err := os .ReadFile (file )
85
85
if err != nil {
86
86
return nil , fmt .Errorf ("read keygen file: %w" , err )
87
87
}
88
+ return PrivateKeyFromSolanaKeygenFileBytes (content )
89
+ }
88
90
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 )
91
94
if err != nil {
92
95
return nil , fmt .Errorf ("decode keygen file: %w" , err )
93
96
}
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 {
96
103
return nil , fmt .Errorf ("invalid private key: %w" , err )
97
104
}
98
- return PrivateKey ( privateKeyBytes ) , nil
105
+ return prk , nil
99
106
}
100
107
101
108
func (k PrivateKey ) String () string {
@@ -726,7 +733,7 @@ func GetAssociatedAuthority(programID PublicKey, marketAddr PublicKey) (PublicKe
726
733
bumpSeed := uint8 (0 )
727
734
endSeed := []byte {0 , 0 , 0 , 0 , 0 , 0 , 0 }
728
735
for bumpSeed < 100 {
729
- address , err = CreateProgramAddress ([][]byte {marketAddr [:], [] byte {byte (bumpSeed )}, endSeed }, programID )
736
+ address , err = CreateProgramAddress ([][]byte {marketAddr [:], {byte (bumpSeed )}, endSeed }, programID )
730
737
if err == nil {
731
738
return address , bumpSeed , nil
732
739
}
0 commit comments