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

Feature: Add support for injected wallet download URL for unavailable injected wallets #1754

Merged
merged 6 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
39 changes: 27 additions & 12 deletions docs/src/routes/docs/[...4]wallets/[...9]injected/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ const equal = {
provider: window.ethereum
}),
// A list of platforms that this wallet supports
platforms: ['desktop']
platforms: ['desktop'],
/**
* A Url to link users to a download page for the wallet
* to be shown if not installed or available on the browser
*/
externalUrl: 'http://www.CoolEqualWalletDownload.com'
}

const injected = injectedModule({
Expand All @@ -172,17 +177,22 @@ You may want to display injected wallets that are not currently available to the

```javascript
const injected = injectedModule({
displayUnavailable: true
// display all unavailable injected wallets
displayUnavailable: true,
||
// display specific unavailable wallets
displayUnavailable: [ProviderLabel.MetaMask, ProviderLabel.Trust]
})
```

This will render every injected wallet as regardless of whether it has been detected in the window, happy days.
Then the issue of the order of wallets displayed becomes apparent when you have 21 injected wallets at the top of the wallets list. To solve this, all injected wallets are sorted alphabetically by default and there is an additional `sort` parameter which receives the final list of wallets and then returns the list to be rendered. This allows for example setting MetaMask and Coinbase first and then just the rest alphabetically:
This can be set to an array top specify which unavailable injected wallets to show or set to true to display all unavailable injected wallets regardless of whether it has been detected in the window, happy days.
Then the issue of the order of wallets displayed becomes apparent when you have 21 injected wallets at the top of the wallets list. To solve this, all injected wallets are sorted **alphabetically** by default and there is an additional `sort` parameter which receives the final list of wallets and then returns the list to be rendered. This allows for example setting MetaMask and Coinbase first and then just the rest alphabetically:

```javascript
const injected = injectedModule({
// display all wallets even if they are unavailable
displayUnavailable: true,
// display specific injected wallets even if they are unavailable
// can also be set to `true` to display all unavailable injected wallets
displayUnavailable: [ProviderLabel.MetaMask, ProviderLabel.Trust],
// do a manual sort of injected wallets so that MetaMask and Coinbase are ordered first
sort: (wallets) => {
const metaMask = wallets.find(({ label }) => label === ProviderLabel.MetaMask)
Expand All @@ -203,11 +213,12 @@ const injected = injectedModule({
})
```

You may want to display all wallets, but filter out specific wallets based on their availability. For example I may want to display all unavailable wallets except when Binance and Bitski wallet is unavailable, then don't show them, but if they are available, then do show them. To do this, the filters value has been extended to have a new value: `'unavailable'`, as in; remove this wallet if it is unavailable, even though `displayUnavailable` wallets is set:
You may want to display all unavailable injected wallets, but filter out specific wallets based on their availability. For example I may want to display all unavailable wallets except when Binance and Bitski wallet is unavailable, then don't show them, but if they are available, then do show them. To do this, the filters value has been extended to have a new value: `'unavailable'`, as in; remove this wallet if it is unavailable, even though `displayUnavailable` wallets is set:

```javascript
const injected = injectedModule({
// display all wallets even if they are unavailable
// display specific injected wallets even if they are unavailable
// can also be set to `true` to display all unavailable injected wallets
displayUnavailable: true,
// but only show Binance and Bitski wallet if they are available
filter: {
Expand All @@ -234,15 +245,16 @@ const injected = injectedModule({
})
```

If a wallet is selected, but is not available the default error message is: `Please install or enable ${walletName} to continue`. You may want to customise that message, so there is the `walletUnavailableMessage` parameter which is a function that takes the wallet object that is unavailable and returns a string which is the message to display:
If a wallet is selected, but is not available the default error message is: `Oops ${wallet.label} is unavailable! Please <a href="${wallet.externalUrl}" target="_blank">install</a>` if a download link is available for that wallet. If there isn't a download link for that wallet the default is: `Please install or enable ${walletName} to continue`. You may want to customize that message, so there is the `walletUnavailableMessage` parameter which is a function that takes the wallet object that is unavailable and returns a string which is the message to display:

```javascript
const injected = injectedModule({
custom: [
// include custom (not natively supported) injected wallet modules here
],
// display all wallets even if they are unavailable
displayUnavailable: true,
// display specific injected wallets even if they are unavailable
// can also be set to `true` to display all unavailable injected wallets
displayUnavailable: [ProviderLabel.MetaMask, ProviderLabel.Trust],
// but only show Binance and Bitski wallet if they are available
filter: {
[ProviderLabel.Binance]: 'unavailable',
Expand All @@ -265,7 +277,10 @@ const injected = injectedModule({
.filter((wallet) => wallet)
)
},
walletUnavailableMessage: (wallet) => `Oops ${wallet.label} is unavailable!`
walletUnavailableMessage: (wallet) =>
wallet.externalUrl
? `Oops ${wallet.label} is unavailable! Please <a href="${wallet.externalUrl}" target="_blank">install</a>`
: `Oops ${wallet.label} is unavailable!`
})
```

Expand Down
2 changes: 1 addition & 1 deletion packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@web3-onboard/gas": "^2.1.7",
"@web3-onboard/gnosis": "^2.1.9",
"@web3-onboard/infinity-wallet": "^2.0.3",
"@web3-onboard/injected-wallets": "^2.9.0",
"@web3-onboard/injected-wallets": "^2.10.0-alpha.1",
"@web3-onboard/keepkey": "^2.3.7",
"@web3-onboard/keystone": "^2.3.7",
"@web3-onboard/ledger": "^2.4.5",
Expand Down
65 changes: 35 additions & 30 deletions packages/demo/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@
const injected = injectedModule({
custom: [
// include custom (not natively supported) injected wallet modules here
]
// display all wallets even if they are unavailable
// displayUnavailable: true
],
// display all unavailable injected wallets
// displayUnavailable: true,
// ||
// display specific unavailable wallets
displayUnavailable: [ProviderLabel.MetaMask, ProviderLabel.Trust],
// but only show Binance and Bitski wallet if they are available
// filter: {
// [ProviderLabel.Binance]: 'unavailable',
Expand Down Expand Up @@ -101,7 +104,10 @@
// .filter(wallet => wallet)
// )
// }
// walletUnavailableMessage: wallet => `Oops ${wallet.label} is unavailable!`
// walletUnavailableMessage: wallet =>
// wallet.externalUrl
// ? `Oops ${wallet.label} is unavailable! Please <a href="${wallet.externalUrl}" target="_blank">install</a>`
// : `Oops ${wallet.label} is unavailable!`
})

const coinbaseWallet = coinbaseModule()
Expand Down Expand Up @@ -645,31 +651,31 @@
}}>Send Success Notification</button
>
<button
on:click={() =>
onboard.state.actions.customNotification({
message:
'This is a custom DApp success notification to use however you want',
autoDismiss: 0,
type: 'pending'
})}>Send Pending Notification</button
>
<button
on:click={() =>
onboard.state.actions.customNotification({
type: 'error',
message:
'This is a custom DApp Error notification to use however you want',
autoDismiss: 0
})}>Send Error Notification</button
>
<button
on:click={() =>
onboard.state.actions.customNotification({
message:
'This is a custom non-descript DApp notification to use however you want',
autoDismiss: 0
})}>Send DApp Notification</button
>
on:click={() =>
onboard.state.actions.customNotification({
message:
'This is a custom DApp success notification to use however you want',
autoDismiss: 0,
type: 'pending'
})}>Send Pending Notification</button
>
<button
on:click={() =>
onboard.state.actions.customNotification({
type: 'error',
message:
'This is a custom DApp Error notification to use however you want',
autoDismiss: 0
})}>Send Error Notification</button
>
<button
on:click={() =>
onboard.state.actions.customNotification({
message:
'This is a custom non-descript DApp notification to use however you want',
autoDismiss: 0
})}>Send DApp Notification</button
>
</div>
<div class="switch-chain-container">
<button on:click={() => onboard.setChain({ chainId: '0x1' })}
Expand All @@ -686,7 +692,6 @@
>
</div>
<div class="position-buttons">

<button
on:click={() =>
onboard.state.actions.updateAccountCenter({
Expand Down
40 changes: 28 additions & 12 deletions packages/injected/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ const equal = {
provider: window.ethereum
}),
// A list of platforms that this wallet supports
platforms: ['desktop']
platforms: ['desktop'],
/**
* A Url to link users to a download page for the wallet
* to be shown if not installed or available on the browser
*/
externalUrl: 'http://www.CoolEqualWalletDownload.com'
}

const injected = injectedModule({
Expand All @@ -197,17 +202,22 @@ You may want to display injected wallets that are not currently available to the

```javascript
const injected = injectedModule({
displayUnavailable: true
// display all unavailable injected wallets
displayUnavailable: true,
||
// display specific unavailable wallets
displayUnavailable: [ProviderLabel.MetaMask, ProviderLabel.Trust]
})
```

This will render every injected wallet as regardless of whether it has been detected in the window, happy days.
Then the issue of the order of wallets displayed becomes apparent when you have 21 injected wallets at the top of the wallets list. To solve this, all injected wallets are sorted alphabetically by default and there is an additional `sort` parameter which receives the final list of wallets and then returns the list to be rendered. This allows for example setting MetaMask and Coinbase first and then just the rest alphabetically:
This can be set to an array top specify which unavailable injected wallets to show or set to true to display all unavailable injected wallets regardless of whether it has been detected in the window, happy days.
Then the issue of the order of wallets displayed becomes apparent when you have 21 injected wallets at the top of the wallets list. To solve this, all injected wallets are sorted **alphabetically** by default and there is an additional `sort` parameter which receives the final list of wallets and then returns the list to be rendered. This allows for example setting MetaMask and Coinbase first and then just the rest alphabetically:

```javascript
const injected = injectedModule({
// display all wallets even if they are unavailable
displayUnavailable: true,
// display specific injected wallets even if they are unavailable
// can also be set to `true` to display all unavailable injected wallets
displayUnavailable: [ProviderLabel.MetaMask, ProviderLabel.Trust],
// do a manual sort of injected wallets so that MetaMask and Coinbase are ordered first
sort: wallets => {
const metaMask = wallets.find(
Expand All @@ -233,11 +243,12 @@ const injected = injectedModule({
})
```

You may want to display all wallets, but filter out specific wallets based on their availability. For example I may want to display all unavailable wallets except when Binance and Bitski wallet is unavailable, then don't show them, but if they are available, then do show them. To do this, the filters value has been extended to have a new value: `'unavailable'`, as in; remove this wallet if it is unavailable, even though `displayUnavailable` wallets is set:
You may want to display all unavailable injected wallets, but filter out specific wallets based on their availability. For example I may want to display all unavailable wallets except when Binance and Bitski wallet is unavailable, then don't show them, but if they are available, then do show them. To do this, the filters value has been extended to have a new value: `'unavailable'`, as in; remove this wallet if it is unavailable, even though `displayUnavailable` wallets is set:

```javascript
const injected = injectedModule({
// display all wallets even if they are unavailable
// display specific injected wallets even if they are unavailable
// can also be set to `true` to display all unavailable injected wallets
displayUnavailable: true,
// but only show Binance and Bitski wallet if they are available
filter: {
Expand Down Expand Up @@ -269,15 +280,17 @@ const injected = injectedModule({
})
```

If a wallet is selected, but is not available the default error message is: `Please install or enable ${walletName} to continue`. You may want to customise that message, so there is the `walletUnavailableMessage` parameter which is a function that takes the wallet object that is unavailable and returns a string which is the message to display:
If a wallet is selected, but is not available the default error message is: `Oops ${wallet.label} is unavailable! Please <a href="${wallet.externalUrl}" target="_blank">install</a>` if a download link is available for that wallet. If there isn't a download link for that wallet the default is: `Please install or enable ${walletName} to continue`. You may want to customize that message, so there is the `walletUnavailableMessage` parameter which is a function that takes the wallet object that is unavailable and returns a string which is the message to display:

```javascript
const injected = injectedModule({
custom: [
// include custom (not natively supported) injected wallet modules here
],
// display all wallets even if they are unavailable
displayUnavailable: true,
// display specific injected wallets even if they are unavailable
// can also be set to `true` to display all unavailable injected wallets
displayUnavailable: [ProviderLabel.MetaMask, ProviderLabel.Trust],

// but only show Binance and Bitski wallet if they are available
filter: {
[ProviderLabel.Binance]: 'unavailable',
Expand Down Expand Up @@ -305,6 +318,9 @@ const injected = injectedModule({
.filter(wallet => wallet)
)
},
walletUnavailableMessage: wallet => `Oops ${wallet.label} is unavailable!`
walletUnavailableMessage: wallet =>
wallet.externalUrl
? `Oops ${wallet.label} is unavailable! Please <a href="${wallet.externalUrl}" target="_blank">install</a>`
: `Oops ${wallet.label} is unavailable!`
})
```
2 changes: 1 addition & 1 deletion packages/injected/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/injected-wallets",
"version": "2.9.0",
"version": "2.10.0-alpha.1",
"description": "Injected wallet module for connecting browser extension and mobile wallets to Web3-Onboard. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
"keywords": [
"Ethereum",
Expand Down
9 changes: 7 additions & 2 deletions packages/injected/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ export class ProviderRpcError extends Error {
}
}

export const defaultWalletUnavailableMsg = ({ label }: InjectedWalletModule) =>
`Please install or enable ${label} to continue`
export const defaultWalletUnavailableMsg = ({
label,
externalUrl
}: InjectedWalletModule) =>
externalUrl
? `Please <a href="${externalUrl}" target="_blank">install</a> or enable ${label} to continue`
: `Please install or enable ${label} to continue`

export const isWalletAvailable = (
provider: InjectedProvider,
Expand Down
Loading