-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (68 loc) · 1.63 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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
)
func init() {
//flags
flag.StringVar(&Username, "username", "", "set username, example 'pchat -username \"user\"'")
flag.StringVar(&PrivateKey, "privatekey", "", "set your private key")
flag.StringVar(&URL, "url", "http://localhost:8545", "set RPC URL for running whisper node")
}
func main() {
flag.Parse()
CheckArgs()
ctx, c, id := Wconfig(URL, PrivateKey)
fID := Receive(ctx, c, id)
var pubKey, msg string
reader := bufio.NewReader(os.Stdin)
prKey, err := c.PrivateKey(ctx, id)
if err != nil {
log.Fatalln(err)
}
fmt.Print("**********************************************************\n")
fmt.Println("Your Private Key:", hexutil.Encode(prKey))
puKey, err := c.PublicKey(ctx, id)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Your Public Key:", hexutil.Encode(puKey))
fmt.Print("**********************************************************\n\n")
fmt.Print("Enter your partner public key: ")
pubKey, _ = reader.ReadString('\n')
// receive message
receivedMsg := make(chan string)
go func(chan string) {
for {
m, err := c.FilterMessages(ctx, fID)
if err != nil {
log.Fatalln(err)
}
if len(m) > 0 {
receivedMsg <- fmt.Sprint(string(m[0].Padding)+": ", string(m[0].Payload))
}
}
}(receivedMsg)
// send message
go func() {
for {
msg, _ = reader.ReadString('\n')
msg = strings.TrimSpace(msg)
if msg != "" {
Post(ctx, c, Username, msg, strings.TrimSpace(pubKey))
msg = ""
}
}
}()
for {
select {
case m := <-receivedMsg:
fmt.Println(m)
}
}
}