2016-10-29 15:11:37 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-02-14 17:44:47 +03:00
|
|
|
package whisperv5
|
2016-10-29 15:11:37 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-12-12 14:25:59 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2016-10-29 15:11:37 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-04-10 00:49:22 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2016-10-29 15:11:37 +03:00
|
|
|
)
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
var whisperOfflineErr = errors.New("whisper is offline")
|
2016-10-29 15:11:37 +03:00
|
|
|
|
|
|
|
// PublicWhisperAPI provides the whisper RPC service.
|
|
|
|
type PublicWhisperAPI struct {
|
2017-02-14 17:44:47 +03:00
|
|
|
whisper *Whisper
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicWhisperAPI create a new RPC whisper service.
|
2017-02-14 17:44:47 +03:00
|
|
|
func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI {
|
2016-10-29 15:11:37 +03:00
|
|
|
return &PublicWhisperAPI{whisper: w}
|
|
|
|
}
|
|
|
|
|
2016-12-20 02:58:01 +03:00
|
|
|
// Start starts the Whisper worker threads.
|
|
|
|
func (api *PublicWhisperAPI) Start() error {
|
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return whisperOfflineErr
|
2016-12-20 02:58:01 +03:00
|
|
|
}
|
|
|
|
return api.whisper.Start(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the Whisper worker threads.
|
|
|
|
func (api *PublicWhisperAPI) Stop() error {
|
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return whisperOfflineErr
|
2016-12-20 02:58:01 +03:00
|
|
|
}
|
|
|
|
return api.whisper.Stop()
|
|
|
|
}
|
|
|
|
|
2016-10-29 15:11:37 +03:00
|
|
|
// Version returns the Whisper version this node offers.
|
2016-12-17 17:39:55 +03:00
|
|
|
func (api *PublicWhisperAPI) Version() (hexutil.Uint, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return 0, whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2016-12-17 17:39:55 +03:00
|
|
|
return hexutil.Uint(api.whisper.Version()), nil
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// Info returns the Whisper statistics for diagnostics.
|
|
|
|
func (api *PublicWhisperAPI) Info() (string, error) {
|
2017-02-23 20:46:32 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return "", whisperOfflineErr
|
2017-02-23 20:46:32 +03:00
|
|
|
}
|
|
|
|
return api.whisper.Stats(), nil
|
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// SetMaxMessageLength sets the maximal message length allowed by this node
|
|
|
|
func (api *PublicWhisperAPI) SetMaxMessageLength(val int) error {
|
|
|
|
if api.whisper == nil {
|
|
|
|
return whisperOfflineErr
|
|
|
|
}
|
|
|
|
return api.whisper.SetMaxMessageLength(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMinimumPoW sets the minimal PoW required by this node
|
|
|
|
func (api *PublicWhisperAPI) SetMinimumPoW(val float64) error {
|
|
|
|
if api.whisper == nil {
|
|
|
|
return whisperOfflineErr
|
|
|
|
}
|
|
|
|
return api.whisper.SetMinimumPoW(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllowP2PMessagesFromPeer marks specific peer trusted, which will allow it
|
2016-10-29 15:11:37 +03:00
|
|
|
// to send historic (expired) messages.
|
2017-04-10 00:49:22 +03:00
|
|
|
func (api *PublicWhisperAPI) AllowP2PMessagesFromPeer(enode string) error {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
n, err := discover.ParseNode(enode)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to parse enode of trusted peer: " + err.Error())
|
|
|
|
}
|
|
|
|
return api.whisper.AllowP2PMessagesFromPeer(n.ID[:])
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// HasKeyPair checks if the whisper node is configured with the private key
|
2016-10-29 15:11:37 +03:00
|
|
|
// of the specified public pair.
|
2017-04-10 00:49:22 +03:00
|
|
|
func (api *PublicWhisperAPI) HasKeyPair(id string) (bool, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return false, whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return api.whisper.HasKeyPair(id), nil
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// DeleteKeyPair deletes the specifies key if it exists.
|
|
|
|
func (api *PublicWhisperAPI) DeleteKeyPair(id string) (bool, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return false, whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return api.whisper.DeleteKeyPair(id), nil
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// NewKeyPair generates a new cryptographic identity for the client, and injects
|
2016-10-29 15:11:37 +03:00
|
|
|
// it into the known identities for message decryption.
|
2017-04-10 00:49:22 +03:00
|
|
|
func (api *PublicWhisperAPI) NewKeyPair() (string, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return "", whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return api.whisper.NewKeyPair()
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// GetPublicKey returns the public key for identity id
|
|
|
|
func (api *PublicWhisperAPI) GetPublicKey(id string) (hexutil.Bytes, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return nil, whisperOfflineErr
|
|
|
|
}
|
|
|
|
key, err := api.whisper.GetPrivateKey(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return crypto.FromECDSAPub(&key.PublicKey), nil
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// GetPrivateKey returns the private key for identity id
|
|
|
|
func (api *PublicWhisperAPI) GetPrivateKey(id string) (string, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return "", whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
key, err := api.whisper.GetPrivateKey(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return common.ToHex(crypto.FromECDSA(key)), nil
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// GenerateSymmetricKey generates a random symmetric key and stores it under id,
|
|
|
|
// which is then returned. Will be used in the future for session key exchange.
|
|
|
|
func (api *PublicWhisperAPI) GenerateSymmetricKey() (string, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return "", whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return api.whisper.GenerateSymKey()
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// AddSymmetricKeyDirect stores the key, and returns its id.
|
|
|
|
func (api *PublicWhisperAPI) AddSymmetricKeyDirect(key hexutil.Bytes) (string, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return "", whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return api.whisper.AddSymKeyDirect(key)
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// AddSymmetricKeyFromPassword generates the key from password, stores it, and returns its id.
|
|
|
|
func (api *PublicWhisperAPI) AddSymmetricKeyFromPassword(password string) (string, error) {
|
2016-10-29 15:11:37 +03:00
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return "", whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return api.whisper.AddSymKeyFromPassword(password)
|
|
|
|
}
|
2016-10-29 15:11:37 +03:00
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// HasSymmetricKey returns true if there is a key associated with the given id.
|
|
|
|
// Otherwise returns false.
|
|
|
|
func (api *PublicWhisperAPI) HasSymmetricKey(id string) (bool, error) {
|
|
|
|
if api.whisper == nil {
|
|
|
|
return false, whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
res := api.whisper.HasSymKey(id)
|
|
|
|
return res, nil
|
|
|
|
}
|
2016-10-29 15:11:37 +03:00
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// GetSymmetricKey returns the symmetric key associated with the given id.
|
|
|
|
func (api *PublicWhisperAPI) GetSymmetricKey(name string) (hexutil.Bytes, error) {
|
|
|
|
if api.whisper == nil {
|
|
|
|
return nil, whisperOfflineErr
|
|
|
|
}
|
|
|
|
b, err := api.whisper.GetSymKey(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
return b, nil
|
|
|
|
}
|
2016-10-29 15:11:37 +03:00
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// DeleteSymmetricKey deletes the key associated with the name string if it exists.
|
|
|
|
func (api *PublicWhisperAPI) DeleteSymmetricKey(name string) (bool, error) {
|
|
|
|
if api.whisper == nil {
|
|
|
|
return false, whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
res := api.whisper.DeleteSymKey(name)
|
|
|
|
return res, nil
|
|
|
|
}
|
2016-10-29 15:11:37 +03:00
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// Subscribe creates and registers a new filter to watch for inbound whisper messages.
|
|
|
|
// Returns the ID of the newly created filter.
|
|
|
|
func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
|
|
|
|
if api.whisper == nil {
|
|
|
|
return "", whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
filter := Filter{
|
|
|
|
Src: crypto.ToECDSAPub(common.FromHex(args.SignedWith)),
|
|
|
|
PoW: args.MinPoW,
|
|
|
|
Messages: make(map[common.Hash]*ReceivedMessage),
|
|
|
|
AllowP2P: args.AllowP2P,
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
var err error
|
|
|
|
for i, bt := range args.Topics {
|
|
|
|
if len(bt) == 0 || len(bt) > 4 {
|
|
|
|
return "", errors.New(fmt.Sprintf("subscribe: topic %d has wrong size: %d", i, len(bt)))
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
filter.Topics = append(filter.Topics, bt)
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
if err = ValidateKeyID(args.Key); err != nil {
|
|
|
|
return "", errors.New("subscribe: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args.SignedWith) > 0 {
|
2017-02-14 17:44:47 +03:00
|
|
|
if !ValidatePublicKey(filter.Src) {
|
2017-04-10 00:49:22 +03:00
|
|
|
return "", errors.New("subscribe: invalid 'SignedWith' field")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Symmetric {
|
|
|
|
if len(args.Topics) == 0 {
|
|
|
|
return "", errors.New("subscribe: at least one topic must be specified with symmetric encryption")
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
filter.KeySym = symKey
|
|
|
|
filter.SymKeyHash = crypto.Keccak256Hash(filter.KeySym)
|
|
|
|
} else {
|
|
|
|
filter.KeyAsym, err = api.whisper.GetPrivateKey(args.Key)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New("subscribe: invalid key ID")
|
|
|
|
}
|
|
|
|
if filter.KeyAsym == nil {
|
|
|
|
return "", errors.New("subscribe: non-existent identity provided")
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
return api.whisper.Subscribe(&filter)
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// Unsubscribe disables and removes an existing filter.
|
|
|
|
func (api *PublicWhisperAPI) Unsubscribe(id string) {
|
|
|
|
api.whisper.Unsubscribe(id)
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// GetSubscriptionMessages retrieves all the new messages matched by a filter since the last retrieval.
|
|
|
|
func (api *PublicWhisperAPI) GetSubscriptionMessages(filterId string) []*WhisperMessage {
|
2016-12-01 22:09:22 +03:00
|
|
|
f := api.whisper.GetFilter(filterId)
|
2016-10-29 15:11:37 +03:00
|
|
|
if f != nil {
|
|
|
|
newMail := f.Retrieve()
|
|
|
|
return toWhisperMessages(newMail)
|
|
|
|
}
|
|
|
|
return toWhisperMessages(nil)
|
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
// GetMessages retrieves all the floating messages that match a specific filter.
|
|
|
|
// It is likely to be called once per session, right after Subscribe call.
|
2017-02-23 11:41:47 +03:00
|
|
|
func (api *PublicWhisperAPI) GetMessages(filterId string) []*WhisperMessage {
|
2016-12-01 22:09:22 +03:00
|
|
|
all := api.whisper.Messages(filterId)
|
2016-10-29 15:11:37 +03:00
|
|
|
return toWhisperMessages(all)
|
|
|
|
}
|
|
|
|
|
|
|
|
// toWhisperMessages converts a Whisper message to a RPC whisper message.
|
2017-02-23 11:41:47 +03:00
|
|
|
func toWhisperMessages(messages []*ReceivedMessage) []*WhisperMessage {
|
|
|
|
msgs := make([]*WhisperMessage, len(messages))
|
2016-10-29 15:11:37 +03:00
|
|
|
for i, msg := range messages {
|
|
|
|
msgs[i] = NewWhisperMessage(msg)
|
|
|
|
}
|
|
|
|
return msgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post creates a whisper message and injects it into the network for distribution.
|
|
|
|
func (api *PublicWhisperAPI) Post(args PostArgs) error {
|
|
|
|
if api.whisper == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return whisperOfflineErr
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
var err error
|
2017-02-14 17:44:47 +03:00
|
|
|
params := MessageParams{
|
2016-10-29 15:11:37 +03:00
|
|
|
TTL: args.TTL,
|
2017-04-10 00:49:22 +03:00
|
|
|
WorkTime: args.PowTime,
|
|
|
|
PoW: args.PowTarget,
|
2016-10-29 15:11:37 +03:00
|
|
|
Payload: args.Payload,
|
|
|
|
Padding: args.Padding,
|
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
if len(args.Key) == 0 {
|
|
|
|
return errors.New("post: key is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args.SignWith) > 0 {
|
|
|
|
params.Src, err = api.whisper.GetPrivateKey(args.SignWith)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
if params.Src == nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
return errors.New("post: empty identity")
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
if len(args.Topic) == TopicLength {
|
|
|
|
params.Topic = BytesToTopic(args.Topic)
|
|
|
|
} else if len(args.Topic) != 0 {
|
|
|
|
return errors.New(fmt.Sprintf("post: wrong topic size %d", len(args.Topic)))
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
if args.Type == "sym" {
|
|
|
|
if err = ValidateKeyID(args.Key); err != nil {
|
|
|
|
return err
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
params.KeySym, err = api.whisper.GetSymKey(args.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
if !validateSymmetricKey(params.KeySym) {
|
|
|
|
return errors.New("post: key for symmetric encryption is invalid")
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
if len(params.Topic) == 0 {
|
|
|
|
return errors.New("post: topic is missing for symmetric encryption")
|
|
|
|
}
|
|
|
|
} else if args.Type == "asym" {
|
|
|
|
params.Dst = crypto.ToECDSAPub(common.FromHex(args.Key))
|
2017-02-14 17:44:47 +03:00
|
|
|
if !ValidatePublicKey(params.Dst) {
|
2017-04-10 00:49:22 +03:00
|
|
|
return errors.New("post: public key for asymmetric encryption is invalid")
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
} else {
|
|
|
|
return errors.New("post: wrong type (sym/asym)")
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// encrypt and send
|
2017-02-14 17:44:47 +03:00
|
|
|
message := NewSentMessage(¶ms)
|
2017-04-10 00:49:22 +03:00
|
|
|
if message == nil {
|
|
|
|
return errors.New("post: failed create new message, probably due to failed rand function (OS level)")
|
|
|
|
}
|
2016-10-29 15:11:37 +03:00
|
|
|
envelope, err := message.Wrap(¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
if envelope.size() > api.whisper.maxMsgLength {
|
|
|
|
return errors.New("post: message is too big")
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
if len(args.TargetPeer) != 0 {
|
|
|
|
n, err := discover.ParseNode(args.TargetPeer)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("post: failed to parse enode of target peer: " + err.Error())
|
|
|
|
}
|
|
|
|
return api.whisper.SendP2PMessage(n.ID[:], envelope)
|
|
|
|
} else if args.PowTarget < api.whisper.minPoW {
|
|
|
|
return errors.New("post: target PoW is less than minimum PoW, the message can not be sent")
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return api.whisper.Send(envelope)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PostArgs struct {
|
2017-04-10 00:49:22 +03:00
|
|
|
Type string `json:"type"` // "sym"/"asym" (symmetric or asymmetric)
|
|
|
|
TTL uint32 `json:"ttl"` // time-to-live in seconds
|
|
|
|
SignWith string `json:"signWith"` // id of the signing key
|
|
|
|
Key string `json:"key"` // id of encryption key
|
|
|
|
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)
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type WhisperFilterArgs struct {
|
2017-04-10 00:49:22 +03:00
|
|
|
Symmetric bool // encryption type
|
|
|
|
Key string // id of the key to be used for decryption
|
|
|
|
SignedWith 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
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a
|
|
|
|
// JSON message blob into a WhisperFilterArgs structure.
|
|
|
|
func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
// Unmarshal the JSON message and sanity check
|
|
|
|
var obj struct {
|
2017-04-10 00:49:22 +03:00
|
|
|
Type string `json:"type"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
SignedWith string `json:"signedWith"`
|
|
|
|
MinPoW float64 `json:"minPoW"`
|
|
|
|
Topics []interface{} `json:"topics"`
|
|
|
|
AllowP2P bool `json:"allowP2P"`
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-10 00:49:22 +03:00
|
|
|
switch obj.Type {
|
|
|
|
case "sym":
|
|
|
|
args.Symmetric = true
|
|
|
|
case "asym":
|
|
|
|
args.Symmetric = false
|
|
|
|
default:
|
|
|
|
return errors.New("wrong type (sym/asym)")
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Key = obj.Key
|
|
|
|
args.SignedWith = obj.SignedWith
|
|
|
|
args.MinPoW = obj.MinPoW
|
|
|
|
args.AllowP2P = obj.AllowP2P
|
2016-10-29 15:11:37 +03:00
|
|
|
|
|
|
|
// Construct the topic array
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
topicsDecoded := make([][]byte, len(topics))
|
2016-10-29 15:11:37 +03:00
|
|
|
for j, s := range topics {
|
|
|
|
x := common.FromHex(s)
|
2017-04-10 00:49:22 +03:00
|
|
|
if x == nil || len(x) > TopicLength {
|
2016-10-29 15:11:37 +03:00
|
|
|
return fmt.Errorf("topic[%d] is invalid", j)
|
|
|
|
}
|
2017-04-10 00:49:22 +03:00
|
|
|
topicsDecoded[j] = x
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
args.Topics = topicsDecoded
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WhisperMessage is the RPC representation of a whisper message.
|
|
|
|
type WhisperMessage struct {
|
2017-04-10 00:49:22 +03:00
|
|
|
Topic string `json:"topic"`
|
|
|
|
Payload string `json:"payload"`
|
|
|
|
Padding string `json:"padding"`
|
|
|
|
Src string `json:"signedWith"`
|
|
|
|
Dst string `json:"recipientPublicKey"`
|
|
|
|
Timestamp uint32 `json:"timestamp"`
|
|
|
|
TTL uint32 `json:"ttl"`
|
|
|
|
PoW float64 `json:"pow"`
|
|
|
|
Hash string `json:"hash"`
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWhisperMessage converts an internal message into an API version.
|
2017-02-23 11:41:47 +03:00
|
|
|
func NewWhisperMessage(message *ReceivedMessage) *WhisperMessage {
|
|
|
|
msg := WhisperMessage{
|
2017-04-10 00:49:22 +03:00
|
|
|
Topic: common.ToHex(message.Topic[:]),
|
|
|
|
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()),
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|
2017-02-23 11:41:47 +03:00
|
|
|
|
|
|
|
if message.Dst != nil {
|
2017-04-10 00:49:22 +03:00
|
|
|
msg.Dst = common.ToHex(crypto.FromECDSAPub(message.Dst))
|
2017-02-23 11:41:47 +03:00
|
|
|
}
|
|
|
|
if isMessageSigned(message.Raw[0]) {
|
2017-04-10 00:49:22 +03:00
|
|
|
msg.Src = common.ToHex(crypto.FromECDSAPub(message.SigToPubKey()))
|
2017-02-23 11:41:47 +03:00
|
|
|
}
|
|
|
|
return &msg
|
2016-10-29 15:11:37 +03:00
|
|
|
}
|