Skip to content

Commit 401a213

Browse files
committed
fetch: support --auth.
1 parent b41260f commit 401a213

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

fetch.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"fiatjaf.com/nostr"
89
"fiatjaf.com/nostr/nip05"
910
"fiatjaf.com/nostr/nip19"
11+
"fiatjaf.com/nostr/nip42"
1012
"fiatjaf.com/nostr/sdk/hints"
13+
"github.com/fatih/color"
1114
"github.com/urfave/cli/v3"
1215
)
1316

@@ -18,12 +21,18 @@ var fetch = &cli.Command{
1821
nak fetch nevent1qqsxrwm0hd3s3fddh4jc2574z3xzufq6qwuyz2rvv3n087zvym3dpaqprpmhxue69uhhqatzd35kxtnjv4kxz7tfdenju6t0xpnej4
1922
echo npub1h8spmtw9m2huyv6v2j2qd5zv956z2zdugl6mgx02f2upffwpm3nqv0j4ps | nak fetch --relay wss://relay.nostr.band`,
2023
DisableSliceFlagSeparator: true,
21-
Flags: append(reqFilterFlags,
22-
&cli.StringSliceFlag{
23-
Name: "relay",
24-
Aliases: []string{"r"},
25-
Usage: "also use these relays to fetch from",
26-
},
24+
Flags: append(defaultKeyFlags,
25+
append(reqFilterFlags,
26+
&cli.StringSliceFlag{
27+
Name: "relay",
28+
Aliases: []string{"r"},
29+
Usage: "also use these relays to fetch from",
30+
},
31+
&cli.BoolFlag{
32+
Name: "auth",
33+
Usage: "always perform nip42 \"AUTH\" when facing an \"auth-required: \" rejection and try again",
34+
},
35+
)...,
2736
),
2837
ArgsUsage: "[nip05_or_nip19_code]",
2938
Action: func(ctx context.Context, c *cli.Command) error {
@@ -106,6 +115,19 @@ var fetch = &cli.Command{
106115
continue
107116
}
108117

118+
sys.Pool.AuthRequiredHandler = func(ctx context.Context, authEvent *nostr.Event) error {
119+
return authSigner(ctx, c, func(s string, args ...any) {
120+
if strings.HasPrefix(s, "authenticating as") {
121+
cleanUrl, _ := strings.CutPrefix(
122+
nip42.GetRelayURLFromAuthEvent(*authEvent),
123+
"wss://",
124+
)
125+
s = "authenticating to " + color.CyanString(cleanUrl) + " as" + s[len("authenticating as"):]
126+
}
127+
log(s+"\n", args...)
128+
}, authEvent)
129+
}
130+
109131
found := false
110132
for ie := range sys.Pool.FetchMany(ctx, relays, filter, nostr.SubscriptionOptions{
111133
Label: "nak-fetch",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/fiatjaf/nak
33
go 1.25
44

55
require (
6-
fiatjaf.com/nostr v0.0.0-20260508155207-03a55cc0b816
6+
fiatjaf.com/nostr v0.0.0-20260602223326-015842e96d86
77
github.com/AlecAivazis/survey/v2 v2.3.7
88
github.com/bep/debounce v1.2.1
99
github.com/btcsuite/btcd/btcec/v2 v2.3.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fiatjaf.com/lib v0.3.7 h1:mXZOn7NrUcjSdy4oNvwQyAmes7Ueb+Zr5hjqMIe2dxI=
22
fiatjaf.com/lib v0.3.7/go.mod h1:UlHaZvPHj25PtKLh9GjZkUHRmQ2xZ8Jkoa4VRaLeeQ8=
3-
fiatjaf.com/nostr v0.0.0-20260508155207-03a55cc0b816 h1:96hiRZUwfiL1Nf3yp0G2TBM85EVZ0deJLl/bVrJy2Gs=
4-
fiatjaf.com/nostr v0.0.0-20260508155207-03a55cc0b816/go.mod h1:b1EIUDnd133Ie8Pg8O/biaKdFyCMz28aD4n64g1GqvM=
3+
fiatjaf.com/nostr v0.0.0-20260602223326-015842e96d86 h1:p3HnX1UDT/CfiMvTc4yTcxHQm08ri7DM32P1uKkFNKg=
4+
fiatjaf.com/nostr v0.0.0-20260602223326-015842e96d86/go.mod h1:b1EIUDnd133Ie8Pg8O/biaKdFyCMz28aD4n64g1GqvM=
55
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
66
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
77
github.com/FastFilter/xorfilter v0.2.1 h1:lbdeLG9BdpquK64ZsleBS8B4xO/QW1IM0gMzF7KaBKc=

0 commit comments

Comments
 (0)