-
Notifications
You must be signed in to change notification settings - Fork 0
/
whisper.go
56 lines (48 loc) · 1.21 KB
/
whisper.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
package main
import (
"context"
"log"
"github.com/ethereum/go-ethereum/common/hexutil"
shh "github.com/ethereum/go-ethereum/whisper/shhclient"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
)
// Wconfig it will retrun context, client configuration, and id
func Wconfig(url, privatekey string) (ctx context.Context, c *shh.Client, id string) {
ctx = context.TODO()
c, err := shh.Dial(url)
if err != nil {
log.Println(err)
}
// if private key set
if privatekey == "" {
id, _ = c.NewKeyPair(ctx)
} else {
prKey, _ := hexutil.Decode(privatekey)
id, err = c.AddPrivateKey(ctx, prKey)
if err != nil {
log.Fatalln(err)
}
}
return
}
// Post ...
func Post(ctx context.Context, c *shh.Client, username string, msg string, toPubKey string) {
_, err := c.Post(ctx, whisper.NewMessage{
TTL: 60,
PowTime: 2,
PowTarget: 2.5,
Padding: []byte(username),
Payload: []byte(msg),
PublicKey: hexutil.MustDecode(toPubKey)})
if err != nil {
log.Println(err)
}
}
// Receive ...
func Receive(ctx context.Context, c *shh.Client, id string) (filterID string) {
filterID, err := c.NewMessageFilter(ctx, whisper.Criteria{PrivateKeyID: id})
if err != nil {
log.Println(err)
}
return
}