fix(helpers): add timeout and verbose logging for bunker connection

- Add a 10-second timeout to the bunker connection process using context
- Include detailed verbose logging for debugging.
This commit is contained in:
Anthony Accioly 2025-06-20 23:11:12 +01:00 committed by fiatjaf
parent 35ea2582d8
commit bd5569955c
1 changed files with 13 additions and 0 deletions

View File

@ -2,9 +2,11 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
"fiatjaf.com/nostr/keyer" "fiatjaf.com/nostr/keyer"
@ -75,10 +77,21 @@ func gatherSecretKeyOrBunkerFromArguments(ctx context.Context, c *cli.Command) (
clientKey = nostr.Generate() clientKey = nostr.Generate()
} }
logverbose("[nip46]: connecting to bunker %s with client key %s", bunkerURL, clientKey.Hex())
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
bunker, err := nip46.ConnectBunker(ctx, clientKey, bunkerURL, nil, func(s string) { bunker, err := nip46.ConnectBunker(ctx, clientKey, bunkerURL, nil, func(s string) {
log(color.CyanString("[nip46]: open the following URL: %s"), s) log(color.CyanString("[nip46]: open the following URL: %s"), s)
}) })
if err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
err = fmt.Errorf("timeout waiting for bunker to respond: %w", err)
}
return nostr.SecretKey{}, nil, fmt.Errorf("failed to connect to bunker %s: %w", bunkerURL, err)
}
return nostr.SecretKey{}, bunker, err return nostr.SecretKey{}, bunker, err
} }