3

My main goal is to call web3.shh functions using RPC API , Web3.py or Web3.js. I can call web3.shh functions when I use geth attach; which is my last option to do.

I am following this answer.

  • geth version: 1.8.0-unstable
  • I am running geth with --shh flag and --rpcapi "admin,eth,net,web3,debug,shh"

  • console.log(web3.version); returns: api: '0.20.5'.

When I run following script:

Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

if(!web3.isConnected()){ //[email protected]
//if(!web3.eth.net.isListening().then(console.log)){ //[email protected]
    console.log("notconnected");
    process.exit();
}

var kId = web3.shh.newSymKey(); //Error occurs. 

var kId = web3.shh.newSymKey(); gives following error with web3@'0.20.5':

Error: The method shh_newSymKey does not exist/is not available
    at Object.InvalidResponse (/home/alper/eBlocBroker/node_modules/web3/lib/web3/errors.js:38:16)
    at RequestManager.send (/home/alper/eBlocBroker/node_modules/web3/lib/web3/requestmanager.js:61:22)
    at Shh.send [as newSymKey] (/home/alper/eBlocBroker/node_modules/web3/lib/web3/method.js:145:58)
    at Object.<anonymous> (/home/alper/eBlocBroker/dd.js:9:20)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)
    at Function.Module.runMain (module.js:690:10)

Please note that I have tried it with [email protected] that also gives this error that seems like the same error:

Promise { <pending> }
true
(node:16162) UnhandledPromiseRejectionWarning: Error: Returned error: The method shh_newSymKey does not exist/is not available
    at Object.ErrorResponse (/home/alper/eBlocBroker/node_modules/web3-shh/node_modules/web3-core-helpers/src/errors.js:29:16)
    at /home/alper/eBlocBroker/node_modules/web3-shh/node_modules/web3-core-requestmanager/src/index.js:140:36
    at XMLHttpRequest.request.onreadystatechange (/home/alper/eBlocBroker/node_modules/web3/node_modules/web3-providers-http/src/index.js:77:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:469:24)
    at IncomingMessage.emit (events.js:185:15)
    at endReadableNT (_stream_readable.js:1101:12)
    at process._tickCallback (internal/process/next_tick.js:114:19)
(node:16162) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16162) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

[Q] How could I fix this error? What do I do wrong?

Please note that available shh functions could be seen here; output of console.log(web3.shh).

9
  • what is your web3 version ? Jun 19, 2018 at 13:34
  • I meant your web3 version, is it web3 1.0 or 0.x because the latter doesn't have the newSymKey() method as of these docs : github.com/ethereum/wiki/wiki/JavaScript-API#web3shh Jun 19, 2018 at 13:51
  • Ah I am sorry I forget to add it. I did console.log(web3.version) and it returns: api: '0.20.5' @KakiMasterOfTime
    – alper
    Jun 19, 2018 at 13:55
  • like I did in your client side js you are using web3 0.x version so that method doesn't exit. you should use the newer version of web3 1.0, it is already stable. Jun 19, 2018 at 14:02
  • 1
    I tried running geth with --shh and then using web3 and it worked perfectly, but I am running the sable version of geth 1.8.7 and the latest web3 1.0. can you use the web3.shh from the javascript geth console ( geth attach ) ? if not try it and see if shh is correctly activated. Jun 19, 2018 at 15:27

1 Answer 1

1
+50

The newSymKey(); function returns a promise, which needs to be resolved, to get the returned value.

This is why the error in version [email protected] gives you a promise pending message with UnhandledPromiseRejectionWarning, and in the [email protected] version, the error states shh_newSymKey does not exist/is not available. [yet]

So try var kId = web3.shh.newSymKey().then(console.log);

or var kId = web3.shh.newSymKey().then(function(result) { console.log(result) //will log results. })

I prefer using the newer async / await function instead of promises (reads easier):

Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));     

async function connect() { //note async function declaration
    if(!await web3.isConnected()){ //await [email protected]
    //if(!await web3.eth.net.isListening()){ //await [email protected]
        console.log("notconnected");
        process.exit();
    }

    var kId = await web3.shh.newSymKey(); //note await
    console.log(kId);

}

connect();
5
  • For shh.getPrivateKey(kId).then(console.log) it returns: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Returned error: invalid argument 0: json: cannot unmarshal object into Go value of type string How could I also fix this error? @Herald
    – alper
    Jun 26, 2018 at 10:46
  • I assume you posted this follow up questions here: ethereum.stackexchange.com/questions/52112/…
    – Herald
    Jun 26, 2018 at 20:29
  • 1
    It is caused by the same issue you had before, the line gets executed before the previous lines' promise has been resolved. So kId is empty when you call getPrivateKey. You need to use await as shown above, or call shh.getPrivateKey(kId); in a callback function instead of console logging the kId the function definitions as per docs are: web3.shh.newSymKey([callback]) web3.shh.getPrivateKey(id, [callback])
    – Herald
    Jun 26, 2018 at 20:31
  • Instead of giving kId , I provided as a string still facing with the same error.
    – alper
    Jun 26, 2018 at 20:38
  • We can continue from the follow up question.
    – alper
    Jun 26, 2018 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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