13

I was looking to develop an application using Whisper & NodeJS and I'm having trouble finding an example that works and is updated.

I've encountered this doc web3js.readthedocs.io/en/1.0/web3-shh.html and this guide https://github.com/ethereum/wiki/wiki/Whisper-Overview, but they are incompatible with each other ( probably version thing).

I've managed to get this far, in what I think should publish a message on Whisper, but I don't know why isn't working nor how to listen to it.

var net = require('net');
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.IpcProvider('/Users/tloriato/Library/Ethereum/geth.ipc', net));

var KeyID;
var PubKey;

var message = web3.utils.toHex("Test");

web3.shh.newKeyPair().then((data) => {
    console.log(data);
    web3.shh.getPublicKey(KeyID).then((data) => {
        console.log(data);
    }).then(() => {
        shh.post({
            pubKey: PubKey,
            sig: KeyID,
            ttl: 120,
            payload: message,
            powTime: 2,
            powTarget: 1
            }, function(err, data){
                if (err) console.log(err);
                console.log(data);
        });
    });
});

[Q]: Does anybody has a working example for whisper working with the recent version of geth or partiy?

3

Based on the Whisper V5 RPC API documentation, I was finally able to construct a working example with private/public keys. I have tested this in a private network with geth 1.7.2 console. In mainnet, whisper will most probably not work, since the majority of nodes will not have this protocol enabled.

On the receiver node:

1/ create a new keypair

var kId = web3.shh.newKeyPair();

2/ create a message filter. The most simple version is to specify the private key. All messages encrypted with the corresponding public key will trigger the callback function. You can narrow this downs by specifying a topic.

web3.shh.newMessageFilter(
    {privateKeyID:kId}, 
    function(err, res) {console.log(JSON.stringify(res))});

3/ get the public key (which is needed by the sender)

web3.shh.getPublicKey(kId)

On the sender node:

(replace PUBLIC_KEY_OF_THE_RECEIVER with the value from Step 3 above in between single quote ('))

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

ttl, topic, powTarget, powTime are values from the documentation.

  • geth version is 1.8.0 but I am having: Error: The method shh_newKeyPair does not exist/is not available. Please see in depth: ethereum.stackexchange.com/q/51585/4575 @ivicaa – alper Jun 19 '18 at 14:44
  • I tested this with geth 1.7.2 as I wrote above... Didn't test it with geht 1.8.0 yet. – ivicaa Jun 19 '18 at 15:00
  • On the receiver node side res.payloadstring should be decoded to see the sender's original message. Please follow (ethereum.stackexchange.com/a/51605/4575) in order to decode the sender's sent message. – alper Jun 19 '18 at 19:33
  • @alper You need to run geth with --shh flag – syzygy Sep 20 '18 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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