2023-09-14 22:28:11 +00:00
|
|
|
import 'dart:async';
|
2023-10-16 21:04:27 +00:00
|
|
|
import 'dart:convert';
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-10-12 20:00:58 +00:00
|
|
|
import 'package:bitbox/bitbox.dart' as bitbox;
|
2023-10-09 21:42:42 +00:00
|
|
|
import 'package:bitcoindart/bitcoindart.dart' as btcdart;
|
2023-10-26 15:54:20 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2023-09-28 16:41:39 +00:00
|
|
|
import 'package:fusiondart/fusiondart.dart' as fusion;
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:stackwallet/db/isar/main_db.dart';
|
2023-11-14 20:31:53 +00:00
|
|
|
import 'package:stackwallet/electrumx_rpc/cached_electrumx_client.dart';
|
2023-09-28 20:16:48 +00:00
|
|
|
import 'package:stackwallet/models/fusion_progress_ui_state.dart';
|
2023-08-07 18:54:44 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
2023-10-12 22:05:17 +00:00
|
|
|
import 'package:stackwallet/pages_desktop_specific/cashfusion/sub_widgets/fusion_dialog.dart';
|
2023-10-25 14:46:00 +00:00
|
|
|
import 'package:stackwallet/services/coins/bitcoincash/bitcoincash_wallet.dart';
|
2023-09-22 21:44:40 +00:00
|
|
|
import 'package:stackwallet/services/fusion_tor_service.dart';
|
2023-10-09 21:42:42 +00:00
|
|
|
import 'package:stackwallet/utilities/bip32_utils.dart';
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-10-12 20:00:58 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2023-10-26 17:29:06 +00:00
|
|
|
import 'package:stackwallet/utilities/prefs.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-10-16 21:04:27 +00:00
|
|
|
class FusionInfo {
|
|
|
|
final String host;
|
|
|
|
final int port;
|
|
|
|
final bool ssl;
|
|
|
|
|
|
|
|
/// set to 0 for continuous
|
|
|
|
final int rounds;
|
|
|
|
|
|
|
|
const FusionInfo({
|
|
|
|
required this.host,
|
|
|
|
required this.port,
|
|
|
|
required this.ssl,
|
|
|
|
required this.rounds,
|
|
|
|
}) : assert(rounds >= 0);
|
|
|
|
|
|
|
|
static const DEFAULTS = FusionInfo(
|
2023-10-26 15:54:20 +00:00
|
|
|
host: "fusion.servo.cash",
|
|
|
|
port: 8789,
|
|
|
|
ssl: true,
|
|
|
|
// host: "cashfusion.stackwallet.com",
|
|
|
|
// port: 8787,
|
|
|
|
// ssl: false,
|
2023-10-16 21:04:27 +00:00
|
|
|
rounds: 0, // 0 is continuous
|
|
|
|
);
|
|
|
|
|
|
|
|
factory FusionInfo.fromJsonString(String jsonString) {
|
|
|
|
final json = jsonDecode(jsonString);
|
|
|
|
return FusionInfo(
|
|
|
|
host: json['host'] as String,
|
|
|
|
port: json['port'] as int,
|
|
|
|
ssl: json['ssl'] as bool,
|
|
|
|
rounds: json['rounds'] as int,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
String toJsonString() {
|
2023-10-17 18:54:23 +00:00
|
|
|
return jsonEncode({
|
2023-10-16 21:04:27 +00:00
|
|
|
'host': host,
|
|
|
|
'port': port,
|
|
|
|
'ssl': ssl,
|
|
|
|
'rounds': rounds,
|
2023-10-17 18:54:23 +00:00
|
|
|
});
|
2023-10-16 21:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return toJsonString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) {
|
|
|
|
if (identical(this, other)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return other is FusionInfo &&
|
|
|
|
other.host == host &&
|
|
|
|
other.port == port &&
|
|
|
|
other.ssl == ssl &&
|
|
|
|
other.rounds == rounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode {
|
|
|
|
return Object.hash(
|
|
|
|
host.hashCode,
|
|
|
|
port.hashCode,
|
|
|
|
ssl.hashCode,
|
|
|
|
rounds.hashCode,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-22 16:48:14 +00:00
|
|
|
late final String _walletId;
|
2023-07-26 22:06:02 +00:00
|
|
|
late final Coin _coin;
|
2023-09-22 16:48:14 +00:00
|
|
|
late final MainDB _db;
|
2023-09-22 21:44:40 +00:00
|
|
|
late final FusionTorService _torService;
|
2023-10-09 21:42:42 +00:00
|
|
|
late final Future<String?> _mnemonic;
|
|
|
|
late final Future<String?> _mnemonicPassphrase;
|
|
|
|
late final btcdart.NetworkType _network;
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-10-26 17:29:06 +00:00
|
|
|
final _prefs = Prefs.instance;
|
|
|
|
|
2023-09-28 20:16:48 +00:00
|
|
|
// setting values on this should notify any listeners (the GUI)
|
|
|
|
FusionProgressUIState? _uiState;
|
|
|
|
FusionProgressUIState get uiState {
|
|
|
|
if (_uiState == null) {
|
|
|
|
throw Exception("FusionProgressUIState has not been set for $_walletId");
|
|
|
|
}
|
|
|
|
return _uiState!;
|
|
|
|
}
|
|
|
|
|
|
|
|
set uiState(FusionProgressUIState state) {
|
|
|
|
if (_uiState != null) {
|
|
|
|
throw Exception("FusionProgressUIState was already set for $_walletId");
|
|
|
|
}
|
|
|
|
_uiState = state;
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Passed in wallet functions.
|
2023-10-13 18:17:59 +00:00
|
|
|
late final Future<List<Address>> Function({int numberOfAddresses})
|
|
|
|
_getNextUnusedChangeAddresses;
|
2023-11-14 20:31:53 +00:00
|
|
|
late final CachedElectrumXClient Function() _getWalletCachedElectrumX;
|
2023-09-28 16:05:06 +00:00
|
|
|
late final Future<int> Function() _getChainHeight;
|
2023-10-18 18:44:05 +00:00
|
|
|
late final Future<void> Function() _updateWalletUTXOS;
|
2023-10-25 16:45:30 +00:00
|
|
|
late final String Function(String bchAddress, btcdart.NetworkType network)
|
|
|
|
_convertToScriptHash;
|
2023-09-22 21:59:07 +00:00
|
|
|
|
2023-10-18 03:16:50 +00:00
|
|
|
// Fusion object.
|
|
|
|
fusion.Fusion? _mainFusionObject;
|
2023-10-18 18:44:05 +00:00
|
|
|
bool _stopRequested = false;
|
2023-10-19 17:06:26 +00:00
|
|
|
|
|
|
|
/// An int storing the number of successfully completed fusion rounds.
|
|
|
|
int _completedFuseCount = 0;
|
|
|
|
|
|
|
|
/// An int storing the number of failed fusion rounds.
|
|
|
|
int _failedFuseCount = 0;
|
|
|
|
|
|
|
|
/// The maximum number of consecutive failed fusion rounds before stopping.
|
|
|
|
int get maxFailedFuseCount => 5;
|
2023-10-18 03:16:50 +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.
|
|
|
|
Future<void> initFusionInterface({
|
2023-07-26 22:06:02 +00:00
|
|
|
required String walletId,
|
|
|
|
required Coin coin,
|
|
|
|
required MainDB db,
|
2023-10-13 18:17:59 +00:00
|
|
|
required Future<List<Address>> Function({int numberOfAddresses})
|
|
|
|
getNextUnusedChangeAddress,
|
2023-11-14 20:31:53 +00:00
|
|
|
required CachedElectrumXClient Function() getWalletCachedElectrumX,
|
2023-09-28 16:05:06 +00:00
|
|
|
required Future<int> Function() getChainHeight,
|
2023-10-18 18:44:05 +00:00
|
|
|
required Future<void> Function() updateWalletUTXOS,
|
2023-10-09 21:42:42 +00:00
|
|
|
required Future<String?> mnemonic,
|
|
|
|
required Future<String?> mnemonicPassphrase,
|
|
|
|
required btcdart.NetworkType network,
|
2023-10-25 16:45:30 +00:00
|
|
|
required final String Function(
|
|
|
|
String bchAddress, btcdart.NetworkType network)
|
|
|
|
convertToScriptHash,
|
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-10-13 18:17:59 +00:00
|
|
|
_getNextUnusedChangeAddresses = getNextUnusedChangeAddress;
|
2023-09-22 21:44:40 +00:00
|
|
|
_torService = FusionTorService.sharedInstance;
|
2023-09-22 21:59:07 +00:00
|
|
|
_getWalletCachedElectrumX = getWalletCachedElectrumX;
|
2023-09-28 16:05:06 +00:00
|
|
|
_getChainHeight = getChainHeight;
|
2023-10-18 18:44:05 +00:00
|
|
|
_updateWalletUTXOS = updateWalletUTXOS;
|
2023-10-09 21:42:42 +00:00
|
|
|
_mnemonic = mnemonic;
|
|
|
|
_mnemonicPassphrase = mnemonicPassphrase;
|
|
|
|
_network = network;
|
2023-10-25 16:45:30 +00:00
|
|
|
_convertToScriptHash = convertToScriptHash;
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 20:16:48 +00:00
|
|
|
// callback to update the ui state object
|
2023-10-17 05:00:54 +00:00
|
|
|
void _updateStatus({required fusion.FusionStatus status, String? info}) {
|
|
|
|
switch (status) {
|
2023-10-13 18:14:16 +00:00
|
|
|
case fusion.FusionStatus.connecting:
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setConnecting(
|
|
|
|
CashFusionState(status: CashFusionStatus.running, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setOutputs(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setPeers(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setFusing(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setComplete(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: true);
|
2023-10-12 22:05:17 +00:00
|
|
|
break;
|
2023-10-13 18:14:16 +00:00
|
|
|
case fusion.FusionStatus.setup:
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setConnecting(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setOutputs(
|
|
|
|
CashFusionState(status: CashFusionStatus.running, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setPeers(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setFusing(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setComplete(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: true);
|
2023-10-12 22:05:17 +00:00
|
|
|
break;
|
2023-10-13 18:14:16 +00:00
|
|
|
case fusion.FusionStatus.waiting:
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setConnecting(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setOutputs(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setPeers(
|
|
|
|
CashFusionState(status: CashFusionStatus.running, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setFusing(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setComplete(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: true);
|
2023-10-12 22:05:17 +00:00
|
|
|
break;
|
|
|
|
case fusion.FusionStatus.running:
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setConnecting(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setOutputs(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setPeers(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setFusing(
|
|
|
|
CashFusionState(status: CashFusionStatus.running, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setComplete(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: null),
|
|
|
|
shouldNotify: true);
|
2023-10-12 22:05:17 +00:00
|
|
|
break;
|
|
|
|
case fusion.FusionStatus.complete:
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setConnecting(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setOutputs(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setPeers(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setFusing(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setComplete(
|
|
|
|
CashFusionState(status: CashFusionStatus.success, info: null),
|
|
|
|
shouldNotify: true);
|
2023-10-12 22:05:17 +00:00
|
|
|
break;
|
|
|
|
case fusion.FusionStatus.failed:
|
2023-10-17 05:00:54 +00:00
|
|
|
failCurrentUiState(info);
|
2023-10-12 22:05:17 +00:00
|
|
|
break;
|
|
|
|
case fusion.FusionStatus.exception:
|
2023-10-17 05:00:54 +00:00
|
|
|
failCurrentUiState(info);
|
2023-10-12 22:05:17 +00:00
|
|
|
break;
|
2023-10-13 18:00:35 +00:00
|
|
|
case fusion.FusionStatus.reset:
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setConnecting(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: info),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setOutputs(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: info),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setPeers(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: info),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setFusing(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: info),
|
|
|
|
shouldNotify: false);
|
|
|
|
_uiState?.setComplete(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: info),
|
|
|
|
shouldNotify: false);
|
|
|
|
|
|
|
|
_uiState?.setFusionState(
|
|
|
|
CashFusionState(status: CashFusionStatus.waiting, info: info),
|
|
|
|
shouldNotify: false);
|
|
|
|
|
|
|
|
_uiState?.setFailed(false, shouldNotify: true);
|
2023-10-13 18:00:35 +00:00
|
|
|
break;
|
2023-10-12 22:05:17 +00:00
|
|
|
}
|
2023-09-28 20:16:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 05:00:54 +00:00
|
|
|
void failCurrentUiState(String? info) {
|
2023-10-13 18:29:04 +00:00
|
|
|
// Check each _uiState value to see if it is running. If so, set it to failed.
|
2023-10-17 16:15:31 +00:00
|
|
|
if (_uiState?.connecting.status == CashFusionStatus.running) {
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setConnecting(
|
|
|
|
CashFusionState(status: CashFusionStatus.failed, info: info),
|
|
|
|
shouldNotify: true);
|
2023-10-17 16:19:12 +00:00
|
|
|
return;
|
2023-10-13 18:29:04 +00:00
|
|
|
}
|
2023-10-17 16:15:31 +00:00
|
|
|
if (_uiState?.outputs.status == CashFusionStatus.running) {
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setOutputs(
|
|
|
|
CashFusionState(status: CashFusionStatus.failed, info: info),
|
|
|
|
shouldNotify: true);
|
2023-10-17 16:19:12 +00:00
|
|
|
return;
|
2023-10-13 18:29:04 +00:00
|
|
|
}
|
2023-10-17 16:15:31 +00:00
|
|
|
if (_uiState?.peers.status == CashFusionStatus.running) {
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setPeers(
|
|
|
|
CashFusionState(status: CashFusionStatus.failed, info: info),
|
|
|
|
shouldNotify: true);
|
2023-10-17 16:19:12 +00:00
|
|
|
return;
|
2023-10-13 18:29:04 +00:00
|
|
|
}
|
2023-10-17 16:15:31 +00:00
|
|
|
if (_uiState?.fusing.status == CashFusionStatus.running) {
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setFusing(
|
|
|
|
CashFusionState(status: CashFusionStatus.failed, info: info),
|
|
|
|
shouldNotify: true);
|
2023-10-17 16:19:12 +00:00
|
|
|
return;
|
2023-10-17 05:00:54 +00:00
|
|
|
}
|
2023-10-17 16:15:31 +00:00
|
|
|
if (_uiState?.complete.status == CashFusionStatus.running) {
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setComplete(
|
|
|
|
CashFusionState(status: CashFusionStatus.failed, info: info),
|
|
|
|
shouldNotify: true);
|
2023-10-17 16:19:12 +00:00
|
|
|
return;
|
2023-10-13 18:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all transactions in the wallet for the given address.
|
2023-10-13 17:17:11 +00:00
|
|
|
Future<List<Map<String, dynamic>>> _getTransactionsByAddress(
|
2023-10-09 16:55:06 +00:00
|
|
|
String address,
|
|
|
|
) async {
|
|
|
|
final txidList =
|
|
|
|
await _db.getTransactions(_walletId).txidProperty().findAll();
|
|
|
|
|
|
|
|
final futures = txidList.map(
|
|
|
|
(e) => _getWalletCachedElectrumX().getTransaction(
|
|
|
|
txHash: e,
|
|
|
|
coin: _coin,
|
2023-09-22 18:50:59 +00:00
|
|
|
),
|
|
|
|
);
|
2023-09-19 22:58:55 +00:00
|
|
|
|
2023-10-09 16:55:06 +00:00
|
|
|
return await Future.wait(futures);
|
2023-08-30 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 17:17:11 +00:00
|
|
|
Future<Uint8List> _getPrivateKeyForPubKey(List<int> pubKey) async {
|
2023-10-09 21:42:42 +00:00
|
|
|
// can't directly query for equal lists in isar so we need to fetch
|
|
|
|
// all addresses then search in dart
|
|
|
|
try {
|
2023-10-13 17:17:11 +00:00
|
|
|
final derivationPath = (await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.typeEqualTo(AddressType.p2pkh)
|
|
|
|
.and()
|
|
|
|
.derivationPathIsNotNull()
|
|
|
|
.and()
|
|
|
|
.group((q) => q
|
|
|
|
.subTypeEqualTo(AddressSubType.receiving)
|
|
|
|
.or()
|
|
|
|
.subTypeEqualTo(AddressSubType.change))
|
|
|
|
.findAll())
|
2023-10-09 21:42:42 +00:00
|
|
|
.firstWhere((e) => e.publicKey.toString() == pubKey.toString())
|
|
|
|
.derivationPath!
|
|
|
|
.value;
|
|
|
|
|
|
|
|
final node = await Bip32Utils.getBip32Node(
|
|
|
|
(await _mnemonic)!,
|
|
|
|
(await _mnemonicPassphrase)!,
|
|
|
|
_network,
|
|
|
|
derivationPath,
|
|
|
|
);
|
|
|
|
|
|
|
|
return node.privateKey!;
|
2023-10-13 22:05:26 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("$e\n$s", level: LogLevel.Fatal);
|
2023-10-09 21:42:42 +00:00
|
|
|
throw Exception("Derivation path for pubkey=$pubKey could not be found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 18:17:59 +00:00
|
|
|
/// Reserve an address for fusion.
|
2023-10-26 22:22:30 +00:00
|
|
|
Future<List<Address>> _reserveAddresses(Iterable<Address> addresses) async {
|
|
|
|
if (addresses.isEmpty) {
|
|
|
|
return [];
|
2023-08-30 16:40:39 +00:00
|
|
|
}
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-10-26 22:22:30 +00:00
|
|
|
final updatedAddresses = addresses
|
|
|
|
.map((e) => e.copyWith(otherData: kReservedFusionAddress))
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
await _db.isar.writeTxn(() async {
|
2023-10-26 22:44:50 +00:00
|
|
|
for (final newAddress in updatedAddresses) {
|
|
|
|
final oldAddress = await _db.getAddress(
|
|
|
|
newAddress.walletId,
|
|
|
|
newAddress.value,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (oldAddress != null) {
|
|
|
|
newAddress.id = oldAddress.id;
|
|
|
|
await _db.isar.addresses.delete(oldAddress.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
await _db.isar.addresses.put(newAddress);
|
|
|
|
}
|
2023-10-26 22:22:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return updatedAddresses;
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 18:17:59 +00:00
|
|
|
/// un reserve a fusion reserved address.
|
|
|
|
/// If [address] is not reserved nothing happens
|
|
|
|
Future<Address> _unReserveAddress(Address address) async {
|
|
|
|
if (address.otherData != kReservedFusionAddress) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
final updated = address.copyWith(otherData: null);
|
|
|
|
|
|
|
|
// Make sure the address is updated in the database.
|
|
|
|
await _db.updateAddress(address, updated);
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
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-10-13 17:17:11 +00:00
|
|
|
Future<List<fusion.Address>> _getUnusedReservedChangeAddresses(
|
2023-08-07 18:54:44 +00:00
|
|
|
int numberOfAddresses,
|
|
|
|
) async {
|
2023-10-13 18:17:59 +00:00
|
|
|
final unusedChangeAddresses = await _getNextUnusedChangeAddresses(
|
|
|
|
numberOfAddresses: numberOfAddresses,
|
|
|
|
);
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Initialize a list of unused reserved change addresses.
|
2023-10-26 22:22:30 +00:00
|
|
|
final List<Address> unusedReservedAddresses = unusedChangeAddresses
|
|
|
|
.where((e) => e.otherData == kReservedFusionAddress)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
unusedReservedAddresses.addAll(await _reserveAddresses(
|
|
|
|
unusedChangeAddresses.where((e) => e.otherData == null)));
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Return the list of unused reserved change addresses.
|
2023-10-13 18:17:59 +00:00
|
|
|
return unusedReservedAddresses
|
|
|
|
.map(
|
|
|
|
(e) => fusion.Address(
|
|
|
|
address: e.value,
|
|
|
|
publicKey: e.publicKey,
|
|
|
|
fusionReserved: true,
|
|
|
|
derivationPath: fusion.DerivationPath(
|
|
|
|
e.derivationPath!.value,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList();
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 21:44:40 +00:00
|
|
|
int _torStartCount = 0;
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns the current Tor proxy address.
|
2023-10-13 17:17:11 +00:00
|
|
|
Future<({InternetAddress host, int port})> _getSocksProxyAddress() async {
|
2023-09-22 21:44:40 +00:00
|
|
|
if (_torStartCount > 5) {
|
|
|
|
// something is quite broken so stop trying to recursively fetch
|
|
|
|
// start up tor and fetch proxy info
|
|
|
|
throw Exception(
|
|
|
|
"Fusion interface attempted to start tor $_torStartCount times and failed!",
|
|
|
|
);
|
2023-09-14 22:28:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 21:44:40 +00:00
|
|
|
try {
|
|
|
|
final info = _torService.getProxyInfo();
|
2023-09-14 22:28:11 +00:00
|
|
|
|
2023-09-22 21:44:40 +00:00
|
|
|
// reset counter before return info;
|
|
|
|
_torStartCount = 0;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
} catch (_) {
|
|
|
|
// tor is probably not running so lets fix that
|
|
|
|
final torDir = await StackFileSystem.applicationTorDirectory();
|
|
|
|
_torService.init(torDataDirPath: torDir.path);
|
|
|
|
|
|
|
|
// increment start attempt count
|
|
|
|
_torStartCount++;
|
|
|
|
|
|
|
|
await _torService.start();
|
|
|
|
|
|
|
|
// try again to fetch proxy info
|
2023-10-13 17:17:11 +00:00
|
|
|
return await _getSocksProxyAddress();
|
2023-09-22 21:44:40 +00:00
|
|
|
}
|
2023-09-14 22:28:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 16:45:30 +00:00
|
|
|
Future<bool> _checkUtxoExists(
|
|
|
|
String address,
|
|
|
|
String prevTxid,
|
|
|
|
int prevIndex,
|
|
|
|
) async {
|
|
|
|
final scriptHash = _convertToScriptHash(address, _network);
|
|
|
|
|
|
|
|
final utxos = await _getWalletCachedElectrumX()
|
|
|
|
.electrumXClient
|
|
|
|
.getUTXOs(scripthash: scriptHash);
|
|
|
|
|
|
|
|
for (final utxo in utxos) {
|
|
|
|
if (utxo["tx_hash"] == prevTxid && utxo["tx_pos"] == prevIndex) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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.
|
2023-10-16 21:04:27 +00:00
|
|
|
Future<void> fuse({
|
|
|
|
required FusionInfo fusionInfo,
|
|
|
|
}) async {
|
2023-07-26 22:06:02 +00:00
|
|
|
// Initial attempt for CashFusion integration goes here.
|
2023-10-16 19:46:45 +00:00
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
try {
|
|
|
|
_updateStatus(status: fusion.FusionStatus.reset);
|
|
|
|
_updateStatus(
|
|
|
|
status: fusion.FusionStatus.connecting,
|
|
|
|
info: "Connecting to the CashFusion server.",
|
|
|
|
);
|
2023-10-16 21:04:27 +00:00
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
// Use server host and port which ultimately come from text fields.
|
|
|
|
fusion.FusionParams serverParams = fusion.FusionParams(
|
|
|
|
serverHost: fusionInfo.host,
|
|
|
|
serverPort: fusionInfo.port,
|
|
|
|
serverSsl: fusionInfo.ssl,
|
2023-10-25 14:46:00 +00:00
|
|
|
genesisHashHex:
|
|
|
|
_coin.isTestNet ? GENESIS_HASH_TESTNET : GENESIS_HASH_MAINNET,
|
2023-10-26 15:54:20 +00:00
|
|
|
enableDebugPrint: kDebugMode,
|
2023-10-26 17:29:06 +00:00
|
|
|
torForOvert: _prefs.useTor,
|
2023-10-26 15:30:12 +00:00
|
|
|
mode: fusion.FusionMode.normal,
|
2023-10-20 18:27:17 +00:00
|
|
|
);
|
2023-08-30 16:40:39 +00:00
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
// Instantiate a Fusion object with custom parameters.
|
|
|
|
_mainFusionObject = fusion.Fusion(serverParams);
|
|
|
|
|
|
|
|
// Pass wallet functions to the Fusion object
|
|
|
|
await _mainFusionObject!.initFusion(
|
|
|
|
getTransactionsByAddress: _getTransactionsByAddress,
|
|
|
|
getUnusedReservedChangeAddresses: _getUnusedReservedChangeAddresses,
|
|
|
|
getSocksProxyAddress: _getSocksProxyAddress,
|
|
|
|
getChainHeight: _getChainHeight,
|
|
|
|
updateStatusCallback: _updateStatus,
|
2023-10-25 16:45:30 +00:00
|
|
|
checkUtxoExists: _checkUtxoExists,
|
2023-10-20 18:27:17 +00:00
|
|
|
getTransactionJson: (String txid) async =>
|
|
|
|
await _getWalletCachedElectrumX().getTransaction(
|
|
|
|
coin: _coin,
|
|
|
|
txHash: txid,
|
|
|
|
),
|
|
|
|
getPrivateKeyForPubKey: _getPrivateKeyForPubKey,
|
|
|
|
broadcastTransaction: (String txHex) => _getWalletCachedElectrumX()
|
|
|
|
.electrumXClient
|
|
|
|
.broadcastTransaction(rawTx: txHex),
|
|
|
|
unReserveAddresses: (List<fusion.Address> addresses) async {
|
|
|
|
final List<Future<void>> futures = [];
|
|
|
|
for (final addr in addresses) {
|
|
|
|
futures.add(
|
|
|
|
_db.getAddress(_walletId, addr.address).then(
|
|
|
|
(address) async {
|
|
|
|
if (address == null) {
|
|
|
|
// matching address not found in db so cannot mark as unreserved
|
|
|
|
// just ignore I guess. Should never actually happen in practice.
|
|
|
|
// Might be useful check in debugging cases?
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
await _unReserveAddress(address);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await Future.wait(futures);
|
|
|
|
},
|
|
|
|
);
|
2023-10-18 21:09:01 +00:00
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
// Reset internal and UI counts and flag.
|
|
|
|
_completedFuseCount = 0;
|
|
|
|
_uiState?.fusionRoundsCompleted = 0;
|
|
|
|
_failedFuseCount = 0;
|
|
|
|
_uiState?.fusionRoundsFailed = 0;
|
|
|
|
_stopRequested = false;
|
|
|
|
|
|
|
|
bool shouldFuzeAgain() {
|
|
|
|
if (fusionInfo.rounds <= 0) {
|
|
|
|
// ignore count if continuous
|
|
|
|
return !_stopRequested;
|
|
|
|
} else {
|
|
|
|
// not continuous
|
|
|
|
// check to make sure we aren't doing more fusions than requested
|
|
|
|
return !_stopRequested && _completedFuseCount < fusionInfo.rounds;
|
|
|
|
}
|
2023-10-19 23:10:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
while (shouldFuzeAgain()) {
|
|
|
|
if (_completedFuseCount > 0 || _failedFuseCount > 0) {
|
|
|
|
_updateStatus(status: fusion.FusionStatus.reset);
|
|
|
|
_updateStatus(
|
|
|
|
status: fusion.FusionStatus.connecting,
|
|
|
|
info: "Connecting to the CashFusion server.",
|
|
|
|
);
|
2023-10-18 18:44:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
// refresh wallet utxos
|
|
|
|
await _updateWalletUTXOS();
|
|
|
|
|
|
|
|
// Add unfrozen stack UTXOs.
|
|
|
|
final List<UTXO> walletUtxos = await _db
|
|
|
|
.getUTXOs(_walletId)
|
2023-10-18 18:44:05 +00:00
|
|
|
.filter()
|
2023-10-20 18:27:17 +00:00
|
|
|
.isBlockedEqualTo(false)
|
2023-10-18 18:44:05 +00:00
|
|
|
.and()
|
2023-10-20 18:27:17 +00:00
|
|
|
.addressIsNotNull()
|
|
|
|
.findAll();
|
|
|
|
|
|
|
|
final List<fusion.UtxoDTO> coinList = [];
|
|
|
|
// Loop through UTXOs, checking and adding valid ones.
|
|
|
|
for (final utxo in walletUtxos) {
|
|
|
|
final String addressString = utxo.address!;
|
|
|
|
final List<String> possibleAddresses = [addressString];
|
|
|
|
|
|
|
|
if (bitbox.Address.detectFormat(addressString) ==
|
|
|
|
bitbox.Address.formatCashAddr) {
|
|
|
|
possibleAddresses
|
|
|
|
.add(bitbox.Address.toLegacyAddress(addressString));
|
|
|
|
} else {
|
|
|
|
possibleAddresses.add(bitbox.Address.toCashAddress(addressString));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch address to get pubkey
|
|
|
|
final addr = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.anyOf<String,
|
|
|
|
QueryBuilder<Address, Address, QAfterFilterCondition>>(
|
|
|
|
possibleAddresses, (q, e) => q.valueEqualTo(e))
|
|
|
|
.and()
|
|
|
|
.group((q) => q
|
|
|
|
.subTypeEqualTo(AddressSubType.change)
|
|
|
|
.or()
|
|
|
|
.subTypeEqualTo(AddressSubType.receiving))
|
|
|
|
.and()
|
|
|
|
.typeEqualTo(AddressType.p2pkh)
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
// depending on the address type in the query above this can be null
|
|
|
|
if (addr == null) {
|
|
|
|
// A utxo object should always have a non null address.
|
|
|
|
// If non found then just ignore the UTXO (aka don't fuse it)
|
|
|
|
Logging.instance.log(
|
|
|
|
"Ignoring utxo=$utxo for address=\"$addressString\" while selecting UTXOs for Fusion",
|
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final dto = fusion.UtxoDTO(
|
|
|
|
txid: utxo.txid,
|
|
|
|
vout: utxo.vout,
|
|
|
|
value: utxo.value,
|
|
|
|
address: utxo.address!,
|
|
|
|
pubKey: addr.publicKey,
|
2023-10-18 18:44:05 +00:00
|
|
|
);
|
2023-10-20 18:27:17 +00:00
|
|
|
|
|
|
|
// Add UTXO to coinList.
|
|
|
|
coinList.add(dto);
|
2023-10-18 18:44:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
// Fuse UTXOs.
|
|
|
|
try {
|
|
|
|
await _mainFusionObject!.fuse(
|
|
|
|
inputsFromWallet: coinList,
|
|
|
|
network: _coin.isTestNet
|
|
|
|
? fusion.Utilities.testNet
|
|
|
|
: fusion.Utilities.mainNet,
|
|
|
|
);
|
2023-10-18 18:44:05 +00:00
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
// Increment the number of successfully completed fusion rounds.
|
|
|
|
_completedFuseCount++;
|
2023-10-12 19:32:01 +00:00
|
|
|
|
2023-10-20 18:27:17 +00:00
|
|
|
// Do the same for the UI state. This also resets the failed count (for
|
|
|
|
// the UI state only).
|
|
|
|
_uiState?.incrementFusionRoundsCompleted();
|
|
|
|
|
|
|
|
// Also reset the failed count here.
|
|
|
|
_failedFuseCount = 0;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"$e\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
// just continue on attempt failure
|
|
|
|
|
|
|
|
// Increment the number of failed fusion rounds.
|
|
|
|
_failedFuseCount++;
|
|
|
|
|
|
|
|
// Do the same for the UI state.
|
|
|
|
_uiState?.incrementFusionRoundsFailed();
|
|
|
|
|
|
|
|
// If we fail too many times in a row, stop trying.
|
|
|
|
if (_failedFuseCount >= maxFailedFuseCount) {
|
|
|
|
_updateStatus(
|
|
|
|
status: fusion.FusionStatus.failed,
|
|
|
|
info: "Failed $maxFailedFuseCount times in a row, stopping.");
|
|
|
|
_stopRequested = true;
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setFailed(true, shouldNotify: true);
|
2023-10-20 18:27:17 +00:00
|
|
|
}
|
2023-10-19 17:06:26 +00:00
|
|
|
}
|
2023-10-18 19:10:19 +00:00
|
|
|
}
|
2023-10-20 18:27:17 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"$e\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Stop the fusion process and update the UI state.
|
|
|
|
await _mainFusionObject?.stop();
|
|
|
|
_mainFusionObject = null;
|
2023-10-27 16:43:42 +00:00
|
|
|
_uiState?.setRunning(false, shouldNotify: true);
|
2023-09-19 22:58:55 +00:00
|
|
|
}
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
2023-10-18 03:16:50 +00:00
|
|
|
|
|
|
|
/// Stop the fusion process.
|
|
|
|
///
|
|
|
|
/// This function is called when the user taps the "Cancel" button in the UI
|
|
|
|
/// or closes the fusion progress dialog.
|
2023-10-18 15:02:25 +00:00
|
|
|
Future<void> stop() async {
|
2023-10-18 18:44:05 +00:00
|
|
|
_stopRequested = true;
|
2023-10-18 15:02:25 +00:00
|
|
|
await _mainFusionObject?.stop();
|
2023-10-18 03:16:50 +00:00
|
|
|
}
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|