Installation
With NPM
npm install @myetherwallet/mewconnect-web-client
Setup Instance
import MEWconnect from "@myetherwallet/mewconnect-web-client"
import Web3 from "web3"
const CHAIN_ID = 1
Initialize the client
export const mewConnect = new MEWconnect.Provider([options])
Initialize a Web3 Provider object
export const ethereumProvider = mewConnect.makeWeb3Provider([chainId, rpcUrl, noUrlCheck])
Initialize a Web3 instance
export const web3 = new Web3(ethereumProvider)
Initiate connection and get Ethereum address
The following code should run in response to a user-initiated action such as clicking a button to ensure the pop up is not blocked by the browser.
you can use ethereum.enable()
ethereum.enable().then((accounts) => {
console.log(`User's address is ${accounts[0]}`)
})
Or you can use eth_requestAccounts
ethereum.send("eth_requestAccounts").then((accounts) => {
console.log(`User's address is ${accounts[0]}`)
})
That’s it! Once the connection between the phone and the site is established, the Web3 object
(web3
) and the Web3 Provider (ethereum
) are ready to be used as usual.
Alternatively, a node rpc url may be supplied
- Note: only websocket urls are supported.
import MEWconnect from "@myetherwallet/mewconnect-web-client"
import Web3 from "web3"
const ETH_JSONRPC_URL = "wss://mainnet.infura.io/v3/<YOUR_INFURA_API_KEY>"
const CHAIN_ID = 1
// Initialize MEWconnect
export const mewConnect = new MEWconnect.Provider([options])
// Initialize a Web3 Provider object
export const ethereum = mewConnect.makeWeb3Provider([chainId, rpcUrl, noUrlCheck])
// Initialize a Web3 object
export const web3 = new Web3(ethereum)