mirror of
https://github.com/fiatjaf/nak.git
synced 2026-07-22 07:48:27 +00:00
Compare commits
10 Commits
v0.20.1
...
1403c7d2cd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1403c7d2cd | ||
|
|
6855722ba5 | ||
|
|
aac995714b | ||
|
|
a9df9c6a21 | ||
|
|
57be7627d4 | ||
|
|
5b7f3eb4db | ||
|
|
6d4f4c03ec | ||
|
|
eb9afd0b54 | ||
|
|
9c35de2b02 | ||
|
|
b65683886b |
18
count.go
18
count.go
@@ -63,11 +63,19 @@ var count = &cli.Command{
|
||||
hll = hyperloglog.New(offset)
|
||||
}
|
||||
for _, relayUrl := range relayUrls {
|
||||
relay, err := sys.Pool.EnsureRelay(relayUrl)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s%s: ", strings.Repeat(" ", biggerUrlSize-len(relayUrl)), relayUrl)
|
||||
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
||||
continue
|
||||
nm := nostr.NormalizeURL(relayUrl)
|
||||
relay, ok := sys.Pool.Relays.Load(nm)
|
||||
if !ok || relay == nil || !relay.IsConnected() {
|
||||
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
var err error
|
||||
relay, err = nostr.RelayConnect(ct, relayUrl, sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s%s: ", strings.Repeat(" ", biggerUrlSize-len(relayUrl)), relayUrl)
|
||||
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
||||
continue
|
||||
}
|
||||
sys.Pool.Relays.Store(nm, relay)
|
||||
}
|
||||
|
||||
count, hllRegisters, err := relay.Count(ctx, filter, nostr.SubscriptionOptions{
|
||||
|
||||
16
event.go
16
event.go
@@ -543,9 +543,19 @@ func publishFlow(ctx context.Context, c *cli.Command, kr nostr.Signer, evt nostr
|
||||
defer cancel()
|
||||
|
||||
if !relay.IsConnected() {
|
||||
if new_, err := sys.Pool.EnsureRelay(relay.URL); err == nil {
|
||||
relays[i] = new_
|
||||
relay = new_
|
||||
nm := nostr.NormalizeURL(relay.URL)
|
||||
if r, ok := sys.Pool.Relays.Load(nm); ok && r != nil && r.IsConnected() {
|
||||
relays[i] = r
|
||||
relay = r
|
||||
} else {
|
||||
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
new_, err := nostr.RelayConnect(ct, relay.URL, sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err == nil {
|
||||
sys.Pool.Relays.Store(nm, new_)
|
||||
relays[i] = new_
|
||||
relay = new_
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
2
fetch.go
2
fetch.go
@@ -101,7 +101,7 @@ var fetch = &cli.Command{
|
||||
sys.Hints.Save(authorHint, nostr.NormalizeURL(url), hints.LastInHint, nostr.Now())
|
||||
}
|
||||
|
||||
for _, url := range sys.FetchOutboxRelays(ctx, authorHint, 3) {
|
||||
for _, url := range sys.FetchOutboxRelays(ctx, authorHint, 6) {
|
||||
relays = append(relays, url)
|
||||
}
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module github.com/fiatjaf/nak
|
||||
go 1.25
|
||||
|
||||
require (
|
||||
fiatjaf.com/nostr v0.0.0-20260625215907-7a78ad0c459f
|
||||
fiatjaf.com/nostr v0.0.0-20260716191248-c205ed45b97e
|
||||
github.com/AlecAivazis/survey/v2 v2.3.7
|
||||
github.com/bep/debounce v1.2.1
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.6
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,7 +1,7 @@
|
||||
fiatjaf.com/lib v0.3.7 h1:mXZOn7NrUcjSdy4oNvwQyAmes7Ueb+Zr5hjqMIe2dxI=
|
||||
fiatjaf.com/lib v0.3.7/go.mod h1:UlHaZvPHj25PtKLh9GjZkUHRmQ2xZ8Jkoa4VRaLeeQ8=
|
||||
fiatjaf.com/nostr v0.0.0-20260625215907-7a78ad0c459f h1:n0G6XmIXS/j+AgDRFiZqK+l1R0ZahlPe/HwHlfxwj/c=
|
||||
fiatjaf.com/nostr v0.0.0-20260625215907-7a78ad0c459f/go.mod h1:b1EIUDnd133Ie8Pg8O/biaKdFyCMz28aD4n64g1GqvM=
|
||||
fiatjaf.com/nostr v0.0.0-20260716191248-c205ed45b97e h1:/vgoytiH4qQ28O/oX1hj656X69CnTy1pGj/17HFjH+A=
|
||||
fiatjaf.com/nostr v0.0.0-20260716191248-c205ed45b97e/go.mod h1:b1EIUDnd133Ie8Pg8O/biaKdFyCMz28aD4n64g1GqvM=
|
||||
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
|
||||
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
|
||||
github.com/FastFilter/xorfilter v0.2.1 h1:lbdeLG9BdpquK64ZsleBS8B4xO/QW1IM0gMzF7KaBKc=
|
||||
|
||||
103
group.go
103
group.go
@@ -29,7 +29,6 @@ var group = &cli.Command{
|
||||
ArgsUsage: "<subcommand> <relay>'<identifier> [flags]",
|
||||
Flags: combineFlags([][]cli.Flag{}),
|
||||
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
|
||||
|
||||
return ctx, nil
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
@@ -244,9 +243,17 @@ var group = &cli.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
r, err := sys.Pool.EnsureRelay(relay)
|
||||
if err != nil {
|
||||
return err
|
||||
nm := nostr.NormalizeURL(relay)
|
||||
r, ok := sys.Pool.Relays.Load(nm)
|
||||
if !ok || r == nil || !r.IsConnected() {
|
||||
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
var err error
|
||||
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sys.Pool.Relays.Store(nm, r)
|
||||
}
|
||||
|
||||
sub, err := r.Subscribe(ctx, nostr.Filter{
|
||||
@@ -317,11 +324,19 @@ var group = &cli.Command{
|
||||
return fmt.Errorf("failed to sign message: %w", err)
|
||||
}
|
||||
|
||||
if r, err := sys.Pool.EnsureRelay(relay); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return r.Publish(ctx, msg)
|
||||
nm := nostr.NormalizeURL(relay)
|
||||
r, ok := sys.Pool.Relays.Load(nm)
|
||||
if !ok || r == nil || !r.IsConnected() {
|
||||
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
var err error
|
||||
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sys.Pool.Relays.Store(nm, r)
|
||||
}
|
||||
return r.Publish(ctx, msg)
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -475,9 +490,17 @@ write your forum post
|
||||
return fmt.Errorf("failed to sign forum topic event: %w", err)
|
||||
}
|
||||
|
||||
r, err := sys.Pool.EnsureRelay(relay)
|
||||
if err != nil {
|
||||
return err
|
||||
nm := nostr.NormalizeURL(relay)
|
||||
r, ok := sys.Pool.Relays.Load(nm)
|
||||
if !ok || r == nil || !r.IsConnected() {
|
||||
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
var err error
|
||||
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sys.Pool.Relays.Store(nm, r)
|
||||
}
|
||||
|
||||
return r.Publish(ctx, evt)
|
||||
@@ -582,9 +605,17 @@ write your forum post
|
||||
return fmt.Errorf("failed to sign forum comment event: %w", err)
|
||||
}
|
||||
|
||||
r, err := sys.Pool.EnsureRelay(relay)
|
||||
if err != nil {
|
||||
return err
|
||||
nm := nostr.NormalizeURL(relay)
|
||||
r, ok := sys.Pool.Relays.Load(nm)
|
||||
if !ok || r == nil || !r.IsConnected() {
|
||||
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
var err error
|
||||
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sys.Pool.Relays.Store(nm, r)
|
||||
}
|
||||
|
||||
return r.Publish(ctx, evt)
|
||||
@@ -696,6 +727,18 @@ write your forum post
|
||||
Name: "all-kinds",
|
||||
Usage: "specify this to delete the supported_kinds property, meaning everything will be supported",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "parent",
|
||||
Usage: "set parent group identifier",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "children",
|
||||
Usage: "set child group identifiers (semicolon-separated)",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "orphan",
|
||||
Usage: "remove parent tag, making this group an orphan",
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
relay, identifier, err := parseGroupIdentifier(c)
|
||||
@@ -760,6 +803,18 @@ write your forum post
|
||||
} else if c.Bool("all-kinds") {
|
||||
group.SupportedKinds = nil
|
||||
}
|
||||
if parent := c.String("parent"); parent != "" {
|
||||
group.Parent = parent
|
||||
}
|
||||
if children := c.String("children"); children != "" {
|
||||
group.Children = strings.Split(children, ";")
|
||||
for i := range group.Children {
|
||||
group.Children[i] = strings.TrimSpace(group.Children[i])
|
||||
}
|
||||
}
|
||||
if c.Bool("orphan") {
|
||||
group.Parent = ""
|
||||
}
|
||||
|
||||
return publishModerationEvent(ctx, c, 9002, func(evt *nostr.Event, args []string) error {
|
||||
evt.Tags = append(evt.Tags, nostr.Tag{"name", group.Name})
|
||||
@@ -790,6 +845,12 @@ write your forum post
|
||||
}
|
||||
evt.Tags = append(evt.Tags, tag)
|
||||
}
|
||||
if group.Parent != "" {
|
||||
evt.Tags = append(evt.Tags, nostr.Tag{"parent", group.Parent})
|
||||
}
|
||||
for _, child := range group.Children {
|
||||
evt.Tags = append(evt.Tags, nostr.Tag{"child", child})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
},
|
||||
@@ -877,9 +938,17 @@ func publishModerationEvent(ctx context.Context, c *cli.Command, kind nostr.Kind
|
||||
|
||||
stdout(evt.String())
|
||||
|
||||
r, err := sys.Pool.EnsureRelay(relay)
|
||||
if err != nil {
|
||||
return err
|
||||
nm := nostr.NormalizeURL(relay)
|
||||
r, ok := sys.Pool.Relays.Load(nm)
|
||||
if !ok || r == nil || !r.IsConnected() {
|
||||
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
var err error
|
||||
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sys.Pool.Relays.Store(nm, r)
|
||||
}
|
||||
|
||||
return r.Publish(ctx, evt)
|
||||
|
||||
100
helpers.go
100
helpers.go
@@ -48,6 +48,8 @@ var (
|
||||
stdout = func(args ...any) { fmt.Fprintln(color.Output, args...) }
|
||||
)
|
||||
|
||||
var connectTimeout = 2 * time.Second
|
||||
|
||||
func isPiped() bool {
|
||||
stat, err := os.Stdin.Stat()
|
||||
if err != nil {
|
||||
@@ -268,63 +270,75 @@ func connectToSingleRelay(
|
||||
colorizepreamble func(c func(string, ...any) string),
|
||||
logthis func(s string, args ...any),
|
||||
) *nostr.Relay {
|
||||
if relay, err := sys.Pool.EnsureRelay(url); err == nil {
|
||||
if c.Bool("force-pre-auth") {
|
||||
if colorizepreamble != nil {
|
||||
colorizepreamble(color.YellowString)
|
||||
}
|
||||
logthis("waiting for auth challenge... ")
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
nm := nostr.NormalizeURL(url)
|
||||
|
||||
for range 5 {
|
||||
if err := relay.Auth(ctx, func(ctx context.Context, authEvent *nostr.Event) error {
|
||||
challengeTag := authEvent.Tags.Find("challenge")
|
||||
if challengeTag == nil || len(challengeTag) < 2 || challengeTag[1] == "" {
|
||||
return fmt.Errorf("auth not received yet *****") // what a giant hack
|
||||
}
|
||||
return authSigner(ctx, c, logthis, authEvent)
|
||||
}); err == nil {
|
||||
// auth succeeded
|
||||
goto preauthSuccess
|
||||
} else {
|
||||
// auth failed
|
||||
if strings.HasSuffix(err.Error(), "auth not received yet *****") {
|
||||
// it failed because we didn't receive the challenge yet, so keep waiting
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
} else {
|
||||
// it failed for some other reason, so skip this relay
|
||||
if colorizepreamble != nil {
|
||||
colorizepreamble(colors.errorf)
|
||||
}
|
||||
logthis(err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
relay, ok := sys.Pool.Relays.Load(nm)
|
||||
if !ok || relay == nil || !relay.IsConnected() {
|
||||
connectCtx, cancel := context.WithTimeout(context.Background(), connectTimeout)
|
||||
defer cancel()
|
||||
|
||||
var err error
|
||||
if relay, err = nostr.RelayConnect(connectCtx, url, sys.Pool.RelayOptions); err != nil {
|
||||
if colorizepreamble != nil {
|
||||
colorizepreamble(colors.errorf)
|
||||
}
|
||||
logthis("failed to get an AUTH challenge in enough time.")
|
||||
logthis(clampError(err, len(url)+12))
|
||||
return nil
|
||||
}
|
||||
|
||||
preauthSuccess:
|
||||
sys.Pool.Relays.Store(nm, relay)
|
||||
go func(r *nostr.Relay, relayURL string) {
|
||||
<-r.Context().Done()
|
||||
if current, ok := sys.Pool.Relays.Load(relayURL); ok && current == r {
|
||||
sys.Pool.Relays.Delete(relayURL)
|
||||
}
|
||||
}(relay, nm)
|
||||
}
|
||||
|
||||
if c.Bool("force-pre-auth") {
|
||||
if colorizepreamble != nil {
|
||||
colorizepreamble(colors.successf)
|
||||
colorizepreamble(color.YellowString)
|
||||
}
|
||||
logthis("waiting for auth challenge... ")
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
|
||||
for range 5 {
|
||||
if err := relay.Auth(ctx, func(ctx context.Context, authEvent *nostr.Event) error {
|
||||
challengeTag := authEvent.Tags.Find("challenge")
|
||||
if challengeTag == nil || len(challengeTag) < 2 || challengeTag[1] == "" {
|
||||
return fmt.Errorf("auth not received yet *****") // what a giant hack
|
||||
}
|
||||
return authSigner(ctx, c, logthis, authEvent)
|
||||
}); err == nil {
|
||||
// auth succeeded
|
||||
goto preauthSuccess
|
||||
} else {
|
||||
// auth failed
|
||||
if strings.HasSuffix(err.Error(), "auth not received yet *****") {
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
} else {
|
||||
if colorizepreamble != nil {
|
||||
colorizepreamble(colors.errorf)
|
||||
}
|
||||
logthis(err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
logthis("ok.")
|
||||
return relay
|
||||
} else {
|
||||
if colorizepreamble != nil {
|
||||
colorizepreamble(colors.errorf)
|
||||
}
|
||||
|
||||
// if we're here that means we've failed to connect, this may be a huge message
|
||||
// but we're likely to only be interested in the lowest level error (although we can leave space)
|
||||
logthis(clampError(err, len(url)+12))
|
||||
logthis("failed to get an AUTH challenge in enough time.")
|
||||
return nil
|
||||
}
|
||||
|
||||
preauthSuccess:
|
||||
if colorizepreamble != nil {
|
||||
colorizepreamble(colors.successf)
|
||||
}
|
||||
logthis("ok.")
|
||||
return relay
|
||||
}
|
||||
|
||||
func clearLines(lineCount int) {
|
||||
|
||||
16
key.go
16
key.go
@@ -57,7 +57,21 @@ var public = &cli.Command{
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
for sk := range getSecretKeysFromStdinLinesOrSlice(ctx, c, c.Args().Slice()) {
|
||||
args := c.Args().Slice()
|
||||
if len(args) == 0 && !isPiped() {
|
||||
kr, _, err := gatherKeyerFromArguments(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pk, err := kr.GetPublicKey(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get public key: %w", err)
|
||||
}
|
||||
stdout(pk.Hex())
|
||||
return nil
|
||||
}
|
||||
|
||||
for sk := range getSecretKeysFromStdinLinesOrSlice(ctx, c, args) {
|
||||
_, pk := btcec.PrivKeyFromBytes(sk[:])
|
||||
|
||||
if c.Bool("with-parity") {
|
||||
|
||||
15
main.go
15
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/nip42"
|
||||
@@ -42,6 +43,7 @@ var defaultKeyFlags = []cli.Flag{
|
||||
Name: "prompt-sec",
|
||||
Usage: "prompt the user to paste a hex or nsec with which to sign the event",
|
||||
Category: CATEGORY_SIGNER,
|
||||
Hidden: true,
|
||||
},
|
||||
&SecretKeyFlag{
|
||||
Name: "connect-as",
|
||||
@@ -50,6 +52,7 @@ var defaultKeyFlags = []cli.Flag{
|
||||
Sources: cli.EnvVars("NOSTR_CLIENT_KEY"),
|
||||
Value: defaultKey(),
|
||||
DefaultText: "the default key (see `nak key default`)",
|
||||
Hidden: true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -128,6 +131,7 @@ var app = &cli.Command{
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Hidden: true,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "verbose",
|
||||
@@ -141,6 +145,17 @@ var app = &cli.Command{
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Hidden: true,
|
||||
},
|
||||
&cli.DurationFlag{
|
||||
Name: "connect-timeout",
|
||||
Usage: "timeout for connecting to relays",
|
||||
Value: connectTimeout,
|
||||
Action: func(ctx context.Context, c *cli.Command, d time.Duration) error {
|
||||
connectTimeout = d
|
||||
return nil
|
||||
},
|
||||
Hidden: true,
|
||||
},
|
||||
},
|
||||
defaultKeyFlags,
|
||||
|
||||
@@ -996,10 +996,13 @@ func (r *NostrRoot) publishNote(path string) {
|
||||
}
|
||||
|
||||
for _, url := range relays {
|
||||
relay, err := r.sys.Pool.EnsureRelay(url)
|
||||
connectCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
relay, err := nostr.RelayConnect(connectCtx, url, r.sys.Pool.RelayOptions)
|
||||
cancel()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
r.sys.Pool.Relays.Store(nostr.NormalizeURL(url), relay)
|
||||
relay.Publish(ctx, *evt)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user