Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Cannot connect with MetaMask using Magic Connect Connector #81

Open
refex opened this issue Jun 27, 2023 · 1 comment
Open

Cannot connect with MetaMask using Magic Connect Connector #81

refex opened this issue Jun 27, 2023 · 1 comment

Comments

@refex
Copy link

refex commented Jun 27, 2023

Cannot connect with MetaMask using Magic Connect Connector
Issue example: https://codesandbox.io/s/fragrant-firefly-yscg8s?file=/src/App.js

Connecting with Metamask results in the error from viem: "InvalidAddressError: Address 'Undefined' is invalid."
I traced the cause to the getAccount method of magicConnector.js.
Basically there are no connected accounts according to Magic:
image

In the console there's an error that got me into digging deeper, the error is "webcrypto error TypeError: SubtleCrypto.sign: Argument 2 does not implement interface CryptoKey". So I think i got almost to the root of the problem, in this file from magic js v17.1.3 (the dep of the latest version of wagmi-magic-connector) there's a weak null check for the public key, infact in my localstorage that key is an empty object {}, thus the null check fails and an undefined key is fetched file source
image. This results in calling the suble.sign function with an undefined key, hence the error in the console.

How can I avoid this? I assume it's either an undiscovered bug or an issue with my wagmi / connector configuration.

Additional context
my libs:

 "@everipedia/wagmi-magic-connector": "^1.0.2",
 "wagmi": "^1.2.0"

My config:

const connectedNetwork = getWagmiNetwork();

const { chains, publicClient, webSocketPublicClient } = configureChains(
    [connectedNetwork],
    [alchemyProvider({ apiKey: '<my api key>' }), publicProvider()],
)

const rpc = chains[0].rpcUrls.default.http[0];
const chainId = chains[0].id;
const magicConnector = new MagicConnectConnector({
    chains,
    options: {
        apiKey: getApiKey(),
        magicSdkConfiguration: {
            network: {
                rpcUrl: rpc,
                chainId: chainId,
            },
        },
    },
});

export const wagmiConfig = createConfig({
    autoConnect: true,
    publicClient,
    webSocketPublicClient,
    connectors: [
        magicConnector as any,
    ],
})

The error can also be triggered by invoking this function, it will fail on getAddress, cause accounts array is always empty when I connect with MetaMask (connection actually fails).

export const magicGetAccount = async () => {
    const sdk = magicConnector.getMagicSDK();
    const provider = sdk?.rpcProvider;
    const accounts = await provider?.request({
        method: 'eth_accounts',
    });
    const account = getAddress(accounts[0]);
    return account
}

To Reproduce
Steps to reproduce the behavior:

  1. Connect using MetaMask with Magic Connect
  2. See error log

Expected behavior
When I connect with MetaMask an address is found and I can use the wallet.

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser Firefox
  • Version 114.0.2 64bit
@refex
Copy link
Author

refex commented Jun 28, 2023

Here's a codesanbox to reproduce the issue https://codesandbox.io/s/fragrant-firefly-yscg8s?file=/src/App.js
Just sign with metamask and the error will be triggered.

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

No branches or pull requests

1 participant