a bunch of [32]byte conversions. still more needed.
This commit is contained in:
41
keys.go
41
keys.go
@@ -2,48 +2,27 @@ package nostr
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
|
||||
"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 ""
|
||||
func GeneratePrivateKey() [32]byte {
|
||||
var sk [32]byte
|
||||
if _, err := io.ReadFull(rand.Reader, sk[:]); err != nil {
|
||||
panic(fmt.Errorf("failed to read random bytes when generating private key"))
|
||||
}
|
||||
|
||||
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())
|
||||
return sk
|
||||
}
|
||||
|
||||
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
|
||||
func GetPublicKey(sk [32]byte) PubKey {
|
||||
_, pk := btcec.PrivKeyFromBytes(sk[:])
|
||||
return [32]byte(pk.SerializeCompressed()[1:])
|
||||
}
|
||||
|
||||
func IsValidPublicKey(pk string) bool {
|
||||
if !isLowerHex(pk) {
|
||||
return false
|
||||
}
|
||||
|
||||
v, _ := hex.DecodeString(pk)
|
||||
_, err := schnorr.ParsePubKey(v)
|
||||
func IsValidPublicKey(pk [32]byte) bool {
|
||||
_, err := schnorr.ParsePubKey(pk[:])
|
||||
return err == nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user