whisperv5

package
v1.6.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2017 License: GPL-3.0 Imports: 30 Imported by: 0

Documentation

Overview

Package whisper implements the Whisper protocol (version 5).

Whisper combines aspects of both DHTs and datagram messaging systems (e.g. UDP). As such it may be likened and compared to both, not dissimilar to the matter/energy duality (apologies to physicists for the blatant abuse of a fundamental and beautiful natural principle).

Whisper is a pure identity-based messaging system. Whisper provides a low-level (non-application-specific) but easily-accessible API without being based upon or prejudiced by the low-level hardware attributes and characteristics, particularly the notion of singular endpoints.

Index

Constants

View Source
const (
	EnvelopeVersion    = uint64(0)
	ProtocolVersion    = uint64(5)
	ProtocolVersionStr = "5.0"
	ProtocolName       = "shh"

	NumberOfMessageCodes = 64

	TopicLength = 4

	AESNonceLength = 12

	DefaultMaxMessageLength = 1024 * 1024
	DefaultMinimumPoW       = 0.2

	DefaultTTL     = 50 // seconds
	SynchAllowance = 10 // seconds
)

Variables

This section is empty.

Functions

func BytesToUintBigEndian added in v1.6.0

func BytesToUintBigEndian(b []byte) (res uint64)

BytesToUintBigEndian converts the slice to 64-bit unsigned integer.

func GenerateRandomID added in v1.6.0

func GenerateRandomID() (id string, err error)

GenerateRandomID generates a random string, which is then returned to be used as a key id

func IsPubKeyEqual added in v1.5.8

func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool

func ValidateKeyID added in v1.6.0

func ValidateKeyID(id string) error

ValidateKeyID checks the format of key id.

func ValidatePublicKey

func ValidatePublicKey(k *ecdsa.PublicKey) bool

ValidatePublicKey checks the format of the given public key.

Types

type Envelope

type Envelope struct {
	Version  []byte
	Expiry   uint32
	TTL      uint32
	Topic    TopicType
	AESNonce []byte
	Data     []byte
	EnvNonce uint64
	// contains filtered or unexported fields
}

Envelope represents a clear-text data packet to transmit through the Whisper network. Its contents may or may not be encrypted and signed.

func NewEnvelope

func NewEnvelope(ttl uint32, topic TopicType, aesNonce []byte, msg *SentMessage) *Envelope

NewEnvelope wraps a Whisper message with expiration and destination data included into an envelope for network forwarding.

func (*Envelope) DecodeRLP

func (e *Envelope) DecodeRLP(s *rlp.Stream) error

DecodeRLP decodes an Envelope from an RLP data stream.

func (*Envelope) Hash

func (e *Envelope) Hash() common.Hash

Hash returns the SHA3 hash of the envelope, calculating it if not yet done.

func (*Envelope) IsSymmetric

func (e *Envelope) IsSymmetric() bool

func (*Envelope) Open

func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage)

Open tries to decrypt an envelope, and populates the message fields in case of success.

func (*Envelope) OpenAsymmetric

func (e *Envelope) OpenAsymmetric(key *ecdsa.PrivateKey) (*ReceivedMessage, error)

OpenAsymmetric tries to decrypt an envelope, potentially encrypted with a particular key.

func (*Envelope) OpenSymmetric

func (e *Envelope) OpenSymmetric(key []byte) (msg *ReceivedMessage, err error)

OpenSymmetric tries to decrypt an envelope, potentially encrypted with a particular key.

func (*Envelope) PoW

func (e *Envelope) PoW() float64

func (*Envelope) Seal

func (e *Envelope) Seal(options *MessageParams) error

Seal closes the envelope by spending the requested amount of time as a proof of work on hashing the data.

func (*Envelope) Ver

func (e *Envelope) Ver() uint64

type Filter

type Filter struct {
	Src        *ecdsa.PublicKey  // Sender of the message
	KeyAsym    *ecdsa.PrivateKey // Private Key of recipient
	KeySym     []byte            // Key associated with the Topic
	Topics     [][]byte          // Topics to filter messages with
	PoW        float64           // Proof of work as described in the Whisper spec
	AllowP2P   bool              // Indicates whether this filter is interested in direct peer-to-peer messages
	SymKeyHash common.Hash       // The Keccak256Hash of the symmetric key, needed for optimization

	Messages map[common.Hash]*ReceivedMessage
	// contains filtered or unexported fields
}

func (*Filter) MatchEnvelope

func (f *Filter) MatchEnvelope(envelope *Envelope) bool

func (*Filter) MatchMessage

func (f *Filter) MatchMessage(msg *ReceivedMessage) bool

func (*Filter) MatchTopic added in v1.5.8

func (f *Filter) MatchTopic(topic TopicType) bool

func (*Filter) Retrieve

func (f *Filter) Retrieve() (all []*ReceivedMessage)

func (*Filter) Trigger

func (f *Filter) Trigger(msg *ReceivedMessage)

type Filters

type Filters struct {
	// contains filtered or unexported fields
}

func NewFilters

func NewFilters(w *Whisper) *Filters

func (*Filters) Get

func (fs *Filters) Get(id string) *Filter

func (*Filters) Install

func (fs *Filters) Install(watcher *Filter) (string, error)

func (*Filters) NotifyWatchers

func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool)

func (*Filters) Uninstall

func (fs *Filters) Uninstall(id string) bool

type MailServer

type MailServer interface {
	Archive(env *Envelope)
	DeliverMail(whisperPeer *Peer, request *Envelope)
}

MailServer represents a mail server, capable of archiving the old messages for subsequent delivery to the peers. Any implementation must ensure that both functions are thread-safe. Also, they must return ASAP. DeliverMail should use directMessagesCode for delivery, in order to bypass the expiry checks.

type MessageParams

type MessageParams struct {
	TTL      uint32
	Src      *ecdsa.PrivateKey
	Dst      *ecdsa.PublicKey
	KeySym   []byte
	Topic    TopicType
	WorkTime uint32
	PoW      float64
	Payload  []byte
	Padding  []byte
}

Options specifies the exact way a message should be wrapped into an Envelope.

type Peer

type Peer struct {
	// contains filtered or unexported fields
}

peer represents a whisper protocol peer connection.

func (*Peer) ID added in v1.5.8

func (p *Peer) ID() []byte

type PostArgs added in v1.6.0

type PostArgs struct {
	Type       string        `json:"type"`       // "sym"/"asym" (symmetric or asymmetric)
	TTL        uint32        `json:"ttl"`        // time-to-live in seconds
	Sig        string        `json:"sig"`        // id of the signing key
	Key        string        `json:"key"`        // key id (in case of sym) or public key (in case of asym)
	Topic      hexutil.Bytes `json:"topic"`      // topic (4 bytes)
	Padding    hexutil.Bytes `json:"padding"`    // optional padding bytes
	Payload    hexutil.Bytes `json:"payload"`    // payload to be encrypted
	PowTime    uint32        `json:"powTime"`    // maximal time in seconds to be spent on PoW
	PowTarget  float64       `json:"powTarget"`  // minimal PoW required for this message
	TargetPeer string        `json:"targetPeer"` // peer id (for p2p message only)
}

type PublicWhisperAPI added in v1.6.0

type PublicWhisperAPI struct {
	// contains filtered or unexported fields
}

PublicWhisperAPI provides the whisper RPC service.

func NewPublicWhisperAPI added in v1.6.0

func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI

NewPublicWhisperAPI create a new RPC whisper service.

func (*PublicWhisperAPI) AddSymmetricKeyDirect added in v1.6.0

func (api *PublicWhisperAPI) AddSymmetricKeyDirect(key hexutil.Bytes) (string, error)

AddSymmetricKeyDirect stores the key, and returns its id.

func (*PublicWhisperAPI) AddSymmetricKeyFromPassword added in v1.6.0

func (api *PublicWhisperAPI) AddSymmetricKeyFromPassword(password string) (string, error)

AddSymmetricKeyFromPassword generates the key from password, stores it, and returns its id.

func (*PublicWhisperAPI) AllowP2PMessagesFromPeer added in v1.6.0

func (api *PublicWhisperAPI) AllowP2PMessagesFromPeer(enode string) error

AllowP2PMessagesFromPeer marks specific peer trusted, which will allow it to send historic (expired) messages.

func (*PublicWhisperAPI) DeleteKeyPair added in v1.6.0

func (api *PublicWhisperAPI) DeleteKeyPair(id string) (bool, error)

DeleteKeyPair deletes the specifies key if it exists.

func (*PublicWhisperAPI) DeleteSymmetricKey added in v1.6.0

func (api *PublicWhisperAPI) DeleteSymmetricKey(name string) (bool, error)

DeleteSymmetricKey deletes the key associated with the name string if it exists.

func (*PublicWhisperAPI) GenerateSymmetricKey added in v1.6.0

func (api *PublicWhisperAPI) GenerateSymmetricKey() (string, error)

GenerateSymmetricKey generates a random symmetric key and stores it under id, which is then returned. Will be used in the future for session key exchange.

func (*PublicWhisperAPI) GetFloatingMessages added in v1.6.4

func (api *PublicWhisperAPI) GetFloatingMessages(id string) []*WhisperMessage

GetMessages retrieves all the floating messages that match a specific subscription filter. It is likely to be called once per session, right after Subscribe call.

func (*PublicWhisperAPI) GetNewSubscriptionMessages added in v1.6.4

func (api *PublicWhisperAPI) GetNewSubscriptionMessages(id string) []*WhisperMessage

GetSubscriptionMessages retrieves all the new messages matched by the corresponding subscription filter since the last retrieval.

func (*PublicWhisperAPI) GetPrivateKey added in v1.6.0

func (api *PublicWhisperAPI) GetPrivateKey(id string) (string, error)

GetPrivateKey returns the private key for identity id

func (*PublicWhisperAPI) GetPublicKey added in v1.6.0

func (api *PublicWhisperAPI) GetPublicKey(id string) (hexutil.Bytes, error)

GetPublicKey returns the public key for identity id

func (*PublicWhisperAPI) GetSymmetricKey added in v1.6.0

func (api *PublicWhisperAPI) GetSymmetricKey(name string) (hexutil.Bytes, error)

GetSymmetricKey returns the symmetric key associated with the given id.

func (*PublicWhisperAPI) HasKeyPair added in v1.6.0

func (api *PublicWhisperAPI) HasKeyPair(id string) (bool, error)

HasKeyPair checks if the whisper node is configured with the private key of the specified public pair.

func (*PublicWhisperAPI) HasSymmetricKey added in v1.6.0

func (api *PublicWhisperAPI) HasSymmetricKey(id string) (bool, error)

HasSymmetricKey returns true if there is a key associated with the given id. Otherwise returns false.

func (*PublicWhisperAPI) Info added in v1.6.0

func (api *PublicWhisperAPI) Info() (string, error)

Info returns the Whisper statistics for diagnostics.

func (*PublicWhisperAPI) NewKeyPair added in v1.6.0

func (api *PublicWhisperAPI) NewKeyPair() (string, error)

NewKeyPair generates a new cryptographic identity for the client, and injects it into the known identities for message decryption.

func (*PublicWhisperAPI) Post added in v1.6.0

func (api *PublicWhisperAPI) Post(args PostArgs) error

Post creates a whisper message and injects it into the network for distribution.

func (*PublicWhisperAPI) SetMaxMessageLength added in v1.6.0

func (api *PublicWhisperAPI) SetMaxMessageLength(val int) error

SetMaxMessageLength sets the maximal message length allowed by this node

func (*PublicWhisperAPI) SetMinimumPoW added in v1.6.0

func (api *PublicWhisperAPI) SetMinimumPoW(val float64) error

SetMinimumPoW sets the minimal PoW required by this node

func (*PublicWhisperAPI) Start added in v1.6.0

func (api *PublicWhisperAPI) Start() error

Start starts the Whisper worker threads.

func (*PublicWhisperAPI) Stop added in v1.6.0

func (api *PublicWhisperAPI) Stop() error

Stop stops the Whisper worker threads.

func (*PublicWhisperAPI) Subscribe added in v1.6.0

func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error)

Subscribe creates and registers a new filter to watch for inbound whisper messages. Returns the ID of the newly created filter.

func (*PublicWhisperAPI) Unsubscribe added in v1.6.0

func (api *PublicWhisperAPI) Unsubscribe(id string)

Unsubscribe disables and removes an existing filter.

func (*PublicWhisperAPI) Version added in v1.6.0

func (api *PublicWhisperAPI) Version() (hexutil.Uint, error)

Version returns the Whisper version this node offers.

type ReceivedMessage

type ReceivedMessage struct {
	Raw []byte

	Payload   []byte
	Padding   []byte
	Signature []byte

	PoW   float64          // Proof of work as described in the Whisper spec
	Sent  uint32           // Time when the message was posted into the network
	TTL   uint32           // Maximum time to live allowed for the message
	Src   *ecdsa.PublicKey // Message recipient (identity used to decode the message)
	Dst   *ecdsa.PublicKey // Message recipient (identity used to decode the message)
	Topic TopicType

	SymKeyHash      common.Hash // The Keccak256Hash of the key, associated with the Topic
	EnvelopeHash    common.Hash // Message envelope hash to act as a unique id
	EnvelopeVersion uint64
}

ReceivedMessage represents a data packet to be received through the Whisper protocol.

func (*ReceivedMessage) SigToPubKey

func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey

Recover retrieves the public key of the message signer.

func (*ReceivedMessage) Validate

func (msg *ReceivedMessage) Validate() bool

Validate checks the validity and extracts the fields in case of success

type SentMessage

type SentMessage struct {
	Raw []byte
}

SentMessage represents an end-user data packet to transmit through the Whisper protocol. These are wrapped into Envelopes that need not be understood by intermediate nodes, just forwarded.

func NewSentMessage

func NewSentMessage(params *MessageParams) (*SentMessage, error)

NewMessage creates and initializes a non-signed, non-encrypted Whisper message.

func (*SentMessage) Wrap

func (msg *SentMessage) Wrap(options *MessageParams) (envelope *Envelope, err error)

Wrap bundles the message into an Envelope to transmit over the network.

type Statistics added in v1.6.0

type Statistics struct {
	// contains filtered or unexported fields
}

type TopicType

type TopicType [TopicLength]byte

Topic represents a cryptographically secure, probabilistic partial classifications of a message, determined as the first (left) 4 bytes of the SHA3 hash of some arbitrary data given by the original author of the message.

func BytesToTopic

func BytesToTopic(b []byte) (t TopicType)

func (*TopicType) String

func (topic *TopicType) String() string

String converts a topic byte array to a string representation.

func (*TopicType) UnmarshalJSON

func (t *TopicType) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hex representation to a topic.

type Whisper

type Whisper struct {
	// contains filtered or unexported fields
}

Whisper represents a dark communication interface through the Ethereum network, using its very own P2P communication layer.

func New added in v1.6.0

func New() *Whisper

New creates a Whisper client ready to communicate through the Ethereum P2P network.

func (*Whisper) APIs added in v1.6.0

func (w *Whisper) APIs() []rpc.API

APIs returns the RPC descriptors the Whisper implementation offers

func (*Whisper) AddSymKeyDirect added in v1.6.0

func (w *Whisper) AddSymKeyDirect(key []byte) (string, error)

AddSymKeyDirect stores the key, and returns its id.

func (*Whisper) AddSymKeyFromPassword added in v1.6.0

func (w *Whisper) AddSymKeyFromPassword(password string) (string, error)

AddSymKeyFromPassword generates the key from password, stores it, and returns its id.

func (*Whisper) AllowP2PMessagesFromPeer added in v1.6.0

func (w *Whisper) AllowP2PMessagesFromPeer(peerID []byte) error

AllowP2PMessagesFromPeer marks specific peer trusted, which will allow it to send historic (expired) messages.

func (*Whisper) DeleteKeyPair added in v1.6.0

func (w *Whisper) DeleteKeyPair(key string) bool

DeleteKeyPair deletes the specified key if it exists.

func (*Whisper) DeleteSymKey

func (w *Whisper) DeleteSymKey(id string) bool

DeleteSymKey deletes the key associated with the name string if it exists.

func (*Whisper) Envelopes

func (w *Whisper) Envelopes() []*Envelope

Envelopes retrieves all the messages currently pooled by the node.

func (*Whisper) GenerateSymKey

func (w *Whisper) GenerateSymKey() (string, error)

GenerateSymKey generates a random symmetric key and stores it under id, which is then returned. Will be used in the future for session key exchange.

func (*Whisper) GetFilter

func (w *Whisper) GetFilter(id string) *Filter

GetFilter returns the filter by id.

func (*Whisper) GetPrivateKey added in v1.6.0

func (w *Whisper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error)

GetPrivateKey retrieves the private key of the specified identity.

func (*Whisper) GetSymKey

func (w *Whisper) GetSymKey(id string) ([]byte, error)

GetSymKey returns the symmetric key associated with the given id.

func (*Whisper) HandlePeer

func (wh *Whisper) HandlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error

HandlePeer is called by the underlying P2P layer when the whisper sub-protocol connection is negotiated.

func (*Whisper) HasKeyPair added in v1.6.0

func (w *Whisper) HasKeyPair(id string) bool

HasKeyPair checks if the the whisper node is configured with the private key of the specified public pair.

func (*Whisper) HasSymKey

func (w *Whisper) HasSymKey(id string) bool

HasSymKey returns true if there is a key associated with the given id. Otherwise returns false.

func (*Whisper) Messages

func (w *Whisper) Messages(id string) []*ReceivedMessage

Messages iterates through all currently floating envelopes and retrieves all the messages, that this filter could decrypt.

func (*Whisper) NewKeyPair added in v1.6.0

func (w *Whisper) NewKeyPair() (string, error)

NewKeyPair generates a new cryptographic identity for the client, and injects it into the known identities for message decryption. Returns ID of the new key pair.

func (*Whisper) Protocols

func (w *Whisper) Protocols() []p2p.Protocol

Protocols returns the whisper sub-protocols ran by this particular client.

func (*Whisper) RegisterServer added in v1.6.0

func (w *Whisper) RegisterServer(server MailServer)

RegisterServer registers MailServer interface. MailServer will process all the incoming messages with p2pRequestCode.

func (*Whisper) RequestHistoricMessages

func (w *Whisper) RequestHistoricMessages(peerID []byte, envelope *Envelope) error

RequestHistoricMessages sends a message with p2pRequestCode to a specific peer, which is known to implement MailServer interface, and is supposed to process this request and respond with a number of peer-to-peer messages (possibly expired), which are not supposed to be forwarded any further. The whisper protocol is agnostic of the format and contents of envelope.

func (*Whisper) Send

func (w *Whisper) Send(envelope *Envelope) error

Send injects a message into the whisper send queue, to be distributed in the network in the coming cycles.

func (*Whisper) SendP2PDirect added in v1.5.8

func (w *Whisper) SendP2PDirect(peer *Peer, envelope *Envelope) error

SendP2PDirect sends a peer-to-peer message to a specific peer.

func (*Whisper) SendP2PMessage

func (w *Whisper) SendP2PMessage(peerID []byte, envelope *Envelope) error

SendP2PMessage sends a peer-to-peer message to a specific peer.

func (*Whisper) SetMaxMessageLength added in v1.6.0

func (w *Whisper) SetMaxMessageLength(val int) error

SetMaxMessageLength sets the maximal message length allowed by this node

func (*Whisper) SetMinimumPoW added in v1.6.0

func (w *Whisper) SetMinimumPoW(val float64) error

SetMinimumPoW sets the minimal PoW required by this node

func (*Whisper) Start

func (w *Whisper) Start(*p2p.Server) error

Start implements node.Service, starting the background data propagation thread of the Whisper protocol.

func (*Whisper) Stats added in v1.6.0

func (w *Whisper) Stats() string

Stats returns the whisper node statistics.

func (*Whisper) Stop

func (w *Whisper) Stop() error

Stop implements node.Service, stopping the background data propagation thread of the Whisper protocol.

func (*Whisper) Subscribe added in v1.6.0

func (w *Whisper) Subscribe(f *Filter) (string, error)

Subscribe installs a new message handler used for filtering, decrypting and subsequent storing of incoming messages.

func (*Whisper) Unsubscribe added in v1.6.0

func (w *Whisper) Unsubscribe(id string) error

Unsubscribe removes an installed message handler.

func (*Whisper) Version

func (w *Whisper) Version() uint

Version returns the whisper sub-protocols version number.

type WhisperFilterArgs added in v1.6.0

type WhisperFilterArgs struct {
	Symmetric bool     // encryption type
	Key       string   // id of the key to be used for decryption
	Sig       string   // public key of the sender to be verified
	MinPoW    float64  // minimal PoW requirement
	Topics    [][]byte // list of topics (up to 4 bytes each) to match
	AllowP2P  bool     // indicates wheather direct p2p messages are allowed for this filter
}

func (*WhisperFilterArgs) UnmarshalJSON added in v1.6.0

func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a JSON message blob into a WhisperFilterArgs structure.

type WhisperMessage added in v1.6.0

type WhisperMessage struct {
	Topic     string  `json:"topic"`
	Payload   string  `json:"payload"`
	Padding   string  `json:"padding"`
	Src       string  `json:"sig"`
	Dst       string  `json:"recipientPublicKey"`
	Timestamp uint32  `json:"timestamp"`
	TTL       uint32  `json:"ttl"`
	PoW       float64 `json:"pow"`
	Hash      string  `json:"hash"`
}

WhisperMessage is the RPC representation of a whisper message.

func NewWhisperMessage added in v1.6.0

func NewWhisperMessage(message *ReceivedMessage) *WhisperMessage

NewWhisperMessage converts an internal message into an API version.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL