2018-09-24 20:07:34 +02:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-09-25 10:05:38 +03:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2018-09-24 20:07:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var discoveryTopic = "contact-discovery"
|
|
|
|
var discoveryTopicBytes = toTopic(discoveryTopic)
|
|
|
|
|
|
|
|
func toTopic(s string) whisper.TopicType {
|
|
|
|
return whisper.BytesToTopic(crypto.Keccak256([]byte(s)))
|
|
|
|
}
|
|
|
|
|
2018-10-16 12:31:05 +02:00
|
|
|
func defaultWhisperMessage() whisper.NewMessage {
|
|
|
|
msg := whisper.NewMessage{}
|
2018-09-24 20:07:34 +02:00
|
|
|
|
|
|
|
msg.TTL = 10
|
|
|
|
msg.PowTarget = 0.002
|
|
|
|
msg.PowTime = 1
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2018-10-16 12:31:05 +02:00
|
|
|
func PublicMessageToWhisper(rpcMsg SendPublicMessageRPC, payload []byte) whisper.NewMessage {
|
2018-09-24 20:07:34 +02:00
|
|
|
msg := defaultWhisperMessage()
|
|
|
|
|
|
|
|
msg.Topic = toTopic(rpcMsg.Chat)
|
|
|
|
|
|
|
|
msg.Payload = payload
|
|
|
|
msg.Sig = rpcMsg.Sig
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2018-10-16 12:31:05 +02:00
|
|
|
func DirectMessageToWhisper(rpcMsg SendDirectMessageRPC, payload []byte) whisper.NewMessage {
|
2018-12-18 16:27:12 +01:00
|
|
|
var topicBytes whisper.TopicType
|
|
|
|
|
|
|
|
if rpcMsg.Chat == "" {
|
|
|
|
topicBytes = discoveryTopicBytes
|
|
|
|
} else {
|
|
|
|
topicBytes = toTopic(rpcMsg.Chat)
|
|
|
|
}
|
2018-09-24 20:07:34 +02:00
|
|
|
|
|
|
|
msg := defaultWhisperMessage()
|
|
|
|
|
2018-12-18 16:27:12 +01:00
|
|
|
msg.Topic = topicBytes
|
2018-09-24 20:07:34 +02:00
|
|
|
|
|
|
|
msg.Payload = payload
|
|
|
|
msg.Sig = rpcMsg.Sig
|
|
|
|
msg.PublicKey = rpcMsg.PubKey
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|