2016-10-29 07:11:37 -05: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 08:44:47 -06:00
|
|
|
package whisperv5
|
2016-10-29 07:11:37 -05:00
|
|
|
|
|
|
|
import (
|
2017-06-13 04:49:07 -05:00
|
|
|
"context"
|
2017-06-21 03:32:36 -05:00
|
|
|
"crypto/ecdsa"
|
2016-10-29 07:11:37 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-06-13 04:49:07 -05:00
|
|
|
"sync"
|
2017-06-21 03:32:36 -05:00
|
|
|
"time"
|
2016-10-29 07:11:37 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-12-12 05:25:59 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2016-10-29 07:11:37 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-06-13 04:49:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2017-06-13 04:49:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2016-10-29 07:11:37 -05:00
|
|
|
)
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
var (
|
2017-08-21 04:22:00 -05:00
|
|
|
ErrSymAsym = errors.New("specify either a symmetric or an asymmetric key")
|
2017-06-21 03:32:36 -05:00
|
|
|
ErrInvalidSymmetricKey = errors.New("invalid symmetric key")
|
|
|
|
ErrInvalidPublicKey = errors.New("invalid public key")
|
|
|
|
ErrInvalidSigningPubKey = errors.New("invalid signing public key")
|
|
|
|
ErrTooLowPoW = errors.New("message rejected, PoW too low")
|
|
|
|
ErrNoTopics = errors.New("missing topic(s)")
|
2017-06-13 04:49:07 -05:00
|
|
|
)
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// PublicWhisperAPI provides the whisper RPC service that can be
|
|
|
|
// use publicly without security implications.
|
2016-10-29 07:11:37 -05:00
|
|
|
type PublicWhisperAPI struct {
|
2017-06-13 04:49:07 -05:00
|
|
|
w *Whisper
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
lastUsed map[string]time.Time // keeps track when a filter was polled for the last time.
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicWhisperAPI create a new RPC whisper service.
|
2017-02-14 08:44:47 -06:00
|
|
|
func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI {
|
2017-06-13 04:49:07 -05:00
|
|
|
api := &PublicWhisperAPI{
|
|
|
|
w: w,
|
|
|
|
lastUsed: make(map[string]time.Time),
|
|
|
|
}
|
|
|
|
return api
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// Version returns the Whisper sub-protocol version.
|
|
|
|
func (api *PublicWhisperAPI) Version(ctx context.Context) string {
|
|
|
|
return ProtocolVersionStr
|
2016-12-19 17:58:01 -06:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// Info contains diagnostic information.
|
|
|
|
type Info struct {
|
|
|
|
Memory int `json:"memory"` // Memory size of the floating messages in bytes.
|
2017-06-23 02:18:02 -05:00
|
|
|
Messages int `json:"messages"` // Number of floating messages.
|
2017-06-13 04:49:07 -05:00
|
|
|
MinPow float64 `json:"minPow"` // Minimal accepted PoW
|
|
|
|
MaxMessageSize uint32 `json:"maxMessageSize"` // Maximum accepted message size
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// Info returns diagnostic information about the whisper node.
|
|
|
|
func (api *PublicWhisperAPI) Info(ctx context.Context) Info {
|
|
|
|
stats := api.w.Stats()
|
|
|
|
return Info{
|
|
|
|
Memory: stats.memoryUsed,
|
2017-10-06 09:04:21 -05:00
|
|
|
Messages: len(api.w.messageQueue) + len(api.w.p2pMsgQueue),
|
2017-06-13 04:49:07 -05:00
|
|
|
MinPow: api.w.MinPow(),
|
|
|
|
MaxMessageSize: api.w.MaxMessageSize(),
|
2017-02-23 11:46:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// SetMaxMessageSize sets the maximum message size that is accepted.
|
|
|
|
// Upper limit is defined in whisperv5.MaxMessageSize.
|
|
|
|
func (api *PublicWhisperAPI) SetMaxMessageSize(ctx context.Context, size uint32) (bool, error) {
|
|
|
|
return true, api.w.SetMaxMessageSize(size)
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 01:17:17 -05:00
|
|
|
// SetMinPoW sets the minimum PoW for a message before it is accepted.
|
2017-06-13 04:49:07 -05:00
|
|
|
func (api *PublicWhisperAPI) SetMinPoW(ctx context.Context, pow float64) (bool, error) {
|
|
|
|
return true, api.w.SetMinimumPoW(pow)
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// 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.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
func (api *PublicWhisperAPI) MarkTrustedPeer(ctx context.Context, url string) (bool, error) {
|
|
|
|
n, err := enode.ParseV4(url)
|
2017-04-09 16:49:22 -05:00
|
|
|
if err != nil {
|
2017-06-13 04:49:07 -05:00
|
|
|
return false, err
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
return true, api.w.AllowP2PMessagesFromPeer(n.ID().Bytes())
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// NewKeyPair generates a new public and private key pair for message decryption and encryption.
|
|
|
|
// It returns an ID that can be used to refer to the keypair.
|
|
|
|
func (api *PublicWhisperAPI) NewKeyPair(ctx context.Context) (string, error) {
|
|
|
|
return api.w.NewKeyPair()
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// AddPrivateKey imports the given private key.
|
|
|
|
func (api *PublicWhisperAPI) AddPrivateKey(ctx context.Context, privateKey hexutil.Bytes) (string, error) {
|
|
|
|
key, err := crypto.ToECDSA(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
return api.w.AddKeyPair(key)
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// DeleteKeyPair removes the key with the given key if it exists.
|
|
|
|
func (api *PublicWhisperAPI) DeleteKeyPair(ctx context.Context, key string) (bool, error) {
|
|
|
|
if ok := api.w.DeleteKeyPair(key); ok {
|
|
|
|
return true, nil
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
return false, fmt.Errorf("key pair %s not found", key)
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// HasKeyPair returns an indication if the node has a key pair that is associated with the given id.
|
|
|
|
func (api *PublicWhisperAPI) HasKeyPair(ctx context.Context, id string) bool {
|
|
|
|
return api.w.HasKeyPair(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)
|
2017-04-09 16:49:22 -05:00
|
|
|
if err != nil {
|
2017-06-13 04:49:07 -05:00
|
|
|
return hexutil.Bytes{}, err
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-04-09 16:49:22 -05:00
|
|
|
return crypto.FromECDSAPub(&key.PublicKey), nil
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 01:17:17 -05:00
|
|
|
// GetPrivateKey returns the private key associated with the given key. The key is the hex
|
2017-06-13 04:49:07 -05:00
|
|
|
// encoded representation of a key in the form specified in section 4.3.6 of ANSI X9.62.
|
|
|
|
func (api *PublicWhisperAPI) GetPrivateKey(ctx context.Context, id string) (hexutil.Bytes, error) {
|
|
|
|
key, err := api.w.GetPrivateKey(id)
|
2017-04-09 16:49:22 -05:00
|
|
|
if err != nil {
|
2017-06-13 04:49:07 -05:00
|
|
|
return hexutil.Bytes{}, err
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
return crypto.FromECDSA(key), nil
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// NewSymKey generate a random symmetric key.
|
|
|
|
// It returns an ID that can be used to refer to the key.
|
|
|
|
// Can be used encrypting and decrypting messages where the key is known to both parties.
|
|
|
|
func (api *PublicWhisperAPI) NewSymKey(ctx context.Context) (string, error) {
|
|
|
|
return api.w.GenerateSymKey()
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// AddSymKey import a symmetric key.
|
|
|
|
// It returns an ID that can be used to refer to the key.
|
|
|
|
// Can be used encrypting and decrypting messages where the key is known to both parties.
|
|
|
|
func (api *PublicWhisperAPI) AddSymKey(ctx context.Context, key hexutil.Bytes) (string, error) {
|
|
|
|
return api.w.AddSymKeyDirect([]byte(key))
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// GenerateSymKeyFromPassword derive a key from the given password, stores it, and returns its ID.
|
|
|
|
func (api *PublicWhisperAPI) GenerateSymKeyFromPassword(ctx context.Context, passwd string) (string, error) {
|
|
|
|
return api.w.AddSymKeyFromPassword(passwd)
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// HasSymKey returns an indication if the node has a symmetric key associated with the given key.
|
|
|
|
func (api *PublicWhisperAPI) HasSymKey(ctx context.Context, id string) bool {
|
|
|
|
return api.w.HasSymKey(id)
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// GetSymKey returns the symmetric key associated with the given id.
|
|
|
|
func (api *PublicWhisperAPI) GetSymKey(ctx context.Context, id string) (hexutil.Bytes, error) {
|
|
|
|
return api.w.GetSymKey(id)
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// DeleteSymKey deletes the symmetric key that is associated with the given id.
|
|
|
|
func (api *PublicWhisperAPI) DeleteSymKey(ctx context.Context, id string) bool {
|
|
|
|
return api.w.DeleteSymKey(id)
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
//go:generate gencodec -type NewMessage -field-override newMessageOverride -out gen_newmessage_json.go
|
|
|
|
|
|
|
|
// NewMessage represents a new whisper message that is posted through the RPC.
|
|
|
|
type NewMessage struct {
|
|
|
|
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"`
|
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
type newMessageOverride struct {
|
|
|
|
PublicKey hexutil.Bytes
|
|
|
|
Payload hexutil.Bytes
|
|
|
|
Padding hexutil.Bytes
|
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2017-08-21 04:22:00 -05:00
|
|
|
// user must specify either a symmetric or an asymmetric key
|
2017-06-13 04:49:07 -05:00
|
|
|
if (symKeyGiven && pubKeyGiven) || (!symKeyGiven && !pubKeyGiven) {
|
2017-06-21 03:32:36 -05:00
|
|
|
return false, ErrSymAsym
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
params := &MessageParams{
|
|
|
|
TTL: req.TTL,
|
|
|
|
Payload: req.Payload,
|
|
|
|
Padding: req.Padding,
|
|
|
|
WorkTime: req.PowTime,
|
|
|
|
PoW: req.PowTarget,
|
|
|
|
Topic: req.Topic,
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// Set key that is used to sign the message
|
|
|
|
if len(req.Sig) > 0 {
|
|
|
|
if params.Src, err = api.w.GetPrivateKey(req.Sig); err != nil {
|
|
|
|
return false, err
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// Set symmetric key that is used to encrypt the message
|
|
|
|
if symKeyGiven {
|
|
|
|
if params.Topic == (TopicType{}) { // topics are mandatory with symmetric encryption
|
2017-06-21 03:32:36 -05:00
|
|
|
return false, ErrNoTopics
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
if params.KeySym, err = api.w.GetSymKey(req.SymKeyID); err != nil {
|
|
|
|
return false, err
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
if !validateSymmetricKey(params.KeySym) {
|
2017-06-21 03:32:36 -05:00
|
|
|
return false, ErrInvalidSymmetricKey
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set asymmetric key that is used to encrypt the message
|
|
|
|
if pubKeyGiven {
|
2018-06-12 08:26:08 -05:00
|
|
|
if params.Dst, err = crypto.UnmarshalPubkey(req.PublicKey); err != nil {
|
2017-06-21 03:32:36 -05:00
|
|
|
return false, ErrInvalidPublicKey
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// encrypt and sent message
|
|
|
|
whisperMsg, err := NewSentMessage(params)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
env, err := whisperMsg.Wrap(params)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// send to specific node (skip PoW check)
|
|
|
|
if len(req.TargetPeer) > 0 {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
n, err := enode.ParseV4(req.TargetPeer)
|
2017-06-13 04:49:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to parse target peer: %s", err)
|
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
return true, api.w.SendP2PMessage(n.ID().Bytes(), env)
|
2017-06-13 04:49:07 -05:00
|
|
|
}
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// ensure that the message PoW meets the node's minimum accepted PoW
|
|
|
|
if req.PowTarget < api.w.MinPow() {
|
2017-06-21 03:32:36 -05:00
|
|
|
return false, ErrTooLowPoW
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
|
|
|
|
return true, api.w.Send(env)
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
//go:generate gencodec -type Criteria -field-override criteriaOverride -out gen_criteria_json.go
|
|
|
|
|
|
|
|
// Criteria holds various filter options for inbound messages.
|
|
|
|
type Criteria struct {
|
|
|
|
SymKeyID string `json:"symKeyID"`
|
|
|
|
PrivateKeyID string `json:"privateKeyID"`
|
|
|
|
Sig []byte `json:"sig"`
|
|
|
|
MinPow float64 `json:"minPow"`
|
|
|
|
Topics []TopicType `json:"topics"`
|
|
|
|
AllowP2P bool `json:"allowP2P"`
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
type criteriaOverride struct {
|
|
|
|
Sig hexutil.Bytes
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// Messages set up a subscription that fires events when messages arrive that match
|
|
|
|
// the given set of criteria.
|
|
|
|
func (api *PublicWhisperAPI) Messages(ctx context.Context, crit Criteria) (*rpc.Subscription, error) {
|
|
|
|
var (
|
|
|
|
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
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-08-21 04:22:00 -05:00
|
|
|
// user must specify either a symmetric or an asymmetric key
|
2017-06-13 04:49:07 -05:00
|
|
|
if (symKeyGiven && pubKeyGiven) || (!symKeyGiven && !pubKeyGiven) {
|
2017-06-21 03:32:36 -05:00
|
|
|
return nil, ErrSymAsym
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
filter := Filter{
|
|
|
|
PoW: crit.MinPow,
|
|
|
|
Messages: make(map[common.Hash]*ReceivedMessage),
|
|
|
|
AllowP2P: crit.AllowP2P,
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
if len(crit.Sig) > 0 {
|
2018-06-12 08:26:08 -05:00
|
|
|
if filter.Src, err = crypto.UnmarshalPubkey(crit.Sig); err != nil {
|
2017-06-21 03:32:36 -05:00
|
|
|
return nil, ErrInvalidSigningPubKey
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
for i, bt := range crit.Topics {
|
|
|
|
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[:])
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// listen for message that are encrypted with the given symmetric key
|
|
|
|
if symKeyGiven {
|
|
|
|
if len(filter.Topics) == 0 {
|
2017-06-21 03:32:36 -05:00
|
|
|
return nil, ErrNoTopics
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
key, err := api.w.GetSymKey(crit.SymKeyID)
|
2017-04-09 16:49:22 -05:00
|
|
|
if err != nil {
|
2017-06-13 04:49:07 -05:00
|
|
|
return nil, err
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
if !validateSymmetricKey(key) {
|
2017-06-21 03:32:36 -05:00
|
|
|
return nil, ErrInvalidSymmetricKey
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
filter.KeySym = key
|
|
|
|
filter.SymKeyHash = crypto.Keccak256Hash(filter.KeySym)
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// listen for messages that are encrypted with the given public key
|
|
|
|
if pubKeyGiven {
|
|
|
|
filter.KeyAsym, err = api.w.GetPrivateKey(crit.PrivateKeyID)
|
|
|
|
if err != nil || filter.KeyAsym == nil {
|
2017-06-21 03:32:36 -05:00
|
|
|
return nil, ErrInvalidPublicKey
|
2017-06-13 04:49:07 -05:00
|
|
|
}
|
2017-04-09 16:49:22 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
|
|
|
|
id, err := api.w.Subscribe(&filter)
|
2016-10-29 07:11:37 -05:00
|
|
|
if err != nil {
|
2017-06-13 04:49:07 -05:00
|
|
|
return nil, err
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-rpcSub.Err():
|
|
|
|
api.w.Unsubscribe(id)
|
|
|
|
return
|
|
|
|
case <-notifier.Closed():
|
|
|
|
api.w.Unsubscribe(id)
|
|
|
|
return
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
}()
|
2016-10-29 07:11:37 -05:00
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
return rpcSub, nil
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
//go:generate gencodec -type Message -field-override messageOverride -out gen_message_json.go
|
|
|
|
|
|
|
|
// Message is the RPC representation of a whisper message.
|
|
|
|
type Message struct {
|
|
|
|
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"`
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:49:07 -05:00
|
|
|
type messageOverride struct {
|
|
|
|
Sig hexutil.Bytes
|
|
|
|
Payload hexutil.Bytes
|
|
|
|
Padding hexutil.Bytes
|
|
|
|
Hash hexutil.Bytes
|
|
|
|
Dst hexutil.Bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToWhisperMessage converts an internal message into an API version.
|
|
|
|
func ToWhisperMessage(message *ReceivedMessage) *Message {
|
|
|
|
msg := Message{
|
|
|
|
Payload: message.Payload,
|
|
|
|
Padding: message.Padding,
|
2017-04-09 16:49:22 -05:00
|
|
|
Timestamp: message.Sent,
|
|
|
|
TTL: message.TTL,
|
|
|
|
PoW: message.PoW,
|
2017-06-13 04:49:07 -05:00
|
|
|
Hash: message.EnvelopeHash.Bytes(),
|
|
|
|
Topic: message.Topic,
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-02-23 02:41:47 -06:00
|
|
|
|
|
|
|
if message.Dst != nil {
|
2017-04-26 14:05:48 -05:00
|
|
|
b := crypto.FromECDSAPub(message.Dst)
|
|
|
|
if b != nil {
|
2017-06-13 04:49:07 -05:00
|
|
|
msg.Dst = b
|
2017-04-26 14:05:48 -05:00
|
|
|
}
|
2017-02-23 02:41:47 -06:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
|
2017-02-23 02:41:47 -06:00
|
|
|
if isMessageSigned(message.Raw[0]) {
|
2017-04-26 14:05:48 -05:00
|
|
|
b := crypto.FromECDSAPub(message.SigToPubKey())
|
|
|
|
if b != nil {
|
2017-06-13 04:49:07 -05:00
|
|
|
msg.Sig = b
|
2017-04-26 14:05:48 -05:00
|
|
|
}
|
2017-02-23 02:41:47 -06:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
|
2017-02-23 02:41:47 -06:00
|
|
|
return &msg
|
2016-10-29 07:11:37 -05:00
|
|
|
}
|
2017-06-13 04:49:07 -05:00
|
|
|
|
|
|
|
// toMessage converts a set of messages to its RPC representation.
|
|
|
|
func toMessage(messages []*ReceivedMessage) []*Message {
|
|
|
|
msgs := make([]*Message, len(messages))
|
|
|
|
for i, msg := range messages {
|
|
|
|
msgs[i] = ToWhisperMessage(msg)
|
|
|
|
}
|
|
|
|
return msgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilterMessages returns the messages that match the filter criteria and
|
|
|
|
// are received between the last poll and now.
|
|
|
|
func (api *PublicWhisperAPI) GetFilterMessages(id string) ([]*Message, error) {
|
|
|
|
api.mu.Lock()
|
|
|
|
f := api.w.GetFilter(id)
|
|
|
|
if f == nil {
|
|
|
|
api.mu.Unlock()
|
|
|
|
return nil, fmt.Errorf("filter not found")
|
|
|
|
}
|
|
|
|
api.lastUsed[id] = time.Now()
|
|
|
|
api.mu.Unlock()
|
|
|
|
|
|
|
|
receivedMessages := f.Retrieve()
|
|
|
|
messages := make([]*Message, 0, len(receivedMessages))
|
|
|
|
for _, msg := range receivedMessages {
|
|
|
|
messages = append(messages, ToWhisperMessage(msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageFilter creates a new filter that can be used to poll for
|
|
|
|
// (new) messages that satisfy the given criteria.
|
|
|
|
func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) {
|
|
|
|
var (
|
|
|
|
src *ecdsa.PublicKey
|
|
|
|
keySym []byte
|
|
|
|
keyAsym *ecdsa.PrivateKey
|
|
|
|
topics [][]byte
|
|
|
|
|
|
|
|
symKeyGiven = len(req.SymKeyID) > 0
|
|
|
|
asymKeyGiven = len(req.PrivateKeyID) > 0
|
|
|
|
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2017-08-21 04:22:00 -05:00
|
|
|
// user must specify either a symmetric or an asymmetric key
|
2017-06-13 04:49:07 -05:00
|
|
|
if (symKeyGiven && asymKeyGiven) || (!symKeyGiven && !asymKeyGiven) {
|
2017-06-21 03:32:36 -05:00
|
|
|
return "", ErrSymAsym
|
2017-06-13 04:49:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Sig) > 0 {
|
2018-06-12 08:26:08 -05:00
|
|
|
if src, err = crypto.UnmarshalPubkey(req.Sig); err != nil {
|
2017-06-21 03:32:36 -05:00
|
|
|
return "", ErrInvalidSigningPubKey
|
2017-06-13 04:49:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if symKeyGiven {
|
|
|
|
if keySym, err = api.w.GetSymKey(req.SymKeyID); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !validateSymmetricKey(keySym) {
|
2017-06-21 03:32:36 -05:00
|
|
|
return "", ErrInvalidSymmetricKey
|
2017-06-13 04:49:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if asymKeyGiven {
|
|
|
|
if keyAsym, err = api.w.GetPrivateKey(req.PrivateKeyID); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Topics) > 0 {
|
2018-01-26 05:41:53 -06:00
|
|
|
topics = make([][]byte, 0, len(req.Topics))
|
2017-06-13 04:49:07 -05:00
|
|
|
for _, topic := range req.Topics {
|
|
|
|
topics = append(topics, topic[:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f := &Filter{
|
|
|
|
Src: src,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
api.mu.Lock()
|
|
|
|
api.lastUsed[id] = time.Now()
|
|
|
|
api.mu.Unlock()
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|