whisperv5

package
v1.5.8 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2017 License: GPL-3.0 Imports: 24 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

	AESNonceMaxLength = 12

	MaxMessageLength = 0xFFFF // todo: remove this restriction after testing. this should be regulated by PoW.
	MinimumPoW       = 1.0    // todo: review after testing.

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

Variables

This section is empty.

Functions

func BytesToIntBigEndian

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

func DeriveOneTimeKey

func DeriveOneTimeKey(key []byte, salt []byte, version uint64) ([]byte, error)

func IsPubKeyEqual

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

func ValidatePublicKey

func ValidatePublicKey(k *ecdsa.PublicKey) bool

Types

type Envelope

type Envelope struct {
	Version  []byte
	Expiry   uint32
	TTL      uint32
	Topic    TopicType
	Salt     []byte
	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, salt []byte, 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     []TopicType       // Topics to filter messages with
	PoW        float64           // Proof of work as described in the Whisper spec
	AcceptP2P  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

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(i uint32) *Filter

func (*Filters) Install

func (fs *Filters) Install(watcher *Filter) uint32

func (*Filters) NotifyWatchers

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

func (*Filters) Uninstall

func (fs *Filters) Uninstall(id uint32)

type MailServer

type MailServer interface {
	Archive(env *Envelope)
	DeliverMail(whisperPeer *Peer, data []byte)
}

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.

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

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.

pow (Proof Of Work) controls how much time to spend on hashing the message, inherently controlling its priority through the network (smaller hash, bigger priority).

The user can control the amount of identity, privacy and encryption through the options parameter as follows:

  • options.From == nil && options.To == nil: anonymous broadcast
  • options.From != nil && options.To == nil: signed broadcast (known sender)
  • options.From == nil && options.To != nil: encrypted anonymous message
  • options.From != nil && options.To != nil: encrypted signed message

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 NewWhisper

func NewWhisper(server MailServer) *Whisper

New creates a Whisper client ready to communicate through the Ethereum P2P network. Param s should be passed if you want to implement mail server, otherwise nil.

func (*Whisper) AddSymKey

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

func (*Whisper) DeleteIdentity

func (w *Whisper) DeleteIdentity(key string)

DeleteIdentity deletes the specified key if it exists.

func (*Whisper) DeleteSymKey

func (w *Whisper) DeleteSymKey(name string)

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(name string) error

func (*Whisper) GetFilter

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

func (*Whisper) GetIdentity

func (w *Whisper) GetIdentity(pubKey string) *ecdsa.PrivateKey

GetIdentity retrieves the private key of the specified public identity.

func (*Whisper) GetSymKey

func (w *Whisper) GetSymKey(name string) []byte

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) HasIdentity

func (w *Whisper) HasIdentity(pubKey string) bool

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

func (*Whisper) HasSymKey

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

func (*Whisper) MarkPeerTrusted

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

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

func (*Whisper) Messages

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

Messages retrieves all the decrypted messages matching a filter id.

func (*Whisper) NewIdentity

func (w *Whisper) NewIdentity() *ecdsa.PrivateKey

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

func (*Whisper) Protocols

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

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

func (*Whisper) RequestHistoricMessages

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

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) SendP2PMessage

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

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) Stop

func (w *Whisper) Stop() error

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

func (*Whisper) Unwatch

func (w *Whisper) Unwatch(id uint32)

Unwatch removes an installed message handler.

func (*Whisper) Version

func (w *Whisper) Version() uint

Version returns the whisper sub-protocols version number.

func (*Whisper) Watch

func (w *Whisper) Watch(f *Filter) uint32

Watch installs a new message handler to run in case a matching packet arrives from the whisper network.

Jump to

Keyboard shortcuts

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