forked from fiatjaf/noscl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
66 lines (56 loc) · 1.45 KB
/
message.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
package main
import (
"log"
"time"
"github.com/docopt/docopt-go"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip04"
)
func message(opts docopt.Opts) {
if config.PrivateKey == "" {
log.Printf("Can't direct message. Private key not set.\n")
return
}
initNostr()
var tags nostr.Tags
receiverKey := opts["<pubkey>"].(string)
tags = append(tags, nostr.Tag{"p", receiverKey})
references, err := optSlice(opts, "--reference")
if err != nil {
return
}
for _, ref := range references {
tags = append(tags, nostr.Tag{"e", ref})
}
// parse and encrypt content
message := opts["<content>"].(string)
if message == "-" {
message, err = readContentStdin(4096)
if err != nil {
log.Printf("Failed reading content from stdin: %s", err)
}
}
sharedSecret, err := nip04.ComputeSharedSecret(config.PrivateKey, receiverKey)
if err != nil {
log.Printf("Error computing shared key: %s. \n", err.Error())
return
}
encryptedMessage, err := nip04.Encrypt(message, sharedSecret)
if err != nil {
log.Printf("Error encrypting message: %s. \n", err.Error())
return
}
event, statuses, err := pool.PublishEvent(&nostr.Event{
CreatedAt: time.Now(),
Kind: nostr.KindEncryptedDirectMessage,
Tags: tags,
Content: encryptedMessage,
})
if err != nil {
log.Printf("Error messaging: %s.\n", err.Error())
return
}
log.Printf("%+v\n", event)
log.Printf("%+v\n", statuses)
// printPublishStatus(event, statuses)
}