2023-10-30 22:58:15 +00:00
|
|
|
import 'package:bitbox/bitbox.dart' as bitbox;
|
2023-10-30 17:45:13 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
2023-10-30 22:58:15 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.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/services/coins/bitcoincash/bch_utils.dart';
|
|
|
|
import 'package:stackwallet/services/coins/bitcoincash/cashtokens.dart'
|
|
|
|
as cash_tokens;
|
2023-11-03 19:46:55 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-10-30 22:58:15 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/extensions/extensions.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2023-11-04 01:18:22 +00:00
|
|
|
import 'package:stackwallet/wallets/crypto_currency/coins/bitcoincash.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';
|
2024-01-17 16:01:31 +00:00
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/bcash_interface.dart';
|
2023-11-16 22:25:20 +00:00
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/cash_fusion_interface.dart';
|
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/coin_control_interface.dart';
|
|
|
|
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart';
|
2023-10-30 17:45:13 +00:00
|
|
|
|
2023-11-14 21:49:37 +00:00
|
|
|
class BitcoincashWallet extends Bip39HDWallet
|
2024-01-17 16:01:31 +00:00
|
|
|
with
|
|
|
|
ElectrumXInterface,
|
|
|
|
BCashInterface,
|
|
|
|
CoinControlInterface,
|
|
|
|
CashFusionInterface {
|
2023-10-30 17:45:13 +00:00
|
|
|
@override
|
|
|
|
int get isarTransactionVersion => 2;
|
|
|
|
|
2023-11-15 17:40:43 +00:00
|
|
|
BitcoincashWallet(CryptoCurrencyNetwork network)
|
|
|
|
: super(Bitcoincash(network));
|
2023-10-30 17:45:13 +00:00
|
|
|
|
2023-11-07 16:25:04 +00:00
|
|
|
@override
|
|
|
|
FilterOperation? get changeAddressFilterOperation => FilterGroup.and(
|
|
|
|
[
|
|
|
|
...standardChangeAddressFilters,
|
|
|
|
FilterGroup.not(
|
2023-11-14 20:42:46 +00:00
|
|
|
const ObjectFilter(
|
2023-11-07 16:25:04 +00:00
|
|
|
property: "derivationPath",
|
2023-11-14 20:42:46 +00:00
|
|
|
filter: FilterCondition.startsWith(
|
|
|
|
property: "value",
|
|
|
|
value: "m/44'/0'",
|
|
|
|
),
|
2023-11-07 16:25:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
FilterOperation? get receivingAddressFilterOperation => FilterGroup.and(
|
|
|
|
[
|
|
|
|
...standardReceivingAddressFilters,
|
|
|
|
FilterGroup.not(
|
2023-11-14 20:42:46 +00:00
|
|
|
const ObjectFilter(
|
2023-11-07 16:25:04 +00:00
|
|
|
property: "derivationPath",
|
2023-11-14 20:42:46 +00:00
|
|
|
filter: FilterCondition.startsWith(
|
|
|
|
property: "value",
|
|
|
|
value: "m/44'/0'",
|
|
|
|
),
|
2023-11-07 16:25:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2023-10-30 17:45:13 +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-10-30 17:45:13 +00:00
|
|
|
final allAddresses = await mainDB
|
|
|
|
.getAddresses(walletId)
|
|
|
|
.filter()
|
|
|
|
.not()
|
2023-10-30 22:58:15 +00:00
|
|
|
.typeEqualTo(AddressType.nonWallet)
|
|
|
|
.and()
|
|
|
|
.group((q) => q
|
|
|
|
.subTypeEqualTo(AddressSubType.receiving)
|
|
|
|
.or()
|
|
|
|
.subTypeEqualTo(AddressSubType.change))
|
2023-10-30 17:45:13 +00:00
|
|
|
.findAll();
|
|
|
|
return allAddresses;
|
|
|
|
}
|
|
|
|
|
2023-11-14 20:01:47 +00:00
|
|
|
@override
|
|
|
|
String convertAddressString(String address) {
|
|
|
|
if (bitbox.Address.detectFormat(address) == bitbox.Address.formatLegacy &&
|
|
|
|
(cryptoCurrency.addressType(address: address) == DerivePathType.bip44 ||
|
|
|
|
cryptoCurrency.addressType(address: address) ==
|
|
|
|
DerivePathType.bch44)) {
|
|
|
|
return bitbox.Address.toCashAddress(address);
|
|
|
|
} else {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 17:45:13 +00:00
|
|
|
// ===========================================================================
|
|
|
|
|
|
|
|
@override
|
2023-10-30 22:58:15 +00:00
|
|
|
Future<void> updateTransactions() async {
|
2023-11-29 15:53:30 +00:00
|
|
|
List<Address> allAddressesOld = await fetchAddressesForElectrumXScan();
|
2023-10-30 22:58:15 +00:00
|
|
|
|
|
|
|
Set<String> receivingAddresses = allAddressesOld
|
|
|
|
.where((e) => e.subType == AddressSubType.receiving)
|
2023-11-14 20:01:47 +00:00
|
|
|
.map((e) => convertAddressString(e.value))
|
|
|
|
.toSet();
|
2023-10-30 22:58:15 +00:00
|
|
|
|
|
|
|
Set<String> changeAddresses = allAddressesOld
|
|
|
|
.where((e) => e.subType == AddressSubType.change)
|
2023-11-14 20:01:47 +00:00
|
|
|
.map((e) => convertAddressString(e.value))
|
|
|
|
.toSet();
|
2023-10-30 22:58:15 +00:00
|
|
|
|
|
|
|
final allAddressesSet = {...receivingAddresses, ...changeAddresses};
|
|
|
|
|
|
|
|
final List<Map<String, dynamic>> allTxHashes =
|
|
|
|
await fetchHistory(allAddressesSet);
|
|
|
|
|
|
|
|
List<Map<String, dynamic>> allTransactions = [];
|
|
|
|
|
|
|
|
for (final txHash in allTxHashes) {
|
|
|
|
final storedTx = await mainDB.isar.transactionV2s
|
|
|
|
.where()
|
|
|
|
.txidWalletIdEqualTo(txHash["tx_hash"] as String, walletId)
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
if (storedTx == null ||
|
|
|
|
storedTx.height == null ||
|
|
|
|
(storedTx.height != null && storedTx.height! <= 0)) {
|
2023-11-14 20:31:53 +00:00
|
|
|
final tx = await electrumXCachedClient.getTransaction(
|
2023-10-30 22:58:15 +00:00
|
|
|
txHash: txHash["tx_hash"] as String,
|
|
|
|
verbose: true,
|
|
|
|
coin: cryptoCurrency.coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
// check for duplicates before adding to list
|
|
|
|
if (allTransactions
|
|
|
|
.indexWhere((e) => e["txid"] == tx["txid"] as String) ==
|
|
|
|
-1) {
|
|
|
|
tx["height"] = txHash["height"];
|
|
|
|
allTransactions.add(tx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<TransactionV2> txns = [];
|
|
|
|
|
|
|
|
for (final txData in allTransactions) {
|
|
|
|
// set to true if any inputs were detected as owned by this wallet
|
|
|
|
bool wasSentFromThisWallet = false;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
final List<InputV2> inputs = [];
|
|
|
|
for (final jsonInput in txData["vin"] as List) {
|
|
|
|
final map = Map<String, dynamic>.from(jsonInput as Map);
|
|
|
|
|
|
|
|
final List<String> addresses = [];
|
|
|
|
String valueStringSats = "0";
|
|
|
|
OutpointV2? outpoint;
|
|
|
|
|
|
|
|
final coinbase = map["coinbase"] as String?;
|
|
|
|
|
|
|
|
if (coinbase == null) {
|
|
|
|
final txid = map["txid"] as String;
|
|
|
|
final vout = map["vout"] as int;
|
|
|
|
|
2023-11-14 20:31:53 +00:00
|
|
|
final inputTx = await electrumXCachedClient.getTransaction(
|
2023-10-30 22:58:15 +00:00
|
|
|
txHash: txid,
|
|
|
|
coin: cryptoCurrency.coin,
|
|
|
|
);
|
|
|
|
|
2024-02-06 14:33:10 +00:00
|
|
|
try {
|
|
|
|
final prevOutJson = Map<String, dynamic>.from(
|
|
|
|
(inputTx["vout"] as List).firstWhere((e) => e["n"] == vout)
|
2024-02-06 18:34:55 +00:00
|
|
|
as Map);
|
2024-02-06 14:33:10 +00:00
|
|
|
final prevOut = OutputV2.fromElectrumXJson(
|
|
|
|
prevOutJson,
|
|
|
|
decimalPlaces: cryptoCurrency.fractionDigits,
|
|
|
|
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);
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
2024-02-10 01:10:37 +00:00
|
|
|
"Error getting prevOutJson: $e\nStack trace: $s",
|
2024-02-06 18:34:55 +00:00
|
|
|
level: LogLevel.Warning);
|
2024-02-06 14:33:10 +00:00
|
|
|
}
|
2023-10-30 22:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InputV2 input = InputV2.isarCantDoRequiredInDefaultConstructor(
|
|
|
|
scriptSigHex: map["scriptSig"]?["hex"] as String?,
|
2024-01-14 17:26:04 +00:00
|
|
|
scriptSigAsm: map["scriptSig"]?["asm"] as String?,
|
2023-10-30 22:58:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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: cryptoCurrency.fractionDigits,
|
|
|
|
// 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 totalOut = outputs
|
|
|
|
.map((e) => e.value)
|
|
|
|
.fold(BigInt.zero, (value, element) => value + element);
|
|
|
|
|
|
|
|
TransactionType type;
|
|
|
|
TransactionSubType subType = TransactionSubType.none;
|
|
|
|
|
|
|
|
// at least one input was owned by this wallet
|
|
|
|
if (wasSentFromThisWallet) {
|
|
|
|
type = TransactionType.outgoing;
|
|
|
|
|
|
|
|
if (wasReceivedInThisWallet) {
|
|
|
|
if (changeAmountReceivedInThisWallet + amountReceivedInThisWallet ==
|
|
|
|
totalOut) {
|
|
|
|
// definitely sent all to self
|
|
|
|
type = 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 = 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 = 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,
|
|
|
|
height: txData["height"] as int?,
|
|
|
|
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,
|
2023-12-14 15:48:49 +00:00
|
|
|
otherData: null,
|
2023-10-30 22:58:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
txns.add(tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
await mainDB.updateOrPutTransactionV2s(txns);
|
|
|
|
}
|
|
|
|
|
2023-11-06 23:10:07 +00:00
|
|
|
@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-10-30 22:58:15 +00:00
|
|
|
bool blocked = false;
|
|
|
|
String? blockedReason;
|
|
|
|
|
|
|
|
if (scriptPubKeyHex != null) {
|
|
|
|
// check for cash tokens
|
|
|
|
try {
|
|
|
|
final ctOutput =
|
|
|
|
cash_tokens.unwrap_spk(scriptPubKeyHex.toUint8ListFromHex);
|
|
|
|
if (ctOutput.token_data != null) {
|
|
|
|
// found a token!
|
|
|
|
blocked = true;
|
|
|
|
blockedReason = "Cash token output detected";
|
|
|
|
}
|
|
|
|
} catch (e, s) {
|
|
|
|
// Probably doesn't contain a cash token so just log failure
|
|
|
|
Logging.instance.log(
|
|
|
|
"Script pub key \"$scriptPubKeyHex\" cash token"
|
|
|
|
" parsing check failed: $e\n$s",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for SLP tokens if not already blocked
|
|
|
|
if (!blocked && BchUtils.isSLP(scriptPubKeyHex.toUint8ListFromHex)) {
|
|
|
|
blocked = true;
|
|
|
|
blockedReason = "SLP token output detected";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 00:37:46 +00:00
|
|
|
return (blockedReason: blockedReason, blocked: blocked, utxoLabel: null);
|
2023-10-30 17:45:13 +00:00
|
|
|
}
|
|
|
|
|
2023-11-03 19:46:55 +00:00
|
|
|
// TODO: correct formula for bch?
|
|
|
|
@override
|
|
|
|
Amount roughFeeEstimate(int inputCount, int outputCount, int feeRatePerKB) {
|
|
|
|
return Amount(
|
|
|
|
rawValue: BigInt.from(((181 * inputCount) + (34 * outputCount) + 10) *
|
|
|
|
(feeRatePerKB / 1000).ceil()),
|
|
|
|
fractionDigits: info.coin.decimals,
|
|
|
|
);
|
|
|
|
}
|
2023-11-07 18:19:42 +00:00
|
|
|
|
2023-11-08 19:57:38 +00:00
|
|
|
@override
|
|
|
|
int estimateTxFee({required int vSize, required int feeRatePerKB}) {
|
|
|
|
return vSize * (feeRatePerKB / 1000).ceil();
|
|
|
|
}
|
2024-01-12 20:49:05 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String normalizeAddress(String address) {
|
|
|
|
try {
|
|
|
|
if (bitbox.Address.detectFormat(address) ==
|
|
|
|
bitbox.Address.formatCashAddr) {
|
|
|
|
return bitbox.Address.toLegacyAddress(address);
|
|
|
|
} else {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
} catch (_) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
2023-10-30 17:45:13 +00:00
|
|
|
}
|