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

Virtual Wallet not loaded properly. #1

Open
khanti42 opened this issue Oct 31, 2024 · 1 comment
Open

Virtual Wallet not loaded properly. #1

khanti42 opened this issue Oct 31, 2024 · 1 comment

Comments

@khanti42
Copy link

In get-starknet VirtualWallet behave differently that Normal Wallet. The enable method defined in https://github.com/starknet-io/get-starknet/blob/51d946452326ae02cf85211ccdd7995f5d7bbce2/packages/core/src/main.ts#L119 is simply a pass through for Normal Wallet (identity). So it can be skipped for Normal Wallet. But when a VirtualWallet is detected then the method resolveVirtualWallet is called to actually resolve the virtual wallet and load it properly.

      if (isVirtualWallet(inputWallet)) {
        wallet = await resolveVirtualWallet(windowObject, inputWallet)

A virtual wallet is :

export const virtualWalletKeys = ensureKeysArray<VirtualWallet>({
  id: true,
  name: true,
  icon: true,
  windowKey: true,
  loadWallet: true,
  hasSupport: true,
})

When calling connect behind the seen the enable method is called and in case the wallet is virtual then the loadWallet method is called.

As neither resolveVirtualWallet nor isVirtualWallet is externally exposed by get-starknet the only way to get this behaviour without calling connect is by calling the loadWallet method of a VirtualWallet directly.

A quick a dirty implementation of this is to slightly change the scanObjectForWalletsCustom method and ensuring that we try to load virtual wallet in all cases.

A simple piece of code to achieve this could be

      // If not valid still check maybe its a virtual wallet ? 
      if(!isValid){
        try{
          resolveVirtualWallet
          wallet = await (wallet as any).loadWallet(window)
        }
        catch(e){
          console.log(e)
        }
        isValid = await checkCompatibility(wallet);
      }

The full method becomes :

export async function scanObjectForWalletsCustom(
  obj: Record<string, any>, // Browser window object
  isWalletObject: (wallet: any) => boolean,
): Promise<ValidWallet[]> {
  const wallets = await sn.getAvailableWallets({});

  const validWallets: ValidWallet[] = await Promise.all(wallets.map(
    async (wallet: WALLET_API.StarknetWindowObject) => {
      let isValid = await checkCompatibility(wallet);
      // If not valid still check maybe its a virtual wallet ? 
      if(!isValid){
        try{
          resolveVirtualWallet
          wallet = await (wallet as any).loadWallet(window)
        }
        catch(e){
          console.log(e)
        }
        isValid = await checkCompatibility(wallet);
      }
      return { wallet: wallet, isValid: isValid } as ValidWallet;
    }
  ))
  console.log(validWallets);
  return validWallets;
}

With this slight change we are able to get a proper detection of MetaMask, see the video below :

Screen.Recording.2024-10-31.at.14.38.51.mov
@PhilippeR26
Copy link
Owner

resolveVirtualWallet seems not to be exported by get-starknet v4

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

No branches or pull requests

2 participants