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

[Bug]: Request object is lacking 'origin' / Unable to resolve module node:crypto #1143

Open
asenrusev opened this issue Nov 22, 2024 · 0 comments

Comments

@asenrusev
Copy link

SDK

Node.js

Provide environment information

We're using react-native "0.66.4". Also, if relevant:
"react-native-crypto": "^2.2.0",
"react-native-get-random-values": "^1.11.0",
"react-native-url-polyfill": "^2.0.0",

MetaMask SDK Version

0.30.3

MetaMask Mobile app Version

N/A

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

We use "@metamask/sdk": "0.3.2" and approximately a month ago we started receiving the following error when users try to login using metamask: "Request object is lacking 'origin'". I tried patching it somehow but decided to update to latest hoping there is a fix.

We've updated "@metamask/sdk" from "0.3.2" to "0.30.3" (latest). During the build we're receiving the following error:

Error: Unable to resolve module node:crypto from "app/node_modules/@metamask/sdk/dist/react-native/es/metamask-sdk.js": node:crypto could not be found within the project or in these directories:
  node_modules/@metamask/sdk/node_modules
  node_modules
  ../../node_modules

App.tsx:

import "node-libs-react-native/globals";
import "react-native-url-polyfill/auto";
import "react-native-get-random-values";

metro.config.js:

const exclusionList = require("metro-config/src/defaults/exclusionList");
const { getMetroTools } = require("react-native-monorepo-tools");
const { getDefaultConfig } = require("metro-config");
const monorepoMetroTools = getMetroTools();
const config = (async () => {
  const {
    resolver: { sourceExts },
  } = await getDefaultConfig();
  return {
    resolver: {
      blacklistRE: exclusionList([
        ...monorepoMetroTools.blockList,
        /\/unity\/.*/,
      ]),
      extraNodeModules: {
        ...require("node-libs-react-native"),
        ...monorepoMetroTools.extraNodeModules,
      },
      sourceExts: sourceExts,
    },
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: true,
        },
      }),
    },
  };
})();
module.exports = config;

I've been checking the guides but there doesn't seem to be anything for only @metamask/sdk that will indicate why we're getting this. Here's how our code looks:

function getSdk() {
    MMSDK = new MetaMaskSDK({
      openDeeplink: async (link) => {
          if (await Linking.canOpenURL(link)) {
            await Linking.openURL(link); 
          }
      },
      dappMetadata: {
        name: "OurApp",
        url: "https://oururl.com", 
      },
    });
  return MMSDK;
}

And this is how we use it in a hook:

const sdk = getSdk();
const authWithWallet = React.useCallback(async () => {
    const connect = async ()=> {
      const provider = sdk.getProvider();
      if (provider.isConnected()) {
        sdk.disconnect();
      }

      const accounts = await provider.request<string[]>({
        method: "eth_requestAccounts",
        params: [],
      });
      if (!accounts || accounts.length === 0) {
        throw new Error("No accounts found");
      }
      return accounts[0] || "";
    };
    const signMessage = async (
      message: string,
      address: string
    ): Promise<string> => {
      const provider = sdk.getProvider();
      if (!provider) {
        throw new Error("metamask provider not found");
      }
      const result = await provider.request<string>({
        method: "personal_sign",
        params: [message, address],
      });
      return result || "";
    };
    return await authenticateWithWalletProvider(
      { connect, signMessage, name: "MetaMask", class: "metamask" },
      setAuthState,
      registerUserForNotifications
    );
  }, [registerUserForNotifications, sdk]);

Essentially this code is used for login with signature. We first ask the user to connect and after that to sign a backend generated message. I can't find any guides on how to use the SDK like this.

Expected Behavior

Available guides in the docs or running successfully connect and sign without errors.

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

No response

To Reproduce

N/A

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

1 participant