Files
next.orly.dev/pkg/encoders/event/signatures.go
mleku 2614b51068
Some checks failed
Go / build (push) Has been cancelled
Go / release (push) Has been cancelled
Refactor crypto package to use p256k1 signer
- Replaced the p256k package with p256k1.mleku.dev/signer across the codebase, updating all instances where the previous signer was utilized.
- Removed the deprecated p256k package, including all related files and tests, to streamline the codebase and improve maintainability.
- Updated various components, including event handling, database interactions, and protocol implementations, to ensure compatibility with the new signer interface.
- Enhanced tests to validate the new signing functionality and ensure robustness across the application.
- Bumped version to v0.23.3 to reflect these changes.
2025-11-03 10:21:31 +00:00

49 lines
1.3 KiB
Go

package event
import (
"lol.mleku.dev/chk"
"lol.mleku.dev/errorf"
"lol.mleku.dev/log"
p256k1signer "p256k1.mleku.dev/signer"
"next.orly.dev/pkg/interfaces/signer"
"next.orly.dev/pkg/utils"
)
// Sign the event using the signer.I. Uses github.com/bitcoin-core/secp256k1 if
// available for much faster signatures.
//
// Note that this only populates the Pubkey, ID and Sig. The caller must
// set the CreatedAt timestamp as intended.
func (ev *E) Sign(keys signer.I) (err error) {
ev.Pubkey = keys.Pub()
ev.ID = ev.GetIDBytes()
if ev.Sig, err = keys.Sign(ev.ID); chk.E(err) {
return
}
return
}
// Verify an event is signed by the pubkey it contains. Uses
// github.com/bitcoin-core/secp256k1 if available for faster verification.
func (ev *E) Verify() (valid bool, err error) {
keys := p256k1signer.NewP256K1Signer()
if err = keys.InitPub(ev.Pubkey); chk.E(err) {
return
}
if valid, err = keys.Verify(ev.ID, ev.Sig); chk.T(err) {
// check that this isn't because of a bogus ID
id := ev.GetIDBytes()
if !utils.FastEqual(id, ev.ID) {
log.E.Ln("event Subscription incorrect")
ev.ID = id
err = nil
if valid, err = keys.Verify(ev.ID, ev.Sig); chk.E(err) {
return
}
err = errorf.W("event Subscription incorrect but signature is valid on correct Subscription")
}
return
}
return
}