refactoring, added documentation
This commit is contained in:
9
keys.go
9
keys.go
@@ -2,10 +2,11 @@ package roots
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||
)
|
||||
|
||||
// GeneratePrivateKey generates a new, random secp256k1 private key and returns
|
||||
// it as a 64-character, lowercase hexadecimal string.
|
||||
func GeneratePrivateKey() (string, error) {
|
||||
sk, err := secp256k1.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
@@ -15,13 +16,15 @@ func GeneratePrivateKey() (string, error) {
|
||||
return hex.EncodeToString(skBytes), nil
|
||||
}
|
||||
|
||||
// GetPublicKey derives the public key from a private key hex string
|
||||
// and returns the x-coordinate as 64 lowercase hex characters.
|
||||
func GetPublicKey(privateKeyHex string) (string, error) {
|
||||
if len(privateKeyHex) != 64 {
|
||||
return "", fmt.Errorf("private key must be 64 hex characters")
|
||||
return "", ErrMalformedPrivKey
|
||||
}
|
||||
skBytes, err := hex.DecodeString(privateKeyHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid private key hex: %w", err)
|
||||
return "", ErrMalformedPrivKey
|
||||
}
|
||||
|
||||
pk := secp256k1.PrivKeyFromBytes(skBytes).PubKey()
|
||||
|
||||
Reference in New Issue
Block a user