mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-10-09 20:33:27 +02:00
move stuff back from nostr package to top level.
because otherwise the path must be specified as github.com/fiatjaf/go-nostr/nostr, which is annoying.
This commit is contained in:
39
keys.go
Normal file
39
keys.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"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)
|
||||
_, 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) {
|
||||
b, err := hex.DecodeString(sk)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, pk := btcec.PrivKeyFromBytes(b)
|
||||
return hex.EncodeToString(schnorr.SerializePubKey(pk)), nil
|
||||
}
|
Reference in New Issue
Block a user