use schnorr from btcec instead of the bip340 library.

This commit is contained in:
fiatjaf
2022-05-01 15:10:18 -03:00
parent c0a2be91a1
commit 00ad1acde9
5 changed files with 87 additions and 40 deletions

28
keys.go
View File

@@ -1,21 +1,39 @@
package nostr
import (
"crypto/rand"
"encoding/hex"
"io"
"math/big"
"github.com/fiatjaf/bip340"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
)
func GeneratePrivateKey() string {
return hex.EncodeToString(bip340.GeneratePrivateKey().Bytes())
params := btcec.S256().Params()
one := new(big.Int).SetInt64(1)
b := make([]byte, params.BitSize/8+8)
_, err := io.ReadFull(rand.Reader, b)
if 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 hex.EncodeToString(k.Bytes())
}
func GetPublicKey(sk string) (string, error) {
privateKey, err := bip340.ParsePrivateKey(sk)
b, err := hex.DecodeString(sk)
if err != nil {
return "", err
}
publicKey := bip340.GetPublicKey(privateKey)
return hex.EncodeToString(publicKey[:]), nil
_, pk := btcec.PrivKeyFromBytes(b)
return hex.EncodeToString(schnorr.SerializePubKey(pk)), nil
}