accept npub/nprofile/nevent instead of just hex in flags.

This commit is contained in:
fiatjaf
2025-11-11 15:58:53 -03:00
parent 210c0aa282
commit bef3739a67
7 changed files with 56 additions and 14 deletions

View File

@@ -7,7 +7,6 @@ import (
"io" "io"
"os" "os"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/keyer" "fiatjaf.com/nostr/keyer"
"fiatjaf.com/nostr/nipb0/blossom" "fiatjaf.com/nostr/nipb0/blossom"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
@@ -38,11 +37,11 @@ var blossomCmd = &cli.Command{
var client *blossom.Client var client *blossom.Client
pubkey := c.Args().First() pubkey := c.Args().First()
if pubkey != "" { if pubkey != "" {
if pk, err := nostr.PubKeyFromHex(pubkey); err != nil { pk, err := parsePubKey(pubkey)
if err != nil {
return fmt.Errorf("invalid public key '%s': %w", pubkey, err) return fmt.Errorf("invalid public key '%s': %w", pubkey, err)
} else {
client = blossom.NewClient(c.String("server"), keyer.NewReadOnlySigner(pk))
} }
client = blossom.NewClient(c.String("server"), keyer.NewReadOnlySigner(pk))
} else { } else {
var err error var err error
client, err = getBlossomClient(ctx, c) client, err = getBlossomClient(ctx, c)

View File

@@ -21,7 +21,7 @@ var count = &cli.Command{
&PubKeySliceFlag{ &PubKeySliceFlag{
Name: "author", Name: "author",
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "only accept events from these authors (pubkey as hex)", Usage: "only accept events from these authors",
Category: CATEGORY_FILTER_ATTRIBUTES, Category: CATEGORY_FILTER_ATTRIBUTES,
}, },
&cli.IntSliceFlag{ &cli.IntSliceFlag{

View File

@@ -163,7 +163,7 @@ var encode = &cli.Command{
DisableSliceFlagSeparator: true, DisableSliceFlagSeparator: true,
Action: func(ctx context.Context, c *cli.Command) error { Action: func(ctx context.Context, c *cli.Command) error {
for target := range getStdinLinesOrArguments(c.Args()) { for target := range getStdinLinesOrArguments(c.Args()) {
id, err := nostr.IDFromHex(target) id, err := parseEventID(target)
if err != nil { if err != nil {
ctx = lineProcessingError(ctx, "invalid event id: %s", target) ctx = lineProcessingError(ctx, "invalid event id: %s", target)
continue continue

View File

@@ -96,8 +96,8 @@ func (t pubkeyValue) Create(val nostr.PubKey, p *nostr.PubKey, c struct{}) cli.V
func (t pubkeyValue) ToString(b nostr.PubKey) string { return t.pubkey.String() } func (t pubkeyValue) ToString(b nostr.PubKey) string { return t.pubkey.String() }
func (t *pubkeyValue) Set(value string) error { func (t *pubkeyValue) Set(value string) error {
pk, err := nostr.PubKeyFromHex(value) pubkey, err := parsePubKey(value)
t.pubkey = pk t.pubkey = pubkey
t.hasBeenSet = true t.hasBeenSet = true
return err return err
} }
@@ -147,8 +147,8 @@ func (t idValue) Create(val nostr.ID, p *nostr.ID, c struct{}) cli.Value {
func (t idValue) ToString(b nostr.ID) string { return t.id.String() } func (t idValue) ToString(b nostr.ID) string { return t.id.String() }
func (t *idValue) Set(value string) error { func (t *idValue) Set(value string) error {
pk, err := nostr.IDFromHex(value) id, err := parseEventID(value)
t.id = pk t.id = id
t.hasBeenSet = true t.hasBeenSet = true
return err return err
} }

View File

@@ -464,6 +464,50 @@ func askConfirmation(msg string) bool {
} }
} }
func parsePubKey(value string) (nostr.PubKey, error) {
pk, err := nostr.PubKeyFromHex(value)
if err == nil {
return pk, nil
}
if prefix, decoded, err := nip19.Decode(value); err == nil {
switch prefix {
case "npub":
if pk, ok := decoded.(nostr.PubKey); ok {
return pk, nil
}
case "nprofile":
if profile, ok := decoded.(nostr.ProfilePointer); ok {
return profile.PublicKey, nil
}
}
}
return nostr.PubKey{}, fmt.Errorf("invalid pubkey (\"%s\"): expected hex, npub, or nprofile", value)
}
func parseEventID(value string) (nostr.ID, error) {
id, err := nostr.IDFromHex(value)
if err == nil {
return id, nil
}
if prefix, decoded, err := nip19.Decode(value); err == nil {
switch prefix {
case "note":
if id, ok := decoded.(nostr.ID); ok {
return id, nil
}
case "nevent":
if event, ok := decoded.(nostr.EventPointer); ok {
return event.ID, nil
}
}
}
return nostr.ID{}, fmt.Errorf("invalid event id (\"%s\"): expected hex, note, or nevent", value)
}
var colors = struct { var colors = struct {
reset func(...any) (int, error) reset func(...any) (int, error)
italic func(...any) string italic func(...any) string

View File

@@ -6,7 +6,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/sdk" "fiatjaf.com/nostr/sdk"
"fiatjaf.com/nostr/sdk/hints/bbolth" "fiatjaf.com/nostr/sdk/hints/bbolth"
"github.com/fatih/color" "github.com/fatih/color"
@@ -82,7 +81,7 @@ var outbox = &cli.Command{
return fmt.Errorf("expected exactly one argument (pubkey)") return fmt.Errorf("expected exactly one argument (pubkey)")
} }
pk, err := nostr.PubKeyFromHex(c.Args().First()) pk, err := parsePubKey(c.Args().First())
if err != nil { if err != nil {
return fmt.Errorf("invalid public key '%s': %w", c.Args().First(), err) return fmt.Errorf("invalid public key '%s': %w", c.Args().First(), err)
} }

4
req.go
View File

@@ -276,13 +276,13 @@ var reqFilterFlags = []cli.Flag{
&PubKeySliceFlag{ &PubKeySliceFlag{
Name: "author", Name: "author",
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "only accept events from these authors (pubkey as hex)", Usage: "only accept events from these authors",
Category: CATEGORY_FILTER_ATTRIBUTES, Category: CATEGORY_FILTER_ATTRIBUTES,
}, },
&IDSliceFlag{ &IDSliceFlag{
Name: "id", Name: "id",
Aliases: []string{"i"}, Aliases: []string{"i"},
Usage: "only accept events with these ids (hex)", Usage: "only accept events with these ids",
Category: CATEGORY_FILTER_ATTRIBUTES, Category: CATEGORY_FILTER_ATTRIBUTES,
}, },
&cli.IntSliceFlag{ &cli.IntSliceFlag{