|
| 1 | +## web3_provider |
| 2 | +The project supported send and receive messages between Dapp and in-app webview “Only EIP-1193 standard supported” |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +* Communicate between dapp and your app. |
| 7 | + |
| 8 | +<img src="https://raw.githubusercontent.com/VNAPNIC/web3-provider/master/art/sequence_diagram_communication.png"/> |
| 9 | + |
| 10 | +* Dapp interact with blockchain. |
| 11 | + |
| 12 | +<img src="https://raw.githubusercontent.com/VNAPNIC/web3-provider/master/art/sequence_diagram_sign_transaction.png"/> |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```dart |
| 17 | +import 'package:web3_provider/web3_provider.dart'; |
| 18 | +
|
| 19 | +InAppWebViewGroupOptions options = InAppWebViewGroupOptions( |
| 20 | + crossPlatform: InAppWebViewOptions( |
| 21 | + useShouldOverrideUrlLoading: true, |
| 22 | + mediaPlaybackRequiresUserGesture: false, |
| 23 | + userAgent: |
| 24 | + "Mozilla/5.0 (Linux; Android 4.4.4; SAMSUNG-SM-N900A Build/tt) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36", |
| 25 | + ), |
| 26 | + android: AndroidInAppWebViewOptions( |
| 27 | + useHybridComposition: true, |
| 28 | + domStorageEnabled: true, |
| 29 | + ), |
| 30 | + ios: IOSInAppWebViewOptions( |
| 31 | + allowsInlineMediaPlayback: true, |
| 32 | + ), |
| 33 | +); |
| 34 | +
|
| 35 | +/// By default config |
| 36 | +InAppWebViewEIP1193( |
| 37 | + chainId: 56, // Replace your chain id network you want connect |
| 38 | + rpcUrl: 'https://bsc-dataseed.binance.org/', // Replace your rpc url network you want connect |
| 39 | + walletAddress: walletAddress, |
| 40 | + signCallback: (rawData, eip1193, controller) { |
| 41 | + // Handler callback when dapp interact with blockchain |
| 42 | + switch (eip1193) { |
| 43 | + case EIP1193.requestAccounts: |
| 44 | + controller?.setAddress(walletAddress, id); |
| 45 | + print('requestAccounts'); |
| 46 | + break; |
| 47 | + case EIP1193.signTransaction: |
| 48 | + print('signTransaction'); |
| 49 | + break; |
| 50 | + case EIP1193.signMessage: |
| 51 | + print('signMessage'); |
| 52 | + break; |
| 53 | + case EIP1193.signPersonalMessage: |
| 54 | + print('signPersonalMessage'); |
| 55 | + break; |
| 56 | + case EIP1193.signTypedMessage: |
| 57 | + print('addEthereumChain'); |
| 58 | + break; |
| 59 | + case EIP1193.addEthereumChain: |
| 60 | + print('addEthereumChain'); |
| 61 | + break; |
| 62 | + }, |
| 63 | + initialUrlRequest: URLRequest( |
| 64 | + url: Uri.parse( |
| 65 | + 'https://position.exchange', // Replace your dapp domain |
| 66 | + ), |
| 67 | + ), |
| 68 | + initialOptions: options |
| 69 | +); |
| 70 | +``` |
| 71 | + |
| 72 | +If you want use your provider script |
| 73 | +you provide [customPathProvider] and [customWalletName] |
| 74 | + |
| 75 | +`signCallback: (rawData, eip1193, controller)`: callback was called when dapp when interact with blockchain. <br/> |
| 76 | +- `rawData`: data received. |
| 77 | +- `eip1193`: type function support. |
| 78 | + - requestAccounts: Pass when web app connect wallet |
| 79 | + - signTransaction: Pass when web app approve contract or send transaction |
| 80 | + - signMessage: Pass when web app sign a message |
| 81 | + - signPersonalMessage: Pass when web app sign a personal message |
| 82 | + - signTypedMessage: Pass when web app sign a type message |
| 83 | + - addEthereumChain: Pass when web app add a new chain |
| 84 | + |
| 85 | + |
| 86 | +When you pass data from dapp to your app |
| 87 | +``` |
| 88 | +const args = {/* Pass data you want */}; |
| 89 | +if (window.flutter_inappwebview.callHandler) { |
| 90 | + window.flutter_inappwebview.callHandler('functionName', args) |
| 91 | + .then(function(result) { |
| 92 | + /// Receive data from your app |
| 93 | + }); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +And receive in your app |
| 98 | +``` |
| 99 | +onWebViewCreated: (controller) { |
| 100 | + controller.addJavaScriptHandler( |
| 101 | + handlerName: 'functionName', |
| 102 | + callback: (args) { |
| 103 | + /// Receive data from dapp |
| 104 | + |
| 105 | + |
| 106 | + /// Send data to dapp; |
| 107 | + return /* anything */; |
| 108 | + }, |
| 109 | + ); |
| 110 | +}, |
| 111 | +``` |
0 commit comments