-
Notifications
You must be signed in to change notification settings - Fork 167
Getting Started
Steve Mieskoski edited this page Jul 6, 2020
·
1 revision
# With NPM
npm install @myetherwallet/mewconnect-web-client
import MEWconnect from "@myetherwallet/mewconnect-web-client"
import Web3 from "web3"
const CHAIN_ID = 1
// Initialize
export const mewConnect = new MEWconnect.Provider()
// Initialize a Web3 Provider object
export const ethereum = mewConnect.makeWeb3Provider(CHAIN_ID)
// Initialize a Web3 object
export const web3 = new Web3(ethereum)
- 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()
// Initialize a Web3 Provider object
export const ethereum = mewConnect.makeWeb3Provider(CHAIN_ID, ETH_JSONRPC_URL)
// Initialize a Web3 object
export const web3 = new Web3(ethereum)
Invoking EIP-1102 will show a QR code dialog if the user's mobile wallet is not already connected to their browser. 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.
// Use eth_RequestAccounts
ethereum.send("eth_requestAccounts").then((accounts) => {
console.log(`User's address is ${accounts[0]}`)
})
// Alternatively, you can use ethereum.enable()
ethereum.enable().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.