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

Detect injected providers #449

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Detect injected providers #449

wants to merge 3 commits into from

Conversation

Lbqds
Copy link
Member

@Lbqds Lbqds commented Nov 18, 2024

When both the onekey wallet and the alephium wallet are enabled(I can't connect to the onekey extension wallet because I don't have the onekey hardware wallet.):

alephium-and-onekey.mov

When only the alephium wallet is enabled

Screen.Recording.2024-11-18.at.11.39.09.mov

When only the onekey wallet is enabled

onekey.mov


const detectProviders = () => {
if (typeof window !== 'undefined') {
const oneKeyProvider = window['alephium']
Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, the onekey wallet does not announce the alephium provider, and since onekey sets window.alephium, we will try to load the onekey provider from window.alephium.

addNewProvider(oneKeyProvider)
}

const defaultProvider = getWalletObject(alephiumProvider.id)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to ensure compatibility with older versions of the alephium extension wallet. If only the alephium wallet is enabled and no provider is announced, we will directly try to load the provider from window.alephiumProviders.alephium.

let allProviders: AlephiumWindowObject[] = []

const addNewProvider = (provider: AlephiumWindowObject) => {
if (allProviders.find((p) => p.icon === provider.icon) === undefined) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, the onekey wallet and the alephium wallet use the same id and name, so we can only differentiate between them using the icon.

await options.onConnected({ account: enabledAccount, signerProvider: provider })
setLastConnectedAccount('injected', enabledAccount, options.network)
// eslint-disable-next-line
;(provider as any)['onDisconnected'] = options.onDisconnected
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to work around the enableIfConnected serialization issue in onekey wallet: https://github.com/OneKeyHQ/cross-inpage-provider/blob/23a0e8e3ef2ace42e8a0359be49ddfd2a653d281/packages/providers/onekey-alph-provider/src/OnekeyAlphProvider.ts#L147, it should handle the callback in the same way as unsafeEnable.

Copy link
Member

@polarker polarker left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants