package
2.0.8+incompatible
Repository: https://github.com/etherzero-org/go-etherzero.git
Documentation: pkg.go.dev

# README

Postal Services over Swarm

pss enables message relay over swarm. This means nodes can send messages to each other without being directly connected with each other, while taking advantage of the efficient routing algorithms that swarm uses for transporting and storing data.

CONTENTS

  • Status of this document
  • Core concepts
  • Caveat
  • Examples
  • API
    • Retrieve node information
    • Receive messages
    • Send messages using public key encryption
    • Send messages using symmetric encryption
    • Querying peer keys
    • Handshakes

STATUS OF THIS DOCUMENT

pss is under active development, and the first implementation is yet to be merged to the Ethereum main branch. Expect things to change.

Details on swarm routing and encryption schemes out of scope of this document.

Please refer to ARCHITECTURE.md for in-depth topics concerning pss.

CORE CONCEPTS

Three things are required to send a pss message:

  1. Encryption key
  2. Topic
  3. Message payload

Encryption key can be a public key or a 32 byte symmetric key. It must be coupled with a peer address in the node prior to sending.

Topic is the initial 4 bytes of a hash value.

Message payload is an arbitrary byte slice of data.

Upon sending the message it is encrypted and passed on from peer to peer. Any node along the route that can successfully decrypt the message is regarded as a recipient. Recipients continue to pass on the message to their peers, to make traffic analysis attacks more difficult.

The Address that is coupled with the encryption keys are used for routing the message. This does not need to be a full addresses; the network will route the message to the best of its ability with the information that is available. If no address is given (zero-length byte slice), routing is effectively deactivated, and the message is passed to all peers by all peers.

CAVEAT

pss connectivity resembles UDP. This means there is no delivery guarantee for a message. Furthermore there is no strict definition of what a connection between two nodes communicating via pss is. Reception acknowledgements and keepalive-schemes is the responsibility of the application.

Due to the inherent properties of the swarm routing algorithm, a node may receive the same message more than once. Message deduplication cannot be guaranteed by pss, and must be handled in the application layer to ensure predictable results.

EXAMPLES

The code tutorial p2p programming in go-ethereum by @nolash provides step-by-step code examples for usage of pss API with go-ethereum nodes.

A quite unpolished example using javascript is available here: https://github.com/nolash/pss-js/tree/withcrypt

API

The pss API is available through IPC and Websockets. There is currently no web3.js implementation, as this does not support message subscription.

For golang clients, please use the rpc.Client provided by the go-ethereum repository. The return values may have special types in golang. Please refer to godoc for details.

RETRIEVE NODE INFORMATION

pss_getPublicKey

Retrieves the public key of the node, in hex format

parameters:
none

returns:
1. publickey (hex)

pss_baseAddr

Retrieves the swarm overlay address of the node, in hex format

parameters:
none

returns:
1. swarm overlay address (hex)

pss_stringToTopic

Creates a deterministic 4 byte topic value from input, returned in hex format

parameters:
1. topic string (string)

returns:
1. pss topic (hex)

RECEIVE MESSAGES

pss_subscribe

Creates a subscription. Received messages with matching topic will be passed to subscription client.

parameters:
1. string("receive")
2. topic (4 bytes in hex)

returns:
1. subscription handle `base64(byte)` `rpc.ClientSubscription`

In golang as special method is used:

rpc.Client.Subscribe(context.Context, "pss", chan pss.APIMsg, "receive", pss.Topic)

Incoming messages are encapsulated in an object (pss.APIMsg in golang) with the following members:

1. Msg (hex) - the message payload
2. Asymmetric (bool) - true if message used public key encryption
3. Key (string) - the encryption key used

SEND MESSAGE USING PUBLIC KEY ENCRYPTION

pss_setPeerPublicKey

Register a peer's public key. This is done once for every topic that will be used with the peer. Address can be anything from 0 to 32 bytes inclusive of the peer's swarm overlay address.

parameters:
1. public key of peer (hex)
2. topic (4 bytes in hex)
3. address of peer (hex)

returns:
none

pss_sendAsym

Encrypts the message using the provided public key, and signs it using the node's private key. It then wraps it in an envelope containing the topic, and sends it to the network.

parameters:
1. public key of peer (hex)
2. topic (4 bytes in hex)
3. message (hex)

returns:
none

SEND MESSAGE USING SYMMETRIC ENCRYPTION

pss_setSymmetricKey

Register a symmetric key shared with a peer. This is done once for every topic that will be used with the peer. Address can be anything from 0 to 32 bytes inclusive of the peer's swarm overlay address.

If the fourth parameter is false, the key will not be added to the list of symmetric keys used for decryption attempts.

parameters:
1. symmetric key (hex)
2. topic (4 bytes in hex)
3. address of peer (hex)
4. use for decryption (bool)

returns:
1. symmetric key id (string)

pss_sendSym

Encrypts the message using the provided symmetric key, wraps it in an envelope containing the topic, and sends it to the network.

parameters:
1. symmetric key id (string)
2. topic (4 bytes in hex)
3. message (hex)

returns:
none

QUERY PEER KEYS

pss_GetSymmetricAddressHint

Return the swarm overlay address associated with the peer registered with the given symmetric key and topic combination.

parameters:
1. topic (4 bytes in hex)
2. symmetric key id (string)

returns:
1. peer address (hex)

pss_GetAsymmetricAddressHint

Return the swarm overlay address associated with the peer registered with the given symmetric key and topic combination.

parameters:
1. topic (4 bytes in hex)
2. public key in hex form (string)

returns:
1. peer address (hex)

HANDSHAKES

Convenience implementation of Diffie-Hellman handshakes using ephemeral symmetric keys. Peers keep separate sets of keys for incoming and outgoing communications.

This functionality is an optional feature in pss. It is compiled in by default, but can be omitted by providing the nopsshandshake build tag.

pss_addHandshake

Activate handshake functionality on the specified topic.

parameters:
1. topic (4 bytes in hex)

returns:
none

pss_removeHandshake

Remove handshake functionality on the specified topic.

parameters:
1. topic (4 bytes in hex)

returns:
none

pss_handshake

Instantiate handshake with peer, refreshing symmetric encryption keys.

If parameter 3 is false, the returned array will be empty.

parameters:
1. public key of peer in hex format (string)
2. topic (4 bytes in hex)
3. block calls until keys are received (bool)
4. flush existing incoming keys (bool)

returns:
1. list of symmetric keys (string[])

pss_getHandshakeKeys

Get valid symmetric encryption keys for a specified peer and topic.

parameters:

  1. public key of peer in hex format (string)
  2. topic (4 bytes in hex)
  3. include keys for incoming messages (bool)
  4. include keys for outgoing messages (bool)

returns:

  1. list of symmetric keys (string[])

pss_getHandshakeKeyCapacity

Get amount of remaining messages the specified key is valid for.

parameters:
1. symmetric key id (string)

returns:
1. number of messages (uint16)

pss_getHandshakePublicKey

Get the peer's public key associated with the specified symmetric key.

parameters:
1. symmetric key id (string)

returns:
1. Associated public key in hex format (string)

pss_releaseHandshakeKey

Invalidate the specified key.

Normally, the key will be kept for a grace period to allow for decryption of delayed messages. If instant removal is set, this grace period is omitted, and the key removed instantaneously.

parameters:
1. public key of peer in hex format (string)
2. topic (4 bytes in hex)
3. symmetric key id to release (string)
4. remove keys instantly (bool)

returns:
1. whether key was successfully removed (bool)

# Packages

simple abstraction for implementing pss functionality the pss client library aims to simplify usage of the p2p.protocols package over pss IO is performed using the ordinary p2p.MsgReadWriter interface, which transparently communicates with a pss node via RPC using websockets as transport layer, using methods in the PssAPI class in the swarm/pss package Minimal-ish usage example (requires a running pss node with websocket RPC): import ( "context" "fmt" "os" pss "github.com/etherzero/go-etherzero/swarm/pss/client" "github.com/etherzero/go-etherzero/p2p/protocols" "github.com/etherzero/go-etherzero/p2p" "github.com/etherzero/go-etherzero/swarm/pot" "github.com/etherzero/go-etherzero/swarm/log" ) type FooMsg struct { Bar int } func fooHandler (msg interface{}) error { foomsg, ok := msg.(*FooMsg) if ok { log.Debug("Yay, just got a message", "msg", foomsg) } return errors.New(fmt.Sprintf("Unknown message")) } spec := &protocols.Spec{ Name: "foo", Version: 1, MaxMsgSize: 1024, Messages: []interface{}{ FooMsg{}, }, } proto := &p2p.Protocol{ Name: spec.Name, Version: spec.Version, Length: uint64(len(spec.Messages)), Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { pp := protocols.NewPeer(p, rw, spec) return pp.Run(fooHandler) }, } func implementation() { cfg := pss.NewClientConfig() psc := pss.NewClient(context.Background(), nil, cfg) err := psc.Start() if err != nil { log.Crit("can't start pss client") os.Exit(1) } log.Debug("connected to pss node", "bzz addr", psc.BaseAddr) err = psc.RunProtocol(proto) if err != nil { log.Crit("can't start protocol on pss websocket") os.Exit(1) } addr := pot.RandomAddress() // should be a real address, of course psc.AddPssPeer(addr, spec) // use the protocol for something psc.Stop() } BUG(test): TestIncoming test times out due to deadlock issues in the swarm hive.
No description provided by the author

# Functions

BytesToTopic hashes an arbitrary length byte slice and truncates it to the length of a topic, using only the first bytes of the digest.
No description provided by the author
NewHandler returns a new message handler.
Sane defaults for HandshakeController initialization.
No description provided by the author
Creates a ProtocolMsg.
Creates a new Pss instance.
Sane defaults for Pss.
Uniform translation of protocol specifiers to topic.
Activates devp2p emulation over a specific pss topic One or both encryption schemes must be specified.
Attach HandshakeController to pss node Must be called before starting the pss node service.
Creates a serialized (non-buffered) version of a p2p.Msg, used in the specialized internal p2p.MsgReadwriter implementations.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author

# Structs

Additional public methods accessible through API for pss.
Wrapper for receiving pss messages when using the pss API providing access to sender of message.
No description provided by the author
Singleton object enabling semi-automatic Diffie-Hellman exchange of ephemeral symmetric keys.
Initialization parameters for the HandshakeController SymKeyRequestExpiry: Timeout for waiting for a handshake reply (default 8000 ms) SymKeySendLimit: Amount of messages symmetric keys issues by this node is valid for (default 256) SymKeyCapacity: Ideal (and maximum) amount of symmetric keys held per direction per peer (default 4).
No description provided by the author
Generic ping protocol implementation for pss devp2p protocol emulation.
Convenience object for emulation devp2p over pss.
Convenience wrapper for devp2p protocol messages for transport over pss.
Protocol options to be passed to a new Protocol instance The parameters specify which encryption schemes to allow.
Toplevel pss object, takes care of message sending, receiving, decryption and encryption, message handler dispatchers and message forwarding.
PssMsg encapsulates messages transported over pss.
Pss configuration parameters.
PssReadWriter bridges pss send/receive with devp2p protocol send/receive Implements p2p.MsgReadWriter.

# Type aliases

Signature for a message handler function for a PssMsg Implementations of this type are passed to Pss.Register together with a topic,.
PssAddress is an alias for []byte.
Topic is the PSS encapsulation of the Whisper topic type.