MetaMask / providers

An Ethereum Provider that connects over a stream, as injected into websites by MetaMask
MIT License
217 stars 123 forks source link

fix accountsChanged callback loop #248

Closed Louis-kupo closed 1 year ago

Louis-kupo commented 1 year ago

Reason

Change the accountsChanged callback object to a new object , or will cause loop.This is because the callback function of accountsChanged returns the reference of the accounts, so request eth_accounts after externally modifying the reference of the account will cause accountsChanged to be triggered again, then it keeps looping

Reproducibility

If you change the result returned by accountsChanged and request eth_accounts again, you will enter a loop .

  window.ethereum.on("accountsChanged", async (newAccounts) => {
    newAccounts[0] = "1234...";
    const nextAccounts = await window.ethereum.request({
      method: "eth_accounts"
    });
    console.log("nextAccounts", nextAccounts);
  });

Solution

In the callback of accountsChanged, return the deep copy object

Gudahtt commented 1 year ago

Thanks, great find! This seems like the right solution to me.

Could you fix the lint error? yarn lint:fix should fix the lint error being highlighted by CI.

Louis-kupo commented 1 year ago

Thanks, great find! This seems like the right solution to me.

Could you fix the lint error? yarn lint:fix should fix the lint error being highlighted by CI.

ok