mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 01:37:54 +00:00
Untested ecash fusion port. Manual port of https://github.com/cypherstack/stack_wallet/pull/705 combined with manual port to v2 transactions for ecash as well as a couple other changes ported from the wallets_refactor branch
This commit is contained in:
parent
747565fa16
commit
9a9c9550ee
10 changed files with 426 additions and 234 deletions
|
@ -61,10 +61,11 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
|
|||
FusionOption _option = FusionOption.continuous;
|
||||
|
||||
Future<void> _startFusion() async {
|
||||
final fusionWallet = ref
|
||||
final wallet = ref
|
||||
.read(walletsChangeNotifierProvider)
|
||||
.getManager(widget.walletId)
|
||||
.wallet as FusionWalletInterface;
|
||||
.wallet;
|
||||
final fusionWallet = wallet as FusionWalletInterface;
|
||||
|
||||
try {
|
||||
fusionWallet.uiState = ref.read(
|
||||
|
@ -89,7 +90,9 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
|
|||
);
|
||||
|
||||
// update user prefs (persistent)
|
||||
ref.read(prefsChangeNotifierProvider).fusionServerInfo = newInfo;
|
||||
ref
|
||||
.read(prefsChangeNotifierProvider)
|
||||
.setFusionServerInfo(wallet.coin, newInfo);
|
||||
|
||||
unawaited(
|
||||
fusionWallet.fuse(
|
||||
|
@ -113,7 +116,11 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
|
|||
portFocusNode = FocusNode();
|
||||
fusionRoundFocusNode = FocusNode();
|
||||
|
||||
final info = ref.read(prefsChangeNotifierProvider).fusionServerInfo;
|
||||
final info = ref.read(prefsChangeNotifierProvider).getFusionServerInfo(ref
|
||||
.read(walletsChangeNotifierProvider)
|
||||
.getManager(widget.walletId)
|
||||
.wallet
|
||||
.coin);
|
||||
serverController.text = info.host;
|
||||
portController.text = info.port.toString();
|
||||
_enableSSLCheckbox = info.ssl;
|
||||
|
@ -150,7 +157,7 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
|
|||
automaticallyImplyLeading: false,
|
||||
leading: const AppBarBackButton(),
|
||||
title: Text(
|
||||
"CashFusion",
|
||||
"Fusion",
|
||||
style: STextStyles.navBarTitle(context),
|
||||
),
|
||||
titleSpacing: 0,
|
||||
|
@ -189,7 +196,7 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
|
|||
children: [
|
||||
RoundedWhiteContainer(
|
||||
child: Text(
|
||||
"CashFusion allows you to anonymize your BCH coins.",
|
||||
"Fusion helps anonymize your coins by mixing them.",
|
||||
style: STextStyles.w500_12(context).copyWith(
|
||||
color: Theme.of(context)
|
||||
.extension<StackColors>()!
|
||||
|
@ -214,7 +221,11 @@ class _CashFusionViewState extends ConsumerState<CashFusionView> {
|
|||
CustomTextButton(
|
||||
text: "Default",
|
||||
onTap: () {
|
||||
const def = FusionInfo.DEFAULTS;
|
||||
final def = kFusionServerInfoDefaults[ref
|
||||
.read(walletsChangeNotifierProvider)
|
||||
.getManager(widget.walletId)
|
||||
.wallet
|
||||
.coin]!;
|
||||
serverController.text = def.host;
|
||||
portController.text = def.port.toString();
|
||||
fusionRoundController.text =
|
||||
|
|
|
@ -18,6 +18,7 @@ import 'package:stackwallet/providers/global/prefs_provider.dart';
|
|||
import 'package:stackwallet/providers/global/wallets_provider.dart';
|
||||
import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart';
|
||||
import 'package:stackwallet/themes/stack_colors.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
import 'package:stackwallet/utilities/show_loading.dart';
|
||||
import 'package:stackwallet/utilities/text_styles.dart';
|
||||
import 'package:stackwallet/utilities/util.dart';
|
||||
|
@ -43,6 +44,8 @@ class FusionProgressView extends ConsumerStatefulWidget {
|
|||
}
|
||||
|
||||
class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
|
||||
late final Coin coin;
|
||||
|
||||
Future<bool> _requestAndProcessCancel() async {
|
||||
final shouldCancel = await showDialog<bool?>(
|
||||
context: context,
|
||||
|
@ -88,6 +91,16 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
|
|||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
coin = ref
|
||||
.read(walletsChangeNotifierProvider)
|
||||
.getManager(widget.walletId)
|
||||
.wallet
|
||||
.coin;
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final bool _succeeded =
|
||||
|
@ -230,7 +243,8 @@ class _FusionProgressViewState extends ConsumerState<FusionProgressView> {
|
|||
.getManager(widget.walletId)
|
||||
.wallet as FusionWalletInterface;
|
||||
|
||||
final fusionInfo = ref.read(prefsChangeNotifierProvider).fusionServerInfo;
|
||||
final fusionInfo =
|
||||
ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin);
|
||||
|
||||
try {
|
||||
fusionWallet.uiState = ref.read(
|
||||
|
|
|
@ -845,7 +845,8 @@ class _WalletViewState extends ConsumerState<WalletView> {
|
|||
onTap: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
coin == Coin.bitcoincash ||
|
||||
coin == Coin.bitcoincashTestnet
|
||||
coin == Coin.bitcoincashTestnet ||
|
||||
coin == Coin.eCash
|
||||
? AllTransactionsV2View.routeName
|
||||
: AllTransactionsView.routeName,
|
||||
arguments: walletId,
|
||||
|
@ -902,7 +903,9 @@ class _WalletViewState extends ConsumerState<WalletView> {
|
|||
children: [
|
||||
Expanded(
|
||||
child: coin == Coin.bitcoincash ||
|
||||
coin == Coin.bitcoincashTestnet
|
||||
coin ==
|
||||
Coin.bitcoincashTestnet ||
|
||||
coin == Coin.eCash
|
||||
? TransactionsV2List(
|
||||
walletId: widget.walletId,
|
||||
)
|
||||
|
|
|
@ -26,6 +26,7 @@ import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart';
|
|||
import 'package:stackwallet/themes/stack_colors.dart';
|
||||
import 'package:stackwallet/utilities/assets.dart';
|
||||
import 'package:stackwallet/utilities/constants.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
import 'package:stackwallet/utilities/text_styles.dart';
|
||||
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
|
||||
import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart';
|
||||
|
@ -58,6 +59,7 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
late final FocusNode portFocusNode;
|
||||
late final TextEditingController fusionRoundController;
|
||||
late final FocusNode fusionRoundFocusNode;
|
||||
late final Coin coin;
|
||||
|
||||
bool _enableStartButton = false;
|
||||
bool _enableSSLCheckbox = false;
|
||||
|
@ -93,7 +95,7 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
);
|
||||
|
||||
// update user prefs (persistent)
|
||||
ref.read(prefsChangeNotifierProvider).fusionServerInfo = newInfo;
|
||||
ref.read(prefsChangeNotifierProvider).setFusionServerInfo(coin, newInfo);
|
||||
|
||||
unawaited(
|
||||
fusionWallet.fuse(
|
||||
|
@ -121,8 +123,14 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
serverFocusNode = FocusNode();
|
||||
portFocusNode = FocusNode();
|
||||
fusionRoundFocusNode = FocusNode();
|
||||
coin = ref
|
||||
.read(walletsChangeNotifierProvider)
|
||||
.getManager(widget.walletId)
|
||||
.wallet
|
||||
.coin;
|
||||
|
||||
final info = ref.read(prefsChangeNotifierProvider).fusionServerInfo;
|
||||
final info =
|
||||
ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin);
|
||||
serverController.text = info.host;
|
||||
portController.text = info.port.toString();
|
||||
_enableSSLCheckbox = info.ssl;
|
||||
|
@ -197,7 +205,7 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
width: 12,
|
||||
),
|
||||
Text(
|
||||
"CashFusion",
|
||||
"Fusion",
|
||||
style: STextStyles.desktopH3(context),
|
||||
),
|
||||
],
|
||||
|
@ -219,7 +227,7 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
),
|
||||
RichText(
|
||||
text: TextSpan(
|
||||
text: "What is CashFusion?",
|
||||
text: "What is Fusion?",
|
||||
style: STextStyles.richLink(context).copyWith(
|
||||
fontSize: 16,
|
||||
),
|
||||
|
@ -248,7 +256,7 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
"What is CashFusion?",
|
||||
"What is Fusion?",
|
||||
style: STextStyles.desktopH2(
|
||||
context),
|
||||
),
|
||||
|
@ -308,7 +316,7 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
"CashFusion allows you to anonymize your BCH coins.",
|
||||
"Fusion helps anonymize your coins by mixing them.",
|
||||
style:
|
||||
STextStyles.desktopTextExtraExtraSmall(context),
|
||||
),
|
||||
|
@ -336,7 +344,11 @@ class _DesktopCashFusion extends ConsumerState<DesktopCashFusionView> {
|
|||
CustomTextButton(
|
||||
text: "Default",
|
||||
onTap: () {
|
||||
const def = FusionInfo.DEFAULTS;
|
||||
final def = kFusionServerInfoDefaults[ref
|
||||
.read(walletsChangeNotifierProvider)
|
||||
.getManager(widget.walletId)
|
||||
.wallet
|
||||
.coin]!;
|
||||
serverController.text = def.host;
|
||||
portController.text = def.port.toString();
|
||||
fusionRoundController.text =
|
||||
|
|
|
@ -283,12 +283,14 @@ class _FusionDialogViewState extends ConsumerState<FusionDialogView> {
|
|||
|
||||
/// Fuse again.
|
||||
void _fuseAgain() async {
|
||||
final fusionWallet = ref
|
||||
final wallet = ref
|
||||
.read(walletsChangeNotifierProvider)
|
||||
.getManager(widget.walletId)
|
||||
.wallet as FusionWalletInterface;
|
||||
.wallet;
|
||||
final fusionWallet = wallet as FusionWalletInterface;
|
||||
|
||||
final fusionInfo = ref.read(prefsChangeNotifierProvider).fusionServerInfo;
|
||||
final fusionInfo =
|
||||
ref.read(prefsChangeNotifierProvider).getFusionServerInfo(wallet.coin);
|
||||
|
||||
try {
|
||||
fusionWallet.uiState = ref.read(
|
||||
|
|
|
@ -482,7 +482,8 @@ class _DesktopWalletViewState extends ConsumerState<DesktopWalletView> {
|
|||
} else {
|
||||
await Navigator.of(context).pushNamed(
|
||||
coin == Coin.bitcoincash ||
|
||||
coin == Coin.bitcoincashTestnet
|
||||
coin == Coin.bitcoincashTestnet ||
|
||||
coin == Coin.eCash
|
||||
? AllTransactionsV2View.routeName
|
||||
: AllTransactionsView.routeName,
|
||||
arguments: widget.walletId,
|
||||
|
@ -520,7 +521,8 @@ class _DesktopWalletViewState extends ConsumerState<DesktopWalletView> {
|
|||
walletId: widget.walletId,
|
||||
)
|
||||
: coin == Coin.bitcoincash ||
|
||||
coin == Coin.bitcoincashTestnet
|
||||
coin == Coin.bitcoincashTestnet ||
|
||||
coin == Coin.eCash
|
||||
? TransactionsV2List(
|
||||
walletId: widget.walletId,
|
||||
)
|
||||
|
|
|
@ -125,8 +125,8 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
|
|||
),
|
||||
if (manager.hasFusionSupport)
|
||||
_MoreFeaturesItem(
|
||||
label: "CashFusion",
|
||||
detail: "Decentralized Bitcoin Cash mixing protocol",
|
||||
label: "Fusion",
|
||||
detail: "Decentralized mixing protocol",
|
||||
iconAsset: Assets.svg.cashFusion,
|
||||
onPressed: () => widget.onFusionPressed?.call(),
|
||||
),
|
||||
|
|
|
@ -26,9 +26,13 @@ import 'package:stackwallet/electrumx_rpc/cached_electrumx.dart';
|
|||
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
|
||||
import 'package:stackwallet/exceptions/electrumx/no_such_transaction.dart';
|
||||
import 'package:stackwallet/models/balance.dart';
|
||||
import 'package:stackwallet/models/isar/models/blockchain_data/v2/input_v2.dart';
|
||||
import 'package:stackwallet/models/isar/models/blockchain_data/v2/output_v2.dart';
|
||||
import 'package:stackwallet/models/isar/models/blockchain_data/v2/transaction_v2.dart';
|
||||
import 'package:stackwallet/models/isar/models/isar_models.dart' as isar_models;
|
||||
import 'package:stackwallet/models/paymint/fee_object_model.dart';
|
||||
import 'package:stackwallet/models/signing_data.dart';
|
||||
import 'package:stackwallet/services/coins/bitcoincash/bch_utils.dart';
|
||||
import 'package:stackwallet/services/coins/coin_service.dart';
|
||||
import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart';
|
||||
import 'package:stackwallet/services/event_bus/events/global/refresh_percent_changed_event.dart';
|
||||
|
@ -37,6 +41,7 @@ import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_
|
|||
import 'package:stackwallet/services/event_bus/global_event_bus.dart';
|
||||
import 'package:stackwallet/services/mixins/coin_control_interface.dart';
|
||||
import 'package:stackwallet/services/mixins/electrum_x_parsing.dart';
|
||||
import 'package:stackwallet/services/mixins/fusion_wallet_interface.dart';
|
||||
import 'package:stackwallet/services/mixins/wallet_cache.dart';
|
||||
import 'package:stackwallet/services/mixins/wallet_db.dart';
|
||||
import 'package:stackwallet/services/mixins/xpubable.dart';
|
||||
|
@ -50,6 +55,7 @@ import 'package:stackwallet/utilities/default_nodes.dart';
|
|||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
||||
import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
|
||||
import 'package:stackwallet/utilities/extensions/extensions.dart';
|
||||
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
||||
import 'package:stackwallet/utilities/logger.dart';
|
||||
import 'package:stackwallet/utilities/prefs.dart';
|
||||
|
@ -130,7 +136,12 @@ String constructDerivePath({
|
|||
}
|
||||
|
||||
class ECashWallet extends CoinServiceAPI
|
||||
with WalletCache, WalletDB, ElectrumXParsing, CoinControlInterface
|
||||
with
|
||||
WalletCache,
|
||||
WalletDB,
|
||||
ElectrumXParsing,
|
||||
CoinControlInterface,
|
||||
FusionWalletInterface
|
||||
implements XPubAble {
|
||||
ECashWallet({
|
||||
required String walletId,
|
||||
|
@ -162,6 +173,19 @@ class ECashWallet extends CoinServiceAPI
|
|||
await updateCachedBalance(_balance!);
|
||||
},
|
||||
);
|
||||
initFusionInterface(
|
||||
walletId: walletId,
|
||||
coin: coin,
|
||||
db: db,
|
||||
getWalletCachedElectrumX: () => cachedElectrumXClient,
|
||||
getNextUnusedChangeAddress: _getUnusedChangeAddresses,
|
||||
getChainHeight: () async => chainHeight,
|
||||
updateWalletUTXOS: _updateUTXOs,
|
||||
mnemonic: mnemonicString,
|
||||
mnemonicPassphrase: mnemonicPassphrase,
|
||||
network: _network,
|
||||
convertToScriptHash: _convertToScriptHash,
|
||||
);
|
||||
}
|
||||
|
||||
static const integrationTestFlag =
|
||||
|
@ -185,6 +209,81 @@ class ECashWallet extends CoinServiceAPI
|
|||
}
|
||||
}
|
||||
|
||||
Future<List<isar_models.Address>> _getUnusedChangeAddresses({
|
||||
int numberOfAddresses = 1,
|
||||
}) async {
|
||||
if (numberOfAddresses < 1) {
|
||||
throw ArgumentError.value(
|
||||
numberOfAddresses,
|
||||
"numberOfAddresses",
|
||||
"Must not be less than 1",
|
||||
);
|
||||
}
|
||||
|
||||
final changeAddresses = await db
|
||||
.getAddresses(walletId)
|
||||
.filter()
|
||||
.typeEqualTo(isar_models.AddressType.p2pkh)
|
||||
.subTypeEqualTo(isar_models.AddressSubType.change)
|
||||
.derivationPath((q) => q.not().valueStartsWith("m/44'/0'"))
|
||||
.sortByDerivationIndex()
|
||||
.findAll();
|
||||
|
||||
final List<isar_models.Address> unused = [];
|
||||
|
||||
for (final addr in changeAddresses) {
|
||||
if (await _isUnused(addr.value)) {
|
||||
unused.add(addr);
|
||||
if (unused.length == numberOfAddresses) {
|
||||
return unused;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if not returned by now, we need to create more addresses
|
||||
int countMissing = numberOfAddresses - unused.length;
|
||||
|
||||
int nextIndex =
|
||||
changeAddresses.isEmpty ? 0 : changeAddresses.last.derivationIndex + 1;
|
||||
|
||||
while (countMissing > 0) {
|
||||
// create a new address
|
||||
final address = await _generateAddressForChain(
|
||||
1,
|
||||
nextIndex,
|
||||
DerivePathTypeExt.primaryFor(coin),
|
||||
);
|
||||
nextIndex++;
|
||||
await db.updateOrPutAddresses([address]);
|
||||
|
||||
// check if it has been used before adding
|
||||
if (await _isUnused(address.value)) {
|
||||
unused.add(address);
|
||||
countMissing--;
|
||||
}
|
||||
}
|
||||
|
||||
return unused;
|
||||
}
|
||||
|
||||
Future<bool> _isUnused(String address) async {
|
||||
final txCountInDB = await db
|
||||
.getTransactions(_walletId)
|
||||
.filter()
|
||||
.address((q) => q.valueEqualTo(address))
|
||||
.count();
|
||||
if (txCountInDB == 0) {
|
||||
// double check via electrumx
|
||||
// _getTxCountForAddress can throw!
|
||||
// final count = await getTxCount(address: address);
|
||||
// if (count == 0) {
|
||||
return true;
|
||||
// }
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@override
|
||||
set isFavorite(bool markFavorite) {
|
||||
_isFavorite = markFavorite;
|
||||
|
@ -1160,6 +1259,8 @@ class ECashWallet extends CoinServiceAPI
|
|||
}
|
||||
}).toSet();
|
||||
|
||||
final allAddressesSet = {...receivingAddresses, ...changeAddresses};
|
||||
|
||||
final List<Map<String, dynamic>> allTxHashes =
|
||||
await _fetchHistory([...receivingAddresses, ...changeAddresses]);
|
||||
|
||||
|
@ -1194,207 +1295,168 @@ class ECashWallet extends CoinServiceAPI
|
|||
}
|
||||
}
|
||||
|
||||
final List<Tuple2<isar_models.Transaction, isar_models.Address?>> txns = [];
|
||||
final List<TransactionV2> txns = [];
|
||||
|
||||
for (final txData in allTransactions) {
|
||||
Set<String> inputAddresses = {};
|
||||
Set<String> outputAddresses = {};
|
||||
// set to true if any inputs were detected as owned by this wallet
|
||||
bool wasSentFromThisWallet = false;
|
||||
|
||||
Logging.instance.log(txData, level: LogLevel.Fatal);
|
||||
|
||||
Amount totalInputValue = Amount(
|
||||
rawValue: BigInt.from(0),
|
||||
fractionDigits: coin.decimals,
|
||||
);
|
||||
Amount totalOutputValue = Amount(
|
||||
rawValue: BigInt.from(0),
|
||||
fractionDigits: coin.decimals,
|
||||
);
|
||||
|
||||
Amount amountSentFromWallet = Amount(
|
||||
rawValue: BigInt.from(0),
|
||||
fractionDigits: coin.decimals,
|
||||
);
|
||||
Amount amountReceivedInWallet = Amount(
|
||||
rawValue: BigInt.from(0),
|
||||
fractionDigits: coin.decimals,
|
||||
);
|
||||
Amount changeAmount = Amount(
|
||||
rawValue: BigInt.from(0),
|
||||
fractionDigits: coin.decimals,
|
||||
);
|
||||
// set to true if any outputs were detected as owned by this wallet
|
||||
bool wasReceivedInThisWallet = false;
|
||||
BigInt amountReceivedInThisWallet = BigInt.zero;
|
||||
BigInt changeAmountReceivedInThisWallet = BigInt.zero;
|
||||
|
||||
// parse inputs
|
||||
for (final input in txData["vin"] as List) {
|
||||
final prevTxid = input["txid"] as String;
|
||||
final prevOut = input["vout"] as int;
|
||||
final List<InputV2> inputs = [];
|
||||
for (final jsonInput in txData["vin"] as List) {
|
||||
final map = Map<String, dynamic>.from(jsonInput as Map);
|
||||
|
||||
// fetch input tx to get address
|
||||
final inputTx = await cachedElectrumXClient.getTransaction(
|
||||
txHash: prevTxid,
|
||||
coin: coin,
|
||||
);
|
||||
final List<String> addresses = [];
|
||||
String valueStringSats = "0";
|
||||
OutpointV2? outpoint;
|
||||
|
||||
for (final output in inputTx["vout"] as List) {
|
||||
// check matching output
|
||||
if (prevOut == output["n"]) {
|
||||
// get value
|
||||
final value = Amount.fromDecimal(
|
||||
Decimal.parse(output["value"].toString()),
|
||||
fractionDigits: coin.decimals,
|
||||
);
|
||||
final coinbase = map["coinbase"] as String?;
|
||||
|
||||
// add value to total
|
||||
totalInputValue = totalInputValue + value;
|
||||
if (coinbase == null) {
|
||||
final txid = map["txid"] as String;
|
||||
final vout = map["vout"] as int;
|
||||
|
||||
// get input(prevOut) address
|
||||
final address =
|
||||
output["scriptPubKey"]?["addresses"]?[0] as String? ??
|
||||
output["scriptPubKey"]?["address"] as String?;
|
||||
|
||||
if (address != null) {
|
||||
inputAddresses.add(address);
|
||||
|
||||
// if input was from my wallet, add value to amount sent
|
||||
if (receivingAddresses.contains(address) ||
|
||||
changeAddresses.contains(address)) {
|
||||
amountSentFromWallet = amountSentFromWallet + value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parse outputs
|
||||
for (final output in txData["vout"] as List) {
|
||||
// get value
|
||||
final value = Amount.fromDecimal(
|
||||
Decimal.parse(output["value"].toString()),
|
||||
fractionDigits: coin.decimals,
|
||||
);
|
||||
|
||||
// add value to total
|
||||
totalOutputValue += value;
|
||||
|
||||
// get output address
|
||||
final address = output["scriptPubKey"]?["addresses"]?[0] as String? ??
|
||||
output["scriptPubKey"]?["address"] as String?;
|
||||
if (address != null) {
|
||||
outputAddresses.add(address);
|
||||
|
||||
// if output was to my wallet, add value to amount received
|
||||
if (receivingAddresses.contains(address)) {
|
||||
amountReceivedInWallet += value;
|
||||
} else if (changeAddresses.contains(address)) {
|
||||
changeAmount += value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final mySentFromAddresses = [
|
||||
...receivingAddresses.intersection(inputAddresses),
|
||||
...changeAddresses.intersection(inputAddresses)
|
||||
];
|
||||
final myReceivedOnAddresses =
|
||||
receivingAddresses.intersection(outputAddresses);
|
||||
final myChangeReceivedOnAddresses =
|
||||
changeAddresses.intersection(outputAddresses);
|
||||
|
||||
final fee = totalInputValue - totalOutputValue;
|
||||
|
||||
// this is the address initially used to fetch the txid
|
||||
isar_models.Address transactionAddress =
|
||||
txData["address"] as isar_models.Address;
|
||||
|
||||
isar_models.TransactionType type;
|
||||
Amount amount;
|
||||
if (mySentFromAddresses.isNotEmpty && myReceivedOnAddresses.isNotEmpty) {
|
||||
// tx is sent to self
|
||||
type = isar_models.TransactionType.sentToSelf;
|
||||
amount =
|
||||
amountSentFromWallet - amountReceivedInWallet - fee - changeAmount;
|
||||
} else if (mySentFromAddresses.isNotEmpty) {
|
||||
// outgoing tx
|
||||
type = isar_models.TransactionType.outgoing;
|
||||
amount = amountSentFromWallet - changeAmount - fee;
|
||||
final possible =
|
||||
outputAddresses.difference(myChangeReceivedOnAddresses).first;
|
||||
|
||||
if (transactionAddress.value != possible) {
|
||||
transactionAddress = isar_models.Address(
|
||||
walletId: walletId,
|
||||
value: possible,
|
||||
publicKey: [],
|
||||
type: isar_models.AddressType.nonWallet,
|
||||
derivationIndex: -1,
|
||||
derivationPath: null,
|
||||
subType: isar_models.AddressSubType.nonWallet,
|
||||
final inputTx = await cachedElectrumXClient.getTransaction(
|
||||
txHash: txid,
|
||||
coin: coin,
|
||||
);
|
||||
|
||||
final prevOutJson = Map<String, dynamic>.from(
|
||||
(inputTx["vout"] as List).firstWhere((e) => e["n"] == vout)
|
||||
as Map);
|
||||
|
||||
final prevOut = OutputV2.fromElectrumXJson(
|
||||
prevOutJson,
|
||||
decimalPlaces: coin.decimals,
|
||||
walletOwns: false, // doesn't matter here as this is not saved
|
||||
);
|
||||
|
||||
outpoint = OutpointV2.isarCantDoRequiredInDefaultConstructor(
|
||||
txid: txid,
|
||||
vout: vout,
|
||||
);
|
||||
valueStringSats = prevOut.valueStringSats;
|
||||
addresses.addAll(prevOut.addresses);
|
||||
}
|
||||
} else {
|
||||
// incoming tx
|
||||
type = isar_models.TransactionType.incoming;
|
||||
amount = amountReceivedInWallet;
|
||||
}
|
||||
|
||||
List<isar_models.Input> inputs = [];
|
||||
List<isar_models.Output> outputs = [];
|
||||
|
||||
for (final json in txData["vin"] as List) {
|
||||
bool isCoinBase = json['coinbase'] != null;
|
||||
final input = isar_models.Input(
|
||||
txid: json['txid'] as String,
|
||||
vout: json['vout'] as int? ?? -1,
|
||||
scriptSig: json['scriptSig']?['hex'] as String?,
|
||||
scriptSigAsm: json['scriptSig']?['asm'] as String?,
|
||||
isCoinbase: isCoinBase ? isCoinBase : json['is_coinbase'] as bool?,
|
||||
sequence: json['sequence'] as int?,
|
||||
innerRedeemScriptAsm: json['innerRedeemscriptAsm'] as String?,
|
||||
InputV2 input = InputV2.isarCantDoRequiredInDefaultConstructor(
|
||||
scriptSigHex: map["scriptSig"]?["hex"] as String?,
|
||||
sequence: map["sequence"] as int?,
|
||||
outpoint: outpoint,
|
||||
valueStringSats: valueStringSats,
|
||||
addresses: addresses,
|
||||
witness: map["witness"] as String?,
|
||||
coinbase: coinbase,
|
||||
innerRedeemScriptAsm: map["innerRedeemscriptAsm"] as String?,
|
||||
// don't know yet if wallet owns. Need addresses first
|
||||
walletOwns: false,
|
||||
);
|
||||
|
||||
if (allAddressesSet.intersection(input.addresses.toSet()).isNotEmpty) {
|
||||
wasSentFromThisWallet = true;
|
||||
input = input.copyWith(walletOwns: true);
|
||||
}
|
||||
|
||||
inputs.add(input);
|
||||
}
|
||||
|
||||
for (final json in txData["vout"] as List) {
|
||||
final output = isar_models.Output(
|
||||
scriptPubKey: json['scriptPubKey']?['hex'] as String?,
|
||||
scriptPubKeyAsm: json['scriptPubKey']?['asm'] as String?,
|
||||
scriptPubKeyType: json['scriptPubKey']?['type'] as String?,
|
||||
scriptPubKeyAddress:
|
||||
json["scriptPubKey"]?["addresses"]?[0] as String? ??
|
||||
json['scriptPubKey']['type'] as String,
|
||||
value: Amount.fromDecimal(
|
||||
Decimal.parse(json["value"].toString()),
|
||||
fractionDigits: coin.decimals,
|
||||
).raw.toInt(),
|
||||
// parse outputs
|
||||
final List<OutputV2> outputs = [];
|
||||
for (final outputJson in txData["vout"] as List) {
|
||||
OutputV2 output = OutputV2.fromElectrumXJson(
|
||||
Map<String, dynamic>.from(outputJson as Map),
|
||||
decimalPlaces: coin.decimals,
|
||||
// don't know yet if wallet owns. Need addresses first
|
||||
walletOwns: false,
|
||||
);
|
||||
|
||||
// if output was to my wallet, add value to amount received
|
||||
if (receivingAddresses
|
||||
.intersection(output.addresses.toSet())
|
||||
.isNotEmpty) {
|
||||
wasReceivedInThisWallet = true;
|
||||
amountReceivedInThisWallet += output.value;
|
||||
output = output.copyWith(walletOwns: true);
|
||||
} else if (changeAddresses
|
||||
.intersection(output.addresses.toSet())
|
||||
.isNotEmpty) {
|
||||
wasReceivedInThisWallet = true;
|
||||
changeAmountReceivedInThisWallet += output.value;
|
||||
output = output.copyWith(walletOwns: true);
|
||||
}
|
||||
|
||||
outputs.add(output);
|
||||
}
|
||||
|
||||
final tx = isar_models.Transaction(
|
||||
final totalOut = outputs
|
||||
.map((e) => e.value)
|
||||
.fold(BigInt.zero, (value, element) => value + element);
|
||||
|
||||
isar_models.TransactionType type;
|
||||
isar_models.TransactionSubType subType =
|
||||
isar_models.TransactionSubType.none;
|
||||
|
||||
// at least one input was owned by this wallet
|
||||
if (wasSentFromThisWallet) {
|
||||
type = isar_models.TransactionType.outgoing;
|
||||
|
||||
if (wasReceivedInThisWallet) {
|
||||
if (changeAmountReceivedInThisWallet + amountReceivedInThisWallet ==
|
||||
totalOut) {
|
||||
// definitely sent all to self
|
||||
type = isar_models.TransactionType.sentToSelf;
|
||||
} else if (amountReceivedInThisWallet == BigInt.zero) {
|
||||
// most likely just a typical send
|
||||
// do nothing here yet
|
||||
}
|
||||
|
||||
// check vout 0 for special scripts
|
||||
if (outputs.isNotEmpty) {
|
||||
final output = outputs.first;
|
||||
|
||||
// check for fusion
|
||||
if (BchUtils.isFUZE(output.scriptPubKeyHex.toUint8ListFromHex)) {
|
||||
subType = isar_models.TransactionSubType.cashFusion;
|
||||
} else {
|
||||
// check other cases here such as SLP or cash tokens etc
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (wasReceivedInThisWallet) {
|
||||
// only found outputs owned by this wallet
|
||||
type = isar_models.TransactionType.incoming;
|
||||
} else {
|
||||
Logging.instance.log(
|
||||
"Unexpected tx found (ignoring it): $txData",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
final tx = TransactionV2(
|
||||
walletId: walletId,
|
||||
blockHash: txData["blockhash"] as String?,
|
||||
hash: txData["hash"] as String,
|
||||
txid: txData["txid"] as String,
|
||||
timestamp: txData["blocktime"] as int? ??
|
||||
(DateTime.now().millisecondsSinceEpoch ~/ 1000),
|
||||
type: type,
|
||||
subType: isar_models.TransactionSubType.none,
|
||||
amount: amount.raw.toInt(),
|
||||
amountString: amount.toJsonString(),
|
||||
fee: fee.raw.toInt(),
|
||||
height: txData["height"] as int?,
|
||||
isCancelled: false,
|
||||
isLelantus: false,
|
||||
slateId: null,
|
||||
otherData: null,
|
||||
nonce: null,
|
||||
inputs: inputs,
|
||||
outputs: outputs,
|
||||
numberOfMessages: null,
|
||||
version: txData["version"] as int,
|
||||
timestamp: txData["blocktime"] as int? ??
|
||||
DateTime.timestamp().millisecondsSinceEpoch ~/ 1000,
|
||||
inputs: List.unmodifiable(inputs),
|
||||
outputs: List.unmodifiable(outputs),
|
||||
type: type,
|
||||
subType: subType,
|
||||
);
|
||||
|
||||
txns.add(Tuple2(tx, transactionAddress));
|
||||
txns.add(tx);
|
||||
}
|
||||
|
||||
await db.addNewTransactionData(txns, walletId);
|
||||
await db.updateOrPutTransactionV2s(txns);
|
||||
|
||||
// quick hack to notify manager to call notifyListeners if
|
||||
// transactions changed
|
||||
|
|
|
@ -22,6 +22,33 @@ import 'package:stackwallet/utilities/stack_file_system.dart';
|
|||
|
||||
const String kReservedFusionAddress = "reserved_fusion_address";
|
||||
|
||||
final kFusionServerInfoDefaults = Map<Coin, FusionInfo>.unmodifiable(const {
|
||||
Coin.bitcoincash: FusionInfo(
|
||||
host: "fusion.servo.cash",
|
||||
port: 8789,
|
||||
ssl: true,
|
||||
// host: "cashfusion.stackwallet.com",
|
||||
// port: 8787,
|
||||
// ssl: false,
|
||||
rounds: 0, // 0 is continuous
|
||||
),
|
||||
Coin.bitcoincashTestnet: FusionInfo(
|
||||
host: "fusion.servo.cash",
|
||||
port: 8789,
|
||||
ssl: true,
|
||||
// host: "cashfusion.stackwallet.com",
|
||||
// port: 8787,
|
||||
// ssl: false,
|
||||
rounds: 0, // 0 is continuous
|
||||
),
|
||||
Coin.eCash: FusionInfo(
|
||||
host: "fusion.tokamak.cash",
|
||||
port: 8788,
|
||||
ssl: true,
|
||||
rounds: 0, // 0 is continuous
|
||||
),
|
||||
});
|
||||
|
||||
class FusionInfo {
|
||||
final String host;
|
||||
final int port;
|
||||
|
@ -37,16 +64,6 @@ class FusionInfo {
|
|||
required this.rounds,
|
||||
}) : assert(rounds >= 0);
|
||||
|
||||
static const DEFAULTS = FusionInfo(
|
||||
host: "fusion.servo.cash",
|
||||
port: 8789,
|
||||
ssl: true,
|
||||
// host: "cashfusion.stackwallet.com",
|
||||
// port: 8787,
|
||||
// ssl: false,
|
||||
rounds: 0, // 0 is continuous
|
||||
);
|
||||
|
||||
factory FusionInfo.fromJsonString(String jsonString) {
|
||||
final json = jsonDecode(jsonString);
|
||||
return FusionInfo(
|
||||
|
@ -95,7 +112,7 @@ class FusionInfo {
|
|||
}
|
||||
}
|
||||
|
||||
/// A mixin for the BitcoinCashWallet class that adds CashFusion functionality.
|
||||
/// A mixin that adds CashFusion functionality.
|
||||
mixin FusionWalletInterface {
|
||||
// Passed in wallet data.
|
||||
late final String _walletId;
|
||||
|
@ -630,14 +647,25 @@ mixin FusionWalletInterface {
|
|||
// Loop through UTXOs, checking and adding valid ones.
|
||||
for (final utxo in walletUtxos) {
|
||||
final String addressString = utxo.address!;
|
||||
final List<String> possibleAddresses = [addressString];
|
||||
final Set<String> possibleAddresses = {};
|
||||
|
||||
if (bitbox.Address.detectFormat(addressString) ==
|
||||
bitbox.Address.formatCashAddr) {
|
||||
possibleAddresses
|
||||
.add(bitbox.Address.toLegacyAddress(addressString));
|
||||
possibleAddresses.add(addressString);
|
||||
possibleAddresses.add(
|
||||
bitbox.Address.toLegacyAddress(addressString),
|
||||
);
|
||||
} else {
|
||||
possibleAddresses.add(bitbox.Address.toCashAddress(addressString));
|
||||
possibleAddresses.add(addressString);
|
||||
if (_coin == Coin.eCash) {
|
||||
possibleAddresses.add(
|
||||
bitbox.Address.toECashAddress(addressString),
|
||||
);
|
||||
} else {
|
||||
possibleAddresses.add(
|
||||
bitbox.Address.toCashAddress(addressString),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch address to get pubkey
|
||||
|
@ -645,13 +673,13 @@ mixin FusionWalletInterface {
|
|||
.getAddresses(_walletId)
|
||||
.filter()
|
||||
.anyOf<String,
|
||||
QueryBuilder<Address, Address, QAfterFilterCondition>>(
|
||||
possibleAddresses, (q, e) => q.valueEqualTo(e))
|
||||
QueryBuilder<Address, Address, QAfterFilterCondition>>(
|
||||
possibleAddresses, (q, e) => q.valueEqualTo(e))
|
||||
.and()
|
||||
.group((q) => q
|
||||
.subTypeEqualTo(AddressSubType.change)
|
||||
.or()
|
||||
.subTypeEqualTo(AddressSubType.receiving))
|
||||
.subTypeEqualTo(AddressSubType.change)
|
||||
.or()
|
||||
.subTypeEqualTo(AddressSubType.receiving))
|
||||
.and()
|
||||
.typeEqualTo(AddressType.p2pkh)
|
||||
.findFirst();
|
||||
|
@ -681,6 +709,10 @@ mixin FusionWalletInterface {
|
|||
|
||||
// Fuse UTXOs.
|
||||
try {
|
||||
if (coinList.isEmpty) {
|
||||
throw Exception("Started with no coins");
|
||||
}
|
||||
|
||||
await _mainFusionObject!.fuse(
|
||||
inputsFromWallet: coinList,
|
||||
network: _coin.isTestNet
|
||||
|
@ -710,6 +742,16 @@ mixin FusionWalletInterface {
|
|||
// Do the same for the UI state.
|
||||
_uiState?.incrementFusionRoundsFailed();
|
||||
|
||||
// If we have no coins, stop trying.
|
||||
if (coinList.isEmpty ||
|
||||
e.toString().contains("Started with no coins")) {
|
||||
_updateStatus(
|
||||
status: fusion.FusionStatus.failed,
|
||||
info: "Started with no coins, stopping.");
|
||||
_stopRequested = true;
|
||||
_uiState?.setFailed(true, shouldNotify: true);
|
||||
}
|
||||
|
||||
// If we fail too many times in a row, stop trying.
|
||||
if (_failedFuseCount >= maxFailedFuseCount) {
|
||||
_updateStatus(
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
*
|
||||
*/
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:stackwallet/db/hive/db.dart';
|
||||
import 'package:stackwallet/services/event_bus/events/global/tor_status_changed_event.dart';
|
||||
|
@ -936,32 +938,74 @@ class Prefs extends ChangeNotifier {
|
|||
|
||||
// fusion server info
|
||||
|
||||
FusionInfo _fusionServerInfo = FusionInfo.DEFAULTS;
|
||||
Map<Coin, FusionInfo> _fusionServerInfo = {};
|
||||
|
||||
FusionInfo get fusionServerInfo => _fusionServerInfo;
|
||||
FusionInfo getFusionServerInfo(Coin coin) {
|
||||
return _fusionServerInfo[coin] ?? kFusionServerInfoDefaults[coin]!;
|
||||
}
|
||||
|
||||
void setFusionServerInfo(Coin coin, FusionInfo fusionServerInfo) {
|
||||
if (_fusionServerInfo[coin] != fusionServerInfo) {
|
||||
_fusionServerInfo[coin] = fusionServerInfo;
|
||||
|
||||
set fusionServerInfo(FusionInfo fusionServerInfo) {
|
||||
if (this.fusionServerInfo != fusionServerInfo) {
|
||||
DB.instance.put<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "fusionServerInfo",
|
||||
value: fusionServerInfo.toJsonString(),
|
||||
key: "fusionServerInfoMap",
|
||||
value: _fusionServerInfo.map(
|
||||
(key, value) => MapEntry(
|
||||
key.name,
|
||||
value.toJsonString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
_fusionServerInfo = fusionServerInfo;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<FusionInfo> _getFusionServerInfo() async {
|
||||
final saved = await DB.instance.get<dynamic>(
|
||||
Future<Map<Coin, FusionInfo>> _getFusionServerInfo() async {
|
||||
final map = await DB.instance.get<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "fusionServerInfo",
|
||||
) as String?;
|
||||
key: "fusionServerInfoMap",
|
||||
) as Map?;
|
||||
|
||||
try {
|
||||
return FusionInfo.fromJsonString(saved!);
|
||||
} catch (_) {
|
||||
return FusionInfo.DEFAULTS;
|
||||
if (map == null) {
|
||||
return _fusionServerInfo;
|
||||
}
|
||||
|
||||
final actualMap = Map<String, String>.from(map).map(
|
||||
(key, value) => MapEntry(
|
||||
coinFromPrettyName(key),
|
||||
FusionInfo.fromJsonString(value),
|
||||
),
|
||||
);
|
||||
|
||||
// legacy bch check
|
||||
if (actualMap[Coin.bitcoincash] == null ||
|
||||
actualMap[Coin.bitcoincashTestnet] == null) {
|
||||
final saved = await DB.instance.get<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "fusionServerInfo",
|
||||
) as String?;
|
||||
|
||||
if (saved != null) {
|
||||
final bchInfo = FusionInfo.fromJsonString(saved);
|
||||
actualMap[Coin.bitcoincash] = bchInfo;
|
||||
actualMap[Coin.bitcoincashTestnet] = bchInfo;
|
||||
unawaited(
|
||||
DB.instance.put<dynamic>(
|
||||
boxName: DB.boxNamePrefs,
|
||||
key: "fusionServerInfoMap",
|
||||
value: actualMap.map(
|
||||
(key, value) => MapEntry(
|
||||
key.name,
|
||||
value.toJsonString(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return actualMap;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue