YSBip.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import 'dart:typed_data';
  2. import 'package:flutter_bitcoin/flutter_bitcoin.dart' as btcWallet;
  3. import 'package:flutter_wallet/tools/YSNetWork.dart';
  4. import 'package:flutter_wallet/tools/YSSqflite.dart';
  5. import 'package:hex/hex.dart';
  6. import 'YSAes.dart';
  7. import 'YSTools.dart';
  8. import 'package:bip39/bip39.dart' as bip39;
  9. import 'package:bip32/bip32.dart' as bip32;
  10. import 'package:web3dart/crypto.dart';
  11. import 'package:web3dart/web3dart.dart';
  12. import 'package:wallet/wallet.dart' as ysWallet;
  13. class YSBipManager {
  14. static setKeystore(String keystore,String password,{bool isInto = false}) async{
  15. try{
  16. Wallet wallet = Wallet.fromJson(keystore, password);
  17. String privateKey = bytesToHex(wallet.privateKey.privateKey);
  18. YSData data = YSData();
  19. String word = getMnemonic();
  20. YSSharedPreferences.setStringValue('word', 'word');
  21. YSSqflite sqflite = YSSqflite().init();
  22. int id = 0;
  23. if(YSData().typeId=='TRON-HD'){
  24. Map tronMap = getTronCode2(privateKey);
  25. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, tronMap['word'], tronMap['privateKey'], tronMap['pubicKey'],0));
  26. }else if(YSData().typeId=='BTC-HD'){
  27. Map btcMap = await getBtcCode2(privateKey);
  28. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, btcMap['word'], btcMap['privateKey'], btcMap['pubicKey'],0));
  29. }else{
  30. EthereumAddress pubicKey = await getPublicAddress(privateKey);
  31. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, word, privateKey, pubicKey.hexEip55,0));
  32. }
  33. return id!=0;
  34. }catch(e){
  35. ysFlutterToast('$e');
  36. return false;
  37. }
  38. }
  39. static setObserve(String address,{bool isInto = false}) async{
  40. try{
  41. String privateKey = '';
  42. YSData data = YSData();
  43. String word = getMnemonic();
  44. YSSharedPreferences.setStringValue('word', 'word');
  45. YSSqflite sqflite = YSSqflite().init();
  46. int id = 0;
  47. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, word, privateKey, address,0));
  48. return id!=0;
  49. }catch(e){
  50. ysFlutterToast('$e');
  51. return false;
  52. }
  53. }
  54. static setPrivate(String privateKey,{bool isInto = false}) async{
  55. YSData data = YSData();
  56. try{
  57. String word = getMnemonic();
  58. YSSharedPreferences.setStringValue('word', 'word');
  59. YSSqflite sqflite = YSSqflite().init();
  60. int id = 0;
  61. if(YSData().typeId=='TRON-HD'){
  62. Map tronMap = getTronCode2(privateKey);
  63. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, tronMap['word'], tronMap['privateKey'], tronMap['pubicKey'],0));
  64. }else if(YSData().typeId=='BTC-HD'){
  65. Map btcMap = await getBtcCode2(privateKey);
  66. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, btcMap['word'], btcMap['privateKey'], btcMap['pubicKey'],0));
  67. }else{
  68. EthereumAddress pubicKey = await getPublicAddress(privateKey);
  69. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, word, privateKey, pubicKey.hexEip55,0));
  70. }
  71. return id!=0;
  72. }catch(e){
  73. LogUtil.d(e);
  74. return false;
  75. }
  76. }
  77. static Future<bool> setMnemonic(String word,{bool isInto = false}) async{
  78. YSData data = YSData();
  79. try{
  80. YSSharedPreferences.setStringValue('word', word);
  81. bool isFirst = await YSSharedPreferences.getBoolValue('isFirst')??true;
  82. int id = 0;
  83. if(isFirst&&isInto==false){
  84. // id = 1;
  85. for (var element in YSData().typeArray) {
  86. String name = '${element['name']}'.replaceAll('-', '').replaceAll(' ', '');
  87. LogUtil.d(name);
  88. YSSqflite sqflite = YSSqflite().init(name: name);
  89. if(element['id']=='TRON-HD'){
  90. Map tronMap = getTronCode(word);
  91. if(tronMap.isNotEmpty)sqflite.rawInsert(YSWalletDbData(data.name, data.password, tronMap['word'], tronMap['privateKey'], tronMap['pubicKey'],0),name: name);
  92. }else if(element['id']=='BTC-HD'){
  93. Map btcMap = await getBtcCode(word);
  94. if(btcMap.isNotEmpty)sqflite.rawInsert(YSWalletDbData(data.name, data.password, btcMap['word'], btcMap['privateKey'], btcMap['pubicKey'],0),name: name);
  95. }else{
  96. String privateKey = getPrivateKey(word);
  97. EthereumAddress pubicKey = await getPublicAddress(privateKey);
  98. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, word, privateKey, pubicKey.hexEip55,0),name: name);
  99. }
  100. }
  101. YSSharedPreferences.setBoolValue('isFirst', false);
  102. }else{
  103. LogUtil.d('obj2');
  104. YSSqflite sqflite = YSSqflite().init();
  105. if(YSData().typeId=='TRON-HD'){
  106. Map tronMap = getTronCode(word);
  107. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, tronMap['word'], tronMap['privateKey'], tronMap['pubicKey'],0));
  108. }else if(YSData().typeId=='BTC-HD'){
  109. Map btcMap = await getBtcCode(word);
  110. sqflite.rawInsert(YSWalletDbData(data.name, data.password, btcMap['word'], btcMap['privateKey'], btcMap['pubicKey'],0));
  111. }else{
  112. String privateKey = getPrivateKey(word);
  113. EthereumAddress pubicKey = await getPublicAddress(privateKey);
  114. id = await sqflite.rawInsert(YSWalletDbData(data.name, data.password, word, privateKey, pubicKey.hexEip55,0));
  115. }
  116. }
  117. return id!=0;
  118. }catch(e){
  119. LogUtil.d(e);
  120. return false;
  121. }
  122. }
  123. static String getMnemonic() {
  124. String randomMnemonic = bip39.generateMnemonic();
  125. LogUtil.d(randomMnemonic);
  126. return randomMnemonic;
  127. }
  128. static bool checkMnemonic(String mnemonic) {
  129. return bip39.validateMnemonic(mnemonic);
  130. }
  131. static String getPrivateKey(String mnemonic) {
  132. final seed = bip39.mnemonicToSeed(mnemonic);
  133. final root = bip32.BIP32.fromSeed(seed);
  134. final child1 = root.derivePath("m/44'/60'/0'/0/0");
  135. String hex = bytesToHex(child1.privateKey!.toList());
  136. LogUtil.d(hex);
  137. return hex;
  138. }
  139. static Future<EthereumAddress> getPublicAddress(String privateKey) async {
  140. final private = EthPrivateKey.fromHex(privateKey);
  141. final address = private.address;
  142. LogUtil.d(address);
  143. return address;
  144. }
  145. static Map getTronCode(String mnemonic) {
  146. const passphrase = '';
  147. final seed = ysWallet.mnemonicToSeed(mnemonic.split(' '), passphrase: passphrase);
  148. final master = ysWallet.ExtendedPrivateKey.master(seed, ysWallet.xprv);
  149. final root = master.forPath("m/44'/195'/0'/0/0");
  150. final privateKey = ysWallet.PrivateKey((root as ysWallet.ExtendedPrivateKey).key);
  151. final publicKey = ysWallet.tron.createPublicKey(privateKey);
  152. final address = ysWallet.tron.createAddress(publicKey);
  153. return {'word':mnemonic,'privateKey':privateKey.value.toRadixString(16),'pubicKey':address};
  154. }
  155. static Map getTronCode2(String private16) {
  156. String word = getMnemonic();
  157. word = '$word ysssss';
  158. final privateKey = ysWallet.PrivateKey(BigInt.parse(private16, radix: 16));
  159. final publicKey = ysWallet.tron.createPublicKey(privateKey);
  160. final address = ysWallet.tron.createAddress(publicKey);
  161. return {'word':word,'privateKey':private16,'pubicKey':address};
  162. }
  163. // static Future<Map> getBtcCode(String mnemonic) async{
  164. // String seed = bip39.mnemonicToSeedHex(mnemonic);
  165. // bip32.BIP32 root = bip32.BIP32.fromSeed(Uint8List.fromList(HEX.decode(seed)));
  166. //
  167. // btcWallet.ECPair keyPair = btcWallet.ECPair.fromPrivateKey(root.privateKey!);
  168. // String private = keyPair.toWIF();
  169. // String privateS = YSAes.aesEncode3('${private}_${DateTime.now().microsecond}');
  170. // Map dict = await YSNetWork.httpNetwork(api: 'bitconinp2sh', request: {'prikey':privateS});
  171. // if(dict.isNotEmpty){
  172. // String address = dict['data']??'';
  173. // return {'word':mnemonic,'privateKey':private,'pubicKey':address};
  174. // }else{
  175. // return {};
  176. // }
  177. // }
  178. static Future<Map> getBtcCode(String mnemonic) async{
  179. const passphrase = '';
  180. final seed = ysWallet.mnemonicToSeed(mnemonic.split(' '), passphrase: passphrase);
  181. final master = ysWallet.ExtendedPrivateKey.master(seed, ysWallet.xprv);
  182. final root = master.forPath("m/49'/0'/0'/0/0");
  183. btcWallet.ECPair keyPair = btcWallet.ECPair.fromPrivateKey(HEX.decode((root as ysWallet.ExtendedPrivateKey).key.toRadixString(16)) as Uint8List);
  184. String private = keyPair.toWIF();
  185. String privateS = YSAes.aesEncode3('${private}_${DateTime.now().microsecond}');
  186. Map dict = await YSNetWork.httpNetwork(api: 'bitconinp2sh', request: {'prikey':privateS});
  187. if(dict.isNotEmpty){
  188. String address = dict['data']??'';
  189. // LogUtil.d('getBtcCode=======$dict');
  190. // return {};
  191. return {'word':mnemonic,'privateKey':private,'pubicKey':address};
  192. }else{
  193. return {};
  194. }
  195. }
  196. static Future<Map> getBtcCode2(String private) async{
  197. String word = getMnemonic();
  198. word = '$word ysssss';
  199. String privateS = YSAes.aesEncode3('${private}_${DateTime.now().microsecond}');
  200. Map dict = await YSNetWork.httpNetwork(api: 'bitconinp2sh', request: {'prikey':privateS});
  201. if(dict.isNotEmpty){
  202. String address = dict['data']??'';
  203. return {'word':word,'privateKey':private,'pubicKey':address};
  204. }else{
  205. return {};
  206. }
  207. }
  208. }