diff --git a/README.md b/README.md index 9024070..92560f9 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,73 @@ function MyApp() { } ``` +## original working code +``` +import React, { useEffect, useState } from 'react'; +import { WebView } from 'react-native-webview'; + +const Web3View = ({address, url}) => { + const [injectedJavaScript, setInjectedJavaScript] = useState(null); + + useEffect(() => { + const injectedScript = ` + console.log("Injected script running..."); + window.ReactNativeWebView.postMessage("Injected script started!"); + + window.ethereum = { + isMetaMask: true, + isConnected: () => true, + chainId: '0x1', + selectedAddress: '${address}', + request: function({ method, params }) { + window.ReactNativeWebView.postMessage("DApp requested method: " + method + ", Params: " + JSON.stringify(params)); + + switch (method) { + case 'eth_accounts': + return Promise.resolve([ '${address}' ]); + case 'eth_requestAccounts': + return Promise.resolve([ '${address}' ]); + case 'eth_chainId': + return Promise.resolve('0x1'); // Ethereum Mainnet + case 'eth_blockNumber': + return Promise.resolve('0x5BAD55'); // Mock block number + case 'eth_call': + // Log params and potentially mock response based on it + console.log("eth_call with params:", params); + return Promise.reject("eth_call not implemented yet."); + default: + console.error("Unhandled method call from DApp:", method); + return Promise.reject(new Error("Unhandled method: " + method)); + } + }, + enable: async function() { + return ['${address}']; + } + }; + + window.web3 = { currentProvider: window.ethereum }; + + true; // ensure the injected script doesn't return a value + `; + + setInjectedJavaScript(injectedScript); +}, [address]); + + return ( { + console.log('Message from WebView:', event.nativeEvent.data); + }} + injectedJavaScript={injectedJavaScript} + />) +}; + +export default Web3View; +``` + ## Contributing See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.