just some basic bip340 key functions instead.

This commit is contained in:
fiatjaf
2022-01-06 08:24:20 -03:00
parent 8558025305
commit 275020a7ef
3 changed files with 12 additions and 559 deletions

32
keys.go
View File

@@ -1,37 +1,21 @@
package nostr
import (
"crypto/ecdsa"
"encoding/hex"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/tyler-smith/go-bip39"
"github.com/fiatjaf/bip340"
)
func PrivateKeyAsHex(key *ecdsa.PrivateKey) string {
skBytes := crypto.FromECDSA(key)
return hex.EncodeToString(skBytes)
func GeneratePrivateKey() string {
return hex.EncodeToString(bip340.GeneratePrivateKey().Bytes())
}
func PrivateKeyAsMnemonic(key *ecdsa.PrivateKey) (string, error) {
skBytes := crypto.FromECDSA(key)
return bip39.NewMnemonic(skBytes)
}
func PublicKeyAsHex(key ecdsa.PublicKey) string {
return hex.EncodeToString(secp256k1.CompressPubkey(key.X, key.Y))
}
func GenerateKey() (*ecdsa.PrivateKey, error) {
return crypto.GenerateKey()
}
func PrivateKeyFromMnemonic(mnemonic string) (*ecdsa.PrivateKey, error) {
e, err := bip39.EntropyFromMnemonic(mnemonic)
func GetPublicKey(sk string) (string, error) {
privateKey, err := bip340.ParsePrivateKey(sk)
if err != nil {
return &ecdsa.PrivateKey{}, err
return "", err
}
return crypto.ToECDSA(e)
publicKey := bip340.GetPublicKey(privateKey)
return hex.EncodeToString(publicKey[:]), nil
}