Skip to content

Update SHH module for Whisper v6 #784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties

# VS Code
.vscode/
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

# General information about the project.
project = u'Web3.py'
copyright = u'2017, Piper Merriam, Jason Carver'
copyright = u'2018, Piper Merriam, Jason Carver'

__version__ = setup_version
# The version info for the project you're documenting, acts as replacement for
Expand Down
245 changes: 200 additions & 45 deletions docs/web3.shh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,113 +19,268 @@ The following properties are available on the ``web.shh`` namespace.

.. py:attribute:: Shh.version

The version of Whisper protocol used by client
Returns the Whisper version this node offers.

.. code-block:: python

>>>web3.shh.version
2
6.0

.. py:attribute:: Shh.info

Returns the Whisper statistics for diagnostics.

.. code-block:: python

>>>web3.shh.info
{'maxMessageSize': 1024, 'memory': 240, 'messages': 0, 'minPow': 0.2}

Methods
-------

The following methods are available on the ``web3.shh`` namespace.


.. py:method:: Shh.post(self, params)
.. py:method:: Shh.post(self, message)

* Delegates to ``shh_post`` RPC method
* Creates a whisper message and injects it into the network for distribution.

* ``params`` cannot be ``None`` and should contain ``topics`` and ``payload``
* Parameters:
* ``symKeyID``: When using symmetric key encryption, holds the symmetric key ID.
* ``pubKey``: When using asymmetric key encryption, holds the public key.
* ``ttl``: Time-to-live in seconds.
* ``sig (optional)``: ID of the signing key.
* ``topic``: Message topic (four bytes of arbitrary data).
* ``payload``: Payload to be encrypted.
* ``padding (optional)``: Padding (byte array of arbitrary length).
* ``powTime``: Maximal time in seconds to be spent on prrof of work.
* ``powTarget``: Minimal PoW target required for this message.
* ``targetPeer (optional)``: Peer ID (for peer-to-peer message only).

* Returns ``True`` if the message was succesfully sent,otherwise ``False``
* Returns ``True`` if the message was succesfully sent, otherwise ``False``

.. code-block:: python

>>>web3.shh.post({"topics":[web3.toHex(text="test_topic")],"payload":web3.toHex(text="test_payload")})
>>>web3.shh.post({'payload': web3.toHex(text="test_payload"), 'pubKey': recipient_public, 'topic': '0x12340000', 'powTarget': 2.5, 'powTime': 2})
True

.. py:method:: Shh.newIdentity(self)
.. py:method:: Shh.newMessageFilter(self, criteria, poll_interval=None)

* Delegates to ``shh_newIdentity`` RPC method
* Create a new filter within the node. This filter can be used to poll for new messages that match the set of criteria.

* Returns ``address`` of newly created identity.
* If a ``poll_interval`` is specified, the client will asynchronously poll for new messages.

* Parameters:
* ``symKeyID``: When using symmetric key encryption, holds the symmetric key ID.
* ``privateKeyID``: When using asymmetric key encryption, holds the private key ID.
* ``sig``: Public key of the signature.
* ``minPoW``: Minimal PoW requirement for incoming messages.
* ``topics``: Array of possible topics (or partial topics).
* ``allowP2P``: Indicates if this filter allows processing of direct peer-to-peer messages.

* Returns ``ShhFilter`` which you can either ``watch(callback)`` or request ``get_new_entries()``

.. code-block:: python

>>>web3.shh.newIdentity()
u'0x045ed8042f436e1b546afd16e1f803888b896962484c0154fcc7c5fc43e276972af85f29a995a3beb232a4e9a0648858c0c8c0639d709f5d3230807d084b2d5030'
>>>web3.shh.newMessageFilter({'topic': '0x12340000', 'privateKeyID': recipient_private})
ShhFilter({'filter_id': 'b37c3106cfb683e8f01b5019342399e0d1d74e9160f69b27625faba7a6738554'})

.. py:method:: Shh.hasIdentity(self, identity)
.. py:method:: Shh.deleteMessageFilter(self, filter_id)

* Delegates to ``shh_hasIdentity`` RPC method
* Deletes a message filter in the node.

* Returns ``True`` if the client holds the private key for the given identity,otherwise ``False``
* Returns ``True`` if the filter was sucesfully uninstalled, otherwise ``False``

.. code-block:: python

>>>web3.shh.hasIdentity(u'0x045ed8042f436e1b546afd16e1f803888b896962484c0154fcc7c5fc43e276972af85f29a995a3beb232a4e9a0648858c0c8c0639d709f5d3230807d084b2d5030')
>>>web3.shh.deleteMessageFilter('b37c3106cfb683e8f01b5019342399e0d1d74e9160f69b27625faba7a6738554')
True

.. py:method:: Shh.newGroup(self)
.. py:method:: Shh.getMessages(self, filter_id)

* Delegates to ``shh_newGroup`` RPC method
* Retrieve messages that match the filter criteria and are received between the last time this function was called and now.

* Returns ``address`` of newly created group.
* Returns all new messages since the last invocation

.. note:: This method is not implemented yet in ``Geth``. `Open Issue <https://github.com/ethereum/go-ethereum/issues/310>`_
.. code-block:: python

.. py:method:: Shh.addToGroup(self, identity)
>>>web3.shh.getMessages('b37c3106cfb683e8f01b5019342399e0d1d74e9160f69b27625faba7a6738554')
[{
'ttl': 50,
'timestamp': 1524497850,
'topic': HexBytes('0x13370000'),
'payload': HexBytes('0x74657374206d657373616765203a29'),
'padding': HexBytes('0x50ab643f1b23bc6df1b1532bb6704ad947c2453366754aade3e3597553eeb96119f4f4299834d9989dc4ecc67e6b6470317bb3f7396ace0417fc0d6d2023900d3'),
'pow': 6.73892030848329,
'hash': HexBytes('0x7418f8f0989655ed2f4f9b496e6b1d9be51ef9f0f5ad89f6f750b0eee268b02f'),
'recipientPublicKey': HexBytes('0x047d36c9e45fa82fcd27d35bc7d2fd41a2e41e512feec9e4b90ee4293ab12dc2cfc98250a6f5689b07650f8a5ca3a6e0fa8808cd0ce1a1962f2551354487a8fc79')
}]

* Delegates to ``shh_addToGroup`` RPC Method
.. py:method:: Shh.setMaxMessageSize(self, size)

* Returns ``True`` if the identity was succesfully added to the group,otherwise ``False``
* 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).

.. note:: This method is not implemented yet in ``Geth``. `Open Issue <https://github.com/ethereum/go-ethereum/issues/310>`_
* Returns ``True`` if the filter was sucesfully uninstalled, otherwise ``False``

.. py:method:: Shh.filter(self, filter_params)
.. code-block:: python

* Delegates to ``shh_newFilter`` RPC Method
>>>web3.shh.setMaxMessageSize(1024)
True

.. py:method:: Shh.setMinPoW(self, min_pow)

* ``filter_params`` should contain the ``topics`` to subscribe
* Sets the minimal PoW required by this node.

* Returns an instance of ``ShhFilter`` on succesful creation of filter,otherwise raises ``ValueError`` exception
* Returns ``True`` if the filter was sucesfully uninstalled, otherwise ``False``

.. code-block:: python

>>>shh_filter = shh.filter({"topics":[web.toHex(text="topic_to_subscribe")]})
>>>shh_filter.filter_id
u'0x0'
>>>web3.shh.setMinPoW(0.4)
True

.. py:method:: Shh.uninstallFilter(self, filter_id)
.. py:method:: Shh.markTrustedPeer(self, enode)

* Delegates to ``shh_uninstallFilter`` RPC Method
* Marks specific peer trusted, which will allow it to send historic (expired) messages.

* Returns ``True`` if the filter was sucesfully uninstalled ,otherwise ``False``
* Returns ``True`` if the filter was sucesfully uninstalled, otherwise ``False``

.. code-block:: python

>>>web3.shh.uninstallFilter("0x2")
>>>web3.shh.markTrustedPeer('enode://d25474361659861e9e651bc728a17e807a3359ca0d344afd544ed0f11a31faecaf4d74b55db53c6670fd624f08d5c79adfc8da5dd4a11b9213db49a3b750845e@52.178.209.125:30379')
True

.. py:method:: Shh.getFilterChanges(self, filter_id)
---------------
Asymmetric Keys
---------------

* Delegates to ``shh_getFilterChanges`` RPC Method
.. py:method:: Shh.newKeyPair(self)

* Returns list of messages recieved since last poll
* Generates a new cryptographic identity for the client, and injects it into the known identities for message decryption

* Returns the new key pair's identity

.. code-block:: python

>>>web3.shh.getFilterChanges(self,"0x2")
[{u'from': u'0x0', u'to': u'0x0', u'ttl': 50, u'hash': u'0xf84900b57d856a6ab1b41afc9784c31be48e841b9bcfc6accac14d05d7189f2f', u'payload': u'0x746573696e67', u'sent': 1476625149}]
>>>web3.shh.newKeyPair()
'86e658cbc6da63120b79b5eec0c67d5dcfb6865a8f983eff08932477282b77bb'

.. py:method:: Shh.getMessages(self, filter_id)
.. py:method:: Shh.addPrivateKey(self, key)

* Stores a key pair derived from a private key, and returns its ID.

* Returns the added key pair's ID

.. code-block:: python

>>>web3.shh.addPrivateKey('0x7b8190d96cd061a102e551ee36d08d4f3ca1f56fb0008ef5d70c56271d8c46d0')
'86e658cbc6da63120b79b5eec0c67d5dcfb6865a8f983eff08932477282b77bb'

.. py:method:: Shh.deleteKeyPair(self, id)

* Deletes the specifies key if it exists.

* Returns ``True`` if the key pair was deleted, otherwise ``False``

.. code-block:: python

>>>web3.shh.deleteKeyPair('86e658cbc6da63120b79b5eec0c67d5dcfb6865a8f983eff08932477282b77bb')
True

.. py:method:: Shh.hasKeyPair(self, id)

* Checks if the whisper node has a private key of a key pair matching the given ID.

* Returns ``True`` if the key pair exists, otherwise ``False``

.. code-block:: python

>>>web3.shh.hasKeyPair('86e658cbc6da63120b79b5eec0c67d5dcfb6865a8f983eff08932477282b77bb')
False

.. py:method:: Shh.getPublicKey(self, id)

* Returns the public key associated with the key pair.

.. code-block:: python

>>>web3.shh.getPublicKey('86e658cbc6da63120b79b5eec0c67d5dcfb6865a8f983eff08932477282b77bb')
'0x041b0777ceb8cf8748fe0bba5e55039d650a03eb0239a909f9ee345bbbad249f2aa236a4b8f41f51bd0a97d87c08e69e67c51f154d634ba51a224195212fc31e4e'

* Delegates to ``shh_getMessages`` RPC Method
.. py:method:: Shh.getPrivateKey(self, id)

* Returns a list of all messages
* Returns the private key associated with the key pair.

.. code-block:: python

>>>web3.shh.getMessages("0x2")
[{u'from': u'0x0', u'to': u'0x0', u'ttl': 50, u'hash': u'0x808d74d003d1dcbed546cca29d7a4e839794c226296b613b0fa7a8c670f84146', u'payload': u'0x746573696e67617364', u'sent': 1476625342}, {u'from': u'0x0', u'to': u'0x0', u'ttl': 50, u'hash': u'0x62a2eb9a19968d59d8a85e6dc8d73deb9b4cd40c83d95b796262d6affe6397c6', u'payload': u'0x746573696e67617364617364', u'sent': 1476625369}]
>>>web3.shh.getPrivateKey('86e658cbc6da63120b79b5eec0c67d5dcfb6865a8f983eff08932477282b77bb')
'0x7b8190d96cd061a102e551ee36d08d4f3ca1f56fb0008ef5d70c56271d8c46d0'

---------------
Symmetric Keys
---------------

.. py:method:: Shh.newSymKey(self)

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

* Returns the new key pair's identity

.. code-block:: python

>>>web3.shh.newSymKey()
'6c388d63003deb378700c9dad87f67df0247e660647d6ba1d04321bbc2f6ce0c'

.. py:method:: Shh.addSymKey(self, key)

* Stores the key, and returns its ID.

* Returns the new key pair's identity

.. code-block:: python

>>>web3.shh.addSymKey('0x58f6556e56a0d41b464a083161377c8a9c2e95156921f954f99ef97d41cebaa2')
'6c388d63003deb378700c9dad87f67df0247e660647d6ba1d04321bbc2f6ce0c'

.. py:method:: Shh.generateSymKeyFromPassword(self)

* Generates the key from password, stores it, and returns its ID.

* Returns the new key pair's identity

.. code-block:: python

>>>web3.shh.generateSymKeyFromPassword('shh secret pwd')
'6c388d63003deb378700c9dad87f67df0247e660647d6ba1d04321bbc2f6ce0c'

.. py:method:: Shh.hasSymKey(self, id)

* Checks if there is a symmetric key stored with the given ID.

* Returns ``True`` if the key exists, otherwise ``False``

.. code-block:: python

>>>web3.shh.hasSymKey('6c388d63003deb378700c9dad87f67df0247e660647d6ba1d04321bbc2f6ce0c')
False

.. py:method:: Shh.getSymKey(self, id)

* Returns the symmetric key associated with the given ID.

* Returns the public key associated with the key pair

.. code-block:: python

>>>web3.shh.getSymKey('6c388d63003deb378700c9dad87f67df0247e660647d6ba1d04321bbc2f6ce0c')
'0x58f6556e56a0d41b464a083161377c8a9c2e95156921f954f99ef97d41cebaa2'

.. py:method:: Shh.deleteSymKey(self, id)

* Deletes the symmetric key associated with the given ID.

* Returns ``True`` if the key pair was deleted, otherwise ``False``

.. code-block:: python

>>>web3.shh.deleteSymKey('6c388d63003deb378700c9dad87f67df0247e660647d6ba1d04321bbc2f6ce0c')
True
10 changes: 10 additions & 0 deletions tests/core/shh-module/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

from web3.shh import (
Shh,
)


@pytest.fixture(autouse=True)
def include_shh_module(web3):
Shh.attach(web3, "shh")
Loading