mirror of https://github.com/ethereum/go-ethereum
Merge pull request #14540 from bas-vk/whisper-api
whisperv5: integrate whisper and implement APIpull/14646/head
commit
feb2932706
@ -0,0 +1,27 @@ |
||||
Copyright (c) 2009 The Go Authors. All rights reserved. |
||||
|
||||
Redistribution and use in source and binary forms, with or without |
||||
modification, are permitted provided that the following conditions are |
||||
met: |
||||
|
||||
* Redistributions of source code must retain the above copyright |
||||
notice, this list of conditions and the following disclaimer. |
||||
* Redistributions in binary form must reproduce the above |
||||
copyright notice, this list of conditions and the following disclaimer |
||||
in the documentation and/or other materials provided with the |
||||
distribution. |
||||
* Neither the name of Google Inc. nor the names of its |
||||
contributors may be used to endorse or promote products derived from |
||||
this software without specific prior written permission. |
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
@ -0,0 +1,22 @@ |
||||
Additional IP Rights Grant (Patents) |
||||
|
||||
"This implementation" means the copyrightable works distributed by |
||||
Google as part of the Go project. |
||||
|
||||
Google hereby grants to You a perpetual, worldwide, non-exclusive, |
||||
no-charge, royalty-free, irrevocable (except as stated in this section) |
||||
patent license to make, have made, use, offer to sell, sell, import, |
||||
transfer and otherwise run, modify and propagate the contents of this |
||||
implementation of Go, where such license applies only to those patent |
||||
claims, both currently owned or controlled by Google and acquired in |
||||
the future, licensable by Google that are necessarily infringed by this |
||||
implementation of Go. This grant does not include claims that would be |
||||
infringed only as a consequence of further modification of this |
||||
implementation. If you or your agent or exclusive licensee institute or |
||||
order or agree to the institution of patent litigation against any |
||||
entity (including a cross-claim or counterclaim in a lawsuit) alleging |
||||
that this implementation of Go or any code incorporated within this |
||||
implementation of Go constitutes direct or contributory patent |
||||
infringement, or inducement of patent infringement, then any patent |
||||
rights granted to you under this License for this implementation of Go |
||||
shall terminate as of the date such litigation is filed. |
@ -0,0 +1,372 @@ |
||||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package syncmap provides a concurrent map implementation.
|
||||
// It is a prototype for a proposed addition to the sync package
|
||||
// in the standard library.
|
||||
// (https://golang.org/issue/18177)
|
||||
package syncmap |
||||
|
||||
import ( |
||||
"sync" |
||||
"sync/atomic" |
||||
"unsafe" |
||||
) |
||||
|
||||
// Map is a concurrent map with amortized-constant-time loads, stores, and deletes.
|
||||
// It is safe for multiple goroutines to call a Map's methods concurrently.
|
||||
//
|
||||
// The zero Map is valid and empty.
|
||||
//
|
||||
// A Map must not be copied after first use.
|
||||
type Map struct { |
||||
mu sync.Mutex |
||||
|
||||
// read contains the portion of the map's contents that are safe for
|
||||
// concurrent access (with or without mu held).
|
||||
//
|
||||
// The read field itself is always safe to load, but must only be stored with
|
||||
// mu held.
|
||||
//
|
||||
// Entries stored in read may be updated concurrently without mu, but updating
|
||||
// a previously-expunged entry requires that the entry be copied to the dirty
|
||||
// map and unexpunged with mu held.
|
||||
read atomic.Value // readOnly
|
||||
|
||||
// dirty contains the portion of the map's contents that require mu to be
|
||||
// held. To ensure that the dirty map can be promoted to the read map quickly,
|
||||
// it also includes all of the non-expunged entries in the read map.
|
||||
//
|
||||
// Expunged entries are not stored in the dirty map. An expunged entry in the
|
||||
// clean map must be unexpunged and added to the dirty map before a new value
|
||||
// can be stored to it.
|
||||
//
|
||||
// If the dirty map is nil, the next write to the map will initialize it by
|
||||
// making a shallow copy of the clean map, omitting stale entries.
|
||||
dirty map[interface{}]*entry |
||||
|
||||
// misses counts the number of loads since the read map was last updated that
|
||||
// needed to lock mu to determine whether the key was present.
|
||||
//
|
||||
// Once enough misses have occurred to cover the cost of copying the dirty
|
||||
// map, the dirty map will be promoted to the read map (in the unamended
|
||||
// state) and the next store to the map will make a new dirty copy.
|
||||
misses int |
||||
} |
||||
|
||||
// readOnly is an immutable struct stored atomically in the Map.read field.
|
||||
type readOnly struct { |
||||
m map[interface{}]*entry |
||||
amended bool // true if the dirty map contains some key not in m.
|
||||
} |
||||
|
||||
// expunged is an arbitrary pointer that marks entries which have been deleted
|
||||
// from the dirty map.
|
||||
var expunged = unsafe.Pointer(new(interface{})) |
||||
|
||||
// An entry is a slot in the map corresponding to a particular key.
|
||||
type entry struct { |
||||
// p points to the interface{} value stored for the entry.
|
||||
//
|
||||
// If p == nil, the entry has been deleted and m.dirty == nil.
|
||||
//
|
||||
// If p == expunged, the entry has been deleted, m.dirty != nil, and the entry
|
||||
// is missing from m.dirty.
|
||||
//
|
||||
// Otherwise, the entry is valid and recorded in m.read.m[key] and, if m.dirty
|
||||
// != nil, in m.dirty[key].
|
||||
//
|
||||
// An entry can be deleted by atomic replacement with nil: when m.dirty is
|
||||
// next created, it will atomically replace nil with expunged and leave
|
||||
// m.dirty[key] unset.
|
||||
//
|
||||
// An entry's associated value can be updated by atomic replacement, provided
|
||||
// p != expunged. If p == expunged, an entry's associated value can be updated
|
||||
// only after first setting m.dirty[key] = e so that lookups using the dirty
|
||||
// map find the entry.
|
||||
p unsafe.Pointer // *interface{}
|
||||
} |
||||
|
||||
func newEntry(i interface{}) *entry { |
||||
return &entry{p: unsafe.Pointer(&i)} |
||||
} |
||||
|
||||
// Load returns the value stored in the map for a key, or nil if no
|
||||
// value is present.
|
||||
// The ok result indicates whether value was found in the map.
|
||||
func (m *Map) Load(key interface{}) (value interface{}, ok bool) { |
||||
read, _ := m.read.Load().(readOnly) |
||||
e, ok := read.m[key] |
||||
if !ok && read.amended { |
||||
m.mu.Lock() |
||||
// Avoid reporting a spurious miss if m.dirty got promoted while we were
|
||||
// blocked on m.mu. (If further loads of the same key will not miss, it's
|
||||
// not worth copying the dirty map for this key.)
|
||||
read, _ = m.read.Load().(readOnly) |
||||
e, ok = read.m[key] |
||||
if !ok && read.amended { |
||||
e, ok = m.dirty[key] |
||||
// Regardless of whether the entry was present, record a miss: this key
|
||||
// will take the slow path until the dirty map is promoted to the read
|
||||
// map.
|
||||
m.missLocked() |
||||
} |
||||
m.mu.Unlock() |
||||
} |
||||
if !ok { |
||||
return nil, false |
||||
} |
||||
return e.load() |
||||
} |
||||
|
||||
func (e *entry) load() (value interface{}, ok bool) { |
||||
p := atomic.LoadPointer(&e.p) |
||||
if p == nil || p == expunged { |
||||
return nil, false |
||||
} |
||||
return *(*interface{})(p), true |
||||
} |
||||
|
||||
// Store sets the value for a key.
|
||||
func (m *Map) Store(key, value interface{}) { |
||||
read, _ := m.read.Load().(readOnly) |
||||
if e, ok := read.m[key]; ok && e.tryStore(&value) { |
||||
return |
||||
} |
||||
|
||||
m.mu.Lock() |
||||
read, _ = m.read.Load().(readOnly) |
||||
if e, ok := read.m[key]; ok { |
||||
if e.unexpungeLocked() { |
||||
// The entry was previously expunged, which implies that there is a
|
||||
// non-nil dirty map and this entry is not in it.
|
||||
m.dirty[key] = e |
||||
} |
||||
e.storeLocked(&value) |
||||
} else if e, ok := m.dirty[key]; ok { |
||||
e.storeLocked(&value) |
||||
} else { |
||||
if !read.amended { |
||||
// We're adding the first new key to the dirty map.
|
||||
// Make sure it is allocated and mark the read-only map as incomplete.
|
||||
m.dirtyLocked() |
||||
m.read.Store(readOnly{m: read.m, amended: true}) |
||||
} |
||||
m.dirty[key] = newEntry(value) |
||||
} |
||||
m.mu.Unlock() |
||||
} |
||||
|
||||
// tryStore stores a value if the entry has not been expunged.
|
||||
//
|
||||
// If the entry is expunged, tryStore returns false and leaves the entry
|
||||
// unchanged.
|
||||
func (e *entry) tryStore(i *interface{}) bool { |
||||
p := atomic.LoadPointer(&e.p) |
||||
if p == expunged { |
||||
return false |
||||
} |
||||
for { |
||||
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) { |
||||
return true |
||||
} |
||||
p = atomic.LoadPointer(&e.p) |
||||
if p == expunged { |
||||
return false |
||||
} |
||||
} |
||||
} |
||||
|
||||
// unexpungeLocked ensures that the entry is not marked as expunged.
|
||||
//
|
||||
// If the entry was previously expunged, it must be added to the dirty map
|
||||
// before m.mu is unlocked.
|
||||
func (e *entry) unexpungeLocked() (wasExpunged bool) { |
||||
return atomic.CompareAndSwapPointer(&e.p, expunged, nil) |
||||
} |
||||
|
||||
// storeLocked unconditionally stores a value to the entry.
|
||||
//
|
||||
// The entry must be known not to be expunged.
|
||||
func (e *entry) storeLocked(i *interface{}) { |
||||
atomic.StorePointer(&e.p, unsafe.Pointer(i)) |
||||
} |
||||
|
||||
// LoadOrStore returns the existing value for the key if present.
|
||||
// Otherwise, it stores and returns the given value.
|
||||
// The loaded result is true if the value was loaded, false if stored.
|
||||
func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) { |
||||
// Avoid locking if it's a clean hit.
|
||||
read, _ := m.read.Load().(readOnly) |
||||
if e, ok := read.m[key]; ok { |
||||
actual, loaded, ok := e.tryLoadOrStore(value) |
||||
if ok { |
||||
return actual, loaded |
||||
} |
||||
} |
||||
|
||||
m.mu.Lock() |
||||
read, _ = m.read.Load().(readOnly) |
||||
if e, ok := read.m[key]; ok { |
||||
if e.unexpungeLocked() { |
||||
m.dirty[key] = e |
||||
} |
||||
actual, loaded, _ = e.tryLoadOrStore(value) |
||||
} else if e, ok := m.dirty[key]; ok { |
||||
actual, loaded, _ = e.tryLoadOrStore(value) |
||||
m.missLocked() |
||||
} else { |
||||
if !read.amended { |
||||
// We're adding the first new key to the dirty map.
|
||||
// Make sure it is allocated and mark the read-only map as incomplete.
|
||||
m.dirtyLocked() |
||||
m.read.Store(readOnly{m: read.m, amended: true}) |
||||
} |
||||
m.dirty[key] = newEntry(value) |
||||
actual, loaded = value, false |
||||
} |
||||
m.mu.Unlock() |
||||
|
||||
return actual, loaded |
||||
} |
||||
|
||||
// tryLoadOrStore atomically loads or stores a value if the entry is not
|
||||
// expunged.
|
||||
//
|
||||
// If the entry is expunged, tryLoadOrStore leaves the entry unchanged and
|
||||
// returns with ok==false.
|
||||
func (e *entry) tryLoadOrStore(i interface{}) (actual interface{}, loaded, ok bool) { |
||||
p := atomic.LoadPointer(&e.p) |
||||
if p == expunged { |
||||
return nil, false, false |
||||
} |
||||
if p != nil { |
||||
return *(*interface{})(p), true, true |
||||
} |
||||
|
||||
// Copy the interface after the first load to make this method more amenable
|
||||
// to escape analysis: if we hit the "load" path or the entry is expunged, we
|
||||
// shouldn't bother heap-allocating.
|
||||
ic := i |
||||
for { |
||||
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) { |
||||
return i, false, true |
||||
} |
||||
p = atomic.LoadPointer(&e.p) |
||||
if p == expunged { |
||||
return nil, false, false |
||||
} |
||||
if p != nil { |
||||
return *(*interface{})(p), true, true |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Delete deletes the value for a key.
|
||||
func (m *Map) Delete(key interface{}) { |
||||
read, _ := m.read.Load().(readOnly) |
||||
e, ok := read.m[key] |
||||
if !ok && read.amended { |
||||
m.mu.Lock() |
||||
read, _ = m.read.Load().(readOnly) |
||||
e, ok = read.m[key] |
||||
if !ok && read.amended { |
||||
delete(m.dirty, key) |
||||
} |
||||
m.mu.Unlock() |
||||
} |
||||
if ok { |
||||
e.delete() |
||||
} |
||||
} |
||||
|
||||
func (e *entry) delete() (hadValue bool) { |
||||
for { |
||||
p := atomic.LoadPointer(&e.p) |
||||
if p == nil || p == expunged { |
||||
return false |
||||
} |
||||
if atomic.CompareAndSwapPointer(&e.p, p, nil) { |
||||
return true |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Range calls f sequentially for each key and value present in the map.
|
||||
// If f returns false, range stops the iteration.
|
||||
//
|
||||
// Range does not necessarily correspond to any consistent snapshot of the Map's
|
||||
// contents: no key will be visited more than once, but if the value for any key
|
||||
// is stored or deleted concurrently, Range may reflect any mapping for that key
|
||||
// from any point during the Range call.
|
||||
//
|
||||
// Range may be O(N) with the number of elements in the map even if f returns
|
||||
// false after a constant number of calls.
|
||||
func (m *Map) Range(f func(key, value interface{}) bool) { |
||||
// We need to be able to iterate over all of the keys that were already
|
||||
// present at the start of the call to Range.
|
||||
// If read.amended is false, then read.m satisfies that property without
|
||||
// requiring us to hold m.mu for a long time.
|
||||
read, _ := m.read.Load().(readOnly) |
||||
if read.amended { |
||||
// m.dirty contains keys not in read.m. Fortunately, Range is already O(N)
|
||||
// (assuming the caller does not break out early), so a call to Range
|
||||
// amortizes an entire copy of the map: we can promote the dirty copy
|
||||
// immediately!
|
||||
m.mu.Lock() |
||||
read, _ = m.read.Load().(readOnly) |
||||
if read.amended { |
||||
read = readOnly{m: m.dirty} |
||||
m.read.Store(read) |
||||
m.dirty = nil |
||||
m.misses = 0 |
||||
} |
||||
m.mu.Unlock() |
||||
} |
||||
|
||||
for k, e := range read.m { |
||||
v, ok := e.load() |
||||
if !ok { |
||||
continue |
||||
} |
||||
if !f(k, v) { |
||||
break |
||||
} |
||||
} |
||||
} |
||||
|
||||
func (m *Map) missLocked() { |
||||
m.misses++ |
||||
if m.misses < len(m.dirty) { |
||||
return |
||||
} |
||||
m.read.Store(readOnly{m: m.dirty}) |
||||
m.dirty = nil |
||||
m.misses = 0 |
||||
} |
||||
|
||||
func (m *Map) dirtyLocked() { |
||||
if m.dirty != nil { |
||||
return |
||||
} |
||||
|
||||
read, _ := m.read.Load().(readOnly) |
||||
m.dirty = make(map[interface{}]*entry, len(read.m)) |
||||
for k, e := range read.m { |
||||
if !e.tryExpungeLocked() { |
||||
m.dirty[k] = e |
||||
} |
||||
} |
||||
} |
||||
|
||||
func (e *entry) tryExpungeLocked() (isExpunged bool) { |
||||
p := atomic.LoadPointer(&e.p) |
||||
for p == nil { |
||||
if atomic.CompareAndSwapPointer(&e.p, nil, expunged) { |
||||
return true |
||||
} |
||||
p = atomic.LoadPointer(&e.p) |
||||
} |
||||
return p == expunged |
||||
} |
@ -0,0 +1,194 @@ |
||||
// Copyright 2017 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/>.
|
||||
|
||||
package shhclient |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/rpc" |
||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" |
||||
) |
||||
|
||||
// Client defines typed wrappers for the Whisper v5 RPC API.
|
||||
type Client struct { |
||||
c *rpc.Client |
||||
} |
||||
|
||||
// Dial connects a client to the given URL.
|
||||
func Dial(rawurl string) (*Client, error) { |
||||
c, err := rpc.Dial(rawurl) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return NewClient(c), nil |
||||
} |
||||
|
||||
// NewClient creates a client that uses the given RPC client.
|
||||
func NewClient(c *rpc.Client) *Client { |
||||
return &Client{c} |
||||
} |
||||
|
||||
// Version returns the Whisper sub-protocol version.
|
||||
func (sc *Client) Version(ctx context.Context) (uint, error) { |
||||
var result uint |
||||
err := sc.c.CallContext(ctx, &result, "shh_version") |
||||
return result, err |
||||
} |
||||
|
||||
// Info returns diagnostic information about the whisper node.
|
||||
func (sc *Client) Info(ctx context.Context) (whisper.Info, error) { |
||||
var info whisper.Info |
||||
err := sc.c.CallContext(ctx, &info, "shh_info") |
||||
return info, err |
||||
} |
||||
|
||||
// SetMaxMessageSize sets the maximal message size allowed by this node. Incoming
|
||||
// and outgoing messages with a larger size will be rejected. Whisper message size
|
||||
// can never exceed the limit imposed by the underlying P2P protocol (10 Mb).
|
||||
func (sc *Client) SetMaxMessageSize(ctx context.Context, size uint32) error { |
||||
var ignored bool |
||||
return sc.c.CallContext(ctx, &ignored, "shh_setMaxMessageSize", size) |
||||
} |
||||
|
||||
// SetMinimumPoW (experimental) sets the minimal PoW required by this node.
|
||||
|
||||
// This experimental function was introduced for the future dynamic adjustment of
|
||||
// PoW requirement. If the node is overwhelmed with messages, it should raise the
|
||||
// PoW requirement and notify the peers. The new value should be set relative to
|
||||
// the old value (e.g. double). The old value could be obtained via shh_info call.
|
||||
func (sc *Client) SetMinimumPoW(ctx context.Context, pow float64) error { |
||||
var ignored bool |
||||
return sc.c.CallContext(ctx, &ignored, "shh_setMinPoW", pow) |
||||
} |
||||
|
||||
// Marks specific 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 (sc *Client) MarkTrustedPeer(ctx context.Context, enode string) error { |
||||
var ignored bool |
||||
return sc.c.CallContext(ctx, &ignored, "shh_markTrustedPeer", enode) |
||||
} |
||||
|
||||
// NewKeyPair generates a new public and private key pair for message decryption and encryption.
|
||||
// It returns an identifier that can be used to refer to the key.
|
||||
func (sc *Client) NewKeyPair(ctx context.Context) (string, error) { |
||||
var id string |
||||
return id, sc.c.CallContext(ctx, &id, "shh_newKeyPair") |
||||
} |
||||
|
||||
// AddPrivateKey stored the key pair, and returns its ID.
|
||||
func (sc *Client) AddPrivateKey(ctx context.Context, key []byte) (string, error) { |
||||
var id string |
||||
return id, sc.c.CallContext(ctx, &id, "shh_addPrivateKey", hexutil.Bytes(key)) |
||||
} |
||||
|
||||
// DeleteKeyPair delete the specifies key.
|
||||
func (sc *Client) DeleteKeyPair(ctx context.Context, id string) (string, error) { |
||||
var ignored bool |
||||
return id, sc.c.CallContext(ctx, &ignored, "shh_deleteKeyPair", id) |
||||
} |
||||
|
||||
// HasKeyPair returns an indication if the node has a private key or
|
||||
// key pair matching the given ID.
|
||||
func (sc *Client) HasKeyPair(ctx context.Context, id string) (bool, error) { |
||||
var has bool |
||||
return has, sc.c.CallContext(ctx, &has, "shh_hasKeyPair", id) |
||||
} |
||||
|
||||
// PublicKey return the public key for a key ID.
|
||||
func (sc *Client) PublicKey(ctx context.Context, id string) ([]byte, error) { |
||||
var key hexutil.Bytes |
||||
return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPublicKey", id) |
||||
} |
||||
|
||||
// PrivateKey return the private key for a key ID.
|
||||
func (sc *Client) PrivateKey(ctx context.Context, id string) ([]byte, error) { |
||||
var key hexutil.Bytes |
||||
return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPrivateKey", id) |
||||
} |
||||
|
||||
// NewSymmetricKey generates a random symmetric key and returns its identifier.
|
||||
// Can be used encrypting and decrypting messages where the key is known to both parties.
|
||||
func (sc *Client) NewSymmetricKey(ctx context.Context) (string, error) { |
||||
var id string |
||||
return id, sc.c.CallContext(ctx, &id, "shh_newSymKey") |
||||
} |
||||
|
||||
// AddSymmetricKey stores the key, and returns its identifier.
|
||||
func (sc *Client) AddSymmetricKey(ctx context.Context, key []byte) (string, error) { |
||||
var id string |
||||
return id, sc.c.CallContext(ctx, &id, "shh_addSymKey", hexutil.Bytes(key)) |
||||
} |
||||
|
||||
// GenerateSymmetricKeyFromPassword generates the key from password, stores it, and returns its identifier.
|
||||
func (sc *Client) GenerateSymmetricKeyFromPassword(ctx context.Context, passwd []byte) (string, error) { |
||||
var id string |
||||
return id, sc.c.CallContext(ctx, &id, "shh_generateSymKeyFromPassword", hexutil.Bytes(passwd)) |
||||
} |
||||
|
||||
// HasSymmetricKey returns an indication if the key associated with the given id is stored in the node.
|
||||
func (sc *Client) HasSymmetricKey(ctx context.Context, id string) (bool, error) { |
||||
var found bool |
||||
return found, sc.c.CallContext(ctx, &found, "shh_hasSymKey", id) |
||||
} |
||||
|
||||
// GetSymmetricKey returns the symmetric key associated with the given identifier.
|
||||
func (sc *Client) GetSymmetricKey(ctx context.Context, id string) ([]byte, error) { |
||||
var key hexutil.Bytes |
||||
return []byte(key), sc.c.CallContext(ctx, &key, "shh_getSymKey", id) |
||||
} |
||||
|
||||
// DeleteSymmetricKey deletes the symmetric key associated with the given identifier.
|
||||
func (sc *Client) DeleteSymmetricKey(ctx context.Context, id string) error { |
||||
var ignored bool |
||||
return sc.c.CallContext(ctx, &ignored, "shh_deleteSymKey", id) |
||||
} |
||||
|
||||
// Post a message onto the network.
|
||||
func (sc *Client) Post(ctx context.Context, message whisper.NewMessage) error { |
||||
var ignored bool |
||||
return sc.c.CallContext(ctx, &ignored, "shh_post", message) |
||||
} |
||||
|
||||
// SubscribeMessages subscribes to messages that match the given criteria. This method
|
||||
// is only supported on bi-directional connections such as websockets and IPC.
|
||||
// NewMessageFilter uses polling and is supported over HTTP.
|
||||
func (ec *Client) SubscribeMessages(ctx context.Context, criteria whisper.Criteria, ch chan<- *whisper.Message) (ethereum.Subscription, error) { |
||||
return ec.c.ShhSubscribe(ctx, ch, "messages", criteria) |
||||
} |
||||
|
||||
// NewMessageFilter creates a filter within the node. This filter can be used to poll
|
||||
// for new messages (see FilterMessages) that satisfy the given criteria. A filter can
|
||||
// timeout when it was polled for in whisper.filterTimeout.
|
||||
func (ec *Client) NewMessageFilter(ctx context.Context, criteria whisper.Criteria) (string, error) { |
||||
var id string |
||||
return id, ec.c.CallContext(ctx, &id, "shh_newMessageFilter", criteria) |
||||
} |
||||
|
||||
// DeleteMessageFilter removes the filter associated with the given id.
|
||||
func (ec *Client) DeleteMessageFilter(ctx context.Context, id string) error { |
||||
var ignored bool |
||||
return ec.c.CallContext(ctx, &ignored, "shh_deleteMessageFilter", id) |
||||
} |
||||
|
||||
// FilterMessages retrieves all messages that are received between the last call to
|
||||
// this function and match the criteria that where given when the filter was created.
|
||||
func (ec *Client) FilterMessages(ctx context.Context, id string) ([]*whisper.Message, error) { |
||||
var messages []*whisper.Message |
||||
return messages, ec.c.CallContext(ctx, &messages, "shh_getFilterMessages", id) |
||||
} |
@ -1,680 +0,0 @@ |
||||
// 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/>.
|
||||
|
||||
package whisperv5 |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/json" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
func TestBasic(t *testing.T) { |
||||
var id string = "test" |
||||
w := New() |
||||
api := NewPublicWhisperAPI(w) |
||||
if api == nil { |
||||
t.Fatalf("failed to create API.") |
||||
} |
||||
|
||||
ver, err := api.Version() |
||||
if err != nil { |
||||
t.Fatalf("failed generateFilter: %s.", err) |
||||
} |
||||
|
||||
if uint64(ver) != ProtocolVersion { |
||||
t.Fatalf("wrong version: %d.", ver) |
||||
} |
||||
|
||||
mail := api.GetNewSubscriptionMessages("non-existent-id") |
||||
if len(mail) != 0 { |
||||
t.Fatalf("failed GetFilterChanges: premature result") |
||||
} |
||||
|
||||
exist, err := api.HasKeyPair(id) |
||||
if err != nil { |
||||
t.Fatalf("failed initial HasIdentity: %s.", err) |
||||
} |
||||
if exist { |
||||
t.Fatalf("failed initial HasIdentity: false positive.") |
||||
} |
||||
|
||||
success, err := api.DeleteKeyPair(id) |
||||
if err != nil { |
||||
t.Fatalf("failed DeleteIdentity: %s.", err) |
||||
} |
||||
if success { |
||||
t.Fatalf("deleted non-existing identity: false positive.") |
||||
} |
||||
|
||||
pub, err := api.NewKeyPair() |
||||
if err != nil { |
||||
t.Fatalf("failed NewIdentity: %s.", err) |
||||
} |
||||
if len(pub) == 0 { |
||||
t.Fatalf("failed NewIdentity: empty") |
||||
} |
||||
|
||||
exist, err = api.HasKeyPair(pub) |
||||
if err != nil { |
||||
t.Fatalf("failed HasIdentity: %s.", err) |
||||
} |
||||
if !exist { |
||||
t.Fatalf("failed HasIdentity: false negative.") |
||||
} |
||||
|
||||
success, err = api.DeleteKeyPair(pub) |
||||
if err != nil { |
||||
t.Fatalf("failed to delete second identity: %s.", err) |
||||
} |
||||
if !success { |
||||
t.Fatalf("failed to delete second identity.") |
||||
} |
||||
|
||||
exist, err = api.HasKeyPair(pub) |
||||
if err != nil { |
||||
t.Fatalf("failed HasIdentity(): %s.", err) |
||||
} |
||||
if exist { |
||||
t.Fatalf("failed HasIdentity(): false positive.") |
||||
} |
||||
|
||||
id = "arbitrary text" |
||||
id2 := "another arbitrary string" |
||||
|
||||
exist, err = api.HasSymmetricKey(id) |
||||
if err != nil { |
||||
t.Fatalf("failed HasSymKey: %s.", err) |
||||
} |
||||
if exist { |
||||
t.Fatalf("failed HasSymKey: false positive.") |
||||
} |
||||
|
||||
id, err = api.GenerateSymmetricKey() |
||||
if err != nil { |
||||
t.Fatalf("failed GenerateSymKey: %s.", err) |
||||
} |
||||
|
||||
exist, err = api.HasSymmetricKey(id) |
||||
if err != nil { |
||||
t.Fatalf("failed HasSymKey(): %s.", err) |
||||
} |
||||
if !exist { |
||||
t.Fatalf("failed HasSymKey(): false negative.") |
||||
} |
||||
|
||||
const password = "some stuff here" |
||||
id, err = api.AddSymmetricKeyFromPassword(password) |
||||
if err != nil { |
||||
t.Fatalf("failed AddSymKey: %s.", err) |
||||
} |
||||
|
||||
id2, err = api.AddSymmetricKeyFromPassword(password) |
||||
if err != nil { |
||||
t.Fatalf("failed AddSymKey: %s.", err) |
||||
} |
||||
|
||||
exist, err = api.HasSymmetricKey(id2) |
||||
if err != nil { |
||||
t.Fatalf("failed HasSymKey(id2): %s.", err) |
||||
} |
||||
if !exist { |
||||
t.Fatalf("failed HasSymKey(id2): false negative.") |
||||
} |
||||
|
||||
k1, err := api.GetSymmetricKey(id) |
||||
if err != nil { |
||||
t.Fatalf("failed GetSymKey(id): %s.", err) |
||||
} |
||||
k2, err := api.GetSymmetricKey(id2) |
||||
if err != nil { |
||||
t.Fatalf("failed GetSymKey(id2): %s.", err) |
||||
} |
||||
|
||||
if !bytes.Equal(k1, k2) { |
||||
t.Fatalf("installed keys are not equal") |
||||
} |
||||
|
||||
exist, err = api.DeleteSymmetricKey(id) |
||||
if err != nil { |
||||
t.Fatalf("failed DeleteSymKey(id): %s.", err) |
||||
} |
||||
if !exist { |
||||
t.Fatalf("failed DeleteSymKey(id): false negative.") |
||||
} |
||||
|
||||
exist, err = api.HasSymmetricKey(id) |
||||
if err != nil { |
||||
t.Fatalf("failed HasSymKey(id): %s.", err) |
||||
} |
||||
if exist { |
||||
t.Fatalf("failed HasSymKey(id): false positive.") |
||||
} |
||||
} |
||||
|
||||
func TestUnmarshalFilterArgs(t *testing.T) { |
||||
s := []byte(`{ |
||||
"type":"sym", |
||||
"key":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d", |
||||
"sig":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83", |
||||
"minPoW":2.34, |
||||
"topics":["0x00000000", "0x007f80ff", "0xff807f00", "0xf26e7779"], |
||||
"allowP2P":true |
||||
}`) |
||||
|
||||
var f WhisperFilterArgs |
||||
err := f.UnmarshalJSON(s) |
||||
if err != nil { |
||||
t.Fatalf("failed UnmarshalJSON: %s.", err) |
||||
} |
||||
|
||||
if !f.Symmetric { |
||||
t.Fatalf("wrong type.") |
||||
} |
||||
if f.Key != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" { |
||||
t.Fatalf("wrong key: %s.", f.Key) |
||||
} |
||||
if f.Sig != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" { |
||||
t.Fatalf("wrong sig: %s.", f.Sig) |
||||
} |
||||
if f.MinPoW != 2.34 { |
||||
t.Fatalf("wrong MinPoW: %f.", f.MinPoW) |
||||
} |
||||
if !f.AllowP2P { |
||||
t.Fatalf("wrong AllowP2P.") |
||||
} |
||||
if len(f.Topics) != 4 { |
||||
t.Fatalf("wrong topics number: %d.", len(f.Topics)) |
||||
} |
||||
|
||||
i := 0 |
||||
if !bytes.Equal(f.Topics[i], []byte{0x00, 0x00, 0x00, 0x00}) { |
||||
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) |
||||
} |
||||
|
||||
i++ |
||||
if !bytes.Equal(f.Topics[i], []byte{0x00, 0x7f, 0x80, 0xff}) { |
||||
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) |
||||
} |
||||
|
||||
i++ |
||||
if !bytes.Equal(f.Topics[i], []byte{0xff, 0x80, 0x7f, 0x00}) { |
||||
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) |
||||
} |
||||
|
||||
i++ |
||||
if !bytes.Equal(f.Topics[i], []byte{0xf2, 0x6e, 0x77, 0x79}) { |
||||
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) |
||||
} |
||||
} |
||||
|
||||
func TestUnmarshalPostArgs(t *testing.T) { |
||||
s := []byte(`{ |
||||
"type":"sym", |
||||
"ttl":12345, |
||||
"sig":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d", |
||||
"key":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83", |
||||
"topic":"0xf26e7779", |
||||
"padding":"0x74686973206973206D79207465737420737472696E67", |
||||
"payload":"0x7061796C6F61642073686F756C642062652070736575646F72616E646F6D", |
||||
"powTime":777, |
||||
"powTarget":3.1416, |
||||
"targetPeer":"enode://915533f667b1369793ebb9bda022416b1295235a1420799cd87a969467372546d808ebf59c5c9ce23f103d59b61b97df8af91f0908552485975397181b993461@127.0.0.1:12345" |
||||
}`) |
||||
|
||||
var a PostArgs |
||||
err := json.Unmarshal(s, &a) |
||||
if err != nil { |
||||
t.Fatalf("failed UnmarshalJSON: %s.", err) |
||||
} |
||||
|
||||
if a.Type != "sym" { |
||||
t.Fatalf("wrong Type: %s.", a.Type) |
||||
} |
||||
if a.TTL != 12345 { |
||||
t.Fatalf("wrong ttl: %d.", a.TTL) |
||||
} |
||||
if a.Sig != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" { |
||||
t.Fatalf("wrong From: %s.", a.Sig) |
||||
} |
||||
if a.Key != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" { |
||||
t.Fatalf("wrong Key: %s.", a.Key) |
||||
} |
||||
|
||||
if BytesToTopic(a.Topic) != (TopicType{0xf2, 0x6e, 0x77, 0x79}) { |
||||
t.Fatalf("wrong topic: %x.", a.Topic) |
||||
} |
||||
if string(a.Padding) != "this is my test string" { |
||||
t.Fatalf("wrong Padding: %s.", string(a.Padding)) |
||||
} |
||||
if string(a.Payload) != "payload should be pseudorandom" { |
||||
t.Fatalf("wrong Payload: %s.", string(a.Payload)) |
||||
} |
||||
if a.PowTime != 777 { |
||||
t.Fatalf("wrong PowTime: %d.", a.PowTime) |
||||
} |
||||
if a.PowTarget != 3.1416 { |
||||
t.Fatalf("wrong PowTarget: %f.", a.PowTarget) |
||||
} |
||||
if a.TargetPeer != "enode://915533f667b1369793ebb9bda022416b1295235a1420799cd87a969467372546d808ebf59c5c9ce23f103d59b61b97df8af91f0908552485975397181b993461@127.0.0.1:12345" { |
||||
t.Fatalf("wrong PeerID: %s.", a.TargetPeer) |
||||
} |
||||
} |
||||
|
||||
func waitForMessages(api *PublicWhisperAPI, id string, target int) []*WhisperMessage { |
||||
// timeout: 2 seconds
|
||||
result := make([]*WhisperMessage, 0, target) |
||||
for i := 0; i < 100; i++ { |
||||
mail := api.GetNewSubscriptionMessages(id) |
||||
if len(mail) > 0 { |
||||
for _, m := range mail { |
||||
result = append(result, m) |
||||
} |
||||
if len(result) >= target { |
||||
break |
||||
} |
||||
} |
||||
time.Sleep(time.Millisecond * 20) |
||||
} |
||||
|
||||
return result |
||||
} |
||||
|
||||
func TestIntegrationAsym(t *testing.T) { |
||||
w := New() |
||||
api := NewPublicWhisperAPI(w) |
||||
if api == nil { |
||||
t.Fatalf("failed to create API.") |
||||
} |
||||
|
||||
api.Start() |
||||
defer api.Stop() |
||||
|
||||
sig, err := api.NewKeyPair() |
||||
if err != nil { |
||||
t.Fatalf("failed NewIdentity: %s.", err) |
||||
} |
||||
if len(sig) == 0 { |
||||
t.Fatalf("wrong signature") |
||||
} |
||||
|
||||
exist, err := api.HasKeyPair(sig) |
||||
if err != nil { |
||||
t.Fatalf("failed HasIdentity: %s.", err) |
||||
} |
||||
if !exist { |
||||
t.Fatalf("failed HasIdentity: false negative.") |
||||
} |
||||
|
||||
sigPubKey, err := api.GetPublicKey(sig) |
||||
if err != nil { |
||||
t.Fatalf("failed GetPublicKey: %s.", err) |
||||
} |
||||
|
||||
key, err := api.NewKeyPair() |
||||
if err != nil { |
||||
t.Fatalf("failed NewIdentity(): %s.", err) |
||||
} |
||||
if len(key) == 0 { |
||||
t.Fatalf("wrong key") |
||||
} |
||||
|
||||
dstPubKey, err := api.GetPublicKey(key) |
||||
if err != nil { |
||||
t.Fatalf("failed GetPublicKey: %s.", err) |
||||
} |
||||
|
||||
var topics [2]TopicType |
||||
topics[0] = TopicType{0x00, 0x64, 0x00, 0xff} |
||||
topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79} |
||||
var f WhisperFilterArgs |
||||
f.Symmetric = false |
||||
f.Key = key |
||||
f.Sig = sigPubKey.String() |
||||
f.Topics = make([][]byte, 2) |
||||
f.Topics[0] = topics[0][:] |
||||
f.Topics[1] = topics[1][:] |
||||
f.MinPoW = DefaultMinimumPoW / 2 |
||||
f.AllowP2P = true |
||||
|
||||
id, err := api.Subscribe(f) |
||||
if err != nil { |
||||
t.Fatalf("failed to create new filter: %s.", err) |
||||
} |
||||
|
||||
var p PostArgs |
||||
p.Type = "asym" |
||||
p.TTL = 2 |
||||
p.Sig = sig |
||||
p.Key = dstPubKey.String() |
||||
p.Padding = []byte("test string") |
||||
p.Payload = []byte("extended test string") |
||||
p.PowTarget = DefaultMinimumPoW |
||||
p.PowTime = 2 |
||||
p.Topic = hexutil.Bytes{0xf2, 0x6e, 0x77, 0x79} // topics[1]
|
||||
|
||||
err = api.Post(p) |
||||
if err != nil { |
||||
t.Errorf("failed to post message: %s.", err) |
||||
} |
||||
|
||||
mail := waitForMessages(api, id, 1) |
||||
if len(mail) != 1 { |
||||
t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) |
||||
} |
||||
|
||||
text := string(common.FromHex(mail[0].Payload)) |
||||
if text != string("extended test string") { |
||||
t.Fatalf("failed to decrypt first message: %s.", text) |
||||
} |
||||
|
||||
p.Padding = []byte("new value") |
||||
p.Payload = []byte("extended new value") |
||||
err = api.Post(p) |
||||
if err != nil { |
||||
t.Fatalf("failed to post next message: %s.", err) |
||||
} |
||||
|
||||
mail = waitForMessages(api, id, 1) |
||||
if len(mail) != 1 { |
||||
t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) |
||||
} |
||||
|
||||
text = string(common.FromHex(mail[0].Payload)) |
||||
if text != string("extended new value") { |
||||
t.Fatalf("failed to decrypt second message: %s.", text) |
||||
} |
||||
} |
||||
|
||||
func TestIntegrationSym(t *testing.T) { |
||||
w := New() |
||||
api := NewPublicWhisperAPI(w) |
||||
if api == nil { |
||||
t.Fatalf("failed to create API.") |
||||
} |
||||
|
||||
api.Start() |
||||
defer api.Stop() |
||||
|
||||
symKeyID, err := api.GenerateSymmetricKey() |
||||
if err != nil { |
||||
t.Fatalf("failed GenerateSymKey: %s.", err) |
||||
} |
||||
|
||||
sig, err := api.NewKeyPair() |
||||
if err != nil { |
||||
t.Fatalf("failed NewKeyPair: %s.", err) |
||||
} |
||||
if len(sig) == 0 { |
||||
t.Fatalf("wrong signature") |
||||
} |
||||
|
||||
sigPubKey, err := api.GetPublicKey(sig) |
||||
if err != nil { |
||||
t.Fatalf("failed GetPublicKey: %s.", err) |
||||
} |
||||
|
||||
exist, err := api.HasKeyPair(sig) |
||||
if err != nil { |
||||
t.Fatalf("failed HasIdentity: %s.", err) |
||||
} |
||||
if !exist { |
||||
t.Fatalf("failed HasIdentity: false negative.") |
||||
} |
||||
|
||||
var topics [2]TopicType |
||||
topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff} |
||||
topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79} |
||||
var f WhisperFilterArgs |
||||
f.Symmetric = true |
||||
f.Key = symKeyID |
||||
f.Topics = make([][]byte, 2) |
||||
f.Topics[0] = topics[0][:] |
||||
f.Topics[1] = topics[1][:] |
||||
f.MinPoW = DefaultMinimumPoW / 2 |
||||
f.Sig = sigPubKey.String() |
||||
f.AllowP2P = false |
||||
|
||||
id, err := api.Subscribe(f) |
||||
if err != nil { |
||||
t.Fatalf("failed to create new filter: %s.", err) |
||||
} |
||||
|
||||
var p PostArgs |
||||
p.Type = "sym" |
||||
p.TTL = 1 |
||||
p.Key = symKeyID |
||||
p.Sig = sig |
||||
p.Padding = []byte("test string") |
||||
p.Payload = []byte("extended test string") |
||||
p.PowTarget = DefaultMinimumPoW |
||||
p.PowTime = 2 |
||||
p.Topic = hexutil.Bytes{0xf2, 0x6e, 0x77, 0x79} |
||||
|
||||
err = api.Post(p) |
||||
if err != nil { |
||||
t.Fatalf("failed to post first message: %s.", err) |
||||
} |
||||
|
||||
mail := waitForMessages(api, id, 1) |
||||
if len(mail) != 1 { |
||||
t.Fatalf("failed GetFilterChanges: got %d messages.", len(mail)) |
||||
} |
||||
|
||||
text := string(common.FromHex(mail[0].Payload)) |
||||
if text != string("extended test string") { |
||||
t.Fatalf("failed to decrypt first message: %s.", text) |
||||
} |
||||
|
||||
p.Padding = []byte("new value") |
||||
p.Payload = []byte("extended new value") |
||||
err = api.Post(p) |
||||
if err != nil { |
||||
t.Fatalf("failed to post second message: %s.", err) |
||||
} |
||||
|
||||
mail = waitForMessages(api, id, 1) |
||||
if len(mail) != 1 { |
||||
t.Fatalf("failed second GetFilterChanges: got %d messages.", len(mail)) |
||||
} |
||||
|
||||
text = string(common.FromHex(mail[0].Payload)) |
||||
if text != string("extended new value") { |
||||
t.Fatalf("failed to decrypt second message: %s.", text) |
||||
} |
||||
} |
||||
|
||||
func TestIntegrationSymWithFilter(t *testing.T) { |
||||
w := New() |
||||
api := NewPublicWhisperAPI(w) |
||||
if api == nil { |
||||
t.Fatalf("failed to create API.") |
||||
} |
||||
|
||||
api.Start() |
||||
defer api.Stop() |
||||
|
||||
symKeyID, err := api.GenerateSymmetricKey() |
||||
if err != nil { |
||||
t.Fatalf("failed to GenerateSymKey: %s.", err) |
||||
} |
||||
|
||||
sigKeyID, err := api.NewKeyPair() |
||||
if err != nil { |
||||
t.Fatalf("failed NewIdentity: %s.", err) |
||||
} |
||||
if len(sigKeyID) == 0 { |
||||
t.Fatalf("wrong signature.") |
||||
} |
||||
|
||||
exist, err := api.HasKeyPair(sigKeyID) |
||||
if err != nil { |
||||
t.Fatalf("failed HasIdentity: %s.", err) |
||||
} |
||||
if !exist { |
||||
t.Fatalf("failed HasIdentity: does not exist.") |
||||
} |
||||
|
||||
sigPubKey, err := api.GetPublicKey(sigKeyID) |
||||
if err != nil { |
||||
t.Fatalf("failed GetPublicKey: %s.", err) |
||||
} |
||||
|
||||
var topics [2]TopicType |
||||
topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff} |
||||
topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79} |
||||
var f WhisperFilterArgs |
||||
f.Symmetric = true |
||||
f.Key = symKeyID |
||||
f.Topics = make([][]byte, 2) |
||||
f.Topics[0] = topics[0][:] |
||||
f.Topics[1] = topics[1][:] |
||||
f.MinPoW = DefaultMinimumPoW / 2 |
||||
f.Sig = sigPubKey.String() |
||||
f.AllowP2P = false |
||||
|
||||
id, err := api.Subscribe(f) |
||||
if err != nil { |
||||
t.Fatalf("failed to create new filter: %s.", err) |
||||
} |
||||
|
||||
var p PostArgs |
||||
p.Type = "sym" |
||||
p.TTL = 1 |
||||
p.Key = symKeyID |
||||
p.Sig = sigKeyID |
||||
p.Padding = []byte("test string") |
||||
p.Payload = []byte("extended test string") |
||||
p.PowTarget = DefaultMinimumPoW |
||||
p.PowTime = 2 |
||||
p.Topic = hexutil.Bytes{0xf2, 0x6e, 0x77, 0x79} |
||||
|
||||
err = api.Post(p) |
||||
if err != nil { |
||||
t.Fatalf("failed to post message: %s.", err) |
||||
} |
||||
|
||||
mail := waitForMessages(api, id, 1) |
||||
if len(mail) != 1 { |
||||
t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) |
||||
} |
||||
|
||||
text := string(common.FromHex(mail[0].Payload)) |
||||
if text != string("extended test string") { |
||||
t.Fatalf("failed to decrypt first message: %s.", text) |
||||
} |
||||
|
||||
p.Padding = []byte("new value") |
||||
p.Payload = []byte("extended new value") |
||||
err = api.Post(p) |
||||
if err != nil { |
||||
t.Fatalf("failed to post next message: %s.", err) |
||||
} |
||||
|
||||
mail = waitForMessages(api, id, 1) |
||||
if len(mail) != 1 { |
||||
t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) |
||||
} |
||||
|
||||
text = string(common.FromHex(mail[0].Payload)) |
||||
if text != string("extended new value") { |
||||
t.Fatalf("failed to decrypt second message: %s.", text) |
||||
} |
||||
} |
||||
|
||||
func TestKey(t *testing.T) { |
||||
w := New() |
||||
api := NewPublicWhisperAPI(w) |
||||
if api == nil { |
||||
t.Fatalf("failed to create API.") |
||||
} |
||||
|
||||
k, err := api.AddSymmetricKeyFromPassword("wwww") |
||||
if err != nil { |
||||
t.Fatalf("failed to create key: %s.", err) |
||||
} |
||||
|
||||
s, err := api.GetSymmetricKey(k) |
||||
if err != nil { |
||||
t.Fatalf("failed to get sym key: %s.", err) |
||||
} |
||||
|
||||
k2, err := api.AddSymmetricKeyDirect(s) |
||||
if err != nil { |
||||
t.Fatalf("failed to add sym key: %s.", err) |
||||
} |
||||
|
||||
s2, err := api.GetSymmetricKey(k2) |
||||
if err != nil { |
||||
t.Fatalf("failed to get sym key: %s.", err) |
||||
} |
||||
|
||||
if s.String() != "0x448652d595bd6ec00b2a9ea220ad6c26592d9bf4cf79023d3c1b30cb681e6e07" { |
||||
t.Fatalf("wrong key from password: %s", s.String()) |
||||
} |
||||
|
||||
if !bytes.Equal(s, s2) { |
||||
t.Fatalf("wrong key") |
||||
} |
||||
} |
||||
|
||||
func TestSubscribe(t *testing.T) { |
||||
var err error |
||||
var s string |
||||
|
||||
w := New() |
||||
api := NewPublicWhisperAPI(w) |
||||
if api == nil { |
||||
t.Fatalf("failed to create API.") |
||||
} |
||||
|
||||
symKeyID, err := api.GenerateSymmetricKey() |
||||
if err != nil { |
||||
t.Fatalf("failed to GenerateSymKey: %s.", err) |
||||
} |
||||
|
||||
var f WhisperFilterArgs |
||||
f.Symmetric = true |
||||
f.Key = symKeyID |
||||
f.Topics = make([][]byte, 5) |
||||
f.Topics[0] = []byte{0x21} |
||||
f.Topics[1] = []byte{0xd2, 0xe3} |
||||
f.Topics[2] = []byte{0x64, 0x75, 0x76} |
||||
f.Topics[3] = []byte{0xf8, 0xe9, 0xa0, 0xba} |
||||
f.Topics[4] = []byte{0xcb, 0x3c, 0xdd, 0xee, 0xff} |
||||
|
||||
s, err = api.Subscribe(f) |
||||
if err == nil { |
||||
t.Fatalf("Subscribe: false positive.") |
||||
} |
||||
|
||||
f.Topics[4] = []byte{} |
||||
if err == nil { |
||||
t.Fatalf("Subscribe: false positive again.") |
||||
} |
||||
|
||||
f.Topics[4] = []byte{0x00} |
||||
s, err = api.Subscribe(f) |
||||
if err != nil { |
||||
t.Fatalf("failed to subscribe: %s.", err) |
||||
} else { |
||||
api.Unsubscribe(s) |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
// Copyright 2017 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/>.
|
||||
|
||||
package whisperv5 |
||||
|
||||
type Config struct { |
||||
MaxMessageSize uint32 `toml:",omitempty"` |
||||
MinimumAcceptedPOW float64 `toml:",omitempty"` |
||||
} |
||||
|
||||
var DefaultConfig = Config{ |
||||
MaxMessageSize: DefaultMaxMessageSize, |
||||
MinimumAcceptedPOW: DefaultMinimumPoW, |
||||
} |
||||
|
||||
var () |
@ -0,0 +1,62 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package whisperv5 |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
func (c Criteria) MarshalJSON() ([]byte, error) { |
||||
type Criteria struct { |
||||
SymKeyID string `json:"symKeyID"` |
||||
PrivateKeyID string `json:"privateKeyID"` |
||||
Sig hexutil.Bytes `json:"sig"` |
||||
MinPow float64 `json:"minPow"` |
||||
Topics []TopicType `json:"topics"` |
||||
AllowP2P bool `json:"allowP2P"` |
||||
} |
||||
var enc Criteria |
||||
enc.SymKeyID = c.SymKeyID |
||||
enc.PrivateKeyID = c.PrivateKeyID |
||||
enc.Sig = c.Sig |
||||
enc.MinPow = c.MinPow |
||||
enc.Topics = c.Topics |
||||
enc.AllowP2P = c.AllowP2P |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
func (c *Criteria) UnmarshalJSON(input []byte) error { |
||||
type Criteria struct { |
||||
SymKeyID *string `json:"symKeyID"` |
||||
PrivateKeyID *string `json:"privateKeyID"` |
||||
Sig hexutil.Bytes `json:"sig"` |
||||
MinPow *float64 `json:"minPow"` |
||||
Topics []TopicType `json:"topics"` |
||||
AllowP2P *bool `json:"allowP2P"` |
||||
} |
||||
var dec Criteria |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
if dec.SymKeyID != nil { |
||||
c.SymKeyID = *dec.SymKeyID |
||||
} |
||||
if dec.PrivateKeyID != nil { |
||||
c.PrivateKeyID = *dec.PrivateKeyID |
||||
} |
||||
if dec.Sig != nil { |
||||
c.Sig = dec.Sig |
||||
} |
||||
if dec.MinPow != nil { |
||||
c.MinPow = *dec.MinPow |
||||
} |
||||
if dec.Topics != nil { |
||||
c.Topics = dec.Topics |
||||
} |
||||
if dec.AllowP2P != nil { |
||||
c.AllowP2P = *dec.AllowP2P |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,80 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package whisperv5 |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
func (m Message) MarshalJSON() ([]byte, error) { |
||||
type Message struct { |
||||
Sig hexutil.Bytes `json:"sig,omitempty"` |
||||
TTL uint32 `json:"ttl"` |
||||
Timestamp uint32 `json:"timestamp"` |
||||
Topic TopicType `json:"topic"` |
||||
Payload hexutil.Bytes `json:"payload"` |
||||
Padding hexutil.Bytes `json:"padding"` |
||||
PoW float64 `json:"pow"` |
||||
Hash hexutil.Bytes `json:"hash"` |
||||
Dst hexutil.Bytes `json:"recipientPublicKey,omitempty"` |
||||
} |
||||
var enc Message |
||||
enc.Sig = m.Sig |
||||
enc.TTL = m.TTL |
||||
enc.Timestamp = m.Timestamp |
||||
enc.Topic = m.Topic |
||||
enc.Payload = m.Payload |
||||
enc.Padding = m.Padding |
||||
enc.PoW = m.PoW |
||||
enc.Hash = m.Hash |
||||
enc.Dst = m.Dst |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
func (m *Message) UnmarshalJSON(input []byte) error { |
||||
type Message struct { |
||||
Sig hexutil.Bytes `json:"sig,omitempty"` |
||||
TTL *uint32 `json:"ttl"` |
||||
Timestamp *uint32 `json:"timestamp"` |
||||
Topic *TopicType `json:"topic"` |
||||
Payload hexutil.Bytes `json:"payload"` |
||||
Padding hexutil.Bytes `json:"padding"` |
||||
PoW *float64 `json:"pow"` |
||||
Hash hexutil.Bytes `json:"hash"` |
||||
Dst hexutil.Bytes `json:"recipientPublicKey,omitempty"` |
||||
} |
||||
var dec Message |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
if dec.Sig != nil { |
||||
m.Sig = dec.Sig |
||||
} |
||||
if dec.TTL != nil { |
||||
m.TTL = *dec.TTL |
||||
} |
||||
if dec.Timestamp != nil { |
||||
m.Timestamp = *dec.Timestamp |
||||
} |
||||
if dec.Topic != nil { |
||||
m.Topic = *dec.Topic |
||||
} |
||||
if dec.Payload != nil { |
||||
m.Payload = dec.Payload |
||||
} |
||||
if dec.Padding != nil { |
||||
m.Padding = dec.Padding |
||||
} |
||||
if dec.PoW != nil { |
||||
m.PoW = *dec.PoW |
||||
} |
||||
if dec.Hash != nil { |
||||
m.Hash = dec.Hash |
||||
} |
||||
if dec.Dst != nil { |
||||
m.Dst = dec.Dst |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,86 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package whisperv5 |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
func (n NewMessage) MarshalJSON() ([]byte, error) { |
||||
type NewMessage struct { |
||||
SymKeyID string `json:"symKeyID"` |
||||
PublicKey hexutil.Bytes `json:"pubKey"` |
||||
Sig string `json:"sig"` |
||||
TTL uint32 `json:"ttl"` |
||||
Topic TopicType `json:"topic"` |
||||
Payload hexutil.Bytes `json:"payload"` |
||||
Padding hexutil.Bytes `json:"padding"` |
||||
PowTime uint32 `json:"powTime"` |
||||
PowTarget float64 `json:"powTarget"` |
||||
TargetPeer string `json:"targetPeer"` |
||||
} |
||||
var enc NewMessage |
||||
enc.SymKeyID = n.SymKeyID |
||||
enc.PublicKey = n.PublicKey |
||||
enc.Sig = n.Sig |
||||
enc.TTL = n.TTL |
||||
enc.Topic = n.Topic |
||||
enc.Payload = n.Payload |
||||
enc.Padding = n.Padding |
||||
enc.PowTime = n.PowTime |
||||
enc.PowTarget = n.PowTarget |
||||
enc.TargetPeer = n.TargetPeer |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
func (n *NewMessage) UnmarshalJSON(input []byte) error { |
||||
type NewMessage struct { |
||||
SymKeyID *string `json:"symKeyID"` |
||||
PublicKey hexutil.Bytes `json:"pubKey"` |
||||
Sig *string `json:"sig"` |
||||
TTL *uint32 `json:"ttl"` |
||||
Topic *TopicType `json:"topic"` |
||||
Payload hexutil.Bytes `json:"payload"` |
||||
Padding hexutil.Bytes `json:"padding"` |
||||
PowTime *uint32 `json:"powTime"` |
||||
PowTarget *float64 `json:"powTarget"` |
||||
TargetPeer *string `json:"targetPeer"` |
||||
} |
||||
var dec NewMessage |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
if dec.SymKeyID != nil { |
||||
n.SymKeyID = *dec.SymKeyID |
||||
} |
||||
if dec.PublicKey != nil { |
||||
n.PublicKey = dec.PublicKey |
||||
} |
||||
if dec.Sig != nil { |
||||
n.Sig = *dec.Sig |
||||
} |
||||
if dec.TTL != nil { |
||||
n.TTL = *dec.TTL |
||||
} |
||||
if dec.Topic != nil { |
||||
n.Topic = *dec.Topic |
||||
} |
||||
if dec.Payload != nil { |
||||
n.Payload = dec.Payload |
||||
} |
||||
if dec.Padding != nil { |
||||
n.Padding = dec.Padding |
||||
} |
||||
if dec.PowTime != nil { |
||||
n.PowTime = *dec.PowTime |
||||
} |
||||
if dec.PowTarget != nil { |
||||
n.PowTarget = *dec.PowTarget |
||||
} |
||||
if dec.TargetPeer != nil { |
||||
n.TargetPeer = *dec.TargetPeer |
||||
} |
||||
return nil |
||||
} |
Loading…
Reference in new issue