Ethers.js: Error: Wallet has error: Error: could not detect network (event=“noNetwork”, code=NETWORK_ERROR, version=providers/5.0.17)

Created on 6 Apr 2021  Â·  7Comments  Â·  Source: ethers-io/ethers.js

After enabling JWT token authentication on besu network, a connectionInfo with JWT token in headers is used to make connection to the network, instead of url.

  import { ContractFactory, ethers } from "ethers";
  let connectionInfo = {  //<<==connectionInfo may have problem. Used to use url and worked fine.
     url: GLOBAL.PROVIDER_URL, //<<=="http://my-ip:80"
     headers: {
       "Bearer": tokenBesu //<<==tokenBesu is validated on jwt.io and OKed with cUrl command querying the network
  },
  };
  const provider = new ethers.providers.JsonRpcProvider(connectionInfo); //<<==used to be url
  wallet = new ethers.Wallet(privateKeyBesu, provider); //<<== this line throws error

Also tried:

const provider = new ethers.providers.JsonRpcProvider(connectionInfo, 2018); //<<==2018 is chain id

The error is the same with wallet.

However querying network with cUrl (provided by Besu doc) in command line returns true.

  $ curl -X POST -H 'Authorization: Bearer tokenBesu' -d '{"jsonrpc":"2.0","method":"net_listening","params":[],"id":1}' http://my-ip:80
  {
    "jsonrpc" : "2.0",
    "id" : 1,
    "result" : true
  }

The network is running fine. The app calling JsonRpcProvider has React Native 0.62.3.

Here are returns of eth_chainId and eth_netowrkId in command line with curl:

  $ curl -X POST -H 'Authorization: Bearer tokenBesu' -d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' http://my-ip:80

{
"jsonrpc" : "2.0",
"id" : 1,
"result" : "0x7e2"  //<<==2018 which is the network id
}

$ curl -X POST -H 'Authorization: Bearer tokenBesu' -d '{"jsonrpc":"2.0","method":"eth_networkId","params":[],"id":1}' http://my-   ip:80

{
  "jsonrpc" : "2.0",
  "id" : 1,
  "error" : {
    "code" : -32601,
    "message" : "Method not found"  //<<==not sure why eth_networkId was not found.
  }
}

Before enabling JWT token authentication, the Besu was running fine with contract deployment, contract method calling and so on.

All 7 comments

Here is the provider generated with connection and networkId (both 2018 and hex equivalent 0x7e2)

    Rpc provider :  {"_emitted": {"block": -2}, "_events": [], "_fastQueryDate": 0, "_isProvider": true, "_lastBlockNumber": -2, "_maxInternalBlockNumber": -1024, "_network": {"chainId": 2018, "name": "unknown"}, "_nextId": 42, "_pollingInterval": 4000, "anyNetwork": false, "connection": {"headers": {"Bearer": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVC....kBouxhrSvWSuLhwd1gyNtwlDnzJpBLflFgM9o-2NF6WR-q68Tfrd1QQ"}, "url": "http://my-ip:80"}, "formatter": {"formats": {"block": [Object], "blockWithTransactions": [Object], "filter": [Object], "filterLog": [Object], "receipt": [Object], "receiptLog": [Object], "transaction": [Object], "transactionRequest": [Object]}}

Does anyone have the idea about the problem? If you do, please don't hesitate to share. I am stuck with it right now. Many thanks.

The JsonRpcProvider makes eth_chainId calls before every request to ensure proper chain id (even though you have already provided chain id when creating the provider). When this call fails for some reason, you get this error could not detect network.

For constant chain id providers, you can use StaticJsonRpcProvider, this assumes the chain id that doesn't change.

zemse, StaticJsonRpcProvider throws en error of not callable module. Is it an old provider? I didn't find description of it in v5. I thought after providing chainId, the JsonRpcProvider won't fallback. Also even fallback, submitting the auth token in header with request makes sense.

StaticJsonRpcProvider is added in v5 (#901), so definitely it should work. Also can you share what exact error you are getting?

zemse, It works after relaunching the app. const provider = new ethers.providers.StaticJsonRpcProvider(connection, 2018); Many thanks.

BTW is there doc about StaticJsonRpcProvider? I didn't find it in v5 doc.

I had a quick look in the docs and seems it's not there. Will be added soon! For now it's API is pretty much same as JsonRpcProvider.

Was this page helpful?
0 / 5 - 0 ratings