-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (85 loc) · 2.47 KB
/
main.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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"math/rand"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
nostr "github.com/fiatjaf/go-nostr"
)
// nonce: rfBd56ti2SMtYvSgD5xAV0YU99zampta7Z7S575KLkIZ9PYkL17LTlsVqMNTZyLK
const alphaBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = alphaBytes[rand.Intn(len(alphaBytes))]
}
return string(b)
}
func main() {
unsignedNonce := RandStringBytes(64)
fmt.Printf("Nonce:\t%s\n", unsignedNonce)
pub, priv := GetKeyPair()
signedNonce, err := Sign(priv, unsignedNonce)
if err != nil {
log.Fatalf("error signing: %s", err)
}
checks, err := CheckSignature(signedNonce, unsignedNonce, pub)
if err != nil {
log.Fatalf("error checking: %s", err)
}
if checks {
fmt.Println("it's an older code sir, but it checks out")
} else {
fmt.Println("something is wrong...")
}
}
func GetKeyPair() (string, string) {
priv := nostr.GeneratePrivateKey()
pub, err := nostr.GetPublicKey(priv)
if err != nil {
log.Fatalf("GetPublicKey errored: %s", err)
}
return pub, priv
}
// taken from github.com/fiatjaf/go-nostr
func Sign(privateKey string, plainText string) (string, error) {
h := sha256.Sum256([]byte(plainText))
s, err := hex.DecodeString(privateKey)
if err != nil {
return "", fmt.Errorf("Sign called with invalid private key '%s': %w", privateKey, err)
}
sk, _ := btcec.PrivKeyFromBytes(s)
sig, err := schnorr.Sign(sk, h[:])
if err != nil {
return "", err
}
signedNonce := hex.EncodeToString(sig.Serialize())
return signedNonce, nil
}
// taken from github.com/fiatjaf/go-nostr
func CheckSignature(signedNonce, unsignedNonce, unparsedpubkey string) (bool, error) {
// read and check pubkey
pk, err := hex.DecodeString(unparsedpubkey)
if err != nil {
return false, fmt.Errorf("event pubkey '%s' is invalid hex: %w", unparsedpubkey, err)
}
pubkey, err := schnorr.ParsePubKey(pk)
if err != nil {
return false, fmt.Errorf("event has invalid pubkey '%s': %w", unparsedpubkey, err)
}
// read signature
s, err := hex.DecodeString(signedNonce)
if err != nil {
return false, fmt.Errorf("signature '%s' is invalid hex: %w", signedNonce, err)
}
sig, err := schnorr.ParseSignature(s)
if err != nil {
return false, fmt.Errorf("failed to parse signature: %w", err)
}
// check signature
hash := sha256.Sum256([]byte(unsignedNonce))
return sig.Verify(hash[:], pubkey), nil
}