mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 16:19:07 -05:00
nak event
This commit is contained in:
parent
0438c819a6
commit
2b08f66520
123
event.go
Normal file
123
event.go
Normal file
|
@ -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=<id>",
|
||||||
|
Category: CATEGORY_EVENT_FIELDS,
|
||||||
|
},
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "e",
|
||||||
|
Usage: "shortcut for --tag e=<value>",
|
||||||
|
Category: CATEGORY_EVENT_FIELDS,
|
||||||
|
},
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "p",
|
||||||
|
Usage: "shortcut for --tag p=<value>",
|
||||||
|
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
|
||||||
|
},
|
||||||
|
}
|
6
main.go
6
main.go
|
@ -1,7 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
@ -13,10 +13,12 @@ func main() {
|
||||||
Usage: "the nostr army knife command-line tool",
|
Usage: "the nostr army knife command-line tool",
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
req,
|
req,
|
||||||
|
event,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.Run(os.Args); err != nil {
|
if err := app.Run(os.Args); err != nil {
|
||||||
log.Fatalf("failed to run cli: %s", err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
req.go
15
req.go
|
@ -15,8 +15,7 @@ var req = &cli.Command{
|
||||||
Name: "req",
|
Name: "req",
|
||||||
Usage: "generates an encoded REQ message to be sent to a relay",
|
Usage: "generates an encoded REQ message to be sent to a relay",
|
||||||
Description: `example usage (with 'nostcat'):
|
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{
|
Flags: []cli.Flag{
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "author",
|
Name: "author",
|
||||||
|
@ -43,14 +42,12 @@ var req = &cli.Command{
|
||||||
Category: CATEGORY_FILTER_ATTRIBUTES,
|
Category: CATEGORY_FILTER_ATTRIBUTES,
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "event-tag",
|
Name: "e",
|
||||||
Aliases: []string{"e"},
|
|
||||||
Usage: "shortcut for --tag e=<value>",
|
Usage: "shortcut for --tag e=<value>",
|
||||||
Category: CATEGORY_FILTER_ATTRIBUTES,
|
Category: CATEGORY_FILTER_ATTRIBUTES,
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "pubkey-tag",
|
Name: "p",
|
||||||
Aliases: []string{"p"},
|
|
||||||
Usage: "shortcut for --tag p=<value>",
|
Usage: "shortcut for --tag p=<value>",
|
||||||
Category: CATEGORY_FILTER_ATTRIBUTES,
|
Category: CATEGORY_FILTER_ATTRIBUTES,
|
||||||
},
|
},
|
||||||
|
@ -95,12 +92,14 @@ var req = &cli.Command{
|
||||||
spl := strings.Split(tagFlag, "=")
|
spl := strings.Split(tagFlag, "=")
|
||||||
if len(spl) == 2 && len(spl[0]) == 1 {
|
if len(spl) == 2 && len(spl[0]) == 1 {
|
||||||
tags = append(tags, spl)
|
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})
|
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})
|
tags = append(tags, []string{"p", ptag})
|
||||||
}
|
}
|
||||||
if len(tags) > 0 {
|
if len(tags) > 0 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user