Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
emclab opened this issue Apr 5, 2021 · 7 comments

Comments

@emclab
Copy link

emclab commented Apr 5, 2021

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.

@emclab
Copy link
Author

emclab commented Apr 8, 2021

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]}}

@emclab
Copy link
Author

emclab commented Apr 9, 2021

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.

@zemse
Copy link
Collaborator

zemse commented Apr 9, 2021

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.

@emclab
Copy link
Author

emclab commented Apr 9, 2021

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.

@zemse
Copy link
Collaborator

zemse commented Apr 9, 2021

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

@emclab
Copy link
Author

emclab commented Apr 9, 2021

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.

@emclab emclab closed this as completed Apr 9, 2021
@zemse
Copy link
Collaborator

zemse commented Apr 9, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants