|
|
@ -17,494 +17,575 @@ |
|
|
|
package whisperv5 |
|
|
|
package whisperv5 |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"context" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
"crypto/ecdsa" |
|
|
|
|
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil" |
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil" |
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover" |
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover" |
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rpc" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var whisperOfflineErr = errors.New("whisper is offline") |
|
|
|
const ( |
|
|
|
|
|
|
|
filterTimeout = 300 // filters are considered timeout out after filterTimeout seconds
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
|
|
SymAsymErr = errors.New("specify either a symetric or a asymmetric key") |
|
|
|
|
|
|
|
InvalidSymmetricKeyErr = errors.New("invalid symmetric key") |
|
|
|
|
|
|
|
InvalidPublicKeyErr = errors.New("invalid public key") |
|
|
|
|
|
|
|
InvalidSigningPubKey = errors.New("invalid signing public key") |
|
|
|
|
|
|
|
TooLowPoWErr = errors.New("message rejected, PoW too low") |
|
|
|
|
|
|
|
NoTopicsErr = errors.New("missing topic(s)") |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// PublicWhisperAPI provides the whisper RPC service.
|
|
|
|
// PublicWhisperAPI provides the whisper RPC service that can be
|
|
|
|
|
|
|
|
// use publicly without security implications.
|
|
|
|
type PublicWhisperAPI struct { |
|
|
|
type PublicWhisperAPI struct { |
|
|
|
whisper *Whisper |
|
|
|
w *Whisper |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mu sync.Mutex |
|
|
|
|
|
|
|
lastUsed map[string]time.Time // keeps track when a filter was polled for the last time.
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewPublicWhisperAPI create a new RPC whisper service.
|
|
|
|
// NewPublicWhisperAPI create a new RPC whisper service.
|
|
|
|
func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI { |
|
|
|
func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI { |
|
|
|
return &PublicWhisperAPI{whisper: w} |
|
|
|
api := &PublicWhisperAPI{ |
|
|
|
|
|
|
|
w: w, |
|
|
|
|
|
|
|
lastUsed: make(map[string]time.Time), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Start starts the Whisper worker threads.
|
|
|
|
go api.run() |
|
|
|
func (api *PublicWhisperAPI) Start() error { |
|
|
|
return api |
|
|
|
if api.whisper == nil { |
|
|
|
|
|
|
|
return whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return api.whisper.Start(nil) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Stop stops the Whisper worker threads.
|
|
|
|
// run the api event loop.
|
|
|
|
func (api *PublicWhisperAPI) Stop() error { |
|
|
|
// this loop deletes filter that have not been used within filterTimeout
|
|
|
|
if api.whisper == nil { |
|
|
|
func (api *PublicWhisperAPI) run() { |
|
|
|
return whisperOfflineErr |
|
|
|
timeout := time.NewTicker(2 * time.Minute) |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
<-timeout.C |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api.mu.Lock() |
|
|
|
|
|
|
|
for id, lastUsed := range api.lastUsed { |
|
|
|
|
|
|
|
if time.Since(lastUsed).Seconds() >= filterTimeout { |
|
|
|
|
|
|
|
delete(api.lastUsed, id) |
|
|
|
|
|
|
|
if err := api.w.Unsubscribe(id); err != nil { |
|
|
|
|
|
|
|
log.Error("could not unsubscribe whisper filter", "error", err) |
|
|
|
} |
|
|
|
} |
|
|
|
return api.whisper.Stop() |
|
|
|
log.Debug("delete whisper filter (timeout)", "id", id) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Version returns the Whisper version this node offers.
|
|
|
|
|
|
|
|
func (api *PublicWhisperAPI) Version() (hexutil.Uint, error) { |
|
|
|
|
|
|
|
if api.whisper == nil { |
|
|
|
|
|
|
|
return 0, whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return hexutil.Uint(api.whisper.Version()), nil |
|
|
|
api.mu.Unlock() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Info returns the Whisper statistics for diagnostics.
|
|
|
|
// Version returns the Whisper sub-protocol version.
|
|
|
|
func (api *PublicWhisperAPI) Info() (string, error) { |
|
|
|
func (api *PublicWhisperAPI) Version(ctx context.Context) string { |
|
|
|
if api.whisper == nil { |
|
|
|
return ProtocolVersionStr |
|
|
|
return "", whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return api.whisper.Stats(), nil |
|
|
|
|
|
|
|
|
|
|
|
// Info contains diagnostic information.
|
|
|
|
|
|
|
|
type Info struct { |
|
|
|
|
|
|
|
Memory int `json:"memory"` // Memory size of the floating messages in bytes.
|
|
|
|
|
|
|
|
Message int `json:"message"` // Number of floating messages.
|
|
|
|
|
|
|
|
MinPow float64 `json:"minPow"` // Minimal accepted PoW
|
|
|
|
|
|
|
|
MaxMessageSize uint32 `json:"maxMessageSize"` // Maximum accepted message size
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SetMaxMessageLength sets the maximal message length allowed by this node
|
|
|
|
// Info returns diagnostic information about the whisper node.
|
|
|
|
func (api *PublicWhisperAPI) SetMaxMessageLength(val int) error { |
|
|
|
func (api *PublicWhisperAPI) Info(ctx context.Context) Info { |
|
|
|
if api.whisper == nil { |
|
|
|
stats := api.w.Stats() |
|
|
|
return whisperOfflineErr |
|
|
|
return Info{ |
|
|
|
|
|
|
|
Memory: stats.memoryUsed, |
|
|
|
|
|
|
|
Message: len(api.w.messageQueue) + len(api.w.p2pMsgQueue), |
|
|
|
|
|
|
|
MinPow: api.w.MinPow(), |
|
|
|
|
|
|
|
MaxMessageSize: api.w.MaxMessageSize(), |
|
|
|
} |
|
|
|
} |
|
|
|
return api.whisper.SetMaxMessageLength(val) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SetMinimumPoW sets the minimal PoW required by this node
|
|
|
|
// SetMaxMessageSize sets the maximum message size that is accepted.
|
|
|
|
func (api *PublicWhisperAPI) SetMinimumPoW(val float64) error { |
|
|
|
// Upper limit is defined in whisperv5.MaxMessageSize.
|
|
|
|
if api.whisper == nil { |
|
|
|
func (api *PublicWhisperAPI) SetMaxMessageSize(ctx context.Context, size uint32) (bool, error) { |
|
|
|
return whisperOfflineErr |
|
|
|
return true, api.w.SetMaxMessageSize(size) |
|
|
|
} |
|
|
|
|
|
|
|
return api.whisper.SetMinimumPoW(val) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AllowP2PMessagesFromPeer marks specific peer trusted, which will allow it
|
|
|
|
// SetMinPow sets the minimum PoW for a message before it is accepted.
|
|
|
|
// to send historic (expired) messages.
|
|
|
|
func (api *PublicWhisperAPI) SetMinPoW(ctx context.Context, pow float64) (bool, error) { |
|
|
|
func (api *PublicWhisperAPI) AllowP2PMessagesFromPeer(enode string) error { |
|
|
|
return true, api.w.SetMinimumPoW(pow) |
|
|
|
if api.whisper == nil { |
|
|
|
|
|
|
|
return whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MarkTrustedPeer marks a peer trusted. , which will allow it to send historic (expired) messages.
|
|
|
|
|
|
|
|
// Note: This function is not adding new nodes, the node needs to exists as a peer.
|
|
|
|
|
|
|
|
func (api *PublicWhisperAPI) MarkTrustedPeer(ctx context.Context, enode string) (bool, error) { |
|
|
|
n, err := discover.ParseNode(enode) |
|
|
|
n, err := discover.ParseNode(enode) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return errors.New("failed to parse enode of trusted peer: " + err.Error()) |
|
|
|
return false, err |
|
|
|
} |
|
|
|
} |
|
|
|
return api.whisper.AllowP2PMessagesFromPeer(n.ID[:]) |
|
|
|
return true, api.w.AllowP2PMessagesFromPeer(n.ID[:]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// HasKeyPair checks if the whisper node is configured with the private key
|
|
|
|
// NewKeyPair generates a new public and private key pair for message decryption and encryption.
|
|
|
|
// of the specified public pair.
|
|
|
|
// It returns an ID that can be used to refer to the keypair.
|
|
|
|
func (api *PublicWhisperAPI) HasKeyPair(id string) (bool, error) { |
|
|
|
func (api *PublicWhisperAPI) NewKeyPair(ctx context.Context) (string, error) { |
|
|
|
if api.whisper == nil { |
|
|
|
return api.w.NewKeyPair() |
|
|
|
return false, whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return api.whisper.HasKeyPair(id), nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DeleteKeyPair deletes the specifies key if it exists.
|
|
|
|
// AddPrivateKey imports the given private key.
|
|
|
|
func (api *PublicWhisperAPI) DeleteKeyPair(id string) (bool, error) { |
|
|
|
func (api *PublicWhisperAPI) AddPrivateKey(ctx context.Context, privateKey hexutil.Bytes) (string, error) { |
|
|
|
if api.whisper == nil { |
|
|
|
key, err := crypto.ToECDSA(privateKey) |
|
|
|
return false, whisperOfflineErr |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
} |
|
|
|
} |
|
|
|
return api.whisper.DeleteKeyPair(id), nil |
|
|
|
return api.w.AddKeyPair(key) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewKeyPair generates a new cryptographic identity for the client, and injects
|
|
|
|
// DeleteKeyPair removes the key with the given key if it exists.
|
|
|
|
// it into the known identities for message decryption.
|
|
|
|
func (api *PublicWhisperAPI) DeleteKeyPair(ctx context.Context, key string) (bool, error) { |
|
|
|
func (api *PublicWhisperAPI) NewKeyPair() (string, error) { |
|
|
|
if ok := api.w.DeleteKeyPair(key); ok { |
|
|
|
if api.whisper == nil { |
|
|
|
return true, nil |
|
|
|
return "", whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return api.whisper.NewKeyPair() |
|
|
|
return false, fmt.Errorf("key pair %s not found", key) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetPublicKey returns the public key for identity id
|
|
|
|
// HasKeyPair returns an indication if the node has a key pair that is associated with the given id.
|
|
|
|
func (api *PublicWhisperAPI) GetPublicKey(id string) (hexutil.Bytes, error) { |
|
|
|
func (api *PublicWhisperAPI) HasKeyPair(ctx context.Context, id string) bool { |
|
|
|
if api.whisper == nil { |
|
|
|
return api.w.HasKeyPair(id) |
|
|
|
return nil, whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
key, err := api.whisper.GetPrivateKey(id) |
|
|
|
|
|
|
|
|
|
|
|
// GetPublicKey returns the public key associated with the given key. The key is the hex
|
|
|
|
|
|
|
|
// encoded representation of a key in the form specified in section 4.3.6 of ANSI X9.62.
|
|
|
|
|
|
|
|
func (api *PublicWhisperAPI) GetPublicKey(ctx context.Context, id string) (hexutil.Bytes, error) { |
|
|
|
|
|
|
|
key, err := api.w.GetPrivateKey(id) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return hexutil.Bytes{}, err |
|
|
|
} |
|
|
|
} |
|
|
|
return crypto.FromECDSAPub(&key.PublicKey), nil |
|
|
|
return crypto.FromECDSAPub(&key.PublicKey), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetPrivateKey returns the private key for identity id
|
|
|
|
// GetPublicKey returns the private key associated with the given key. The key is the hex
|
|
|
|
func (api *PublicWhisperAPI) GetPrivateKey(id string) (string, error) { |
|
|
|
// encoded representation of a key in the form specified in section 4.3.6 of ANSI X9.62.
|
|
|
|
if api.whisper == nil { |
|
|
|
func (api *PublicWhisperAPI) GetPrivateKey(ctx context.Context, id string) (hexutil.Bytes, error) { |
|
|
|
return "", whisperOfflineErr |
|
|
|
key, err := api.w.GetPrivateKey(id) |
|
|
|
} |
|
|
|
|
|
|
|
key, err := api.whisper.GetPrivateKey(id) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", err |
|
|
|
return hexutil.Bytes{}, err |
|
|
|
} |
|
|
|
} |
|
|
|
return common.ToHex(crypto.FromECDSA(key)), nil |
|
|
|
return crypto.FromECDSA(key), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GenerateSymmetricKey generates a random symmetric key and stores it under id,
|
|
|
|
// NewSymKey generate a random symmetric key.
|
|
|
|
// which is then returned. Will be used in the future for session key exchange.
|
|
|
|
// It returns an ID that can be used to refer to the key.
|
|
|
|
func (api *PublicWhisperAPI) GenerateSymmetricKey() (string, error) { |
|
|
|
// Can be used encrypting and decrypting messages where the key is known to both parties.
|
|
|
|
if api.whisper == nil { |
|
|
|
func (api *PublicWhisperAPI) NewSymKey(ctx context.Context) (string, error) { |
|
|
|
return "", whisperOfflineErr |
|
|
|
return api.w.GenerateSymKey() |
|
|
|
} |
|
|
|
|
|
|
|
return api.whisper.GenerateSymKey() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AddSymmetricKeyDirect stores the key, and returns its id.
|
|
|
|
// AddSymKey import a symmetric key.
|
|
|
|
func (api *PublicWhisperAPI) AddSymmetricKeyDirect(key hexutil.Bytes) (string, error) { |
|
|
|
// It returns an ID that can be used to refer to the key.
|
|
|
|
if api.whisper == nil { |
|
|
|
// Can be used encrypting and decrypting messages where the key is known to both parties.
|
|
|
|
return "", whisperOfflineErr |
|
|
|
func (api *PublicWhisperAPI) AddSymKey(ctx context.Context, key hexutil.Bytes) (string, error) { |
|
|
|
} |
|
|
|
return api.w.AddSymKeyDirect([]byte(key)) |
|
|
|
return api.whisper.AddSymKeyDirect(key) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AddSymmetricKeyFromPassword generates the key from password, stores it, and returns its id.
|
|
|
|
// GenerateSymKeyFromPassword derive a key from the given password, stores it, and returns its ID.
|
|
|
|
func (api *PublicWhisperAPI) AddSymmetricKeyFromPassword(password string) (string, error) { |
|
|
|
func (api *PublicWhisperAPI) GenerateSymKeyFromPassword(ctx context.Context, passwd string) (string, error) { |
|
|
|
if api.whisper == nil { |
|
|
|
return api.w.AddSymKeyFromPassword(passwd) |
|
|
|
return "", whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return api.whisper.AddSymKeyFromPassword(password) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// HasSymmetricKey returns true if there is a key associated with the given id.
|
|
|
|
// HasSymKey returns an indication if the node has a symmetric key associated with the given key.
|
|
|
|
// Otherwise returns false.
|
|
|
|
func (api *PublicWhisperAPI) HasSymKey(ctx context.Context, id string) bool { |
|
|
|
func (api *PublicWhisperAPI) HasSymmetricKey(id string) (bool, error) { |
|
|
|
return api.w.HasSymKey(id) |
|
|
|
if api.whisper == nil { |
|
|
|
|
|
|
|
return false, whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
res := api.whisper.HasSymKey(id) |
|
|
|
|
|
|
|
return res, nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetSymmetricKey returns the symmetric key associated with the given id.
|
|
|
|
// GetSymKey returns the symmetric key associated with the given id.
|
|
|
|
func (api *PublicWhisperAPI) GetSymmetricKey(name string) (hexutil.Bytes, error) { |
|
|
|
func (api *PublicWhisperAPI) GetSymKey(ctx context.Context, id string) (hexutil.Bytes, error) { |
|
|
|
if api.whisper == nil { |
|
|
|
return api.w.GetSymKey(id) |
|
|
|
return nil, whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
b, err := api.whisper.GetSymKey(name) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return b, nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DeleteSymmetricKey deletes the key associated with the name string if it exists.
|
|
|
|
// DeleteSymKey deletes the symmetric key that is associated with the given id.
|
|
|
|
func (api *PublicWhisperAPI) DeleteSymmetricKey(name string) (bool, error) { |
|
|
|
func (api *PublicWhisperAPI) DeleteSymKey(ctx context.Context, id string) bool { |
|
|
|
if api.whisper == nil { |
|
|
|
return api.w.DeleteSymKey(id) |
|
|
|
return false, whisperOfflineErr |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
res := api.whisper.DeleteSymKey(name) |
|
|
|
|
|
|
|
return res, nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Subscribe creates and registers a new filter to watch for inbound whisper messages.
|
|
|
|
//go:generate gencodec -type NewMessage -field-override newMessageOverride -out gen_newmessage_json.go
|
|
|
|
// Returns the ID of the newly created filter.
|
|
|
|
|
|
|
|
func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) { |
|
|
|
// NewMessage represents a new whisper message that is posted through the RPC.
|
|
|
|
if api.whisper == nil { |
|
|
|
type NewMessage struct { |
|
|
|
return "", whisperOfflineErr |
|
|
|
SymKeyID string `json:"symKeyID"` |
|
|
|
|
|
|
|
PublicKey []byte `json:"pubKey"` |
|
|
|
|
|
|
|
Sig string `json:"sig"` |
|
|
|
|
|
|
|
TTL uint32 `json:"ttl"` |
|
|
|
|
|
|
|
Topic TopicType `json:"topic"` |
|
|
|
|
|
|
|
Payload []byte `json:"payload"` |
|
|
|
|
|
|
|
Padding []byte `json:"padding"` |
|
|
|
|
|
|
|
PowTime uint32 `json:"powTime"` |
|
|
|
|
|
|
|
PowTarget float64 `json:"powTarget"` |
|
|
|
|
|
|
|
TargetPeer string `json:"targetPeer"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type newMessageOverride struct { |
|
|
|
|
|
|
|
PublicKey hexutil.Bytes |
|
|
|
|
|
|
|
Payload hexutil.Bytes |
|
|
|
|
|
|
|
Padding hexutil.Bytes |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Post a message on the Whisper network.
|
|
|
|
|
|
|
|
func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (bool, error) { |
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
|
|
symKeyGiven = len(req.SymKeyID) > 0 |
|
|
|
|
|
|
|
pubKeyGiven = len(req.PublicKey) > 0 |
|
|
|
|
|
|
|
err error |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// user must specify either a symmetric or a asymmetric key
|
|
|
|
|
|
|
|
if (symKeyGiven && pubKeyGiven) || (!symKeyGiven && !pubKeyGiven) { |
|
|
|
|
|
|
|
return false, SymAsymErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
filter := Filter{ |
|
|
|
params := &MessageParams{ |
|
|
|
PoW: args.MinPoW, |
|
|
|
TTL: req.TTL, |
|
|
|
Messages: make(map[common.Hash]*ReceivedMessage), |
|
|
|
Payload: req.Payload, |
|
|
|
AllowP2P: args.AllowP2P, |
|
|
|
Padding: req.Padding, |
|
|
|
|
|
|
|
WorkTime: req.PowTime, |
|
|
|
|
|
|
|
PoW: req.PowTarget, |
|
|
|
|
|
|
|
Topic: req.Topic, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
// Set key that is used to sign the message
|
|
|
|
for i, bt := range args.Topics { |
|
|
|
if len(req.Sig) > 0 { |
|
|
|
if len(bt) == 0 || len(bt) > 4 { |
|
|
|
if params.Src, err = api.w.GetPrivateKey(req.Sig); err != nil { |
|
|
|
return "", errors.New(fmt.Sprintf("subscribe: topic %d has wrong size: %d", i, len(bt))) |
|
|
|
return false, err |
|
|
|
} |
|
|
|
} |
|
|
|
filter.Topics = append(filter.Topics, bt) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err = ValidateKeyID(args.Key); err != nil { |
|
|
|
// Set symmetric key that is used to encrypt the message
|
|
|
|
return "", errors.New("subscribe: " + err.Error()) |
|
|
|
if symKeyGiven { |
|
|
|
|
|
|
|
if params.Topic == (TopicType{}) { // topics are mandatory with symmetric encryption
|
|
|
|
|
|
|
|
return false, NoTopicsErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if params.KeySym, err = api.w.GetSymKey(req.SymKeyID); err != nil { |
|
|
|
if len(args.Sig) > 0 { |
|
|
|
return false, err |
|
|
|
sb := common.FromHex(args.Sig) |
|
|
|
|
|
|
|
if sb == nil { |
|
|
|
|
|
|
|
return "", errors.New("subscribe: sig parameter is invalid") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
filter.Src = crypto.ToECDSAPub(sb) |
|
|
|
if !validateSymmetricKey(params.KeySym) { |
|
|
|
if !ValidatePublicKey(filter.Src) { |
|
|
|
return false, InvalidSymmetricKeyErr |
|
|
|
return "", errors.New("subscribe: invalid 'sig' field") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if args.Symmetric { |
|
|
|
// Set asymmetric key that is used to encrypt the message
|
|
|
|
if len(args.Topics) == 0 { |
|
|
|
if pubKeyGiven { |
|
|
|
return "", errors.New("subscribe: at least one topic must be specified with symmetric encryption") |
|
|
|
params.Dst = crypto.ToECDSAPub(req.PublicKey) |
|
|
|
|
|
|
|
if !ValidatePublicKey(params.Dst) { |
|
|
|
|
|
|
|
return false, InvalidPublicKeyErr |
|
|
|
} |
|
|
|
} |
|
|
|
symKey, err := api.whisper.GetSymKey(args.Key) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", errors.New("subscribe: invalid key ID") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if !validateSymmetricKey(symKey) { |
|
|
|
|
|
|
|
return "", errors.New("subscribe: retrieved key is invalid") |
|
|
|
// encrypt and sent message
|
|
|
|
|
|
|
|
whisperMsg, err := NewSentMessage(params) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return false, err |
|
|
|
} |
|
|
|
} |
|
|
|
filter.KeySym = symKey |
|
|
|
|
|
|
|
filter.SymKeyHash = crypto.Keccak256Hash(filter.KeySym) |
|
|
|
env, err := whisperMsg.Wrap(params) |
|
|
|
} else { |
|
|
|
|
|
|
|
filter.KeyAsym, err = api.whisper.GetPrivateKey(args.Key) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", errors.New("subscribe: invalid key ID") |
|
|
|
return false, err |
|
|
|
} |
|
|
|
} |
|
|
|
if filter.KeyAsym == nil { |
|
|
|
|
|
|
|
return "", errors.New("subscribe: non-existent identity provided") |
|
|
|
// send to specific node (skip PoW check)
|
|
|
|
|
|
|
|
if len(req.TargetPeer) > 0 { |
|
|
|
|
|
|
|
n, err := discover.ParseNode(req.TargetPeer) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return false, fmt.Errorf("failed to parse target peer: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true, api.w.SendP2PMessage(n.ID[:], env) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return api.whisper.Subscribe(&filter) |
|
|
|
// ensure that the message PoW meets the node's minimum accepted PoW
|
|
|
|
|
|
|
|
if req.PowTarget < api.w.MinPow() { |
|
|
|
|
|
|
|
return false, TooLowPoWErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Unsubscribe disables and removes an existing filter.
|
|
|
|
return true, api.w.Send(env) |
|
|
|
func (api *PublicWhisperAPI) Unsubscribe(id string) { |
|
|
|
|
|
|
|
api.whisper.Unsubscribe(id) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetSubscriptionMessages retrieves all the new messages matched by the corresponding
|
|
|
|
//go:generate gencodec -type Criteria -field-override criteriaOverride -out gen_criteria_json.go
|
|
|
|
// subscription filter since the last retrieval.
|
|
|
|
|
|
|
|
func (api *PublicWhisperAPI) GetNewSubscriptionMessages(id string) []*WhisperMessage { |
|
|
|
// Criteria holds various filter options for inbound messages.
|
|
|
|
f := api.whisper.GetFilter(id) |
|
|
|
type Criteria struct { |
|
|
|
if f != nil { |
|
|
|
SymKeyID string `json:"symKeyID"` |
|
|
|
newMail := f.Retrieve() |
|
|
|
PrivateKeyID string `json:"privateKeyID"` |
|
|
|
return toWhisperMessages(newMail) |
|
|
|
Sig []byte `json:"sig"` |
|
|
|
} |
|
|
|
MinPow float64 `json:"minPow"` |
|
|
|
return toWhisperMessages(nil) |
|
|
|
Topics []TopicType `json:"topics"` |
|
|
|
|
|
|
|
AllowP2P bool `json:"allowP2P"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetMessages retrieves all the floating messages that match a specific subscription filter.
|
|
|
|
type criteriaOverride struct { |
|
|
|
// It is likely to be called once per session, right after Subscribe call.
|
|
|
|
Sig hexutil.Bytes |
|
|
|
func (api *PublicWhisperAPI) GetFloatingMessages(id string) []*WhisperMessage { |
|
|
|
|
|
|
|
all := api.whisper.Messages(id) |
|
|
|
|
|
|
|
return toWhisperMessages(all) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// toWhisperMessages converts a Whisper message to a RPC whisper message.
|
|
|
|
// Messages set up a subscription that fires events when messages arrive that match
|
|
|
|
func toWhisperMessages(messages []*ReceivedMessage) []*WhisperMessage { |
|
|
|
// the given set of criteria.
|
|
|
|
msgs := make([]*WhisperMessage, len(messages)) |
|
|
|
func (api *PublicWhisperAPI) Messages(ctx context.Context, crit Criteria) (*rpc.Subscription, error) { |
|
|
|
for i, msg := range messages { |
|
|
|
var ( |
|
|
|
msgs[i] = NewWhisperMessage(msg) |
|
|
|
symKeyGiven = len(crit.SymKeyID) > 0 |
|
|
|
|
|
|
|
pubKeyGiven = len(crit.PrivateKeyID) > 0 |
|
|
|
|
|
|
|
err error |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ensure that the RPC connection supports subscriptions
|
|
|
|
|
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx) |
|
|
|
|
|
|
|
if !supported { |
|
|
|
|
|
|
|
return nil, rpc.ErrNotificationsUnsupported |
|
|
|
} |
|
|
|
} |
|
|
|
return msgs |
|
|
|
|
|
|
|
|
|
|
|
// user must specify either a symmetric or a asymmetric key
|
|
|
|
|
|
|
|
if (symKeyGiven && pubKeyGiven) || (!symKeyGiven && !pubKeyGiven) { |
|
|
|
|
|
|
|
return nil, SymAsymErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Post creates a whisper message and injects it into the network for distribution.
|
|
|
|
filter := Filter{ |
|
|
|
func (api *PublicWhisperAPI) Post(args PostArgs) error { |
|
|
|
PoW: crit.MinPow, |
|
|
|
if api.whisper == nil { |
|
|
|
Messages: make(map[common.Hash]*ReceivedMessage), |
|
|
|
return whisperOfflineErr |
|
|
|
AllowP2P: crit.AllowP2P, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
if len(crit.Sig) > 0 { |
|
|
|
params := MessageParams{ |
|
|
|
filter.Src = crypto.ToECDSAPub(crit.Sig) |
|
|
|
TTL: args.TTL, |
|
|
|
if !ValidatePublicKey(filter.Src) { |
|
|
|
WorkTime: args.PowTime, |
|
|
|
return nil, InvalidSigningPubKey |
|
|
|
PoW: args.PowTarget, |
|
|
|
} |
|
|
|
Payload: args.Payload, |
|
|
|
|
|
|
|
Padding: args.Padding, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(args.Key) == 0 { |
|
|
|
for i, bt := range crit.Topics { |
|
|
|
return errors.New("post: key is missing") |
|
|
|
if len(bt) == 0 || len(bt) > 4 { |
|
|
|
|
|
|
|
return nil, fmt.Errorf("subscribe: topic %d has wrong size: %d", i, len(bt)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
filter.Topics = append(filter.Topics, bt[:]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(args.Sig) > 0 { |
|
|
|
// listen for message that are encrypted with the given symmetric key
|
|
|
|
params.Src, err = api.whisper.GetPrivateKey(args.Sig) |
|
|
|
if symKeyGiven { |
|
|
|
|
|
|
|
if len(filter.Topics) == 0 { |
|
|
|
|
|
|
|
return nil, NoTopicsErr |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
key, err := api.w.GetSymKey(crit.SymKeyID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
if params.Src == nil { |
|
|
|
if !validateSymmetricKey(key) { |
|
|
|
return errors.New("post: empty identity") |
|
|
|
return nil, InvalidSymmetricKeyErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
filter.KeySym = key |
|
|
|
|
|
|
|
filter.SymKeyHash = crypto.Keccak256Hash(filter.KeySym) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(args.Topic) == TopicLength { |
|
|
|
// listen for messages that are encrypted with the given public key
|
|
|
|
params.Topic = BytesToTopic(args.Topic) |
|
|
|
if pubKeyGiven { |
|
|
|
} else if len(args.Topic) != 0 { |
|
|
|
filter.KeyAsym, err = api.w.GetPrivateKey(crit.PrivateKeyID) |
|
|
|
return errors.New(fmt.Sprintf("post: wrong topic size %d", len(args.Topic))) |
|
|
|
if err != nil || filter.KeyAsym == nil { |
|
|
|
|
|
|
|
return nil, InvalidPublicKeyErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if args.Type == "sym" { |
|
|
|
|
|
|
|
if err = ValidateKeyID(args.Key); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
params.KeySym, err = api.whisper.GetSymKey(args.Key) |
|
|
|
|
|
|
|
|
|
|
|
id, err := api.w.Subscribe(&filter) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
if !validateSymmetricKey(params.KeySym) { |
|
|
|
|
|
|
|
return errors.New("post: key for symmetric encryption is invalid") |
|
|
|
// create subscription and start waiting for message events
|
|
|
|
|
|
|
|
rpcSub := notifier.CreateSubscription() |
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
// for now poll internally, refactor whisper internal for channel support
|
|
|
|
|
|
|
|
ticker := time.NewTicker(250 * time.Millisecond) |
|
|
|
|
|
|
|
defer ticker.Stop() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
select { |
|
|
|
|
|
|
|
case <-ticker.C: |
|
|
|
|
|
|
|
if filter := api.w.GetFilter(id); filter != nil { |
|
|
|
|
|
|
|
for _, rpcMessage := range toMessage(filter.Retrieve()) { |
|
|
|
|
|
|
|
if err := notifier.Notify(rpcSub.ID, rpcMessage); err != nil { |
|
|
|
|
|
|
|
log.Error("Failed to send notification", "err", err) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(params.Topic) == 0 { |
|
|
|
|
|
|
|
return errors.New("post: topic is missing for symmetric encryption") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else if args.Type == "asym" { |
|
|
|
|
|
|
|
kb := common.FromHex(args.Key) |
|
|
|
|
|
|
|
if kb == nil { |
|
|
|
|
|
|
|
return errors.New("post: public key for asymmetric encryption is invalid") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
params.Dst = crypto.ToECDSAPub(kb) |
|
|
|
case <-rpcSub.Err(): |
|
|
|
if !ValidatePublicKey(params.Dst) { |
|
|
|
api.w.Unsubscribe(id) |
|
|
|
return errors.New("post: public key for asymmetric encryption is invalid") |
|
|
|
return |
|
|
|
|
|
|
|
case <-notifier.Closed(): |
|
|
|
|
|
|
|
api.w.Unsubscribe(id) |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
return errors.New("post: wrong type (sym/asym)") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
// encrypt and send
|
|
|
|
return rpcSub, nil |
|
|
|
message, err := NewSentMessage(¶ms) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
envelope, err := message.Wrap(¶ms) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
//go:generate gencodec -type Message -field-override messageOverride -out gen_message_json.go
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
// Message is the RPC representation of a whisper message.
|
|
|
|
if envelope.size() > api.whisper.maxMsgLength { |
|
|
|
type Message struct { |
|
|
|
return errors.New("post: message is too big") |
|
|
|
Sig []byte `json:"sig,omitempty"` |
|
|
|
|
|
|
|
TTL uint32 `json:"ttl"` |
|
|
|
|
|
|
|
Timestamp uint32 `json:"timestamp"` |
|
|
|
|
|
|
|
Topic TopicType `json:"topic"` |
|
|
|
|
|
|
|
Payload []byte `json:"payload"` |
|
|
|
|
|
|
|
Padding []byte `json:"padding"` |
|
|
|
|
|
|
|
PoW float64 `json:"pow"` |
|
|
|
|
|
|
|
Hash []byte `json:"hash"` |
|
|
|
|
|
|
|
Dst []byte `json:"recipientPublicKey,omitempty"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(args.TargetPeer) != 0 { |
|
|
|
type messageOverride struct { |
|
|
|
n, err := discover.ParseNode(args.TargetPeer) |
|
|
|
Sig hexutil.Bytes |
|
|
|
if err != nil { |
|
|
|
Payload hexutil.Bytes |
|
|
|
return errors.New("post: failed to parse enode of target peer: " + err.Error()) |
|
|
|
Padding hexutil.Bytes |
|
|
|
|
|
|
|
Hash hexutil.Bytes |
|
|
|
|
|
|
|
Dst hexutil.Bytes |
|
|
|
} |
|
|
|
} |
|
|
|
return api.whisper.SendP2PMessage(n.ID[:], envelope) |
|
|
|
|
|
|
|
} else if args.PowTarget < api.whisper.minPoW { |
|
|
|
// ToWhisperMessage converts an internal message into an API version.
|
|
|
|
return errors.New("post: target PoW is less than minimum PoW, the message can not be sent") |
|
|
|
func ToWhisperMessage(message *ReceivedMessage) *Message { |
|
|
|
|
|
|
|
msg := Message{ |
|
|
|
|
|
|
|
Payload: message.Payload, |
|
|
|
|
|
|
|
Padding: message.Padding, |
|
|
|
|
|
|
|
Timestamp: message.Sent, |
|
|
|
|
|
|
|
TTL: message.TTL, |
|
|
|
|
|
|
|
PoW: message.PoW, |
|
|
|
|
|
|
|
Hash: message.EnvelopeHash.Bytes(), |
|
|
|
|
|
|
|
Topic: message.Topic, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return api.whisper.Send(envelope) |
|
|
|
if message.Dst != nil { |
|
|
|
|
|
|
|
b := crypto.FromECDSAPub(message.Dst) |
|
|
|
|
|
|
|
if b != nil { |
|
|
|
|
|
|
|
msg.Dst = b |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type PostArgs struct { |
|
|
|
if isMessageSigned(message.Raw[0]) { |
|
|
|
Type string `json:"type"` // "sym"/"asym" (symmetric or asymmetric)
|
|
|
|
b := crypto.FromECDSAPub(message.SigToPubKey()) |
|
|
|
TTL uint32 `json:"ttl"` // time-to-live in seconds
|
|
|
|
if b != nil { |
|
|
|
Sig string `json:"sig"` // id of the signing key
|
|
|
|
msg.Sig = b |
|
|
|
Key string `json:"key"` // key id (in case of sym) or public key (in case of asym)
|
|
|
|
} |
|
|
|
Topic hexutil.Bytes `json:"topic"` // topic (4 bytes)
|
|
|
|
|
|
|
|
Padding hexutil.Bytes `json:"padding"` // optional padding bytes
|
|
|
|
|
|
|
|
Payload hexutil.Bytes `json:"payload"` // payload to be encrypted
|
|
|
|
|
|
|
|
PowTime uint32 `json:"powTime"` // maximal time in seconds to be spent on PoW
|
|
|
|
|
|
|
|
PowTarget float64 `json:"powTarget"` // minimal PoW required for this message
|
|
|
|
|
|
|
|
TargetPeer string `json:"targetPeer"` // peer id (for p2p message only)
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type WhisperFilterArgs struct { |
|
|
|
return &msg |
|
|
|
Symmetric bool // encryption type
|
|
|
|
|
|
|
|
Key string // id of the key to be used for decryption
|
|
|
|
|
|
|
|
Sig string // public key of the sender to be verified
|
|
|
|
|
|
|
|
MinPoW float64 // minimal PoW requirement
|
|
|
|
|
|
|
|
Topics [][]byte // list of topics (up to 4 bytes each) to match
|
|
|
|
|
|
|
|
AllowP2P bool // indicates wheather direct p2p messages are allowed for this filter
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a
|
|
|
|
// toMessage converts a set of messages to its RPC representation.
|
|
|
|
// JSON message blob into a WhisperFilterArgs structure.
|
|
|
|
func toMessage(messages []*ReceivedMessage) []*Message { |
|
|
|
func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) { |
|
|
|
msgs := make([]*Message, len(messages)) |
|
|
|
// Unmarshal the JSON message and sanity check
|
|
|
|
for i, msg := range messages { |
|
|
|
var obj struct { |
|
|
|
msgs[i] = ToWhisperMessage(msg) |
|
|
|
Type string `json:"type"` |
|
|
|
|
|
|
|
Key string `json:"key"` |
|
|
|
|
|
|
|
Sig string `json:"sig"` |
|
|
|
|
|
|
|
MinPoW float64 `json:"minPoW"` |
|
|
|
|
|
|
|
Topics []interface{} `json:"topics"` |
|
|
|
|
|
|
|
AllowP2P bool `json:"allowP2P"` |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if err := json.Unmarshal(b, &obj); err != nil { |
|
|
|
return msgs |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch obj.Type { |
|
|
|
// GetFilterMessages returns the messages that match the filter criteria and
|
|
|
|
case "sym": |
|
|
|
// are received between the last poll and now.
|
|
|
|
args.Symmetric = true |
|
|
|
func (api *PublicWhisperAPI) GetFilterMessages(id string) ([]*Message, error) { |
|
|
|
case "asym": |
|
|
|
api.mu.Lock() |
|
|
|
args.Symmetric = false |
|
|
|
f := api.w.GetFilter(id) |
|
|
|
default: |
|
|
|
if f == nil { |
|
|
|
return errors.New("wrong type (sym/asym)") |
|
|
|
api.mu.Unlock() |
|
|
|
|
|
|
|
return nil, fmt.Errorf("filter not found") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
api.lastUsed[id] = time.Now() |
|
|
|
|
|
|
|
api.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
args.Key = obj.Key |
|
|
|
receivedMessages := f.Retrieve() |
|
|
|
args.Sig = obj.Sig |
|
|
|
messages := make([]*Message, 0, len(receivedMessages)) |
|
|
|
args.MinPoW = obj.MinPoW |
|
|
|
for _, msg := range receivedMessages { |
|
|
|
args.AllowP2P = obj.AllowP2P |
|
|
|
messages = append(messages, ToWhisperMessage(msg)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Construct the topic array
|
|
|
|
return messages, nil |
|
|
|
if obj.Topics != nil { |
|
|
|
|
|
|
|
topics := make([]string, len(obj.Topics)) |
|
|
|
|
|
|
|
for i, field := range obj.Topics { |
|
|
|
|
|
|
|
switch value := field.(type) { |
|
|
|
|
|
|
|
case string: |
|
|
|
|
|
|
|
topics[i] = value |
|
|
|
|
|
|
|
case nil: |
|
|
|
|
|
|
|
return fmt.Errorf("topic[%d] is empty", i) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return fmt.Errorf("topic[%d] is not a string", i) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteMessageFilter deletes a filter.
|
|
|
|
|
|
|
|
func (api *PublicWhisperAPI) DeleteMessageFilter(id string) (bool, error) { |
|
|
|
|
|
|
|
api.mu.Lock() |
|
|
|
|
|
|
|
defer api.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete(api.lastUsed, id) |
|
|
|
|
|
|
|
return true, api.w.Unsubscribe(id) |
|
|
|
} |
|
|
|
} |
|
|
|
topicsDecoded := make([][]byte, len(topics)) |
|
|
|
|
|
|
|
for j, s := range topics { |
|
|
|
// NewMessageFilter creates a new filter that can be used to poll for
|
|
|
|
x := common.FromHex(s) |
|
|
|
// (new) messages that satisfy the given criteria.
|
|
|
|
if x == nil || len(x) > TopicLength { |
|
|
|
func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) { |
|
|
|
return fmt.Errorf("topic[%d] is invalid", j) |
|
|
|
var ( |
|
|
|
|
|
|
|
src *ecdsa.PublicKey |
|
|
|
|
|
|
|
keySym []byte |
|
|
|
|
|
|
|
keyAsym *ecdsa.PrivateKey |
|
|
|
|
|
|
|
topics [][]byte |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
symKeyGiven = len(req.SymKeyID) > 0 |
|
|
|
|
|
|
|
asymKeyGiven = len(req.PrivateKeyID) > 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err error |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// user must specify either a symmetric or a asymmetric key
|
|
|
|
|
|
|
|
if (symKeyGiven && asymKeyGiven) || (!symKeyGiven && !asymKeyGiven) { |
|
|
|
|
|
|
|
return "", SymAsymErr |
|
|
|
} |
|
|
|
} |
|
|
|
topicsDecoded[j] = x |
|
|
|
|
|
|
|
|
|
|
|
if len(req.Sig) > 0 { |
|
|
|
|
|
|
|
src = crypto.ToECDSAPub(req.Sig) |
|
|
|
|
|
|
|
if !ValidatePublicKey(src) { |
|
|
|
|
|
|
|
return "", InvalidSigningPubKey |
|
|
|
} |
|
|
|
} |
|
|
|
args.Topics = topicsDecoded |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
if symKeyGiven { |
|
|
|
|
|
|
|
if keySym, err = api.w.GetSymKey(req.SymKeyID); err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if !validateSymmetricKey(keySym) { |
|
|
|
// WhisperMessage is the RPC representation of a whisper message.
|
|
|
|
return "", InvalidSymmetricKeyErr |
|
|
|
type WhisperMessage struct { |
|
|
|
|
|
|
|
Topic string `json:"topic"` |
|
|
|
|
|
|
|
Payload string `json:"payload"` |
|
|
|
|
|
|
|
Padding string `json:"padding"` |
|
|
|
|
|
|
|
Src string `json:"sig"` |
|
|
|
|
|
|
|
Dst string `json:"recipientPublicKey"` |
|
|
|
|
|
|
|
Timestamp uint32 `json:"timestamp"` |
|
|
|
|
|
|
|
TTL uint32 `json:"ttl"` |
|
|
|
|
|
|
|
PoW float64 `json:"pow"` |
|
|
|
|
|
|
|
Hash string `json:"hash"` |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewWhisperMessage converts an internal message into an API version.
|
|
|
|
|
|
|
|
func NewWhisperMessage(message *ReceivedMessage) *WhisperMessage { |
|
|
|
|
|
|
|
msg := WhisperMessage{ |
|
|
|
|
|
|
|
Payload: common.ToHex(message.Payload), |
|
|
|
|
|
|
|
Padding: common.ToHex(message.Padding), |
|
|
|
|
|
|
|
Timestamp: message.Sent, |
|
|
|
|
|
|
|
TTL: message.TTL, |
|
|
|
|
|
|
|
PoW: message.PoW, |
|
|
|
|
|
|
|
Hash: common.ToHex(message.EnvelopeHash.Bytes()), |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(message.Topic) == TopicLength { |
|
|
|
if asymKeyGiven { |
|
|
|
msg.Topic = common.ToHex(message.Topic[:]) |
|
|
|
if keyAsym, err = api.w.GetPrivateKey(req.PrivateKeyID); err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
} |
|
|
|
} |
|
|
|
if message.Dst != nil { |
|
|
|
} |
|
|
|
b := crypto.FromECDSAPub(message.Dst) |
|
|
|
|
|
|
|
if b != nil { |
|
|
|
if len(req.Topics) > 0 { |
|
|
|
msg.Dst = common.ToHex(b) |
|
|
|
topics = make([][]byte, 1) |
|
|
|
|
|
|
|
for _, topic := range req.Topics { |
|
|
|
|
|
|
|
topics = append(topics, topic[:]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if isMessageSigned(message.Raw[0]) { |
|
|
|
|
|
|
|
b := crypto.FromECDSAPub(message.SigToPubKey()) |
|
|
|
f := &Filter{ |
|
|
|
if b != nil { |
|
|
|
Src: src, |
|
|
|
msg.Src = common.ToHex(b) |
|
|
|
KeySym: keySym, |
|
|
|
|
|
|
|
KeyAsym: keyAsym, |
|
|
|
|
|
|
|
PoW: req.MinPow, |
|
|
|
|
|
|
|
AllowP2P: req.AllowP2P, |
|
|
|
|
|
|
|
Topics: topics, |
|
|
|
|
|
|
|
Messages: make(map[common.Hash]*ReceivedMessage), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
id, err := api.w.Subscribe(f) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
} |
|
|
|
} |
|
|
|
return &msg |
|
|
|
|
|
|
|
|
|
|
|
api.mu.Lock() |
|
|
|
|
|
|
|
api.lastUsed[id] = time.Now() |
|
|
|
|
|
|
|
api.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return id, nil |
|
|
|
} |
|
|
|
} |
|
|
|