2019-10-09 14:22:53 +00:00
|
|
|
package gethbridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"time"
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2021-01-14 22:15:13 +00:00
|
|
|
"github.com/status-im/status-go/whisper"
|
2019-10-09 14:22:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type gethWhisperWrapper struct {
|
|
|
|
whisper *whisper.Whisper
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
// NewGethWhisperWrapper returns an object that wraps Geth's Whisper in a types interface
|
|
|
|
func NewGethWhisperWrapper(w *whisper.Whisper) types.Whisper {
|
2019-10-09 14:22:53 +00:00
|
|
|
if w == nil {
|
|
|
|
panic("w cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gethWhisperWrapper{
|
|
|
|
whisper: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGethWhisperFrom retrieves the underlying whisper Whisper struct from a wrapped Whisper interface
|
2019-11-23 17:57:05 +00:00
|
|
|
func GetGethWhisperFrom(m types.Whisper) *whisper.Whisper {
|
2019-10-09 14:22:53 +00:00
|
|
|
return m.(*gethWhisperWrapper).whisper
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) PublicWhisperAPI() types.PublicWhisperAPI {
|
2019-10-09 14:22:53 +00:00
|
|
|
return NewGethPublicWhisperAPIWrapper(whisper.NewPublicWhisperAPI(w.whisper))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MinPow returns the PoW value required by this node.
|
|
|
|
func (w *gethWhisperWrapper) MinPow() float64 {
|
|
|
|
return w.whisper.MinPow()
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:42:42 +00:00
|
|
|
// MaxMessageSize returns the MaxMessageSize set
|
|
|
|
func (w *gethWhisperWrapper) MaxMessageSize() uint32 {
|
|
|
|
return w.whisper.MaxMessageSize()
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
// BloomFilter returns the aggregated bloom filter for all the topics of interest.
|
|
|
|
// The nodes are required to send only messages that match the advertised bloom filter.
|
|
|
|
// If a message does not match the bloom, it will tantamount to spam, and the peer will
|
|
|
|
// be disconnected.
|
|
|
|
func (w *gethWhisperWrapper) BloomFilter() []byte {
|
|
|
|
return w.whisper.BloomFilter()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentTime returns current time.
|
|
|
|
func (w *gethWhisperWrapper) GetCurrentTime() time.Time {
|
|
|
|
return w.whisper.GetCurrentTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeSource assigns a particular source of time to a whisper object.
|
|
|
|
func (w *gethWhisperWrapper) SetTimeSource(timesource func() time.Time) {
|
|
|
|
w.whisper.SetTimeSource(timesource)
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) SubscribeEnvelopeEvents(eventsProxy chan<- types.EnvelopeEvent) types.Subscription {
|
2019-10-09 14:22:53 +00:00
|
|
|
events := make(chan whisper.EnvelopeEvent, 100) // must be buffered to prevent blocking whisper
|
|
|
|
go func() {
|
|
|
|
for e := range events {
|
2020-01-13 19:17:30 +00:00
|
|
|
eventsProxy <- *NewWhisperEnvelopeEventWrapper(&e)
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return NewGethSubscriptionWrapper(w.whisper.SubscribeEnvelopeEvents(events))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWhisperWrapper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
|
|
|
|
return w.whisper.GetPrivateKey(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddKeyPair imports a asymmetric private key and returns a deterministic identifier.
|
|
|
|
func (w *gethWhisperWrapper) AddKeyPair(key *ecdsa.PrivateKey) (string, error) {
|
|
|
|
return w.whisper.AddKeyPair(key)
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:54:46 +00:00
|
|
|
// DeleteKeyPair deletes the key with the specified ID if it exists.
|
|
|
|
func (w *gethWhisperWrapper) DeleteKeyPair(keyID string) bool {
|
|
|
|
return w.whisper.DeleteKeyPair(keyID)
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:23 +00:00
|
|
|
// DeleteKeyPairs removes all cryptographic identities known to the node
|
|
|
|
func (w *gethWhisperWrapper) DeleteKeyPairs() error {
|
|
|
|
return w.whisper.DeleteKeyPairs()
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
func (w *gethWhisperWrapper) AddSymKeyDirect(key []byte) (string, error) {
|
|
|
|
return w.whisper.AddSymKeyDirect(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWhisperWrapper) AddSymKeyFromPassword(password string) (string, error) {
|
|
|
|
return w.whisper.AddSymKeyFromPassword(password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWhisperWrapper) DeleteSymKey(id string) bool {
|
|
|
|
return w.whisper.DeleteSymKey(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWhisperWrapper) GetSymKey(id string) ([]byte, error) {
|
|
|
|
return w.whisper.GetSymKey(id)
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) Subscribe(opts *types.SubscriptionOptions) (string, error) {
|
2019-10-28 13:50:33 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
keyAsym *ecdsa.PrivateKey
|
|
|
|
keySym []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if opts.SymKeyID != "" {
|
|
|
|
keySym, err = w.GetSymKey(opts.SymKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opts.PrivateKeyID != "" {
|
|
|
|
keyAsym, err = w.GetPrivateKey(opts.PrivateKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := w.createFilterWrapper("", keyAsym, keySym, opts.PoW, opts.Topics)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
id, err := w.whisper.Subscribe(GetWhisperFilterFrom(f))
|
2019-10-28 13:50:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
f.(*whisperFilterWrapper).id = id
|
2019-10-28 13:50:33 +00:00
|
|
|
return id, nil
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) GetFilter(id string) types.Filter {
|
2020-01-13 19:17:30 +00:00
|
|
|
return NewWhisperFilterWrapper(w.whisper.GetFilter(id), id)
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethWhisperWrapper) Unsubscribe(id string) error {
|
|
|
|
return w.whisper.Unsubscribe(id)
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:15:13 +00:00
|
|
|
func (w *gethWhisperWrapper) UnsubscribeMany(ids []string) error {
|
|
|
|
return w.whisper.UnsubscribeMany(ids)
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) createFilterWrapper(id string, keyAsym *ecdsa.PrivateKey, keySym []byte, pow float64, topics [][]byte) (types.Filter, error) {
|
2020-01-13 19:17:30 +00:00
|
|
|
return NewWhisperFilterWrapper(&whisper.Filter{
|
2019-10-09 14:22:53 +00:00
|
|
|
KeyAsym: keyAsym,
|
|
|
|
KeySym: keySym,
|
|
|
|
PoW: pow,
|
|
|
|
AllowP2P: true,
|
|
|
|
Topics: topics,
|
2019-10-28 13:50:33 +00:00
|
|
|
Messages: whisper.NewMemoryMessageStore(),
|
|
|
|
}, id), nil
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) SendMessagesRequest(peerID []byte, r types.MessagesRequest) error {
|
2019-11-06 16:23:11 +00:00
|
|
|
return w.whisper.SendMessagesRequest(peerID, whisper.MessagesRequest{
|
|
|
|
ID: r.ID,
|
|
|
|
From: r.From,
|
|
|
|
To: r.To,
|
|
|
|
Limit: r.Limit,
|
|
|
|
Cursor: r.Cursor,
|
|
|
|
Bloom: r.Bloom,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
// RequestHistoricMessages sends a message with p2pRequestCode to a specific peer,
|
|
|
|
// which is known to implement MailServer interface, and is supposed to process this
|
|
|
|
// request and respond with a number of peer-to-peer messages (possibly expired),
|
|
|
|
// which are not supposed to be forwarded any further.
|
|
|
|
// The whisper protocol is agnostic of the format and contents of envelope.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) RequestHistoricMessagesWithTimeout(peerID []byte, envelope types.Envelope, timeout time.Duration) error {
|
2020-01-21 07:11:24 +00:00
|
|
|
return w.whisper.RequestHistoricMessagesWithTimeout(peerID, envelope.Unwrap().(*whisper.Envelope), timeout)
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SyncMessages can be sent between two Mail Servers and syncs envelopes between them.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethWhisperWrapper) SyncMessages(peerID []byte, req types.SyncMailRequest) error {
|
2019-10-09 14:22:53 +00:00
|
|
|
return w.whisper.SyncMessages(peerID, *GetGethSyncMailRequestFrom(&req))
|
|
|
|
}
|
2020-01-13 19:17:30 +00:00
|
|
|
|
|
|
|
type whisperFilterWrapper struct {
|
|
|
|
filter *whisper.Filter
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWhisperFilterWrapper returns an object that wraps Geth's Filter in a types interface
|
|
|
|
func NewWhisperFilterWrapper(f *whisper.Filter, id string) types.Filter {
|
|
|
|
if f.Messages == nil {
|
|
|
|
panic("Messages should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &whisperFilterWrapper{
|
|
|
|
filter: f,
|
|
|
|
id: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWhisperFilterFrom retrieves the underlying whisper Filter struct from a wrapped Filter interface
|
|
|
|
func GetWhisperFilterFrom(f types.Filter) *whisper.Filter {
|
|
|
|
return f.(*whisperFilterWrapper).filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the filter ID
|
|
|
|
func (w *whisperFilterWrapper) ID() string {
|
|
|
|
return w.id
|
|
|
|
}
|