well-goknown/vendor/github.com/nbd-wtf/go-nostr/keys.go

56 lines
1.1 KiB
Go
Raw Normal View History

package nostr
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"math/big"
"strings"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
)
func GeneratePrivateKey() string {
params := btcec.S256().Params()
one := new(big.Int).SetInt64(1)
b := make([]byte, params.BitSize/8+8)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return ""
}
k := new(big.Int).SetBytes(b)
n := new(big.Int).Sub(params.N, one)
k.Mod(k, n)
k.Add(k, one)
return fmt.Sprintf("%064x", k.Bytes())
}
func GetPublicKey(sk string) (string, error) {
b, err := hex.DecodeString(sk)
if err != nil {
return "", err
}
_, pk := btcec.PrivKeyFromBytes(b)
return hex.EncodeToString(schnorr.SerializePubKey(pk)), nil
}
// Deprecated: use IsValid32ByteHex instead -- functionality unchanged.
func IsValidPublicKeyHex(pk string) bool {
if strings.ToLower(pk) != pk {
return false
}
dec, _ := hex.DecodeString(pk)
return len(dec) == 32
}
func IsValidPublicKey(pk string) bool {
v, _ := hex.DecodeString(pk)
_, err := schnorr.ParsePubKey(v)
return err == nil
}