NearSocial / VM

Near Social VM
The Unlicense
36 stars 58 forks source link

wallet signIn without modal window causes to inability to sign transactions #186

Closed ArturLevchuk closed 4 months ago

ArturLevchuk commented 5 months ago

Describe the bug

It seems that this problem arose with recent updates to near social. And now, with a local login by writing data to the local data store and using signIn, authorization seems to work and Widgets work, but as soon as the moment comes when you need to sign the transaction, the error "Cannot find matching key for transaction sent to social.near" occurs. However, there is no such problem when authorizing through a modal window

Steps To Reproduceå

const network = props.network;
  const widgetSrc = props.widgetSrc;
  const widgetProps = JSON.parse(props.widgetProps);
  const PRIVATE_KEY = props.privateKey;
  const accountId = props.accountId;

  console.log("NEAR objects will be initialized");
  console.log(
    network,
    widgetSrc,
    JSON.stringify(widgetProps),
    accountId,
    PRIVATE_KEY
  );

  const anonymousWidget = PRIVATE_KEY === "" || accountId === "";

  const { initNear } = useInitNear();
  const near = useNear();
  const [isInitialized, setIsInitialized] = useState(false);
  const [nearInitialized, setNearInitialized] = useState(false);

  const WalletSelectorDefaultValues = {
    "near-wallet-selector:selectedWalletId": "near-wallet",
    "near-wallet-selector:recentlySignedInWallets": ["near-wallet"],
    "near-wallet-selector:contract": {
      contractId: network === "testnet" ? "v1.social08.testnet" : "social.near",
      methodNames: [],
    },
  };

  useEffect(() => {
    const myKeyStore = new nearAPI.keyStores.BrowserLocalStorageKeyStore();
    async function setData() {
      ls.clear();
      const keyPair = nearAPI.KeyPair.fromString(PRIVATE_KEY);
      await myKeyStore.setKey(network, accountId, keyPair);
      Object.entries(WalletSelectorDefaultValues).forEach(([key, value]) => {
        ls.set(key, value);
      });
      ls.set(WalletSelectorAuthKey, {
        accountId: accountId,
        allKeys: [keyPair.publicKey.toString()],
      });
    }
    if (!anonymousWidget) {
      setData();
    }

    const config = {
      networkId: network,
      selector: setupWalletSelector({
        network: network,
        modules: [setupMyNearWallet()],
      }),
      customElements: {
        Link: (props) => {
          if (!props.to && props.href) {
            props.to = props.href;
            delete props.href;
          }
          if (props.to) {
            props.to =
              typeof props.to === "string" &&
              isValidAttribute("a", "href", props.to)
                ? props.to
                : "about:blank";
          }
          return <Link {...props} />;
        },
      },
      config: {
        defaultFinality: undefined,
      },
    };

    initNear && initNear(config);

    setNearInitialized(true);
  }, [initNear]);

  useEffect(() => {
    async function loginInAccount() {
      const wallet = await (await near.selector).wallet("my-near-wallet");
      wallet.signIn({ contractId: near.config.contractName });
      setIsInitialized(true);
    }
    if (nearInitialized && !anonymousWidget) {
      loginInAccount();
    }
    if (anonymousWidget) {
      setIsInitialized(true);
    }
  }, [nearInitialized, near]);

  if (!isInitialized) {
    return (
      <div class="centered-spinner">
        <div class="spinner-grow" role="status">
          <span class="visually-hidden">Loading...</span>
        </div>
      </div>
    );
  } else {
    const widgetSettings = {
      widgetSrc,
      widgetProps,
    };
    return (
      <Router>
        <ViewPage {...widgetSettings} />
      </Router>
    );
  }

Expected behavior

Transactions work.

Screenshots

image_2024-04-15_162842931 image_2024-04-15_162911901

Package versions

"@near-wallet-selector/core": "^8.9.3", "@near-wallet-selector/my-near-wallet": "^8.9.3", "near-api-js": "2.1.3", "near-social-vm": "git+https://github.com/NearSocial/VM.git#2.6.0",