From 2b08f6652073e8d66911fa6791d9243489386da7 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 3 May 2023 18:14:06 -0300 Subject: [PATCH] nak event --- event.go | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 ++- req.go | 15 ++++--- 3 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 event.go diff --git a/event.go b/event.go new file mode 100644 index 0000000..7135d1c --- /dev/null +++ b/event.go @@ -0,0 +1,123 @@ +package main + +import ( + "encoding/json" + "fmt" + "strconv" + "strings" + "time" + + "github.com/nbd-wtf/go-nostr" + "github.com/urfave/cli/v2" +) + +const CATEGORY_EVENT_FIELDS = "EVENT FIELDS" + +var event = &cli.Command{ + Name: "event", + Usage: "generates an encoded event", + Description: `example usage (for sending directly to a relay with 'nostcat'): + nak event -k 1 -c hello --envelope | nostcat wss://nos.lol`, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "sec", + Usage: "secret key to sign the event", + DefaultText: "the key '1'", + Value: "0000000000000000000000000000000000000000000000000000000000000001", + }, + &cli.BoolFlag{ + Name: "envelope", + Usage: "print the event enveloped in a [\"EVENT\", ...] message ready to be sent to a relay", + }, + &cli.IntFlag{ + Name: "kind", + Aliases: []string{"k"}, + Usage: "event kind", + DefaultText: "1", + Value: 1, + Category: CATEGORY_EVENT_FIELDS, + }, + &cli.StringFlag{ + Name: "content", + Aliases: []string{"c"}, + Usage: "event content", + DefaultText: "hello from the nostr army knife", + Value: "hello from the nostr army knife", + Category: CATEGORY_EVENT_FIELDS, + }, + &cli.StringSliceFlag{ + Name: "tag", + Aliases: []string{"t"}, + Usage: "sets a tag field on the event, takes a value like -t e=", + Category: CATEGORY_EVENT_FIELDS, + }, + &cli.StringSliceFlag{ + Name: "e", + Usage: "shortcut for --tag e=", + Category: CATEGORY_EVENT_FIELDS, + }, + &cli.StringSliceFlag{ + Name: "p", + Usage: "shortcut for --tag p=", + Category: CATEGORY_EVENT_FIELDS, + }, + &cli.StringFlag{ + Name: "created-at", + Aliases: []string{"time", "ts"}, + Usage: "unix timestamp value for the created_at field", + DefaultText: "now", + Value: "now", + Category: CATEGORY_EVENT_FIELDS, + }, + }, + Action: func(c *cli.Context) error { + evt := nostr.Event{ + Kind: c.Int("kind"), + Content: c.String("content"), + Tags: make(nostr.Tags, 0, 3), + } + + tags := make([][]string, 0, 5) + for _, tagFlag := range c.StringSlice("tag") { + spl := strings.Split(tagFlag, "=") + if len(spl) == 2 && len(spl[0]) == 1 { + tags = append(tags, spl) + } + } + for _, etag := range c.StringSlice("e") { + tags = append(tags, []string{"e", etag}) + } + for _, ptag := range c.StringSlice("p") { + tags = append(tags, []string{"p", ptag}) + } + if len(tags) > 0 { + for _, tag := range tags { + evt.Tags = append(evt.Tags, tag) + } + } + + createdAt := c.String("created_at") + ts := time.Now() + if createdAt != "now" { + if v, err := strconv.ParseInt(createdAt, 10, 64); err != nil { + ts = time.Unix(v, 0) + } + } + evt.CreatedAt = nostr.Timestamp(ts.Unix()) + + if err := evt.Sign(c.String("sec")); err != nil { + return fmt.Errorf("error signing with provided key: %w", err) + } + + var result string + if c.Bool("envelope") { + j, _ := json.Marshal([]any{"EVENT", evt}) + result = string(j) + } else { + result = evt.String() + } + + fmt.Println(result) + return nil + }, +} diff --git a/main.go b/main.go index b86826d..3f7a8aa 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "log" + "fmt" "os" "github.com/urfave/cli/v2" @@ -13,10 +13,12 @@ func main() { Usage: "the nostr army knife command-line tool", Commands: []*cli.Command{ req, + event, }, } if err := app.Run(os.Args); err != nil { - log.Fatalf("failed to run cli: %s", err) + fmt.Println(err) + os.Exit(1) } } diff --git a/req.go b/req.go index 5b98b81..e916b13 100644 --- a/req.go +++ b/req.go @@ -15,8 +15,7 @@ var req = &cli.Command{ Name: "req", Usage: "generates an encoded REQ message to be sent to a relay", Description: `example usage (with 'nostcat'): - nak req -k 1 -a 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d | nostcat wss://nostr-pub.wellorder.net - `, + nak req -k 1 -a 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d | nostcat wss://nos.lol`, Flags: []cli.Flag{ &cli.StringSliceFlag{ Name: "author", @@ -43,14 +42,12 @@ var req = &cli.Command{ Category: CATEGORY_FILTER_ATTRIBUTES, }, &cli.StringSliceFlag{ - Name: "event-tag", - Aliases: []string{"e"}, + Name: "e", Usage: "shortcut for --tag e=", Category: CATEGORY_FILTER_ATTRIBUTES, }, &cli.StringSliceFlag{ - Name: "pubkey-tag", - Aliases: []string{"p"}, + Name: "p", Usage: "shortcut for --tag p=", Category: CATEGORY_FILTER_ATTRIBUTES, }, @@ -95,12 +92,14 @@ var req = &cli.Command{ spl := strings.Split(tagFlag, "=") if len(spl) == 2 && len(spl[0]) == 1 { tags = append(tags, spl) + } else { + return fmt.Errorf("invalid --tag '%s'", tagFlag) } } - for _, etag := range c.StringSlice("event-tag") { + for _, etag := range c.StringSlice("e") { tags = append(tags, []string{"e", etag}) } - for _, ptag := range c.StringSlice("pubkey-tag") { + for _, ptag := range c.StringSlice("p") { tags = append(tags, []string{"p", ptag}) } if len(tags) > 0 {