2016-09-11 11:44:14 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2016-11-25 08:06:47 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
whisperFilters []int
|
|
|
|
)
|
|
|
|
|
|
|
|
func onWhisperMessage(message *whisper.Message) {
|
2016-12-18 20:36:17 +00:00
|
|
|
SendSignal(SignalEnvelope{
|
2016-09-11 11:44:14 +00:00
|
|
|
Type: "whisper",
|
|
|
|
Event: WhisperMessageEvent{
|
|
|
|
Payload: string(message.Payload),
|
|
|
|
From: common.ToHex(crypto.FromECDSAPub(message.Recover())),
|
|
|
|
To: common.ToHex(crypto.FromECDSAPub(message.To)),
|
|
|
|
Sent: message.Sent.Unix(),
|
|
|
|
TTL: int64(message.TTL / time.Second),
|
|
|
|
Hash: common.ToHex(message.Hash.Bytes()),
|
|
|
|
},
|
2016-12-18 20:36:17 +00:00
|
|
|
})
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddWhisperFilter(args whisper.NewFilterArgs) int {
|
2016-12-07 21:07:08 +00:00
|
|
|
whisperService, err := NodeManagerInstance().WhisperService()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := whisper.Filter{
|
|
|
|
To: crypto.ToECDSAPub(common.FromHex(args.To)),
|
|
|
|
From: crypto.ToECDSAPub(common.FromHex(args.From)),
|
|
|
|
Topics: whisper.NewFilterTopics(args.Topics...),
|
|
|
|
Fn: onWhisperMessage,
|
|
|
|
}
|
|
|
|
|
|
|
|
id := whisperService.Watch(filter)
|
|
|
|
whisperFilters = append(whisperFilters, id)
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveWhisperFilter(idFilter int) {
|
2016-12-07 21:07:08 +00:00
|
|
|
whisperService, err := NodeManagerInstance().WhisperService()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
whisperService.Unwatch(idFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClearWhisperFilters() {
|
|
|
|
for _, idFilter := range whisperFilters {
|
|
|
|
RemoveWhisperFilter(idFilter)
|
|
|
|
}
|
|
|
|
whisperFilters = nil
|
|
|
|
}
|