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 support for WalletConnect v2 #13

Merged
merged 26 commits into from
Jun 22, 2023

Conversation

hlopes-ledger
Copy link
Contributor

@hlopes-ledger hlopes-ledger commented Mar 1, 2023

Changes

  • add workspaces to root package.json so we can test demo app with local loader package without manual linking
  • moved checkSupport parameter types and their management to a supportOptions module, added parameters for WalletConnect v2, sets defaults for chain parameters, getSupportOptions is called by other modules
  • renamed ExtensionUnavailableModal to ExtensionInstallModal
  • renamed ConnectWithLedgerLiveModal to UseLedgerLiveModal
  • renamed the Ethereum provider to ExtensionEvm since it is used for Polygon and future EVM chains, moved chain compatibility checks there, renamed related constants and types
  • renamed Solana to ExtensionSolana since this is through the extension, and we might support Solana through WalletConnect as well in the future
  • renamed the original WalletConnect module to WalletConnectLegacy which now imports the dist file from @walletconnect/legacy-provider, updated the module declaration in src/@types/index.d.ts
  • created a new WalletConnectEvm module for the v2 provider which imports the dist file from @walletconnect/ethereum-provider v2, added a new module declaration in src/@types/index.d.ts
  • removed the TryConnectEthereum module - when the extension is supported but not installed we now default to the WalletConnect provider instead of the extension one, this will make it possible to use WC in that case if the user needs it
  • the modal module now has a showExtensionOrLLModal function with the logic to show the ExtensionInstallModal if both the user's platform and the required chains are supported, or the UseLedgerLiveModal one
  • the eth_requestAccounts request on both WalletConnect provider modules now call showExtensionOrLLModal
  • getProvider now returns the WalletConnectLegacy provider if the version parameter is 1 or the WalletConnectEvm one otherwise
  • store result of checkSupport in a module variable and make it available through getSupportResult, is used by showExtensionOrLLModal to decide which modal to show
  • made some changes to the rollup config to be able to bundle @walletconnect/ethereum-provider v2

WC v1 testing information

  • demo app, https://63f771085fca280e96307935--monumental-dolphin-d87e18.netlify.app
  • it initializes Connect Kit with chainId = 1
  • you should see [WalletConnectLegacy] getWalletConnectLegacyProvider on the console
  • on Safari iOS/macOS it should show the "Try Ledger Connect" modal
  • if Extension is enabled it should pop up
  • on other browsers it should show the "Do you have Ledger Live?" modal
  • switching chains on the wallet should be reflected on the app

WC v2 testing information

  • demo app, https://63ff7d8f84b8a9320aadac90--monumental-dolphin-d87e18.netlify.app/
  • it initializes Connect Kit with version = 2, projectId, chains = [1, 137], optionalChains = [5]
  • you should see [WalletConnectEvm] getWalletConnectProvider on the console
  • same as above for browser behaviour
  • switching chains must be done programatically on the app, the buttons should update app state

@netlify
Copy link

netlify bot commented Mar 1, 2023

Deploy Preview for legder-connect-kit failed.

Name Link
🔨 Latest commit fbb2a81
🔍 Latest deploy log https://app.netlify.com/sites/legder-connect-kit/deploys/64944f5758aa1500074a0fc6

@hlopes-ledger hlopes-ledger reopened this Mar 1, 2023
@hlopes-ledger hlopes-ledger force-pushed the feat/lc-513-wallet-connect-v2-launch branch 3 times, most recently from 89e3d61 to 92527ae Compare March 1, 2023 16:55
@nenadV91
Copy link

nenadV91 commented Mar 6, 2023

Hello, just want to give my feedback.

When I click on "Open ledger live Desktop" button,
Screenshot 2023-03-06 at 13 27 46

in the ledger-live I see this
Screenshot 2023-03-06 at 13 25 59

I'm using
Macbook, os Ventura 13.1
Browser: Chrome

@hlopes-ledger
Copy link
Contributor Author

hlopes-ledger commented Mar 6, 2023

Hi @nenadV91,

I forgot to mention that to test it with Ledger Live you need the new version of the WalletConnect Live App (not released yet). To confirm that v2 works without depending on LL you can scan the QR code (or copy and paste it from the browser console) with a third party mobile wallet that supports v2, like Trust or Zerion. I will make the encoded URI the ledgerlive:// deep link when the Live App is released.

Instructions for installing the new Live App are on the Ledger Slack (accessible to Ledger employees only): https://ledger.slack.com/archives/C04JHL7AQBF/p1677492460127719. It might be enough to check that the connection works but it is outdated and has some bugs that have already been fixed. I've been running the latest version directly from the repo.

@hlopes-ledger hlopes-ledger requested a review from a team March 14, 2023 09:22
Copy link

@Justkant Justkant left a comment

Choose a reason for hiding this comment

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

Some small stuff, if you want more infos or discuss some of my comments we can do that on a huddle too

packages/connect-kit/package.json Show resolved Hide resolved
declare module '@walletconnect/legacy-provider/dist/umd/index.min.js' {
import WalletConnectProvider from '@walletconnect/legacy-provider/dist/umd/index.min.js';
export default WalletConnectProvider
};

Choose a reason for hiding this comment

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

Why do you need this file and types declarations ?

Copy link
Contributor Author

@hlopes-ledger hlopes-ledger Mar 15, 2023

Choose a reason for hiding this comment

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

The WalletConnect packages are not browser safe, there is an open issue since 2020 and I've commented on it: WalletConnect/walletconnect-monorepo#341

The solution is to consume the pre bundled package instead of trying to bundle it together with your own lib: WalletConnect/walletconnect-monorepo#341

If importing the @walletconnect/ethereum-provider directly you will have all sorts of problems with nodejs dependencies, even after trying to polyfill them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment on the file mentioning 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.

Making it a peer dependency instead of bundling it might avoid this altogether.

Comment on lines +15 to +22
// placeholder functions until the component is initialized
let setModalUri = (uri: string) => {};

// called by the WalletConnect display_uri event handler to set a new URI
export let setWalletConnectUri = (uri: string): void => {
log('setModalUri', uri);
setModalUri(uri);
}

Choose a reason for hiding this comment

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

You might want to use a provider instead for managing a shared/global state

Comment on lines +47 to +49
useEffect(() => {
previousUriRef.current = uri;
});

Choose a reason for hiding this comment

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

I think it's better to only update when needed

Suggested change
useEffect(() => {
previousUriRef.current = uri;
});
useEffect(() => {
previousUriRef.current = uri;
}, [uri]);

Comment on lines +43 to +45
if (uri !== previousUri && uri !== wcUri) {
setWcUri(uri);
}

Choose a reason for hiding this comment

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

I think we should move this inside the useEffect

Comment on lines 97 to 127
return new Promise(async (resolve, reject) => {
try {
if (!provider?.session?.connected) {
showExtensionOrLLModal('', reject),

// connect initializes the session and waits for connection
await provider.connect();
} else {
log('reusing existing session');
}

// call the original provider request
return resolve(await baseRequest({ method, params }));
} catch(err) {
logError('error', err);
return reject(err);
}
});

Choose a reason for hiding this comment

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

You might want to change the way this Promise is done as it looks like an anti-pattern with the async/await: https://stackoverflow.com/a/43050114

Also this talk (https://www.youtube.com/watch?v=XV-u_Ow47s0) is interesting

Copy link
Contributor Author

@hlopes-ledger hlopes-ledger Mar 15, 2023

Choose a reason for hiding this comment

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

Fixed, I've replaced the async/await with .then

I understand the "anti-pattern" issue is due to the risk of not correctly handling the errors when using async within a promise constructor. The whole purpose of patching the provider in this way is to get the "user rejected request" error to the dApp UI when the modal is closed. So in this case reject is being passed as the onClose callback to the modal component and also in the catch block to hadle the error correctly.

Nice talk!

if (method === 'eth_requestAccounts') {
log('calling patched', method, params);

return new Promise(async (resolve, reject) => {

Choose a reason for hiding this comment

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

Same comment as for WalletConnectEvm

@hlopes-ledger hlopes-ledger force-pushed the feat/lc-513-wallet-connect-v2-launch branch 2 times, most recently from 389b08f to ccff5af Compare March 15, 2023 21:25
@hlopes-ledger hlopes-ledger force-pushed the main branch 2 times, most recently from 2cfabdb to a431778 Compare April 28, 2023 11:46
- add workspaces to root `package.json` so we can test demo app with
  local loader package without manual linking
- moved `checkSupport` parameter types and their management to a
  `supportOptions` module, added parameters for WalletConnect v2, sets
  defaults for chain parameters, `getSupportOptions` is called by other
  modules
- renamed `ExtensionUnavailableModal` to `ExtensionInstallModal`
- renamed `ConnectWithLedgerLiveModal` to `UseLedgerLiveModal`
- renamed the `Ethereum` provider to `ExtensionEvm` since it is used for
  Polygon and future EVM chains, moved chain compatibility checks there,
  renamed related constants and types
- renamed `Solana` to `ExtensionSolana` since this is through the
  extension, and we might support Solana through WalletConnect as well in
  the future
- renamed the original `WalletConnect` module to `WalletConnectLegacy`
  which now imports the dist file from `@walletconnect/legacy-provider`,
  updated the module declaration in `src/@types/index.d.ts`
- created a new `WalletConnectEvm` module for the v2 provider which
  imports the dist file from `@walletconnect/ethereum-provider` v2, added
  a new module declaration in `src/@types/index.d.ts`
- removed the `TryConnectEthereum` module
    - when the extension is supported but not installed we now default
      to the `WalletConnect` provider instead of the extension one, this
      will make it possible to use WC in that case if the user needs it
    - the modal module now has a `showExtensionOrLLModal` function with
      the logic to show the `ExtensionInstallModal` if both the user's
      platform and the required chains are supported, or the
      `UseLedgerLiveModal` one
    - the `eth_requestAccounts` request on both WalletConnect provider
      modules now call `showExtensionOrLLModal`
- `getProvider` now returns the `WalletConnectLegacy` provider if the
  version parameter is 1 or the `WalletConnectEvm` one otherwise
- store result of `checkSupport` in a module variable and make it
  available through `getSupportResult`, is used by
  `showExtensionOrLLModal` to decide which modal to show
- made some changes to the rollup config to be able to bundle
  @walletconnect/ethereum-provider v2
@hlopes-ledger hlopes-ledger force-pushed the feat/lc-513-wallet-connect-v2-launch branch from 60ebfac to 431dfbe Compare June 20, 2023 16:45
@hlopes-ledger hlopes-ledger force-pushed the feat/lc-513-wallet-connect-v2-launch branch 3 times, most recently from 2595543 to f57c449 Compare June 21, 2023 15:10
- update package versions
- rename version parameter to walletConnectVersion
- update connect-kit-loader's tsconfig outDir
@hlopes-ledger hlopes-ledger force-pushed the feat/lc-513-wallet-connect-v2-launch branch from f57c449 to ee9cbcc Compare June 21, 2023 15:15
@hlopes-ledger hlopes-ledger merged commit 909c1ad into main Jun 22, 2023
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.

3 participants