-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhisper_helper.go
66 lines (56 loc) · 1.42 KB
/
whisper_helper.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 (
"encoding/hex"
"errors"
"math/big"
"github.com/enkhalifapro/go-web3/shh"
)
// WhisperHelper contains messaging functions
type WhisperHelper struct {
shh *shh.SHH
}
// Message - whisper message DTO
type Message struct {
From string
To string
Topic string
Content string
TTL int64
}
// NewWhisperHelper constructs whisperHelper
func NewWhisperHelper(shh *shh.SHH) *WhisperHelper {
return &WhisperHelper{shh}
}
// SendAsymMsg sends a message with asymmetric encryption
func (w *WhisperHelper) SendAsymMsg(msg *Message) error {
// validate sender key
if msg.From == "" {
return errors.New("sender key is empty")
}
_, err := hex.DecodeString(msg.From)
if err != nil {
return errors.New("invalid sender key")
}
// validate recipient key
if msg.To == "" {
return errors.New("recipient key is empty")
}
_, err = hex.DecodeString(msg.To)
if err != nil {
return errors.New("invalid recipient key")
}
_, err = w.shh.AsymPost(msg.From, msg.To, msg.Topic, msg.Content, big.NewInt(msg.TTL))
return err
}
// SendSymMsg sends a message with symmetric encryption
func (w *WhisperHelper) SendSymMsg(password string, msg *Message) error {
if password == "" {
return errors.New("password is empty")
}
symKey, err := w.shh.GenerateSymKeyFromPassword(password)
if err != nil {
return err
}
_, err = w.shh.SymPost(symKey, msg.To, msg.Topic, msg.Content, big.NewInt(msg.TTL))
return err
}