Skip to content

Commit

Permalink
added original code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeanclaudeaoun committed Sep 15, 2023
1 parent a60c0ae commit d3e866a
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<WebView
source={{ uri: url }}
style={{ flex: 1, width: '100%' }}
startInLoadingState={true}
javaScriptEnabledAndroid={true}
onMessage={(event) => {
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.
Expand Down

0 comments on commit d3e866a

Please sign in to comment.