Giter VIP home page Giter VIP logo

eth-provider's Introduction


eth-provider


A Universal Ethereum Provider Client

Seamlessly connect to HTTP, WebSocket, IPC and Injected RPC transports in Node and the Browser!



Goals

  • Follows EIP 1193 Spec
  • Support all transport types (websocket, http, ipc & injected)
  • Attempt connection to an array of RPC endpoints until successful connection
  • Reconnect when connection is lost
  • Emit helpful status updates so apps can handle changes gracefully

Install

npm install eth-provider --save

Use

const provider = require('eth-provider')
const web3 = new Web3(provider())
  • By default, eth-provider will first try to discover providers injected by the environment, usually by a browser or extension
  • If eth-provider fails to find an injected provider it will attempt to connect to local providers running on the user's device like Frame, Geth or Parity
  • You can override these defaults by passing in your own RPC targets
const provider = require('eth-provider')
const web3 = new Web3(provider('wss://sepolia.infura.io/ws/v3/${INFURA_ID}))
  • When passing in multiple RPC targets order them by priority
  • When eth-provider fails to connect to a target it will automatically attempt to connect to the next priority target
  • For example ['injected', 'wss://sepolia.infura.io/ws/v3/${INFURA_ID}'] will first try to discover injected providers and if unsuccessful connect to the Infura endpoint
const provider = require('eth-provider')
const web3 = new Web3(provider(['injected', 'wss://sepolia.infura.io/ws/v3/${INFURA_ID}']))
  • In Node and Electron you'll have access to IPC endpoints created by Geth or Parity that cannot be accessed by the Browser. You can connect to these by using the 'direct' preset, or by passing custom IPC paths
const provider = require('eth-provider')
const web3 = new Web3(provider('direct'))

Presets

  • injected - Discover providers injected by environment, usually by the browser or a browser extension
    • Browser
      • ['injected']
  • frame - Connect to Frame running on the user's device
    • Browser/Node/Electron
      • ['ws://127.0.0.1:1248', 'http://127.0.0.1:1248']
  • direct - Connect to local Ethereum nodes running on the user's device
    • Browser
      • ['ws://127.0.0.1:8546', 'http://127.0.0.1:8545']
    • Node/Electron
      • [/* Default IPC paths for platform */, 'ws://127.0.0.1:8546', 'http://127.0.0.1:8545']
  • infura - Connect to Mainnet Infura
    • Browser/Node/Electron
      • ['wss://mainnet.infura.io/ws/v3/${infuraId}', 'https://mainnet.infura.io/v3/${infuraId}']
  • alchemy - Connect to Mainnet Alchemy
    • Browser/Node/Electron
      • ['wss://eth-mainnet.ws.alchemyapi.io/v2/${alchemyId}', 'https://eth-mainnet.alchemyapi.io/v2/${alchemyId}']

View all possible presets here

If you do not pass any targets, eth-provider will use default targets ['injected', 'frame'] in the Browser and ['frame', 'direct'] in Node and Electron.

Options

When creating the provider you can also pass an options object

  • infuraId - Your projects Infura ID
  • alchemyId - Your projects Alchemy ID
  • origin - Used when connecting from outside of a browser env to declare the identity of your connection to interfaces like Frame (this currently doesn't work with HTTP connections)

provider('infura', { infuraId: '123abc' }) or provider({ origin: 'DappName', infuraId: '123abc' })

The origin setting will only be applied when a dapp is connecting to from outside of a browser env.

eth-provider's People

Contributors

bpierre avatar floating avatar goosewobbler avatar isoch avatar mholtzman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eth-provider's Issues

help deploying contract with ledger

I have seen your example here
NomicFoundation/hardhat#1159 (comment)

and I follow you instructions

    const frame = ethProvider('frame');
    const contractFactory = await ethers.getContractFactory("MyContract");
    const tx = await contractFactory.getDeployTransaction();

but get this:

 const tx = await this.signer.sendTransaction(unsignedTx);                              ^
TypeError: Cannot read properties of null (reading 'sendTransaction')

Also, can your example be adapter to deploy an upgradable contract?

const test = await upgrades.deployProxy(contractFactory);

There is a vulnerability introduced in your package

Hi, @floating @mholtzman,I’d like to report a vulnerability introduced in your package eth-provider:

Issue Description

A vulnerability CVE-2021-32640 detected in package ws(<5.2.3,>=6.0.0 <6.2.2,>=7.0.0 <7.4.6) is directly referenced by [email protected]. We noticed that such a vulnerability has been removed since [email protected].

However, eth-provider's popular previous version [email protected] (4,847 downloads per week) is still transitively referenced by a large amount of latest versions of active and popular downstream projects (about 32 downstream projects, e.g., use-wallet 0.9.0, @binance-chain/bsc-use-wallet 0.8.1, @sekmet/next-auth 0.1.49, cryptowalletconnector 1.0.14, hubot-kredits 3.8.0, @aragon/[email protected], etc.).
As such, issue CVE-2021-32640 can be propagated into these downstream projects and expose security threats to them.

These projects cannot easily upgrade eth-provider from version 0.2.5 to (>=0.7.0). For instance, [email protected] is introduced into the above projects via the following package dependency paths:
(1)@aragon/[email protected][email protected] ➔ @web3-react/[email protected][email protected][email protected]
......

The projects such as @web3-react/frame-connector, which introduced [email protected], are not maintained anymore. These unmaintained packages can neither upgrade eth-provider nor be easily migrated by the large amount of affected downstream projects.
On behalf the downstream users, could you help us remove the vulnerability from package [email protected]?

Suggested Solution

Since these inactive projects set a version constaint 0.2.* for eth-provider on the above vulnerable dependency paths, if eth-provider removes the vulnerability from 0.2.5 and releases a new patched version [email protected], such a vulnerability patch can be automatically propagated into the 32 affected downstream projects.

In [email protected], you can kindly try to perform the following upgrade:
ws 7.1.2 ➔ 7.4.6;
Note:
[email protected] has fixed the vulnerability (CVE-2021-32640)

Thank you for your help.

Best regards,
Paimon

hardhat deploy with ledger

I have seen your example here
NomicFoundation/hardhat#1159 (comment)

and I follow you instructions

    const frame = ethProvider('frame');
    const contractFactory = await ethers.getContractFactory("MyContract");
    const tx = await contractFactory.getDeployTransaction();

but get this:

 const tx = await this.signer.sendTransaction(unsignedTx);                              ^
TypeError: Cannot read properties of null (reading 'sendTransaction')

Also, can your example be adapter to deploy an upgradable contract?

const test = await upgrades.deployProxy(contractFactory);

Invalid provider preset/location: "infuraGoerli"

Hi, author. Thank you for this awesome library.

I have a problem connecting to infura goerli network using preset.
Error message => eth-provider | Invalid provider preset/location: "infuraGoerli"

const ethProvider = require('eth-provider');

const target = 'infuraGoerli'

this.provider = ethProvider(target, {
  infuraId: 'my infura id',
});

this.provider.send(
  'eth_chainId'
).then(console.log);

Kovan and other test networks are working ok.

`Closing WebSocket Connection` log

It looks like a recent commit introduced a new console.log anytime process.env.NODE_ENV !== 'development'. Using the development environment will turn that log off but cause this to get logged instead:

A connection cycle started for provider with name: default
Successfully connected to: ws://127.0.0.1:8546

ws connection 'connect' event firing before provider is connected

It appears that the 'connect' event is being emitted before its provider is connected.

If I create a new Web3 instance as such:

const provider = require('eth-provider')
const Web3 = require('web3')
const web3Instance = new Web3(provider('ws://127.0.0.1:8546'))
web3Instance.currentProvider.connection.once('connect', () => {
  console.log(web3Instance)
})

The log output shows the provider as connected: false and status: loading

I believe the expected behavior should be that the 'connect' event is emitted iff the provider is connected and usable.

Let me know if I can help or if my understanding is incorrect. Thanks!

How to use library in flutter webview

How to use this library in flutter web view to inject Wallet, Is there any way to download dist/eth-provider.min.js file to add in mobile wallet?

Next EIP1193 call

Hi @floating,

I'm opening this issue because between a few interested parties we had an open call today to discuss possible changes on EIP1193 and its release schedule. The call wasn't very representative, so nothing was decided, and we are scheduling another one.

As you are the author of this library and Frame, I think you may be interested in joining the next call, or the discord channel where this is being discussed. There are also some interesting discussions going on on ethereum magicians provider ring, and you may want to check them.

Best,
Patricio

Custom origin

User should be able to pass custom origin in options

eth-provider fails to fallback if frame is not available

It seems to me that eth-provider fails to fallback to the next target.

I am trying to use eth-provider connected to frame and if frame is not available/running it should fallback to infura.

The call to connect to frame fails with an SyntaxError: Unexpected end of JSON input error and thus the provider does not try to do the call through the next target.

my code:

const Web3 = require('web3');
const provider = require('eth-provider');

web3 = new Web3(provider('frame', 'infuraRinkeby'));
web3.eth.getBlockNumber().then(console.log)

which will result in:

(node:24771) UnhandledPromiseRejectionWarning: #<Object>
(node:24771) 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: 2)

The error seems to happen in: https://github.com/floating/eth-provider/blob/master/connections/http.js#L103
A console.log in the catch block gives:

SyntaxError: Unexpected end of JSON input

Do you have an idea? If I understand the readme correct it should automatically fall back to infura in my code above, shouldn't it?

Ethereum providers may not be EventEmitters

Line causing crashes.

Although EIP-1193 loosely defines events as being emitted through EventEmitter, there is no actual hard spec that says the window.ethereum object must be an EventEmitter implementation (nor which API version).

This has lead to some environments where the window.ethereum object doesn't implement an emit() API (e.g. Status), and thus crashes eth-provider.

Furthermore, it seems like this bit of code (monitor()) only serves to add a few additional events to the provider which are not included in EIP-1193 and are not used internally at all. I assume Frame uses these events?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.