refactoring, added documentation

This commit is contained in:
Jay
2025-10-23 16:16:27 -04:00
parent e0c669351c
commit 205aafcfe5
13 changed files with 342 additions and 228 deletions

View File

@@ -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()