a bunch of [32]byte conversions. still more needed.
This commit is contained in:
34
signature.go
34
signature.go
@@ -4,7 +4,6 @@ package nostr
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
@@ -16,22 +15,13 @@ import (
|
||||
// If the signature is invalid bool will be false and err will be set.
|
||||
func (evt Event) CheckSignature() (bool, error) {
|
||||
// read and check pubkey
|
||||
pk, err := hex.DecodeString(evt.PubKey)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("event pubkey '%s' is invalid hex: %w", evt.PubKey, err)
|
||||
}
|
||||
|
||||
pubkey, err := schnorr.ParsePubKey(pk)
|
||||
pubkey, err := schnorr.ParsePubKey(evt.PubKey[:])
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("event has invalid pubkey '%s': %w", evt.PubKey, err)
|
||||
}
|
||||
|
||||
// read signature
|
||||
s, err := hex.DecodeString(evt.Sig)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("signature '%s' is invalid hex: %w", evt.Sig, err)
|
||||
}
|
||||
sig, err := schnorr.ParseSignature(s)
|
||||
sig, err := schnorr.ParseSignature(evt.Sig[:])
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to parse signature: %w", err)
|
||||
}
|
||||
@@ -42,21 +32,18 @@ func (evt Event) CheckSignature() (bool, error) {
|
||||
}
|
||||
|
||||
// Sign signs an event with a given privateKey.
|
||||
//
|
||||
// It sets the event's ID, PubKey, and Sig fields.
|
||||
//
|
||||
// Returns an error if the private key is invalid or if signing fails.
|
||||
func (evt *Event) Sign(secretKey string) error {
|
||||
s, err := hex.DecodeString(secretKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Sign called with invalid secret key '%s': %w", secretKey, err)
|
||||
}
|
||||
|
||||
func (evt *Event) Sign(secretKey [32]byte) error {
|
||||
if evt.Tags == nil {
|
||||
evt.Tags = make(Tags, 0)
|
||||
}
|
||||
|
||||
sk, pk := btcec.PrivKeyFromBytes(s)
|
||||
pkBytes := pk.SerializeCompressed()
|
||||
evt.PubKey = hex.EncodeToString(pkBytes[1:])
|
||||
sk, pk := btcec.PrivKeyFromBytes(secretKey[:])
|
||||
pkBytes := pk.SerializeCompressed()[1:]
|
||||
evt.PubKey = [32]byte(pkBytes)
|
||||
|
||||
h := sha256.Sum256(evt.Serialize())
|
||||
sig, err := schnorr.Sign(sk, h[:], schnorr.FastSign())
|
||||
@@ -64,8 +51,9 @@ func (evt *Event) Sign(secretKey string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
evt.ID = hex.EncodeToString(h[:])
|
||||
evt.Sig = hex.EncodeToString(sig.Serialize())
|
||||
evt.ID = h
|
||||
sigb := sig.Serialize()
|
||||
evt.Sig = [64]byte(sigb)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user