Completed event and key libraries.
This commit is contained in:
30
keys.go
Normal file
30
keys.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
||||
)
|
||||
|
||||
func GeneratePrivateKey() (string, error) {
|
||||
sk, err := secp256k1.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
skBytes := sk.Serialize()
|
||||
return hex.EncodeToString(skBytes), nil
|
||||
}
|
||||
|
||||
func GetPublicKey(privateKeyHex string) (string, error) {
|
||||
if len(privateKeyHex) != 64 {
|
||||
return "", fmt.Errorf("private key must be 64 hex characters")
|
||||
}
|
||||
skBytes, err := hex.DecodeString(privateKeyHex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid private key hex: %w", err)
|
||||
}
|
||||
|
||||
pk := secp256k1.PrivKeyFromBytes(skBytes).PubKey()
|
||||
pkBytes := pk.SerializeCompressed()[1:]
|
||||
return hex.EncodeToString(pkBytes), nil
|
||||
}
|
||||
Reference in New Issue
Block a user