2023-09-18 21:28:31 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
2023-11-03 19:46:55 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-11-06 23:10:07 +00:00
|
|
|
import 'package:stackwallet/utilities/extensions/extensions.dart';
|
2023-09-18 21:28:31 +00:00
|
|
|
import 'package:stackwallet/wallets/crypto_currency/coins/bitcoin.dart';
|
2023-11-15 17:40:43 +00:00
|
|
|
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
|
2023-11-04 01:18:22 +00:00
|
|
|
import 'package:stackwallet/wallets/wallet/intermediate/bip39_hd_wallet.dart';
|
2023-11-16 22:25:20 +00:00
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/coin_control_interface.dart';
|
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart';
|
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart';
|
2023-09-18 21:28:31 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
|
|
|
|
2023-11-15 22:43:46 +00:00
|
|
|
class BitcoinWallet extends Bip39HDWallet
|
2023-11-16 22:25:20 +00:00
|
|
|
with ElectrumXInterface, CoinControlInterface, PaynymInterface {
|
2023-10-30 17:41:03 +00:00
|
|
|
@override
|
2023-11-08 21:48:57 +00:00
|
|
|
int get isarTransactionVersion => 1; // TODO actually set this to 2
|
2023-10-30 17:41:03 +00:00
|
|
|
|
2023-11-15 17:40:43 +00:00
|
|
|
BitcoinWallet(CryptoCurrencyNetwork network) : super(Bitcoin(network));
|
2023-09-18 21:28:31 +00:00
|
|
|
|
2023-11-07 16:25:04 +00:00
|
|
|
@override
|
|
|
|
FilterOperation? get changeAddressFilterOperation =>
|
|
|
|
FilterGroup.and(standardChangeAddressFilters);
|
|
|
|
|
|
|
|
@override
|
|
|
|
FilterOperation? get receivingAddressFilterOperation =>
|
|
|
|
FilterGroup.and(standardReceivingAddressFilters);
|
|
|
|
|
2023-09-18 21:28:31 +00:00
|
|
|
// ===========================================================================
|
|
|
|
|
2023-11-06 23:10:07 +00:00
|
|
|
@override
|
2023-11-29 15:53:30 +00:00
|
|
|
Future<List<Address>> fetchAddressesForElectrumXScan() async {
|
2023-09-18 21:28:31 +00:00
|
|
|
final allAddresses = await mainDB
|
|
|
|
.getAddresses(walletId)
|
|
|
|
.filter()
|
|
|
|
.not()
|
|
|
|
.group(
|
|
|
|
(q) => q
|
|
|
|
.typeEqualTo(AddressType.nonWallet)
|
|
|
|
.or()
|
|
|
|
.subTypeEqualTo(AddressSubType.nonWallet),
|
|
|
|
)
|
|
|
|
.findAll();
|
|
|
|
return allAddresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> updateTransactions() async {
|
|
|
|
final currentChainHeight = await fetchChainHeight();
|
|
|
|
|
2023-11-06 23:10:07 +00:00
|
|
|
// TODO: [prio=med] switch to V2 transactions
|
2023-10-30 22:58:15 +00:00
|
|
|
final data = await fetchTransactionsV1(
|
2023-11-29 15:53:30 +00:00
|
|
|
addresses: await fetchAddressesForElectrumXScan(),
|
2023-09-18 21:28:31 +00:00
|
|
|
currentChainHeight: currentChainHeight,
|
|
|
|
);
|
|
|
|
|
|
|
|
await mainDB.addNewTransactionData(
|
|
|
|
data
|
2023-11-06 23:10:07 +00:00
|
|
|
.map((e) => Tuple2(
|
|
|
|
e.transaction,
|
|
|
|
e.address,
|
|
|
|
))
|
2023-09-18 21:28:31 +00:00
|
|
|
.toList(),
|
|
|
|
walletId,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-01-05 00:37:46 +00:00
|
|
|
Future<({String? blockedReason, bool blocked, String? utxoLabel})>
|
|
|
|
checkBlockUTXO(
|
2023-11-06 23:10:07 +00:00
|
|
|
Map<String, dynamic> jsonUTXO,
|
|
|
|
String? scriptPubKeyHex,
|
|
|
|
Map<String, dynamic>? jsonTX,
|
2024-01-05 00:37:46 +00:00
|
|
|
String? utxoOwnerAddress,
|
|
|
|
) async {
|
2023-11-06 23:10:07 +00:00
|
|
|
bool blocked = false;
|
|
|
|
String? blockedReason;
|
2023-10-30 22:58:15 +00:00
|
|
|
|
2023-11-06 23:10:07 +00:00
|
|
|
if (jsonTX != null) {
|
|
|
|
// check for bip47 notification
|
|
|
|
final outputs = jsonTX["vout"] as List;
|
|
|
|
for (final output in outputs) {
|
|
|
|
List<String>? scriptChunks =
|
|
|
|
(output['scriptPubKey']?['asm'] as String?)?.split(" ");
|
|
|
|
if (scriptChunks?.length == 2 && scriptChunks?[0] == "OP_RETURN") {
|
|
|
|
final blindedPaymentCode = scriptChunks![1];
|
|
|
|
final bytes = blindedPaymentCode.toUint8ListFromHex;
|
|
|
|
|
|
|
|
// https://en.bitcoin.it/wiki/BIP_0047#Sending
|
|
|
|
if (bytes.length == 80 && bytes.first == 1) {
|
|
|
|
blocked = true;
|
|
|
|
blockedReason = "Paynym notification output. Incautious "
|
|
|
|
"handling of outputs from notification transactions "
|
|
|
|
"may cause unintended loss of privacy.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-30 22:58:15 +00:00
|
|
|
}
|
2023-10-31 16:06:35 +00:00
|
|
|
|
2024-01-05 00:37:46 +00:00
|
|
|
return (blockedReason: blockedReason, blocked: blocked, utxoLabel: null);
|
2023-10-31 16:06:35 +00:00
|
|
|
}
|
2023-11-03 19:46:55 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Amount roughFeeEstimate(int inputCount, int outputCount, int feeRatePerKB) {
|
|
|
|
return Amount(
|
|
|
|
rawValue: BigInt.from(
|
|
|
|
((42 + (272 * inputCount) + (128 * outputCount)) / 4).ceil() *
|
|
|
|
(feeRatePerKB / 1000).ceil()),
|
2023-11-16 21:30:01 +00:00
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
2023-11-03 19:46:55 +00:00
|
|
|
);
|
|
|
|
}
|
2023-11-08 19:57:38 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
int estimateTxFee({required int vSize, required int feeRatePerKB}) {
|
|
|
|
return vSize * (feeRatePerKB / 1000).ceil();
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// @override
|
|
|
|
// Future<TxData> coinSelection({required TxData txData}) async {
|
|
|
|
// final isCoinControl = txData.utxos != null;
|
|
|
|
// final isSendAll = txData.amount == info.cachedBalance.spendable;
|
|
|
|
//
|
|
|
|
// final utxos =
|
|
|
|
// txData.utxos?.toList() ?? await mainDB.getUTXOs(walletId).findAll();
|
|
|
|
//
|
|
|
|
// final currentChainHeight = await chainHeight;
|
|
|
|
// final List<UTXO> spendableOutputs = [];
|
|
|
|
// int spendableSatoshiValue = 0;
|
|
|
|
//
|
|
|
|
// // Build list of spendable outputs and totaling their satoshi amount
|
|
|
|
// for (final utxo in utxos) {
|
|
|
|
// if (utxo.isBlocked == false &&
|
|
|
|
// utxo.isConfirmed(currentChainHeight, cryptoCurrency.minConfirms) &&
|
|
|
|
// utxo.used != true) {
|
|
|
|
// spendableOutputs.add(utxo);
|
|
|
|
// spendableSatoshiValue += utxo.value;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (isCoinControl && spendableOutputs.length < utxos.length) {
|
|
|
|
// throw ArgumentError("Attempted to use an unavailable utxo");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (spendableSatoshiValue < txData.amount!.raw.toInt()) {
|
|
|
|
// throw Exception("Insufficient balance");
|
|
|
|
// } else if (spendableSatoshiValue == txData.amount!.raw.toInt() &&
|
|
|
|
// !isSendAll) {
|
|
|
|
// throw Exception("Insufficient balance to pay transaction fee");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (isCoinControl) {
|
|
|
|
// } else {
|
|
|
|
// final selection = cs.coinSelection(
|
|
|
|
// spendableOutputs
|
|
|
|
// .map((e) => cs.InputModel(
|
|
|
|
// i: e.vout,
|
|
|
|
// txid: e.txid,
|
|
|
|
// value: e.value,
|
|
|
|
// address: e.address,
|
|
|
|
// ))
|
|
|
|
// .toList(),
|
|
|
|
// txData.recipients!
|
|
|
|
// .map((e) => cs.OutputModel(
|
|
|
|
// address: e.address,
|
|
|
|
// value: e.amount.raw.toInt(),
|
|
|
|
// ))
|
|
|
|
// .toList(),
|
|
|
|
// txData.feeRateAmount!,
|
|
|
|
// 10, // TODO: ???????????????????????????????
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// // .inputs and .outputs will be null if no solution was found
|
|
|
|
// if (selection.inputs!.isEmpty || selection.outputs!.isEmpty) {
|
|
|
|
// throw Exception("coin selection failed");
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2023-09-18 21:28:31 +00:00
|
|
|
}
|