forked from fiatjaf/noscl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign-verify.go
60 lines (49 loc) · 1.25 KB
/
sign-verify.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
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/docopt/docopt-go"
"github.com/fiatjaf/go-nostr"
)
func signEventJSON(opts docopt.Opts) {
if config.PrivateKey == "" {
log.Printf("Can't sign. Private key not set.\n")
return
}
j, err := opts.String("<event-json>")
if err != nil {
log.Printf("Provide the JSON event as a quoted argument: %s.\n", err.Error())
return
}
var event nostr.Event
if err := json.Unmarshal([]byte(j), &event); err != nil {
log.Printf("Invalid event JSON: %s.\n", err.Error())
return
}
if err := event.Sign(config.PrivateKey); err != nil {
log.Printf("Failed to sign: %s.\n", err.Error())
return
}
fmt.Printf("Signature:\n %s\n", event.Sig)
}
func verifyEventJSON(opts docopt.Opts) {
j, err := opts.String("<event-json>")
if err != nil {
log.Printf("Provide the JSON event as a quoted argument: %s.\n", err.Error())
return
}
var event nostr.Event
if err := json.Unmarshal([]byte(j), &event); err != nil {
log.Printf("Invalid event JSON: %s.\n", err.Error())
return
}
if ok, err := event.CheckSignature(); err != nil {
log.Printf("Failed to verify: %s.\n", err.Error())
return
} else if !ok {
fmt.Printf("Signature is invalid.\n")
return
}
fmt.Printf("Signature is valid.\n")
}