stack_wallet/lib/services/mixins/fusion_interface.dart

180 lines
4.4 KiB
Dart
Raw Normal View History

2023-07-26 22:06:02 +00:00
import 'dart:io';
2023-07-27 23:13:03 +00:00
import 'package:fusiondart/fusiondart.dart';
import 'package:fusiondart/src/models/address.dart' as fusion_address;
2023-07-26 22:06:02 +00:00
import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart';
2023-08-07 02:15:05 +00:00
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
2023-07-26 22:06:02 +00:00
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
2023-08-07 02:15:05 +00:00
import 'package:stackwallet/utilities/logger.dart';
2023-07-26 22:06:02 +00:00
mixin FusionInterface {
2023-08-07 02:15:05 +00:00
// passed in wallet data
2023-07-26 22:06:02 +00:00
late final String _walletId;
late final Coin _coin;
late final MainDB _db;
2023-08-07 02:15:05 +00:00
// passed in wallet functions
late final Future<String> Function() _getCurrentChangeAddress;
late final Future<String> Function() _getNextChangeAddress;
2023-08-07 02:15:05 +00:00
2023-07-26 22:06:02 +00:00
void initFusionInterface({
required String walletId,
required Coin coin,
required MainDB db,
2023-08-07 02:15:05 +00:00
required Future<String> Function() getCurrentChangeAddress,
required Future<String> Function() getNextChangeAddress,
2023-07-26 22:06:02 +00:00
}) {
_walletId = walletId;
_coin = coin;
_db = db;
2023-08-07 02:15:05 +00:00
_getCurrentChangeAddress = getCurrentChangeAddress;
_getNextChangeAddress = getNextChangeAddress;
2023-07-26 22:06:02 +00:00
}
static List<Address> reserve_change_addresses(int number_addresses) {
// TODO
// get current change address
// get int number_addresses next addresses
return [];
}
static List<Address> unreserve_change_address(Address addr) {
//implement later based on wallet.
return [];
}
2023-07-26 22:06:02 +00:00
void fuse() async {
// Initial attempt for CashFusion integration goes here.
Fusion mainFusionObject =
Fusion(generateChangeAddress: () => _getNextChangeAddress());
2023-07-26 22:06:02 +00:00
2023-08-07 02:15:05 +00:00
// add stack utxos
2023-07-26 22:06:02 +00:00
List<UTXO> utxos = await _db.getUTXOs(_walletId).findAll();
2023-07-27 23:23:25 +00:00
await mainFusionObject.add_coins_from_wallet(utxos
.map((e) => (txid: e.txid, vout: e.vout, value: e.value))
.toList());
2023-08-07 02:15:05 +00:00
// add stack change address
final String currentChangeAddress = await _getCurrentChangeAddress();
// cast from String to Address
2023-08-07 02:15:05 +00:00
final Address? changeAddress =
await _db.getAddress(_walletId, currentChangeAddress);
// cast from Stack's Address to Fusiondart's Address
final fusion_address.Address fusionChangeAddress =
changeAddress!.toFusionAddress();
await mainFusionObject.addChangeAddress(fusionChangeAddress);
2023-08-07 02:15:05 +00:00
Logging.instance.log(
"FusionInterface fuse() changeAddress: $changeAddress",
level: LogLevel.Info,
);
// fuse utxos
2023-07-26 22:06:02 +00:00
await mainFusionObject.fusion_run();
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");
2023-08-07 02:15:05 +00:00
/*
2023-07-26 22:06:02 +00:00
print("DEBUG: Waiting for any potential incoming data...");
try {
await Future.delayed(Duration(seconds: 5)); // wait for 5 seconds
}
catch (e) {
print (e);
}
print("DEBUG: Done waiting.");
*/
bool mydebug1 = false;
if (mydebug1 == true) {
var serverIp = '167.114.119.46';
var serverPort = 8787;
List<int> frame = [
118,
91,
232,
180,
228,
57,
109,
207,
0,
0,
0,
45,
10,
43,
10,
7,
97,
108,
112,
104,
97,
49,
51,
18,
32,
111,
226,
140,
10,
182,
241,
179,
114,
193,
166,
162,
70,
174,
99,
247,
79,
147,
30,
131,
101,
225,
90,
8,
156,
104,
214,
25,
0,
0,
0,
0,
0
];
print("lets try to connect to a socket again");
var socket = await Socket.connect(serverIp, serverPort);
print('Connected to the server.');
socket.add(frame);
print('Sent frame: $frame');
socket.listen((data) {
print('Received from server: $data');
}, onDone: () {
print('Server closed connection.');
socket.destroy();
}, onError: (error) {
print('Error: $error');
socket.destroy();
});
}
// await _checkCurrentChangeAddressesForTransactions();
// await _checkCurrentReceivingAddressesForTransactions();
}
Future<void> refreshFusion() {
// TODO
throw UnimplementedError(
"TODO refreshFusion eg look up number of fusion participants connected/coordinating");
2023-07-26 22:06:02 +00:00
}
}