YSLoadUrl.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import 'dart:typed_data';
  2. import 'package:flutter_injected_web3/flutter_injected_web3.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_inappwebview/flutter_inappwebview.dart';
  5. import 'package:flutter_wallet/base/YSBase.dart';
  6. import 'package:flutter_wallet/tools/YSAlertView.dart';
  7. import 'package:web3dart/credentials.dart';
  8. import 'package:web3dart/crypto.dart';
  9. import 'package:web3dart/web3dart.dart';
  10. import '../generated/l10n.dart';
  11. import '../tools/YSBip2.dart';
  12. import '../tools/YSColors.dart';
  13. import '../tools/YSTools.dart';
  14. import 'package:http/http.dart';
  15. import '../tools/YSTron.dart';
  16. class YSLoadUrl extends StatefulWidget {
  17. final String url;
  18. const YSLoadUrl({Key? key, required this.url}) : super(key: key);
  19. @override
  20. YSLoadUrlState createState() => YSLoadUrlState();
  21. }
  22. class YSLoadUrlState extends State<YSLoadUrl> {
  23. String rpc = YSData().rpc;
  24. int chainId = 0;
  25. InAppWebViewController? _webViewController;
  26. @override
  27. void initState() {
  28. _getChainId();
  29. super.initState();
  30. }
  31. _getChainId() async{
  32. Web3Client? client = Web3Client(YSData().rpc, Client());
  33. chainId = await client.getNetworkId();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return YSBase(
  38. ysTitle: '',
  39. ysLeftWidget: Row(
  40. children: [
  41. GestureDetector(
  42. onTap: () async{
  43. if(_webViewController!=null){
  44. bool canGoBack = await _webViewController?.canGoBack()??false;
  45. if(canGoBack){
  46. _webViewController?.goBack();
  47. }else{
  48. if(!mounted)return;
  49. Navigator.pop(context);
  50. }
  51. }
  52. },
  53. child: Icon(Icons.chevron_left,size: zsp(30),color: YSColors.containColor(context),),
  54. ),
  55. GestureDetector(
  56. onTap: (){
  57. Navigator.pop(context);
  58. },
  59. child: Icon(Icons.close,size: zsp(25),color: YSColors.containColor(context),),
  60. )
  61. ],
  62. ),
  63. ysChild: SizedBox(
  64. height: ysHeight(context)-ysTOP(context)-hsp(60),
  65. width: ysWidth(context),
  66. child: InjectedWebview(
  67. addEthereumChain: changeNetwork,
  68. requestAccounts: getAccount,
  69. signTransaction: signTransaction,
  70. signPersonalMessage: signPersonelMessage,
  71. isDebug: true,
  72. initialUrlRequest: URLRequest(url: Uri.parse(widget.url)),
  73. chainId: chainId,
  74. rpc: rpc,
  75. onWebViewCreated: (controller){
  76. _webViewController = controller;
  77. },
  78. ),
  79. ),
  80. );
  81. }
  82. Future<String> changeNetwork(InAppWebViewController controller,
  83. JsAddEthereumChain data, int chainId) async {
  84. try {
  85. rpc = YSData().rpc;
  86. chainId = int.parse(data.chainId!);
  87. } catch (e) {
  88. LogUtil.d("$e");
  89. }
  90. return rpc;
  91. }
  92. Future<IncomingAccountsModel> getAccount(
  93. InAppWebViewController _, String ___, int __) async {
  94. Credentials fromHex = EthPrivateKey.fromHex(YSData().wallet['private']);
  95. final address = fromHex.address;
  96. return IncomingAccountsModel(
  97. address: address.toString(), chainId: chainId, rpcUrl: rpc);
  98. }
  99. Future<String> signTransaction(InAppWebViewController _, JsTransactionObject data, int chainId) async {
  100. bool isCheck = await YSAuthCheck().check();
  101. if(isCheck){
  102. Uint8List bytes = hexToBytes(data.data!);
  103. String? from = data.from;
  104. // YSData().contAddress = data.to!;
  105. String? to = data.to;
  106. String? value = data.value;
  107. String? gas = data.gas;
  108. gas = gas?.replaceAll('0x', '');
  109. value = value?.replaceAll('0x', '');
  110. int gasT = int.parse(gas!, radix: 16);
  111. int valueT = int.parse(value!, radix: 16);
  112. double gasD = gasT*1;
  113. double valueD = valueT*1;
  114. if(from!.contains('T')){
  115. // ignore: use_build_context_synchronously
  116. String resultStr = await YSTron.transferData(context, from: from, to: to!, private: YSData().wallet['private'], amount: '$valueD',);
  117. return resultStr;
  118. }else{
  119. Wbe3Api? web = await Wbe3Api().getInstances();
  120. web?.adppContaractDecimals(to!);
  121. var result = await web?.signETHTransaction(from,to!, YSData().wallet['private'], '$gasD', '$valueD',data: bytes);
  122. return '$result';
  123. }
  124. }else{
  125. return '';
  126. }
  127. }
  128. Future<String> signPersonelMessage(InAppWebViewController _, String data, int chainId) async {
  129. try {
  130. Credentials fromHex = EthPrivateKey.fromHex(YSData().wallet['private']);
  131. final sig = fromHex.signPersonalMessageToUint8List(hexToBytes(data));
  132. LogUtil.d("SignedTx $sig");
  133. return bytesToHex(sig, include0x: true);
  134. } catch (e) {
  135. LogUtil.d("$e");
  136. }
  137. return "";
  138. }
  139. }