MetaMask / snaps

Extend the functionality of MetaMask using Snaps
https://metamask.io/snaps/
Other
724 stars 557 forks source link

Automatic connections FAILED on local setup #2529

Open noahsilencelab opened 3 months ago

noahsilencelab commented 3 months ago

Hi team,

Today, I tried your new feature https://docs.metamask.io/snaps/how-to/allow-automatic-connections/ but immediately got some weird error that I couldn't debug myself. Please shed some light for my debugging. The error is thrown after I send RPC wallet_requestSnaps like below:

    return await provider.request({
      method: 'wallet_requestSnaps',
      params: {
        [SNAP_ID]: snapVersion
          ? {
              version: snapVersion,
            }
          : {},
      } as any,
    });

The error:

Screenshot 2024-07-02 at 20 58 36

Environment:

Metamask dependencies package.json:

    "@metamask/eth-sig-util": "^7.0.1",
    "@metamask/keyring-api": "^1.1.0",
    "@metamask/snaps-sdk": "^5.0.0",
    "@metamask/snaps-types": "^3.1.0",
    "@metamask/snaps-ui": "^3.1.0",
    "@metamask/utils": "^8.5.0",

Snap Manifest:

{
  "version": "1.2.9-alpha.2",
  "description": "Silent Shard creates a distributed self-custody account in MetaMask by generating a Distributed Private key between the browser extension and the Silent Shard mobile app using Multi-Party Computation (MPC). Thereby enabling a seamless 2FA experience for secure transactions.",
  "proposedName": "Silent Shard",
  "repository": {
    "type": "git",
    "url": "https://www.npmjs.com/package/@silencelaboratories/silent-shard-snap-staging"
  },
  "source": {
    "shasum": "kgUGyPyxOt+5t2kbxCOHu4vPY0l+oj0EvWda5vOY8B4=",
    "location": {
      "npm": {
        "filePath": "dist/bundle.js",
        "iconPath": "images/silentShardLogo.svg",
        "packageName": "@silencelaboratories/silent-shard-snap-staging",
        "registry": "https://registry.npmjs.org/"
      }
    }
  },
  "initialConnections": {
    "https://snap-staging.silencelaboratories.com": {},
    "http://localhost:3000": {}
  },
  "initialPermissions": {
    "endowment:keyring": {
      "allowedOrigins": [
        "https://snap-staging.silencelaboratories.com",
        "http://localhost:3000"
      ]
    },
    "snap_dialog": {},
    "snap_notify": {},
    "snap_manageState": {},
    "endowment:network-access": {},
    "endowment:rpc": {
      "allowedOrigins": [
        "https://snap-staging.silencelaboratories.com",
        "http://localhost:3000"
      ]
    },
    "snap_getEntropy": {},
    "snap_manageAccounts": {}
  },
  "manifestVersion": "0.1"
}
Montoya commented 3 months ago

Hi, please update your dapp connect code to use a string as the key for the Snap ID instead of an array, like so:

await window.ethereum.request({
  "method": "wallet_requestSnaps",
  "params": [
    {
      `${SNAP_ID}`: {
        "version": `^${snapVersion}`
      }
    }
  ]
});

Please let me know if you still see the error after making this change.

noahsilencelab commented 3 months ago

Hi, its not an array but a Computed property names syntax. It's actually a string key.

Montoya commented 3 months ago

What is the value of SNAP_ID? Usually this error is the result of an invalid ID.

noahsilencelab commented 3 months ago

The value of SNAP_ID local:http://localhost:8080

Montoya commented 3 months ago

And just to be certain, snapVersion is resolving to a string?

noahsilencelab commented 3 months ago

snapVersion is an empty string in this case

Montoya commented 3 months ago

Hmmm... what happens if you remove it entirely? Just:

return await provider.request({
  method: 'wallet_requestSnaps',
  params: {
    [SNAP_ID]: {},
  } as any,
});
Montoya commented 3 months ago

Also, sorry, I'm noticing one other things that may or may not be an issue, we have params for wallet_requestSnaps as an array of objects in our docs, may be just optional but you can try:

return await provider.request({
  method: 'wallet_requestSnaps',
  params: [ {
    [SNAP_ID]: {},
  } ] as Array<any>,
});
noahsilencelab commented 3 months ago

If I have params as an array, I will face the error '"params" must be an object.'

Screenshot 2024-07-03 at 12 27 08
noahsilencelab commented 3 months ago

Hmmm... what happens if you remove it entirely? Just:

The same error happened. RPC Error: Expected caveat to have a value property of a non-empty object of snap IDs.: At path: value.type -- Expected the value to satisfy a union ofintersection | string, but received: "type".