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

Add metamask extension web3 #566

Merged
merged 40 commits into from
Sep 3, 2021
Merged

Add metamask extension web3 #566

merged 40 commits into from
Sep 3, 2021

Conversation

joelamouche
Copy link
Contributor

@joelamouche joelamouche commented Dec 8, 2020

  • Create injectMetaMaskWeb3 function to inject MetaMask accounts and allow signing Tx
  • First sends a request to MM to ask the user to authorize the app
  • Metamask accounts are now displayed in the accounts section
  • User can use theses accounts to sign tx
  • Signing will trigger a MM window with a warning (because signing unprefixed messages is dangerous)

@jacogr jacogr mentioned this pull request Dec 9, 2020
@joelamouche
Copy link
Contributor Author

updated with current state of my modifs

transfer get stuck here:
Screenshot 2020-12-10 at 17 05 03

@joelamouche
Copy link
Contributor Author

joelamouche commented May 5, 2021

MetaMask integration is back 🥳 @jacogr

After a long inquiry, I discovered that eth_sign (unprefixed) is available.
Here is a working test: https://github.com/joelamouche/test-dapp/tree/testEthSign

@joelamouche
Copy link
Contributor Author

joelamouche commented May 5, 2021

@jacogr There is a problem though: when I run yarn polkadot-dev-copy-to apps in extensions, and then yarn start in apps, I get:

ERROR in ../../node_modules/@polkadot/extension-dapp/compat/Web3Source.js 7:0-24
<e> Module not found: Error: Can't resolve 'web3' in '/Users/antoineestienne/Github_Files/apps/node_modules/@polkadot/extension-dapp/compat'
<e> resolve 'web3' in '/Users/antoineestienne/Github_Files/apps/node_modules/@polkadot/extension-dapp/compat'
<e>   Parsed request is a module
<e>   using description file: /Users/antoineestienne/Github_Files/apps/node_modules/@polkadot/extension-dapp/package.json (relative path: ./compat)
<e>     Field 'browser' doesn't contain a valid alias configuration
<e>     resolve as module
<e>       /Users/antoineestienne/Github_Files/apps/node_modules/@polkadot/extension-dapp/compat/node_modules doesn't exist or is not a directory
<e>       /Users/antoineestienne/Github_Files/apps/node_modules/@polkadot/extension-dapp/node_modules doesn't exist or is not a directory
<e>       /Users/antoineestienne/Github_Files/apps/node_modules/@polkadot/node_modules doesn't exist or is not a directory
<e>       /Users/antoineestienne/Github_Files/apps/node_modules/node_modules doesn't exist or is not a directory
<e>       looking for modules in /Users/antoineestienne/Github_Files/apps/node_modules
<e>         single file module
<e>           using description file: /Users/antoineestienne/Github_Files/apps/package.json (relative path: ./node_modules/web3)
<e>             no extension
<e>               Field 'browser' doesn't contain a valid alias configuration
<e>               /Users/antoineestienne/Github_Files/apps/node_modules/web3 doesn't exist
<e>             .js
<e>               Field 'browser' doesn't contain a valid alias configuration
<e>               /Users/antoineestienne/Github_Files/apps/node_modules/web3.js doesn't exist
<e>             .jsx
<e>               Field 'browser' doesn't contain a valid alias configuration
<e>               /Users/antoineestienne/Github_Files/apps/node_modules/web3.jsx doesn't exist
<e>             .mjs
<e>               Field 'browser' doesn't contain a valid alias configuration
<e>               /Users/antoineestienne/Github_Files/apps/node_modules/web3.mjs doesn't exist
<e>             .ts
<e>               Field 'browser' doesn't contain a valid alias configuration
<e>               /Users/antoineestienne/Github_Files/apps/node_modules/web3.ts doesn't exist
<e>             .tsx
<e>               Field 'browser' doesn't contain a valid alias configuration
<e>               /Users/antoineestienne/Github_Files/apps/node_modules/web3.tsx doesn't exist
<e>         /Users/antoineestienne/Github_Files/apps/node_modules/web3 doesn't exist
<e>       /Users/antoineestienne/Github_Files/node_modules doesn't exist or is not a directory
<e>       /Users/antoineestienne/node_modules doesn't exist or is not a directory
<e>       /Users/node_modules doesn't exist or is not a directory
<e>       /node_modules doesn't exist or is not a directory
<e>  @ ../../node_modules/@polkadot/extension-dapp/compat/index.js 4:0-41 8:38-48
<e>  @ ../../node_modules/@polkadot/extension-dapp/index.js 11:0-43 71:49-59

web3 being a dependency added by my extension

@joelamouche
Copy link
Contributor Author

And when I install yarn add web3 I get:

ERROR in ../../node_modules/ethereumjs-util/dist/account.js 4:13-30
<e> Module not found: Error: Can't resolve 'assert' in '/Users/antoineestienne/Github_Files/apps/node_modules/ethereumjs-util/dist'
<e> 
<e> BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
<e> This is no longer the case. Verify if you need this module and configure a polyfill for it.
<e> 
<e> If you want to include a polyfill, you need to:
<e> 	- add a fallback 'resolve.fallback: { "assert": require.resolve("assert/") }'
<e> 	- install 'assert'
<e> If you don't want to include a polyfill, you can use an empty module like this:
<e> 	resolve.fallback: { "assert": false }
<e>  @ ../../node_modules/ethereumjs-util/dist/index.js 29:13-33
<e>  @ ../../node_modules/ethereumjs-tx/dist/transaction.js 14:24-50
<e>  @ ../../node_modules/ethereumjs-tx/dist/index.js 3:20-44
<e>  @ ../../node_modules/web3-eth-accounts/lib/index.js 35:18-54
<e>  @ ../../node_modules/web3-eth/lib/index.js 34:15-43
<e>  @ ../../node_modules/web3/lib/index.js 30:10-29
<e>  @ ../../node_modules/@polkadot/extension-dapp/compat/Web3Source.js 7:0-24 33:21-25
<e>  @ ../../node_modules/@polkadot/extension-dapp/compat/index.js 4:0-41 8:38-48
<e>  @ ../../node_modules/@polkadot/extension-dapp/index.js 11:0-43 71:49-59
<e>  @ ../react-api/src/Api.tsx 14:0-68 52:27-39 188:30-40
<e>  @ ../react-api/src/index.ts 3:0-65 6:0-124 6:0-124 6:0-124 6:0-124
<e>  @ ./src/Root.tsx 6:0-42 35:36-39
<e>  @ ./src/index.tsx 9:0-26 18:35-39

And similar errors for http, https, os

using yarn add to add them doesnt work

@joelamouche
Copy link
Contributor Author

Adapting the apps helped: polkadot-js/apps#5216

The console shows the right logs and MetaMask does ask me for user permissions. but then the address doesn't display in the accounts. Can you refresh my memory: what do I need to do to display it and test it?

@joelamouche
Copy link
Contributor Author

joelamouche commented May 5, 2021

I think the problem is that the injcted accounts are injected without a type and default to substrate type addresses.

Furthermore, it looks like getInjectedAccounts doesnt pass down the signer in the app

@joelamouche
Copy link
Contributor Author

joelamouche commented May 6, 2021

polkadot-js/common#969 (ready)
#566 (wip)
polkadot-js/ui#464 (ready)
polkadot-js/apps#5216 (wip)

Are all part of a first milestone: displaying injected accounts from metamask into the app

@joelamouche joelamouche marked this pull request as ready for review May 10, 2021 17:21
@joelamouche
Copy link
Contributor Author

@ubaistp ?

@joelamouche
Copy link
Contributor Author

@jacogr ready for review (tested)

@joelamouche
Copy link
Contributor Author

@jacogr updated and ready for review

Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we are close. Some small comments and updates.

(Bit difficult wading through the reformats as well to ensure that the logic is stil in-tact)

packages/extension-base/src/background/handlers/Tabs.ts Outdated Show resolved Hide resolved
packages/extension-dapp/src/compat/metaMaskSource.ts Outdated Show resolved Hide resolved
packages/extension-dapp/src/compat/metaMaskSource.ts Outdated Show resolved Hide resolved
@@ -73,15 +73,13 @@ function injectSingleSource (win: SingleWindow): void {
// https://github.com/cennznet/singlesource-extension/blob/f7cb35b54e820bf46339f6b88ffede1b8e140de0/react-example/src/App.js#L19
export default function initSingleSource (): Promise<boolean> {
return new Promise((resolve): void => {
window.addEventListener('load', (): void => {
const win = window as Window & SingleWindow;
const win = window as Window & SingleWindow;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Believe I commented elsewhere - the original is correct. (All ok, since this will be dropped anyway)

console.error(`Error initializing ${name}: ${error.message}`);
})
])
Object.entries(win.injectedWeb3).map(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still applicable, but will do a manual revert of this and the one above once in.

injected.map(
async ({ accounts, name: source, signer }): Promise<InjectedAccountWithMeta[]> => {
try {
const list = await accounts.get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So on web3Accounts() would just add an extra flag for the types, e.g. ['sr25519', 'ed25519', 'ethereum'] - there may be some dance required by users to correctly pass the flag, but it is all ok.

So I think the easiest is to indeed initialise all source, but then just filter the accounts if need be. By default would not use (if nothing passed) the filter as `['sr25519', 'ed25519'] - obviously if no type is available, just pass it through.

The tricky bit here is that if we don't have sr25519 we never pass through the accounts with no type.

Should work in the case of the apps UI as well, e.g. in the case of Moonbeam, we just pass through the ['ethereum'] type. (As mentioned above that interface may also require some additional juggling, but all ok)

@jacogr jacogr mentioned this pull request Aug 16, 2021
@jacogr
Copy link
Member

jacogr commented Aug 16, 2021

Added a deps PR (web3 & detect-provider) in #794 just to eliminate conflicts.

@joelamouche
Copy link
Contributor Author

@jacogr updated and ready for review

@joelamouche
Copy link
Contributor Author

@jacogr Updated!
So is ed25519 & sr25519 the default for regular substrate parachains? Asking for the polakdot/apps PR

@jacogr
Copy link
Member

jacogr commented Aug 26, 2021

Yes, sr/ed is default. However the Substrate secp accounts can also be used and it valid. In broad terms there is just a split between the Ethereum-style and Substrate-style.

@joelamouche
Copy link
Contributor Author

@jacogr updated and ready for review

@joelamouche
Copy link
Contributor Author

@jacogr please review when you have some time

@jacogr
Copy link
Member

jacogr commented Sep 2, 2021

On it, want to get it in ASAP. (And importantly, get a release out with this)

Could you just look at the yarn.lock conflicts?

@joelamouche
Copy link
Contributor Author

@jacogr done

Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@jacogr jacogr merged commit 5c53ee7 into polkadot-js:master Sep 3, 2021
@polkadot-js-bot
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Sep 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants