-
Notifications
You must be signed in to change notification settings - Fork 28
/
message.go
142 lines (123 loc) · 3.75 KB
/
message.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package nkn
import (
"crypto/rand"
"github.com/nknorg/nkn-sdk-go/payloads"
"golang.org/x/crypto/nacl/box"
"google.golang.org/protobuf/proto"
)
const (
nonceSize = 24
sharedKeySize = 32
)
// Payload type alias for gomobile compatibility.
const (
BinaryType = int32(payloads.PayloadType_BINARY)
TextType = int32(payloads.PayloadType_TEXT)
AckType = int32(payloads.PayloadType_ACK)
SessionType = int32(payloads.PayloadType_SESSION)
)
// Message contains the info of received message.
type Message struct {
Src string // Sender's NKN client address.
Data []byte // Message data. If data type is string, one can call string() to convert it to original string data.
Type int32 // Message data type.
Encrypted bool // Whether message is encrypted.
MessageID []byte // Message ID.
NoReply bool // Indicating no reply or ACK should be sent.
reply func(interface{}) error
}
// Reply sends bytes or string data as reply to message sender.
func (msg *Message) Reply(data interface{}) error {
return msg.reply(data)
}
// ReplyBinary is a wrapper of Reply without interface type for gomobile
// compatibility.
func (msg *Message) ReplyBinary(data []byte) error {
return msg.Reply(data)
}
// ReplyText is a wrapper of Reply without interface type for gomobile
// compatibility.
func (msg *Message) ReplyText(data string) error {
return msg.Reply(data)
}
func encrypt(message []byte, sharedKey *[sharedKeySize]byte) ([]byte, []byte, error) {
encrypted := make([]byte, len(message)+box.Overhead)
var nonce [nonceSize]byte
if _, err := rand.Read(nonce[:]); err != nil {
return nil, nil, err
}
box.SealAfterPrecomputation(encrypted[:0], message, &nonce, sharedKey)
return encrypted, nonce[:], nil
}
func decrypt(message []byte, nonce [nonceSize]byte, sharedKey *[sharedKeySize]byte) ([]byte, error) {
decrypted := make([]byte, len(message)-box.Overhead)
_, ok := box.OpenAfterPrecomputation(decrypted[:0], message, &nonce, sharedKey)
if !ok {
return nil, ErrDecryptFailed
}
return decrypted, nil
}
func newBinaryPayload(data, messageID, replyToID []byte, noReply bool) (*payloads.Payload, error) {
if len(messageID) == 0 && len(replyToID) == 0 {
var err error
messageID, err = RandomBytes(MessageIDSize)
if err != nil {
return nil, err
}
}
return &payloads.Payload{
Type: payloads.PayloadType_BINARY,
MessageId: messageID,
Data: data,
ReplyToId: replyToID,
NoReply: noReply,
}, nil
}
func newTextPayload(text string, messageID, replyToID []byte, noReply bool) (*payloads.Payload, error) {
if len(messageID) == 0 && len(replyToID) == 0 {
var err error
messageID, err = RandomBytes(MessageIDSize)
if err != nil {
return nil, err
}
}
data, err := proto.Marshal(&payloads.TextData{Text: text})
if err != nil {
return nil, err
}
return &payloads.Payload{
Type: payloads.PayloadType_TEXT,
MessageId: messageID,
Data: data,
ReplyToId: replyToID,
NoReply: noReply,
}, nil
}
func newAckPayload(replyToID []byte) (*payloads.Payload, error) {
return &payloads.Payload{
Type: payloads.PayloadType_ACK,
ReplyToId: replyToID,
}, nil
}
func newMessagePayload(data interface{}, messageID []byte, noReply bool) (*payloads.Payload, error) {
switch v := data.(type) {
case []byte:
return newBinaryPayload(v, messageID, nil, noReply)
case string:
return newTextPayload(v, messageID, nil, noReply)
default:
return nil, ErrInvalidPayloadType
}
}
func NewReplyPayload(data interface{}, replyToID []byte) (*payloads.Payload, error) {
switch v := data.(type) {
case []byte:
return newBinaryPayload(v, nil, replyToID, false)
case string:
return newTextPayload(v, nil, replyToID, false)
case nil:
return newAckPayload(replyToID)
default:
return nil, ErrInvalidPayloadType
}
}