From Whisper wiki:

Low-latency, 1-1 or 1-N signalling messages.

Shh.post(self, message:) and Whisper Usage / shh.post

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

Example usage of web3.shh.post, for more details please see this answer:

web3.shh.post({
  pubKey: 'PUBLIC_KEY_OF_THE_RECEIVER',
  ttl: 3600,
  topic: '0x07678231',
  powTarget: 2.01,
  powTime: 2,
  payload: web3.fromAscii("Hello there!")
  });

When I remove pubKey from web3.shh.post as arguments; it says: Error: specify either a symmetric or an asymmetric key.

shh.post({ "topic": t, "payload": p }); No signature, no encryption: Anonymous broadcast; a bit like an anonymous subject-filtered twitter feed.

[Q] Since web3.shh.post()requires us to provide single public key of the receiver; is it possible to send 1-N or broadcast message using whisper protocol? If yes, how?

  • I wrote the implementation on python3 here it is – Sergey Zaharov Jul 3 at 10:17
  • Your answer does not sent 1-N messages. @Sergey Zaharov – alper Jul 3 at 15:08
up vote 2 down vote accepted
+50

I would recommended to use this documentation : https://github.com/ethereum/go-ethereum/wiki/Whisper

Whisper usage

your posted links are outdated and moreover are for Whisper v 2.0

Answer: It's possible 1:N message if you provide identical symKey for all recipients.

P.S. My posted links are for Whisper 5.0 , current version of Whisper is 6.0 but but the API for v6 is almost identical to v5 anyway.


On node-1's geth-client:

generatedSymKey=shh.generateSymKeyFromPassword("hello");    
symKey=shh.getSymKey(generatedSymKey)    
symKeyID=shh.addSymKey(symKey) //ex: "d5212e736703afbb21246e8acd192e4345ea910398544d765ed6b49f0ec524b5"
filter = web3.shh.newMessageFilter(
        {symKeyID:symKeyID, topic: '0x07678231'}, 
        function(err, res) {console.log(web3.toUtf8(res.payload))});

On node-2's geth-client:

generatedSymKey=shh.generateSymKeyFromPassword("hello")    
symKey=shh.getSymKey(generatedSymKey)    
symKeyID=shh.addSymKey(symKey) //ex: "c4c4cecf6ad9499c2386b8ce6416f44684e042e491726b38315646c0b4afadc6"
filter = web3.shh.newMessageFilter(
        {symKeyID:symKeyID, topic: '0x07678231'}, 
        function(err, res) {console.log(web3.toUtf8(res.payload))});

Than, sending message on another node and both messages show up on node-1 and node-2 even if symKeyID is either node-1's or node-2's on shh.post() function.

Following code is run on node-1's geth-client:

node-1's symKeyID is given:

web3.shh.post({
  symKeyID: 'd5212e736703afbb21246e8acd192e4345ea910398544d765ed6b49f0ec524b5', //symKeyID of the node-1
  ttl: 10,
  topic: '0x07678231',
  powTarget: 2.01,
  powTime: 2,
  payload: web3.fromAscii("Hello there!")
  });

OR


Following code is run on node-2's geth-client:

node-2's symKeyID is given:

web3.shh.post({
  symKeyID: 'c4c4cecf6ad9499c2386b8ce6416f44684e042e491726b38315646c0b4afadc6', //symKeyID of the node-2
  ttl: 10,
  topic: '0x07678231',
  powTarget: 2.01,
  powTime: 2,
  payload: web3.fromAscii("Hello there!")
  });
  • Thanks for the updated links. But how could recipients have identical symKey or publicKey since they are unique for each recipient? @Baracuda – alper Jul 8 at 17:29
  • I have just pushed working ssh demo: github.com/achiko/whisper-v6-test I used shh.generateSymKeyFromPassword('....' ) method with one password. – Baracuda Jul 8 at 17:37
  • 1
    I am pretty sure each receipt should have a unique symKey. What do you get symKey with following: shh.generateSymKeyFromPassword("hello") which I get : a0ce4c9103be09614d59103ca5aeee0bda8e59e9f6112f6c0b40f439ab0b4ab9 for receipt-1 and for another receipt I get different one. 4a6c15f288bfa9949702aa9a5e5bd305b6f48681513a266e8a3c42e58433f859 . – alper Jul 8 at 17:43
  • generateSymKeyFromPassword Generates the key from password, stores it, and returns its id. github.com/ethereum/go-ethereum/wiki/… Its just ID stored locally. – Baracuda Jul 9 at 9:55
  • 1
    I got your point, I added your answer with a tutorial code. @Baracuda – alper Jul 9 at 12:51

Solution using Web3.py:

I have followed following ssh-mode code.


receiver.py that runs on node-1:

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh
Shh.attach(web3, "shh")

import time, sys;

from hexbytes import (
    HexBytes,
)

kId = web3.shh.newKeyPair()
receiver_pub = web3.shh.getPublicKey(kId)
print('receiverPubK: ' + receiver_pub);

topic = '0x07678231'

shh_filter = web3.shh.newMessageFilter({
    'privateKeyID': kId,
    'topics': [topic]
})

input("Press Enter to continue...");

received_messages = [];
received_messages = shh_filter.get_new_entries()
print(len(received_messages)) # Returns '0'
print(web3.shh.info.memory)  # Returns '0'

Output:

receiverPubK: 0x04226d96bf9857ac0ba429c1e8b480a2811ce47cb526dbd3829d7586e5cae740198ba291f3eca0f279f82db8a136be90ea9ec629ed6cd1d45cc7f873159811757d
Press Enter to continue...

After Node_1's messages are sent; I have copied printed receiverPublicKey into receiver_pub variable on the following code. I run following code on Node 2. Node 2 sends a message to the network. Later I press enter on the Node_1 and Node_1 prints the message

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh
Shh.attach(web3, "shh")

import time, sys;

from hexbytes import (
    HexBytes,
)

receiver_pub='0x04226d96bf9857ac0ba429c1e8b480a2811ce47cb526dbd3829d7586e5cae740198ba291f3eca0f279f82db8a136be90ea9ec629ed6cd1'; # obtained from node_1 and assigned here.

topic = '0x07678231'
payloads = [web3.toHex(text="test message :)"), web3.toHex(text="2nd test message")]

web3.shh.post({
        'powTarget': 2.5,
        'powTime': 2,
        'ttl': 60,
        'payload': payloads[0],
        'topic': topic,
        'pubKey': receiver_pub
})    

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.