diff --git a/blossom.go b/blossom.go index b5e6e75..45a5eaa 100644 --- a/blossom.go +++ b/blossom.go @@ -7,7 +7,6 @@ import ( "io" "os" - "fiatjaf.com/nostr" "fiatjaf.com/nostr/keyer" "fiatjaf.com/nostr/nipb0/blossom" "github.com/urfave/cli/v3" @@ -38,11 +37,11 @@ var blossomCmd = &cli.Command{ var client *blossom.Client pubkey := c.Args().First() 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) - } else { - client = blossom.NewClient(c.String("server"), keyer.NewReadOnlySigner(pk)) } + client = blossom.NewClient(c.String("server"), keyer.NewReadOnlySigner(pk)) } else { var err error client, err = getBlossomClient(ctx, c) diff --git a/count.go b/count.go index 662baf7..48bcaa4 100644 --- a/count.go +++ b/count.go @@ -21,7 +21,7 @@ var count = &cli.Command{ &PubKeySliceFlag{ Name: "author", Aliases: []string{"a"}, - Usage: "only accept events from these authors (pubkey as hex)", + Usage: "only accept events from these authors", Category: CATEGORY_FILTER_ATTRIBUTES, }, &cli.IntSliceFlag{ diff --git a/encode.go b/encode.go index 59c5a58..d15bf90 100644 --- a/encode.go +++ b/encode.go @@ -163,7 +163,7 @@ var encode = &cli.Command{ DisableSliceFlagSeparator: true, Action: func(ctx context.Context, c *cli.Command) error { for target := range getStdinLinesOrArguments(c.Args()) { - id, err := nostr.IDFromHex(target) + id, err := parseEventID(target) if err != nil { ctx = lineProcessingError(ctx, "invalid event id: %s", target) continue diff --git a/flags.go b/flags.go index 7e4e32d..671e2d2 100644 --- a/flags.go +++ b/flags.go @@ -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) Set(value string) error { - pk, err := nostr.PubKeyFromHex(value) - t.pubkey = pk + pubkey, err := parsePubKey(value) + t.pubkey = pubkey t.hasBeenSet = true 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) Set(value string) error { - pk, err := nostr.IDFromHex(value) - t.id = pk + id, err := parseEventID(value) + t.id = id t.hasBeenSet = true return err } diff --git a/helpers.go b/helpers.go index de5da1b..02d30ed 100644 --- a/helpers.go +++ b/helpers.go @@ -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 { reset func(...any) (int, error) italic func(...any) string diff --git a/outbox.go b/outbox.go index fc916f0..82fc775 100644 --- a/outbox.go +++ b/outbox.go @@ -6,7 +6,6 @@ import ( "os" "path/filepath" - "fiatjaf.com/nostr" "fiatjaf.com/nostr/sdk" "fiatjaf.com/nostr/sdk/hints/bbolth" "github.com/fatih/color" @@ -82,7 +81,7 @@ var outbox = &cli.Command{ return fmt.Errorf("expected exactly one argument (pubkey)") } - pk, err := nostr.PubKeyFromHex(c.Args().First()) + pk, err := parsePubKey(c.Args().First()) if err != nil { return fmt.Errorf("invalid public key '%s': %w", c.Args().First(), err) } diff --git a/req.go b/req.go index bd410a4..554f0db 100644 --- a/req.go +++ b/req.go @@ -276,13 +276,13 @@ var reqFilterFlags = []cli.Flag{ &PubKeySliceFlag{ Name: "author", Aliases: []string{"a"}, - Usage: "only accept events from these authors (pubkey as hex)", + Usage: "only accept events from these authors", Category: CATEGORY_FILTER_ATTRIBUTES, }, &IDSliceFlag{ Name: "id", Aliases: []string{"i"}, - Usage: "only accept events with these ids (hex)", + Usage: "only accept events with these ids", Category: CATEGORY_FILTER_ATTRIBUTES, }, &cli.IntSliceFlag{