2023-09-14 22:28:11 +00:00
|
|
|
import 'dart:async';
|
2023-08-30 16:40:39 +00:00
|
|
|
import 'dart:convert';
|
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-30 16:40:39 +00:00
|
|
|
import 'package:fusiondart/src/models/address.dart' as fusion_address;
|
|
|
|
import 'package:fusiondart/src/models/input.dart' as fusion_input;
|
|
|
|
import 'package:fusiondart/src/models/transaction.dart' as fusion_tx;
|
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-09-15 19:21:36 +00:00
|
|
|
import 'package:stackwallet/services/tor_service.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-09-15 21:02:25 +00:00
|
|
|
import 'package:stackwallet/utilities/stack_file_system.dart';
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-08-07 18:54:44 +00:00
|
|
|
const String kReservedFusionAddress = "reserved_fusion_address";
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// A mixin for the BitcoinCashWallet class that adds CashFusion functionality.
|
2023-08-24 16:22:13 +00:00
|
|
|
mixin FusionWalletInterface {
|
2023-09-14 22:28:11 +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-09-14 22:28:11 +00:00
|
|
|
late final TorService _torService;
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-09-14 22:28:11 +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-09-15 21:02:25 +00:00
|
|
|
/// Initializes the FusionWalletInterface mixin.
|
|
|
|
///
|
|
|
|
/// This function must be called before any other functions in this mixin.
|
|
|
|
///
|
|
|
|
/// Returns a `Future<void>` that resolves when Tor has been started.
|
|
|
|
Future<void> initFusionInterface({
|
2023-07-26 22:06:02 +00:00
|
|
|
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-09-15 21:02:25 +00:00
|
|
|
}) async {
|
|
|
|
// Set passed in wallet data.
|
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-09-14 22:28:11 +00:00
|
|
|
_torService = TorService.sharedInstance;
|
|
|
|
|
2023-09-15 21:02:25 +00:00
|
|
|
// Try getting the proxy info.
|
|
|
|
//
|
|
|
|
// Start the Tor service if it's not already running. Returns if Tor is already
|
|
|
|
// connected or else after Tor returns from start().
|
|
|
|
try {
|
|
|
|
_torService.getProxyInfo();
|
|
|
|
// Proxy info successfully retrieved, Tor is connected.
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
// Init the Tor service if it hasn't already been.
|
|
|
|
final torDir = await StackFileSystem.applicationTorDirectory();
|
|
|
|
_torService.init(torDataDirPath: torDir.path);
|
2023-09-14 22:28:11 +00:00
|
|
|
|
|
|
|
// Start the Tor service.
|
2023-09-15 21:02:25 +00:00
|
|
|
return await _torService.start();
|
2023-09-14 22:28:11 +00:00
|
|
|
}
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all addresses in the wallet.
|
2023-08-30 16:40:39 +00:00
|
|
|
Future<List<fusion_address.Address>> getFusionAddresses() async {
|
|
|
|
List<Address> _addresses = await _db.getAddresses(_walletId).findAll();
|
|
|
|
return _addresses.map((address) => address.toFusionAddress()).toList();
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all transactions in the wallet for the given address.
|
2023-08-30 16:40:39 +00:00
|
|
|
Future<Set<fusion_tx.Transaction>> getTransactionsByAddress(
|
|
|
|
String address) async {
|
|
|
|
var _txs = await _db.getTransactions(_walletId).findAll();
|
|
|
|
|
|
|
|
return _txs
|
|
|
|
.map((tx) => tx.toFusionTransaction())
|
|
|
|
.toSet(); // TODO feed in proper public key
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all UTXOs in the wallet for the given address.
|
2023-08-30 16:40:39 +00:00
|
|
|
Future<List<fusion_input.Input>> getInputsByAddress(String address) async {
|
|
|
|
var _utxos = await _db.getUTXOsByAddress(_walletId, address).findAll();
|
|
|
|
|
|
|
|
return _utxos
|
|
|
|
.map((utxo) => utxo.toFusionInput(
|
|
|
|
pubKey: utf8.encode('0000'))) // TODO feed in proper public key
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Creates a new reserved change address.
|
2023-08-30 16:40:39 +00:00
|
|
|
Future<fusion_address.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);
|
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// TODO if we really want to be sure it's not used, call electrumx and check it
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
Address? _address = await _db.getAddress(_walletId, address.value);
|
|
|
|
if (_address != null) {
|
|
|
|
// throw Exception("Address already exists");
|
|
|
|
await _db.updateAddress(_address, address);
|
|
|
|
} else {
|
|
|
|
await _db.putAddress(address);
|
|
|
|
}
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-08-07 19:15:08 +00:00
|
|
|
return address.toFusionAddress();
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of unused reserved change addresses.
|
|
|
|
///
|
|
|
|
/// If there are not enough unused reserved change addresses, new ones are created.
|
2023-08-30 16:40:39 +00:00
|
|
|
Future<List<fusion_address.Address>> getUnusedReservedChangeAddresses(
|
2023-08-07 18:54:44 +00:00
|
|
|
int numberOfAddresses,
|
|
|
|
) async {
|
2023-09-14 22:28:11 +00:00
|
|
|
// Fetch all transactions that have been sent to a reserved change address.
|
2023-08-07 18:54:44 +00:00
|
|
|
final txns = await _db
|
|
|
|
.getTransactions(_walletId)
|
|
|
|
.filter()
|
|
|
|
.address((q) => q.otherDataEqualTo(kReservedFusionAddress))
|
|
|
|
.findAll();
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Fetch all addresses that have been used in a transaction.
|
2023-08-07 18:54:44 +00:00
|
|
|
final List<String> usedAddresses = txns
|
|
|
|
.where((e) => e.address.value != null)
|
|
|
|
.map((e) => e.address.value!.value)
|
|
|
|
.toList(growable: false);
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Fetch all reserved change addresses.
|
2023-08-07 18:54:44 +00:00
|
|
|
final List<Address> addresses = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.otherDataEqualTo(kReservedFusionAddress)
|
|
|
|
.findAll();
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Initialize a list of unused reserved change addresses.
|
2023-08-30 16:40:39 +00:00
|
|
|
final List<fusion_address.Address> unusedAddresses = [];
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Add any unused reserved change addresses to the list.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// If there are not enough unused reserved change addresses, create new ones.
|
2023-08-07 18:54:44 +00:00
|
|
|
if (unusedAddresses.length < numberOfAddresses) {
|
|
|
|
for (int i = unusedAddresses.length; i < numberOfAddresses; i++) {
|
|
|
|
unusedAddresses.add(await createNewReservedChangeAddress());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Return the list of unused reserved change addresses.
|
2023-08-07 18:54:44 +00:00
|
|
|
return unusedAddresses;
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns the current Tor proxy address.
|
|
|
|
Future<({InternetAddress host, int port})> getSocksProxyAddress() async {
|
|
|
|
/*
|
|
|
|
// Start the Tor service if it's not already running.
|
|
|
|
if (_torService.proxyInfo.port == -1) { // -1 indicates that the proxy is not running.
|
|
|
|
await _torService.start(); // We already unawaited this in initFusionInterface...
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TODO make sure we've properly awaited the Tor service starting before
|
|
|
|
// returning the proxy address.
|
|
|
|
|
|
|
|
// Return the proxy address.
|
2023-09-15 21:02:25 +00:00
|
|
|
return _torService.getProxyInfo();
|
2023-09-14 22:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initial attempt for CashFusion integration goes here.
|
|
|
|
|
|
|
|
/// Fuse the wallet's UTXOs.
|
|
|
|
///
|
|
|
|
/// This function is called when the user taps the "Fuse" button in the UI.
|
|
|
|
///
|
|
|
|
/// Returns:
|
|
|
|
/// A `Future<void>` that resolves when the fusion operation is finished.
|
|
|
|
Future<void> fuse() async {
|
2023-07-26 22:06:02 +00:00
|
|
|
// Initial attempt for CashFusion integration goes here.
|
2023-08-07 19:15:08 +00:00
|
|
|
Fusion mainFusionObject = Fusion(
|
2023-08-30 16:40:39 +00:00
|
|
|
getAddresses: () => getFusionAddresses(),
|
|
|
|
getTransactionsByAddress: (String address) =>
|
|
|
|
getTransactionsByAddress(address),
|
|
|
|
getInputsByAddress: (String address) => getInputsByAddress(address),
|
|
|
|
// createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
|
2023-08-08 14:32:11 +00:00
|
|
|
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
|
|
|
|
getUnusedReservedChangeAddresses(numberOfAddresses),
|
2023-09-14 22:28:11 +00:00
|
|
|
getSocksProxyAddress: () => getSocksProxyAddress(),
|
2023-08-08 14:32:11 +00:00
|
|
|
);
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// Pass wallet functions to the Fusion object
|
|
|
|
mainFusionObject.initFusion(
|
|
|
|
getAddresses: getFusionAddresses,
|
|
|
|
getTransactionsByAddress: getTransactionsByAddress,
|
|
|
|
getInputsByAddress: getInputsByAddress,
|
|
|
|
/*createNewReservedChangeAddress: createNewReservedChangeAddress,*/
|
2023-09-14 22:28:11 +00:00
|
|
|
getUnusedReservedChangeAddresses: getUnusedReservedChangeAddresses,
|
|
|
|
getSocksProxyAddress: getSocksProxyAddress);
|
2023-08-30 16:40:39 +00:00
|
|
|
|
|
|
|
// Add stack UTXOs.
|
2023-07-26 22:06:02 +00:00
|
|
|
List<UTXO> utxos = await _db.getUTXOs(_walletId).findAll();
|
2023-08-30 16:40:39 +00:00
|
|
|
|
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
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// Fuse UTXOs.
|
2023-09-14 22:28:11 +00:00
|
|
|
return await mainFusionObject.fuse();
|
2023-07-26 22:06:02 +00:00
|
|
|
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// TODO remove or fix code below.
|
|
|
|
|
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();
|
2023-09-14 22:28:11 +00:00
|
|
|
*/
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|