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

Compat w/ Brave to support multi web3 providers #7728

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/scripts/contentscript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/*global chrome*/
Copy link
Member

Choose a reason for hiding this comment

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

I was going to suggest that you use extensionizer instead, but that wouldn't work - that doesn't expose the braveWallet API.

Maybe we should update extensionizer to add that API? 🤔 In the meantime, this seems OK

Copy link
Contributor

Choose a reason for hiding this comment

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

If braveWallet is a similar API to the chrome/extension APIs that are common in browsers, then it would be a good fit. By the name, it sounds more like a provider..

Copy link
Member

Choose a reason for hiding this comment

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

braveWallet is an additional API. It would need to be added to the list of APIs in extensionizer. At the moment, that package only exports the APIs in that list.

It's not a cross-browser API of course, so I wasn't sure whether it was a good fit for that package.


import pump from 'pump'
import querystring from 'querystring'
import LocalMessageDuplexStream from 'post-message-stream'
Expand All @@ -21,8 +23,19 @@ const inpageBundle = inpageContent + inpageSuffix
// MetaMask will be much faster loading and performant on Firefox.

if (shouldInjectProvider()) {
injectScript(inpageBundle)
start()
// If this is Brave, it requires coordination to know if we should be the
// web3 provider.
if (chrome.braveWallet && chrome.braveWallet.getWeb3Provider) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (chrome.braveWallet && chrome.braveWallet.getWeb3Provider) {
if (chrome && chrome.braveWallet && chrome.braveWallet.getWeb3Provider) {

Correct me if I'm wrong, but I suspect that the chrome global wouldn't exist on Firefox? I think we need to check for that here too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, since chrome doesn't exist in firefox, this first clause would throw an undefined error. Either needs to be typeof chrome !== 'undefined' or you could import extensionizer and check extension.braveWallet to avoid this problem.

chrome.braveWallet.getWeb3Provider((provider) => {
if (provider === extension.runtime.id) {
injectScript(inpageBundle)
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @Gudahtt that injecting async is asking for trouble, so I really recommend addressing that for Brave's sake. Since this doesn't introduce any async operation to our WebExtension-based injection, I'm okay with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep I'll get it working sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add the typeof check on chrome too.

start()
}
})
} else {
injectScript(inpageBundle)
start()
}
}

/**
Expand Down