我尝试通过 SDK 将我的应用程序与元掩码钱包连接,但失败并出现以下错误:
[ TypeError: Cannot read property 'createHash' of undefined, js engine: hermes\]
.
真的很感激任何人都可以提供帮助。
我正在关注这个视频
这是我的代码
metro.config.js:
const { getDefaultConfig } = require("@expo/metro-config");
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts.push("cjs");
defaultConfig.resolver.extraNodeModules = {
...require("node-libs-expo"),
};
defaultConfig.transformer.getTransformOptions = async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
});
module.exports = defaultConfig;
// module.exports = {
// ...defaultConfig,
// resolver: {
// ...defaultConfig.resolver,
// extraNodeModules: {
// ...defaultConfig.resolver,
// ...require("node-libs-react-native"),
// },
// },
// };
metamask.ts:
import MetaMaskSDK from "@metamask/sdk";
import { Linking } from "react-native";
import BackgroundTimer from "react-native-background-timer";
export const MMSDK = new MetaMaskSDK({
openDeeplink: (link) => {
Linking.openURL(link);
},
timer: BackgroundTimer,
dappMetadata: {
name: "BALLOTEXPRESS",
url: "https://mydapp.com",
},
});
menu.js:
import "node-libs-expo/globals";
import "node-libs-react-native/globals";
import "react-native-url-polyfill/auto";
import { MMSDK } from "../metamask.ts";
import "react-native-get-random-values";
export default function ElectionMenu() {
const handleCastBallot = async () => {
await MMSDK.connect();
const ethereum = MMSDK.getProvider();
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
console.log(accounts);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.topContainer}>
<Text style={styles.title}>Election Menu</Text>
<TouchableOpacity onPress={handleBack} style={styles.backIcon}>
<Icon.Ionicons name="arrow-back" size={35} color="black" />
</TouchableOpacity>
</View>
<CustomButton
text="Cast Your Ballot"
imageSource={goVote}
onPress={handleCastBallot}
/>
<CustomButton
text="View Election Result"
imageSource={result}
onPress={handleViewResults}
/>
<CustomButton
text="View Vote Transaction"
imageSource={transaction}
onPress={handleViewTransactions}
/>
</SafeAreaView>
);
}
依赖关系(package.json)
{
"name": "fyp",
"version": "1.0.0",
"main": "expo-router/entry",
"scripts": {
"start": "expo start",
"android": "expo run:android",
"ios": "expo run:ios",
"web": "expo start --web"
},
"dependencies": {
"@expo/vector-icons": "^13.0.0",
"@metamask/sdk": "^0.16.0",
"@metamask/sdk-react": "^0.16.0",
"@react-native-async-storage/async-storage": "^1.18.1",
"@react-native-firebase/app": "^18.8.0",
"@react-native-firebase/auth": "^18.8.0",
"@react-native-firebase/firestore": "^18.8.0",
"@react-navigation/bottom-tabs": "^6.5.16",
"@react-navigation/native": "^6.1.9",
"@react-navigation/stack": "^6.3.20",
"@types/react": "~18.2.14",
"@types/react-native-background-timer": "^2.0.2",
"axios": "^1.6.3",
"babel-plugin-inline-import": "^3.0.0",
"crypto-js": "^4.2.0",
"eciesjs": "^0.3.18",
"ethers": "^5.7.2",
"events": "^3.3.0",
"expo": "^49.0.21",
"expo-constants": "~14.4.2",
"expo-crypto": "~12.4.1",
"expo-dev-client": "~2.4.13",
"expo-firebase-recaptcha": "^2.3.1",
"expo-font": "~11.4.0",
"expo-image-picker": "^14.7.1",
"expo-linear-gradient": "~12.3.0",
"expo-linking": "~5.0.2",
"expo-local-authentication": "~13.4.1",
"expo-router": "^2.0.14",
"expo-screen-orientation": "~6.0.6",
"expo-splash-screen": "~0.20.5",
"expo-status-bar": "~1.6.0",
"firebase": "^10.8.0",
"node-libs-expo": "^0.0.3",
"node-libs-react-native": "^1.2.1",
"process": "^0.11.10",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.72.10",
"react-native-background-timer": "^2.4.1",
"react-native-crypto": "^2.2.0",
"react-native-dotenv": "^3.4.9",
"react-native-gesture-handler": "~2.12.0",
"react-native-get-random-values": "~1.9.0",
"react-native-linear-gradient": "^2.8.3",
"react-native-orientation-locker": "^1.6.0",
"react-native-randombytes": "^3.6.1",
"react-native-reanimated": "^3.3.0",
"react-native-redash": "^18.1.3",
"react-native-safe-area-context": "4.6.3",
"react-native-screens": "~3.22.0",
"react-native-svg": "^13.9.0",
"react-native-url-polyfill": "^1.3.0",
"react-native-vector-icons": "^10.0.3",
"react-native-web": "~0.19.6",
"react-native-webview": "^13.2.2",
"readable-stream": "^1.0.33",
"rn-nodeify": "^10.3.0",
"stream": "^0.0.2",
"stream-browserify": "^1.0.0",
"typescript": "^5.1.3"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"react-native-svg-transformer": "^1.3.0"
},
"private": true,
"react-native": {
"_stream_transform": "readable-stream/transform",
"_stream_readable": "readable-stream/readable",
"_stream_writable": "readable-stream/writable",
"_stream_duplex": "readable-stream/duplex",
"_stream_passthrough": "readable-stream/passthrough",
"stream": "stream-browserify"
},
"browser": {
"_stream_transform": "readable-stream/transform",
"_stream_readable": "readable-stream/readable",
"_stream_writable": "readable-stream/writable",
"_stream_duplex": "readable-stream/duplex",
"_stream_passthrough": "readable-stream/passthrough",
"stream": "stream-browserify"
}
}
我遵循了此文档,但也不起作用。我想连接到元掩码并将其用于我的区块链投票应用程序(以太坊可靠性)
已经很久了,但是你解决了吗?我现在也面临同样的问题