web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.33k stars 4.96k forks source link

Using the whisper api #1311

Closed justinmchase closed 6 years ago

justinmchase commented 6 years ago

Hello!

I'm trying to simply test out using the whisper api. I'm very new to this but what I am doing is running a local ethereum node using docker and using nodejs to attempt to communicate with that server. I am getting some errors though and I'm not sure what I'm doing wrong.

Here is my basic docker-compose file:

eth:
  image: ethereum/client-go
  stdin_open: true
  tty: true
  ports:
    - "8545:8545"
    - "30303:30303"
  entrypoint: geth --testnet --fast --cache=512 --rpc --rpcaddr 0.0.0.0 --rpcapi eth,net,web3,shh

And my basic nodejs script:

var Web3 = require('web3')
var web3 = web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
var shh = web3.shh
shh.version((err, version) => {
  if (err) return console.error(err)
  console.log(version)
})
web3.shh.info((err, info) => {
  if (err) return console.error(err)
  console.log(info)
})

And then to run:

docker-compose up -d
node .

The errors I am getting are:

Error: The method shh_version does not exist/is not available
Error: The method shh_info does not exist/is not available

What am I doing wrong?

justinmchase commented 6 years ago

Actually using any api seems to not work. I'm using INFURA now instead of a local docker container and even the simplest examples from the main README don't seem to work. It seems like there is a version mismatch somewhere, any advice here on which versions of things I should be using to match up?

var Web3 = require('web3')
var web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider('https://ropsten.infura.io/...'))
console.log('coinbase:', web3.eth.coinbase) // Error: invalid JSON RPC response
justinmchase commented 6 years ago

I managed to get it to work but it was a bit of an ordeal. First off I installed the latest 1.0 release:

npm install web3@1.0.0-beta.28

Next I was missing the --shh flag in my docker compose command, which is apparently necessary since whisper is still an experimental feature. Here is my docker-compose.yml file:

eth:
  image: ethereum/client-go
  stdin_open: true
  tty: true
  ports:
    - "8545:8545" # rpc
    - "8546:8546" # ws
    - "30303:30303"
  command: --fast --cache=512 --shh --ws --wsaddr=0.0.0.0 --wsorigins *

Then finally, which I'm a little embarassed to admit but the fact that ws and rpc use different ports by 1 took me too long to figure out. Thus listing both ports above and my below finally working script:

var Web3 = require('web3')
var type = 'ws' // 'rpc'
var connectionString = type === 'ws'
  ? 'ws://localhost:8546'
  : 'http://localhost:8545'

var web3 = new Web3(Web3.givenProvider || connectionString)
web3.shh.getVersion((err, version) => {
  if (err) return console.error(err.message)
  console.log(version)
})

Its now working! And it prints out 5.0