aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/crypto.go
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/crypto.go')
-rw-r--r--crypto/crypto.go29
1 files changed, 17 insertions, 12 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 9a1559fbf..9865c87c4 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -68,10 +68,8 @@ func Ripemd160(data []byte) []byte {
return ripemd.Sum(nil)
}
-func Ecrecover(hash, sig []byte) []byte {
- r, _ := secp256k1.RecoverPubkey(hash, sig)
-
- return r
+func Ecrecover(hash, sig []byte) ([]byte, error) {
+ return secp256k1.RecoverPubkey(hash, sig)
}
// New methods using proper ecdsa keys from the stdlib
@@ -123,7 +121,7 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
// LoadECDSA loads a secp256k1 private key from the given file.
func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
- buf := make([]byte, 32)
+ buf := make([]byte, 64)
fd, err := os.Open(file)
if err != nil {
return nil, err
@@ -132,27 +130,34 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
if _, err := io.ReadFull(fd, buf); err != nil {
return nil, err
}
- return ToECDSA(buf), nil
+
+ key, err := hex.DecodeString(string(buf))
+ if err != nil {
+ return nil, err
+ }
+
+ return ToECDSA(key), nil
}
// SaveECDSA saves a secp256k1 private key to the given file with restrictive
// permissions
func SaveECDSA(file string, key *ecdsa.PrivateKey) error {
- return ioutil.WriteFile(file, FromECDSA(key), 0600)
+ k := hex.EncodeToString(FromECDSA(key))
+ return ioutil.WriteFile(file, []byte(k), 0600)
}
func GenerateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(S256(), rand.Reader)
}
-func SigToPub(hash, sig []byte) *ecdsa.PublicKey {
- s := Ecrecover(hash, sig)
- if s == nil || len(s) != 65 {
- return nil
+func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
+ s, err := Ecrecover(hash, sig)
+ if err != nil {
+ return nil, err
}
x, y := elliptic.Unmarshal(S256(), s)
- return &ecdsa.PublicKey{S256(), x, y}
+ return &ecdsa.PublicKey{S256(), x, y}, nil
}
func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {