MetaMask / extension-provider

A module for allowing a WebExtension to access the web3 provider from an installed MetaMask instance
MIT License
68 stars 28 forks source link

Fix sample extension #13

Open danfinlay opened 4 years ago

danfinlay commented 4 years ago

The sample extension has not been updated since we made some changes to the underlying transport. Should be fairly simple and could facilitate more external extension interactions.

mohsinaliryk commented 3 years ago

The sample extension has not been updated since we made some changes to the underlying transport. Should be fairly simple and could facilitate more external extension interactions.

Can you explain how to get account information using extension provider. I'm receiving empty array

toki-sean commented 3 years ago

We're also facing the same issue as @mohsinaliryk . Thanks!

artem-bayandin commented 3 years ago

Still empty array. I'm trying to create a Chrome extension and use MM. Tried to get user accounts by eth.accounts (etherjs), web3.accounts (web3js), MetaMaskInpageProvider.request({method:'eth_accounts'}) - all return an empty array and a warning saying ObjectMultiplex - orphaned data for stream "publicConfig"

[update]

Well, I think I've found at least entry point to happily communicate with MM extension, so that MM extension got opened, I can authorize, and receive an array of 1 element, which is current accountId

file 'myService.js':

const createMetaMaskProvider = require('metamask-extension-provider')
const Web3 = require('web3')

const web3Provider = createMetaMaskProvider()
const web3 = new Web3(web3Provider)

export { web3, web3Provider }

file 'App.js':

import React, { useState } from 'react'
import { web3, web3Provider } from './services/web3provider'

export const App = () => {
    const [ account, setAccount ] = useState()

    const login = (e) => {
        e.preventDefault()
        web3Provider
            .enable()
            .then(loadAccounts)
    }

    const loadAccounts = () => {
        web3
            .eth
            .getAccounts()
            .then(accounts => {
                if (accounts && accounts.length) {
                    setAccount(accounts[0])
                } else {
                    setAccount(null)
                }
            })
    }

    return (
        <div>
            { account && <div>account: {account}</div> }
            { !account && <div onClick={login}>login</div> }
        </div>
    )
}

[update2]

As MM suggests, you might use await provider.request({method: 'eth_requestAccounts'}) instead of await provider.enable() - and it works, I have just tested.

cryptoKevinL commented 2 years ago

Would it be possible to update the example so this works after a git clone of the repo? I'm seeing the same error.

ogiste commented 1 year ago

Faced a similar problem recently trying to connect to metamask from an extension project we're building. Any help here would be greatly appreciated!

bug-author commented 5 months ago

Facing the same problem, it was working fine but when I started porting the code to my own repo now I dont get any accounts and this warning.

legobeat commented 4 months ago

metamask-extension-provider@4.0.0 has just been published and with a rebuilt sample.

Is this still an issue with the new version?