Refactor crypto package to use p256k1 signer
Some checks failed
Go / build (push) Has been cancelled
Go / release (push) Has been cancelled

- 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.
This commit is contained in:
2025-11-03 10:21:31 +00:00
parent edcdec9c7e
commit 2614b51068
50 changed files with 312 additions and 972 deletions

View File

@@ -36,6 +36,19 @@ func NewClient(url string) (c *Client, err error) {
cancel()
return
}
// Set up ping/pong handling to keep connection alive
pongWait := 60 * time.Second
conn.SetReadDeadline(time.Now().Add(pongWait))
// Set pong handler to extend deadline when pongs are received
// Note: Relay sends pings, gorilla/websocket auto-responds with pongs
// The relay typically doesn't send pongs back, so we also handle timeouts in readLoop
conn.SetPongHandler(func(string) error {
conn.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
// Don't set ping handler - let gorilla/websocket auto-respond to pings
c = &Client{
conn: conn,
url: url,
@@ -78,16 +91,41 @@ func (c *Client) Send(msg interface{}) (err error) {
// readLoop reads messages from the relay and routes them to subscriptions.
func (c *Client) readLoop() {
defer c.conn.Close()
pongWait := 60 * time.Second
for {
select {
case <-c.ctx.Done():
return
default:
}
// Don't set deadline here - let pong handler manage it
// SetReadDeadline is called initially in NewClient and extended by pong handler
_, msg, err := c.conn.ReadMessage()
if err != nil {
// Check if context is done
select {
case <-c.ctx.Done():
return
default:
}
// Check if it's a timeout - connection might still be alive
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
// Pong handler should have extended deadline, but if we timeout,
// reset it and continue - connection might still be alive
// This can happen during idle periods when no messages are received
c.conn.SetReadDeadline(time.Now().Add(pongWait))
// Continue reading - connection should still be alive if pings/pongs are working
continue
}
// For other errors, check if it's a close error
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
return
}
// For other errors, return (connection is likely dead)
return
}
// Extend read deadline on successful read
c.conn.SetReadDeadline(time.Now().Add(pongWait))
var raw []interface{}
if err = json.Unmarshal(msg, &raw); err != nil {
continue

View File

@@ -6,7 +6,7 @@ import (
"time"
"lol.mleku.dev/chk"
"next.orly.dev/pkg/crypto/p256k"
p256k1signer "p256k1.mleku.dev/signer"
"next.orly.dev/pkg/encoders/bech32encoding"
"next.orly.dev/pkg/encoders/event"
"next.orly.dev/pkg/encoders/hex"
@@ -16,7 +16,7 @@ import (
// KeyPair represents a test keypair.
type KeyPair struct {
Secret *p256k.Signer
Secret *p256k1signer.P256K1Signer
Pubkey []byte
Nsec string
Npub string
@@ -25,7 +25,7 @@ type KeyPair struct {
// GenerateKeyPair generates a new keypair for testing.
func GenerateKeyPair() (kp *KeyPair, err error) {
kp = &KeyPair{}
kp.Secret = &p256k.Signer{}
kp.Secret = p256k1signer.NewP256K1Signer()
if err = kp.Secret.Generate(); chk.E(err) {
return
}
@@ -44,7 +44,7 @@ func GenerateKeyPair() (kp *KeyPair, err error) {
}
// CreateEvent creates a signed event with the given parameters.
func CreateEvent(signer *p256k.Signer, kindNum uint16, content string, tags *tag.S) (ev *event.E, err error) {
func CreateEvent(signer *p256k1signer.P256K1Signer, kindNum uint16, content string, tags *tag.S) (ev *event.E, err error) {
ev = event.New()
ev.CreatedAt = time.Now().Unix()
ev.Kind = kindNum
@@ -61,7 +61,7 @@ func CreateEvent(signer *p256k.Signer, kindNum uint16, content string, tags *tag
}
// CreateEventWithTags creates an event with specific tags.
func CreateEventWithTags(signer *p256k.Signer, kindNum uint16, content string, tagPairs [][]string) (ev *event.E, err error) {
func CreateEventWithTags(signer *p256k1signer.P256K1Signer, kindNum uint16, content string, tagPairs [][]string) (ev *event.E, err error) {
tags := tag.NewS()
for _, pair := range tagPairs {
if len(pair) >= 2 {
@@ -78,17 +78,17 @@ func CreateEventWithTags(signer *p256k.Signer, kindNum uint16, content string, t
}
// CreateReplaceableEvent creates a replaceable event (kind 0-3, 10000-19999).
func CreateReplaceableEvent(signer *p256k.Signer, kindNum uint16, content string) (ev *event.E, err error) {
func CreateReplaceableEvent(signer *p256k1signer.P256K1Signer, kindNum uint16, content string) (ev *event.E, err error) {
return CreateEvent(signer, kindNum, content, nil)
}
// CreateEphemeralEvent creates an ephemeral event (kind 20000-29999).
func CreateEphemeralEvent(signer *p256k.Signer, kindNum uint16, content string) (ev *event.E, err error) {
func CreateEphemeralEvent(signer *p256k1signer.P256K1Signer, kindNum uint16, content string) (ev *event.E, err error) {
return CreateEvent(signer, kindNum, content, nil)
}
// CreateDeleteEvent creates a deletion event (kind 5).
func CreateDeleteEvent(signer *p256k.Signer, eventIDs [][]byte, reason string) (ev *event.E, err error) {
func CreateDeleteEvent(signer *p256k1signer.P256K1Signer, eventIDs [][]byte, reason string) (ev *event.E, err error) {
tags := tag.NewS()
for _, id := range eventIDs {
// e tags must contain hex-encoded event IDs
@@ -101,7 +101,7 @@ func CreateDeleteEvent(signer *p256k.Signer, eventIDs [][]byte, reason string) (
}
// CreateParameterizedReplaceableEvent creates a parameterized replaceable event (kind 30000-39999).
func CreateParameterizedReplaceableEvent(signer *p256k.Signer, kindNum uint16, content string, dTag string) (ev *event.E, err error) {
func CreateParameterizedReplaceableEvent(signer *p256k1signer.P256K1Signer, kindNum uint16, content string, dTag string) (ev *event.E, err error) {
tags := tag.NewS()
tags.Append(tag.NewFromBytesSlice([]byte("d"), []byte(dTag)))
return CreateEvent(signer, kindNum, content, tags)