-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
118 lines (101 loc) · 2.3 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"os"
"strings"
"github.com/nbd-wtf/go-nostr"
"github.com/urfave/cli/v2"
)
var eventCmd = &cli.Command{
Name: "event",
Usage: "generate nostr event",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "content",
Aliases: []string{"c"},
Usage: "set content for event",
},
&cli.IntFlag{
Name: "kind",
Aliases: []string{"k"},
Usage: "set event kind",
Value: 1,
},
&cli.StringFlag{
Name: "pk",
Usage: "private key (does not have to be specified if key was set with 'key' command)",
},
&cli.StringSliceFlag{
Name: "tags",
Aliases: []string{"t"},
Usage: "set tags for event: -t e=<eventId>,p=<pubkey>",
},
&cli.BoolFlag{
Name: "wrap",
Aliases: []string{"w"},
Usage: `wrap event for sending to relay ["EVENT", <json event>]`,
DisableDefaultText: true,
},
},
Action: generateEvent,
}
func generateEvent(ctx *cli.Context) error {
privKey := ""
if ctx.IsSet("pk") {
privKey = ctx.String("pk")
} else {
config := getConfig()
if config == nil || config.PrivateKey == "" {
printErr("private key not set")
}
privKey = config.PrivateKey
}
pubkey, err := nostr.GetPublicKey(privKey)
if err != nil {
return err
}
tags := []nostr.Tag{}
tagsFlag := ctx.StringSlice("tags")
for _, t := range tagsFlag {
if len(t) < 2 {
printErr("specify tags in format: -t e=<eventId>,p=<pubkey>")
return nil
}
prefix := t[:2]
if prefix == "e=" || prefix == "p=" {
tagStr, _ := strings.CutPrefix(t, prefix)
if prefix == "p=" {
if !nostr.IsValidPublicKeyHex(tagStr) {
printErr("invalid pubkey")
}
}
tag := nostr.Tag{string(prefix[0]), tagStr}
tags = append(tags, tag)
} else {
printErr("specify tags in format: -t e=<eventId>,p=<pubkey>")
}
}
evt := &nostr.Event{
PubKey: pubkey,
CreatedAt: nostr.Now(),
Kind: ctx.Int("kind"),
Tags: tags,
Content: ctx.String("content"),
}
err = evt.Sign(privKey)
if err != nil {
printErr("error signing event")
}
eventStr := evt.String()
// wrap event in ["EVENT", <json event>] if flag is present
if ctx.Bool("wrap") {
fmt.Printf("[\"EVENT\", %v]\n", eventStr)
} else {
fmt.Println(eventStr)
}
return nil
}
func printErr(err string) {
fmt.Println(err)
os.Exit(0)
}