wootzapp / browser-v1-deprecated

Other
2 stars 1 forks source link

flutter 2-way js communication #3

Open wootzappcom opened 3 years ago

wootzappcom commented 3 years ago

https://stackoverflow.com/questions/53689662/flutter-webview-two-way-communication-with-javascript

wootzappcom commented 3 years ago

https://github.com/trustwallet/trust-web3-provider/blob/master/src/index.js


class TrustWeb3Provider extends EventEmitter {

  setConfig(config) {
    this.setAddress(config.address);

    this.chainId = config.chainId;
    this.rpc = new RPCServer(config.rpcUrl);
    this.isDebug = !!config.isDebug;
  }

  request(payload) {
    // this points to window in methods like web3.eth.getAccounts()
    var that = this;
    if (!(this instanceof TrustWeb3Provider)) {
      that = window.ethereum;
    }
    return that._request(payload, false);
  }

}

  eth_sendTransaction(payload) {
    this.postMessage("signTransaction", payload.id, payload.params[0]);
  }

  eth_requestAccounts(payload) {
    this.postMessage("requestAccounts", payload.id, {});
  }

window.trustwallet = {
  Provider: TrustWeb3Provider,
  Web3: Web3,
  postMessage: null,
};

Android specific

https://github.com/trustwallet/trust-web3-provider/blob/master/android/app/src/main/java/com/trust/web3/demo/MainActivity.kt#L50-L64

webview.addJavascriptInterface(this, "_tw_")

 val source = """
        (function() {
            var config = {
                chainId: $chainId,
                rpcUrl: "$rpcUrl",
                isDebug: true
            };
            window.ethereum = new trustwallet.Provider(config);
            window.web3 = new trustwallet.Web3(window.ethereum);
            trustwallet.postMessage = (json) => {
                window._tw_.postMessage(JSON.stringify(json));
            }
        })();
        """
view?.evaluateJavascript(initJs, null)

Flutter specific

https://github.com/lonerchen/flutter_web3view/blob/6cf7d604a4972df9b84d6072b425bbd6848c3702/assets/init.js


    var config = {
        address: "$address",
         rpcUrl: "$rpcurl",
        chainId: "$chainId"

    };
const provider = new window.Trust(config);
window.ethereum = provider;
    provider.setMaxListeners(1000);
    provider.postMessage = function(handler, id, data) {

      switch (handler) {
        case 'signTransaction':
          var gasLimit = data.gasLimit || data.gas || null;
          var gasPrice = data.gasPrice || null;
          var nonce = data.nonce || -1;
          console.log("signTransaction")
          signTransaction.postMessage(id.toLocaleString('fullwide',{useGrouping:false}) + "#" + data);
//          return trust.signTransaction(id, data.to || null, data.value, nonce, gasLimit, gasPrice, data.data || null);

        case 'signMessage':
          console.log("signMessage")
          signMessage.postMessage(id.toLocaleString('fullwide',{useGrouping:false}) + "#" + data);
        case 'signPersonalMessage':
          console.log("signPersonalMessage")
          signPersonalMessage.postMessage(id.toLocaleString('fullwide',{useGrouping:false}) + "#" + data);
        case 'signTypedMessage':
        case 'eth_signTypedData_v3':
          console.log("eth_signTypedData_v3")
          eth_signTypedData_v3.postMessage(id.toLocaleString('fullwide',{useGrouping:false}) + "#" + data);
        case 'requestAccounts':
        case 'eth_requestAccounts':
          console.log("eth_requestAccounts" + id)
          eth_requestAccounts.postMessage(id.toLocaleString('fullwide',{useGrouping:false}) + "#" + data);
      }
    };
    window.web3 = new window.Web3(provider);
    window.web3.eth.defaultAccount = config.address;
    //Test.postMessage(window.web3.eth.defaultAccount);
    window.chrome = {webstore: {}};
    //Test.postMessage("over");
})();
wootzappcom commented 3 years ago

https://stackoverflow.com/questions/52330102/use-js-library-in-flutter

wootzappcom commented 3 years ago

https://github.com/ONTO-Data-Wallet/ONTO-web3-provider/blob/a1f954257f5d66ed6e872376fe4a893870a8cdab/index.js

wootzappcom commented 3 years ago

https://github.com/lonerchen/my_wallet/blob/16d8e02acfdd81901e05da779fa3a8f4260e8a4c/lib/pages/tabs/tab_defi.dart

wootzappcom commented 3 years ago

reference - https://github.com/hhstore/blog/issues/271