2023-07-26 22:06:02 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-07-27 23:13:03 +00:00
|
|
|
import 'package:fusiondart/fusiondart.dart';
|
2023-08-07 19:15:08 +00:00
|
|
|
import 'package:fusiondart/src/models/address.dart' as cash_fusion;
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:stackwallet/db/isar/main_db.dart';
|
2023-08-07 18:54:44 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-08-07 18:54:44 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-08-07 18:54:44 +00:00
|
|
|
const String kReservedFusionAddress = "reserved_fusion_address";
|
|
|
|
|
2023-08-24 16:22:13 +00:00
|
|
|
mixin FusionWalletInterface {
|
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
|
2023-08-07 18:54:44 +00:00
|
|
|
late final Future<Address> Function(
|
|
|
|
int chain,
|
|
|
|
int index,
|
|
|
|
DerivePathType derivePathType,
|
|
|
|
) _generateAddressForChain;
|
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 18:54:44 +00:00
|
|
|
required Future<Address> Function(
|
|
|
|
int,
|
|
|
|
int,
|
|
|
|
DerivePathType,
|
|
|
|
) generateAddressForChain,
|
2023-07-26 22:06:02 +00:00
|
|
|
}) {
|
|
|
|
_walletId = walletId;
|
|
|
|
_coin = coin;
|
|
|
|
_db = db;
|
2023-08-07 18:54:44 +00:00
|
|
|
_generateAddressForChain = generateAddressForChain;
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 19:15:08 +00:00
|
|
|
Future<cash_fusion.Address> createNewReservedChangeAddress() async {
|
2023-08-07 18:54:44 +00:00
|
|
|
int? highestChangeIndex = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.typeEqualTo(AddressType.p2pkh)
|
|
|
|
.subTypeEqualTo(AddressSubType.change)
|
|
|
|
.derivationPath((q) => q.not().valueStartsWith("m/44'/0'"))
|
|
|
|
.sortByDerivationIndexDesc()
|
|
|
|
.derivationIndexProperty()
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
Address address = await _generateAddressForChain(
|
|
|
|
1, // change chain
|
|
|
|
highestChangeIndex ?? 0,
|
|
|
|
DerivePathTypeExt.primaryFor(_coin),
|
|
|
|
);
|
|
|
|
address = address.copyWith(otherData: kReservedFusionAddress);
|
|
|
|
|
|
|
|
// TODO if we really want to be sure its not used, call electrumx and check it
|
|
|
|
|
|
|
|
await _db.putAddress(address);
|
|
|
|
|
2023-08-07 19:15:08 +00:00
|
|
|
return address.toFusionAddress();
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 19:29:56 +00:00
|
|
|
Future<List<cash_fusion.Address>> getUnusedReservedChangeAddresses(
|
2023-08-07 18:54:44 +00:00
|
|
|
int numberOfAddresses,
|
|
|
|
) async {
|
|
|
|
final txns = await _db
|
|
|
|
.getTransactions(_walletId)
|
|
|
|
.filter()
|
|
|
|
.address((q) => q.otherDataEqualTo(kReservedFusionAddress))
|
|
|
|
.findAll();
|
|
|
|
|
|
|
|
final List<String> usedAddresses = txns
|
|
|
|
.where((e) => e.address.value != null)
|
|
|
|
.map((e) => e.address.value!.value)
|
|
|
|
.toList(growable: false);
|
|
|
|
|
|
|
|
final List<Address> addresses = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.otherDataEqualTo(kReservedFusionAddress)
|
|
|
|
.findAll();
|
|
|
|
|
2023-08-07 19:29:56 +00:00
|
|
|
final List<cash_fusion.Address> unusedAddresses = [];
|
2023-08-07 18:54:44 +00:00
|
|
|
|
|
|
|
for (final address in addresses) {
|
|
|
|
if (!usedAddresses.contains(address.value)) {
|
2023-08-07 19:29:56 +00:00
|
|
|
unusedAddresses.add(address.toFusionAddress());
|
2023-08-07 18:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unusedAddresses.length < numberOfAddresses) {
|
|
|
|
for (int i = unusedAddresses.length; i < numberOfAddresses; i++) {
|
|
|
|
unusedAddresses.add(await createNewReservedChangeAddress());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return unusedAddresses;
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 22:06:02 +00:00
|
|
|
void fuse() async {
|
|
|
|
// Initial attempt for CashFusion integration goes here.
|
2023-08-07 19:15:08 +00:00
|
|
|
Fusion mainFusionObject = Fusion(
|
2023-08-08 14:32:11 +00:00
|
|
|
createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
|
|
|
|
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
|
|
|
|
getUnusedReservedChangeAddresses(numberOfAddresses),
|
|
|
|
);
|
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-08-25 18:50:18 +00:00
|
|
|
await mainFusionObject.addCoinsFromWallet(
|
|
|
|
utxos.map((e) => (e.txid, e.vout, e.value)).toList());
|
2023-08-07 02:15:05 +00:00
|
|
|
|
|
|
|
// fuse utxos
|
2023-08-25 18:50:18 +00:00
|
|
|
await mainFusionObject.fuse();
|
2023-07-26 22:06:02 +00:00
|
|
|
//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
|
2023-07-27 19:39:36 +00:00
|
|
|
throw UnimplementedError(
|
|
|
|
"TODO refreshFusion eg look up number of fusion participants connected/coordinating");
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
}
|