2022-12-22 01:07:13 +00:00
|
|
|
import 'dart:convert';
|
2023-01-13 17:11:41 +00:00
|
|
|
import 'dart:developer';
|
2022-12-20 23:00:03 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:bip47/bip47.dart';
|
2023-01-05 18:48:18 +00:00
|
|
|
import 'package:bip47/src/util.dart';
|
2023-01-10 23:55:19 +00:00
|
|
|
import 'package:bitcoindart/bitcoindart.dart' as btc_dart;
|
2023-01-05 18:48:18 +00:00
|
|
|
import 'package:bitcoindart/src/utils/constants/op.dart' as op;
|
|
|
|
import 'package:bitcoindart/src/utils/script.dart' as bscript;
|
|
|
|
import 'package:decimal/decimal.dart';
|
2023-01-12 03:14:04 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2022-12-20 23:00:03 +00:00
|
|
|
import 'package:pointycastle/digests/sha256.dart';
|
|
|
|
import 'package:stackwallet/hive/db.dart';
|
2023-01-10 23:55:19 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
2022-12-20 23:00:03 +00:00
|
|
|
import 'package:stackwallet/services/coins/dogecoin/dogecoin_wallet.dart';
|
2023-01-09 17:05:13 +00:00
|
|
|
import 'package:stackwallet/utilities/address_utils.dart';
|
2023-01-05 18:48:18 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2022-12-21 23:02:14 +00:00
|
|
|
import 'package:stackwallet/utilities/format.dart';
|
2023-01-09 17:05:13 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2023-01-06 19:55:47 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
|
|
|
|
|
|
|
class SWException with Exception {
|
|
|
|
SWException(this.message);
|
|
|
|
|
|
|
|
final String message;
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() => message;
|
|
|
|
}
|
|
|
|
|
|
|
|
class InsufficientBalanceException extends SWException {
|
|
|
|
InsufficientBalanceException(super.message);
|
|
|
|
}
|
2022-12-20 23:00:03 +00:00
|
|
|
|
2023-01-09 17:05:13 +00:00
|
|
|
class PaynymSendException extends SWException {
|
|
|
|
PaynymSendException(super.message);
|
|
|
|
}
|
|
|
|
|
2022-12-20 23:00:03 +00:00
|
|
|
extension PayNym on DogecoinWallet {
|
|
|
|
// fetch or generate this wallet's bip47 payment code
|
|
|
|
Future<PaymentCode> getPaymentCode() async {
|
|
|
|
final paymentCodeString = DB.instance
|
|
|
|
.get<dynamic>(boxName: walletId, key: "paymentCodeString") as String?;
|
|
|
|
PaymentCode paymentCode;
|
|
|
|
if (paymentCodeString == null) {
|
|
|
|
final node = getBip32Root((await mnemonic).join(" "), network)
|
|
|
|
.derivePath("m/47'/0'/0'");
|
|
|
|
paymentCode =
|
|
|
|
PaymentCode.initFromPubKey(node.publicKey, node.chainCode, network);
|
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: walletId,
|
|
|
|
key: "paymentCodeString",
|
|
|
|
value: paymentCode.toString());
|
|
|
|
} else {
|
|
|
|
paymentCode = PaymentCode.fromPaymentCode(paymentCodeString, network);
|
|
|
|
}
|
|
|
|
return paymentCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Uint8List> signWithNotificationKey(Uint8List data) async {
|
|
|
|
final node = getBip32Root((await mnemonic).join(" "), network)
|
|
|
|
.derivePath("m/47'/0'/0'");
|
2023-01-10 23:55:19 +00:00
|
|
|
final pair =
|
|
|
|
btc_dart.ECPair.fromPrivateKey(node.privateKey!, network: network);
|
2022-12-20 23:00:03 +00:00
|
|
|
final signed = pair.sign(SHA256Digest().process(data));
|
|
|
|
return signed;
|
|
|
|
}
|
2022-12-21 16:17:53 +00:00
|
|
|
|
2022-12-21 23:02:14 +00:00
|
|
|
Future<String> signStringWithNotificationKey(String data) async {
|
|
|
|
final bytes =
|
2022-12-22 01:07:13 +00:00
|
|
|
await signWithNotificationKey(Uint8List.fromList(utf8.encode(data)));
|
2022-12-21 23:02:14 +00:00
|
|
|
return Format.uint8listToString(bytes);
|
|
|
|
// final bytes =
|
|
|
|
// await signWithNotificationKey(Uint8List.fromList(utf8.encode(data)));
|
|
|
|
// return Format.uint8listToString(bytes);
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:09:26 +00:00
|
|
|
/// Update cached lists of notification transaction IDs.
|
|
|
|
/// Returns true if there are new notification transactions found since last
|
|
|
|
/// checked.
|
|
|
|
Future<bool> checkForNotificationTransactions() async {
|
|
|
|
final myPCode = await getPaymentCode();
|
|
|
|
|
|
|
|
final transactionIds = await electrumXClient.getHistory(
|
|
|
|
scripthash: AddressUtils.convertToScriptHash(
|
|
|
|
myPCode.notificationAddress(),
|
|
|
|
network,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final confirmedNotificationTransactionIds = DB.instance.get<dynamic>(
|
|
|
|
boxName: walletId,
|
|
|
|
key: "confirmedNotificationTransactionIds",
|
|
|
|
) as Set? ??
|
|
|
|
{};
|
|
|
|
|
|
|
|
final unconfirmedNotificationTransactionIds = DB.instance.get<dynamic>(
|
|
|
|
boxName: walletId,
|
|
|
|
key: "unconfirmedNotificationTransactionIds",
|
|
|
|
) as Set? ??
|
|
|
|
{};
|
|
|
|
|
|
|
|
// since we are only checking for newly found transactions here we can use the sum
|
|
|
|
final totalCount = confirmedNotificationTransactionIds.length +
|
|
|
|
unconfirmedNotificationTransactionIds.length;
|
|
|
|
|
|
|
|
for (final entry in transactionIds) {
|
|
|
|
final txid = entry["tx_hash"] as String;
|
|
|
|
|
|
|
|
final tx = await cachedElectrumXClient.getTransaction(
|
|
|
|
txHash: txid,
|
|
|
|
coin: coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
// check if tx is confirmed
|
|
|
|
if ((tx["confirmations"] as int? ?? 0) > MINIMUM_CONFIRMATIONS) {
|
|
|
|
// remove it from unconfirmed set
|
|
|
|
unconfirmedNotificationTransactionIds.remove(txid);
|
|
|
|
|
|
|
|
// add it to confirmed set
|
|
|
|
confirmedNotificationTransactionIds.add(txid);
|
|
|
|
} else {
|
|
|
|
// otherwise add it to the unconfirmed set
|
|
|
|
unconfirmedNotificationTransactionIds.add(txid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final newTotalCount = confirmedNotificationTransactionIds.length +
|
|
|
|
unconfirmedNotificationTransactionIds.length;
|
|
|
|
|
|
|
|
return newTotalCount > totalCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return the notification tx sent from my wallet if it exists
|
2023-01-12 03:14:04 +00:00
|
|
|
Future<Transaction?> hasSentNotificationTx(PaymentCode pCode) async {
|
|
|
|
final tx = await isar.transactions
|
|
|
|
.filter()
|
2023-01-13 18:00:02 +00:00
|
|
|
.address((q) => q.valueEqualTo(pCode.notificationAddress()))
|
2023-01-12 03:14:04 +00:00
|
|
|
.findFirst();
|
|
|
|
return tx;
|
2023-01-09 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 17:05:13 +00:00
|
|
|
void preparePaymentCodeSend(PaymentCode pCode) async {
|
|
|
|
final notifTx = await hasSentNotificationTx(pCode);
|
2023-01-12 03:14:04 +00:00
|
|
|
final currentHeight = await chainHeight;
|
2023-01-09 17:05:13 +00:00
|
|
|
|
|
|
|
if (notifTx == null) {
|
|
|
|
throw PaynymSendException("No notification transaction sent to $pCode");
|
2023-01-12 03:14:04 +00:00
|
|
|
} else if (!notifTx.isConfirmed(currentHeight, MINIMUM_CONFIRMATIONS)) {
|
2023-01-09 17:05:13 +00:00
|
|
|
throw PaynymSendException(
|
|
|
|
"Notification transaction sent to $pCode has not confirmed yet");
|
|
|
|
} else {
|
|
|
|
final node = getBip32Root((await mnemonic).join(" "), network)
|
|
|
|
.derivePath("m/47'/0'/0'");
|
|
|
|
final sendToAddress = await nextUnusedSendAddressFrom(
|
|
|
|
pCode,
|
|
|
|
node.derive(0).privateKey!,
|
|
|
|
);
|
|
|
|
|
|
|
|
// todo: Actual transaction build
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get the next unused address to send to given the receiver's payment code
|
|
|
|
/// and your own private key
|
|
|
|
Future<String> nextUnusedSendAddressFrom(
|
|
|
|
PaymentCode pCode,
|
|
|
|
Uint8List privateKey,
|
|
|
|
) async {
|
|
|
|
// https://en.bitcoin.it/wiki/BIP_0047#Path_levels
|
|
|
|
const maxCount = 2147483647;
|
|
|
|
|
|
|
|
final paymentAddress = PaymentAddress.initWithPrivateKey(
|
|
|
|
privateKey,
|
|
|
|
pCode,
|
|
|
|
0, // initial index to check
|
|
|
|
);
|
|
|
|
|
|
|
|
for (paymentAddress.index = 0;
|
|
|
|
paymentAddress.index <= maxCount;
|
|
|
|
paymentAddress.index++) {
|
|
|
|
final address = paymentAddress.getSendAddress();
|
|
|
|
|
|
|
|
final transactionIds = await electrumXClient.getHistory(
|
|
|
|
scripthash: AddressUtils.convertToScriptHash(
|
|
|
|
address,
|
|
|
|
network,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (transactionIds.isEmpty) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw PaynymSendException("Exhausted unused send addresses!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get your receiving addresses given the sender's payment code and your own
|
|
|
|
/// private key
|
|
|
|
List<String> deriveReceivingAddressesFor(
|
|
|
|
PaymentCode pCode,
|
|
|
|
Uint8List privateKey,
|
|
|
|
int count,
|
|
|
|
) {
|
|
|
|
// https://en.bitcoin.it/wiki/BIP_0047#Path_levels
|
|
|
|
const maxCount = 2147483647;
|
|
|
|
assert(count <= maxCount);
|
|
|
|
|
|
|
|
final paymentAddress = PaymentAddress.initWithPrivateKey(
|
|
|
|
privateKey,
|
|
|
|
pCode,
|
|
|
|
0, // initial index
|
|
|
|
);
|
|
|
|
|
|
|
|
final List<String> result = [];
|
|
|
|
for (paymentAddress.index = 0;
|
|
|
|
paymentAddress.index < count;
|
|
|
|
paymentAddress.index++) {
|
|
|
|
final address = paymentAddress.getReceiveAddress();
|
|
|
|
|
|
|
|
result.add(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-01-05 18:48:18 +00:00
|
|
|
|
2023-01-06 19:55:47 +00:00
|
|
|
Future<Map<String, dynamic>> buildNotificationTx({
|
|
|
|
required int selectedTxFeeRate,
|
|
|
|
required String targetPaymentCodeString,
|
|
|
|
int additionalOutputs = 0,
|
2023-01-12 03:14:04 +00:00
|
|
|
List<UTXO>? utxos,
|
2023-01-06 19:55:47 +00:00
|
|
|
}) async {
|
|
|
|
const amountToSend = DUST_LIMIT;
|
2023-01-12 03:14:04 +00:00
|
|
|
final List<UTXO> availableOutputs = utxos ?? await this.utxos;
|
|
|
|
final List<UTXO> spendableOutputs = [];
|
2023-01-06 19:55:47 +00:00
|
|
|
int spendableSatoshiValue = 0;
|
|
|
|
|
|
|
|
// Build list of spendable outputs and totaling their satoshi amount
|
|
|
|
for (var i = 0; i < availableOutputs.length; i++) {
|
2023-01-12 03:14:04 +00:00
|
|
|
if (availableOutputs[i].isBlocked == false &&
|
|
|
|
availableOutputs[i]
|
|
|
|
.isConfirmed(await chainHeight, MINIMUM_CONFIRMATIONS) ==
|
|
|
|
true) {
|
2023-01-06 19:55:47 +00:00
|
|
|
spendableOutputs.add(availableOutputs[i]);
|
|
|
|
spendableSatoshiValue += availableOutputs[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spendableSatoshiValue < amountToSend) {
|
|
|
|
// insufficient balance
|
|
|
|
throw InsufficientBalanceException(
|
|
|
|
"Spendable balance is less than the minimum required for a notification transaction.");
|
|
|
|
} else if (spendableSatoshiValue == amountToSend) {
|
|
|
|
// insufficient balance due to missing amount to cover fee
|
|
|
|
throw InsufficientBalanceException(
|
|
|
|
"Remaining balance does not cover the network fee.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort spendable by age (oldest first)
|
2023-01-12 03:14:04 +00:00
|
|
|
spendableOutputs.sort((a, b) => b.blockTime!.compareTo(a.blockTime!));
|
2023-01-06 19:55:47 +00:00
|
|
|
|
|
|
|
int satoshisBeingUsed = 0;
|
|
|
|
int outputsBeingUsed = 0;
|
2023-01-12 03:14:04 +00:00
|
|
|
List<UTXO> utxoObjectsToUse = [];
|
2023-01-06 19:55:47 +00:00
|
|
|
|
|
|
|
for (int i = 0;
|
|
|
|
satoshisBeingUsed < amountToSend && i < spendableOutputs.length;
|
|
|
|
i++) {
|
|
|
|
utxoObjectsToUse.add(spendableOutputs[i]);
|
|
|
|
satoshisBeingUsed += spendableOutputs[i].value;
|
|
|
|
outputsBeingUsed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add additional outputs if required
|
|
|
|
for (int i = 0;
|
|
|
|
i < additionalOutputs && outputsBeingUsed < spendableOutputs.length;
|
|
|
|
i++) {
|
|
|
|
utxoObjectsToUse.add(spendableOutputs[outputsBeingUsed]);
|
|
|
|
satoshisBeingUsed += spendableOutputs[outputsBeingUsed].value;
|
|
|
|
outputsBeingUsed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gather required signing data
|
|
|
|
final utxoSigningData = await fetchBuildTxData(utxoObjectsToUse);
|
|
|
|
|
|
|
|
final int vSizeForNoChange = (await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: 0))
|
|
|
|
.item2;
|
|
|
|
|
|
|
|
final int vSizeForWithChange = (await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: satoshisBeingUsed - amountToSend))
|
|
|
|
.item2;
|
|
|
|
|
|
|
|
// Assume 2 outputs, for recipient and payment code script
|
|
|
|
int feeForNoChange = estimateTxFee(
|
|
|
|
vSize: vSizeForNoChange,
|
|
|
|
feeRatePerKB: selectedTxFeeRate,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Assume 3 outputs, for recipient, payment code script, and change
|
|
|
|
int feeForWithChange = estimateTxFee(
|
|
|
|
vSize: vSizeForWithChange,
|
|
|
|
feeRatePerKB: selectedTxFeeRate,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (feeForNoChange < vSizeForNoChange * 1000) {
|
|
|
|
feeForNoChange = vSizeForNoChange * 1000;
|
|
|
|
}
|
|
|
|
if (feeForWithChange < vSizeForWithChange * 1000) {
|
|
|
|
feeForWithChange = vSizeForWithChange * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (satoshisBeingUsed - amountToSend > feeForNoChange + DUST_LIMIT) {
|
|
|
|
// try to add change output due to "left over" amount being greater than
|
|
|
|
// the estimated fee + the dust limit
|
|
|
|
int changeAmount = satoshisBeingUsed - amountToSend - feeForWithChange;
|
|
|
|
|
|
|
|
// check estimates are correct and build notification tx
|
|
|
|
if (changeAmount >= DUST_LIMIT &&
|
|
|
|
satoshisBeingUsed - amountToSend - changeAmount == feeForWithChange) {
|
|
|
|
final txn = await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: changeAmount,
|
|
|
|
);
|
|
|
|
|
|
|
|
int feeBeingPaid = satoshisBeingUsed - amountToSend - changeAmount;
|
|
|
|
|
|
|
|
Map<String, dynamic> transactionObject = {
|
|
|
|
"hex": txn.item1,
|
|
|
|
"recipientPaynym": targetPaymentCodeString,
|
2023-01-06 22:31:36 +00:00
|
|
|
"amount": amountToSend,
|
2023-01-06 19:55:47 +00:00
|
|
|
"fee": feeBeingPaid,
|
|
|
|
"vSize": txn.item2,
|
|
|
|
};
|
|
|
|
return transactionObject;
|
|
|
|
} else {
|
|
|
|
// something broke during fee estimation or the change amount is smaller
|
|
|
|
// than the dust limit. Try without change
|
|
|
|
final txn = await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
int feeBeingPaid = satoshisBeingUsed - amountToSend;
|
|
|
|
|
|
|
|
Map<String, dynamic> transactionObject = {
|
|
|
|
"hex": txn.item1,
|
|
|
|
"recipientPaynym": targetPaymentCodeString,
|
2023-01-06 22:31:36 +00:00
|
|
|
"amount": amountToSend,
|
2023-01-06 19:55:47 +00:00
|
|
|
"fee": feeBeingPaid,
|
|
|
|
"vSize": txn.item2,
|
|
|
|
};
|
|
|
|
return transactionObject;
|
|
|
|
}
|
|
|
|
} else if (satoshisBeingUsed - amountToSend >= feeForNoChange) {
|
|
|
|
// since we already checked if we need to add a change output we can just
|
|
|
|
// build without change here
|
|
|
|
final txn = await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
int feeBeingPaid = satoshisBeingUsed - amountToSend;
|
|
|
|
|
|
|
|
Map<String, dynamic> transactionObject = {
|
|
|
|
"hex": txn.item1,
|
|
|
|
"recipientPaynym": targetPaymentCodeString,
|
2023-01-06 22:31:36 +00:00
|
|
|
"amount": amountToSend,
|
2023-01-06 19:55:47 +00:00
|
|
|
"fee": feeBeingPaid,
|
|
|
|
"vSize": txn.item2,
|
|
|
|
};
|
|
|
|
return transactionObject;
|
|
|
|
} else {
|
|
|
|
// if we get here we do not have enough funds to cover the tx total so we
|
|
|
|
// check if we have any more available outputs and try again
|
|
|
|
if (spendableOutputs.length > outputsBeingUsed) {
|
|
|
|
return buildNotificationTx(
|
|
|
|
selectedTxFeeRate: selectedTxFeeRate,
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
additionalOutputs: additionalOutputs + 1,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw InsufficientBalanceException(
|
|
|
|
"Remaining balance does not cover the network fee.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return tuple with string value equal to the raw tx hex and the int value
|
|
|
|
// equal to its vSize
|
|
|
|
Future<Tuple2<String, int>> _createNotificationTx({
|
|
|
|
required String targetPaymentCodeString,
|
2023-01-12 03:14:04 +00:00
|
|
|
required List<UTXO> utxosToUse,
|
2023-01-06 19:55:47 +00:00
|
|
|
required Map<String, dynamic> utxoSigningData,
|
|
|
|
required int change,
|
|
|
|
}) async {
|
2023-01-05 18:48:18 +00:00
|
|
|
final targetPaymentCode =
|
|
|
|
PaymentCode.fromPaymentCode(targetPaymentCodeString, network);
|
|
|
|
final myCode = await getPaymentCode();
|
|
|
|
|
|
|
|
final utxo = utxosToUse.first;
|
|
|
|
final txPoint = utxo.txid.fromHex.toList();
|
|
|
|
final txPointIndex = utxo.vout;
|
|
|
|
|
|
|
|
final rev = Uint8List(txPoint.length + 4);
|
|
|
|
Util.copyBytes(Uint8List.fromList(txPoint), 0, rev, 0, txPoint.length);
|
|
|
|
final buffer = rev.buffer.asByteData();
|
|
|
|
buffer.setUint32(txPoint.length, txPointIndex, Endian.little);
|
|
|
|
|
2023-01-10 23:55:19 +00:00
|
|
|
final myKeyPair = utxoSigningData[utxo.txid]["keyPair"] as btc_dart.ECPair;
|
2023-01-05 18:48:18 +00:00
|
|
|
|
|
|
|
final S = SecretPoint(
|
|
|
|
myKeyPair.privateKey!,
|
|
|
|
targetPaymentCode.notificationPublicKey(),
|
|
|
|
);
|
|
|
|
|
|
|
|
final blindingMask = PaymentCode.getMask(S.ecdhSecret(), rev);
|
|
|
|
|
|
|
|
final blindedPaymentCode = PaymentCode.blind(
|
|
|
|
myCode.getPayload(),
|
|
|
|
blindingMask,
|
|
|
|
);
|
|
|
|
|
|
|
|
final opReturnScript = bscript.compile([
|
|
|
|
(op.OPS["OP_RETURN"] as int),
|
|
|
|
blindedPaymentCode,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// build a notification tx
|
2023-01-10 23:55:19 +00:00
|
|
|
final txb = btc_dart.TransactionBuilder(network: network);
|
2023-01-05 18:48:18 +00:00
|
|
|
txb.setVersion(1);
|
|
|
|
|
|
|
|
txb.addInput(
|
2023-01-05 22:57:53 +00:00
|
|
|
utxo.txid,
|
2023-01-05 18:48:18 +00:00
|
|
|
txPointIndex,
|
|
|
|
);
|
|
|
|
|
2023-01-06 19:55:47 +00:00
|
|
|
txb.addOutput(targetPaymentCode.notificationAddress(), DUST_LIMIT);
|
2023-01-05 22:57:53 +00:00
|
|
|
txb.addOutput(opReturnScript, 0);
|
|
|
|
|
|
|
|
// TODO: add possible change output and mark output as dangerous
|
2023-01-06 19:55:47 +00:00
|
|
|
if (change > 0) {
|
|
|
|
// generate new change address if current change address has been used
|
2023-01-10 23:55:19 +00:00
|
|
|
await checkChangeAddressForTransactions();
|
2023-01-12 03:14:04 +00:00
|
|
|
final String changeAddress = await currentChangeAddress;
|
2023-01-06 19:55:47 +00:00
|
|
|
txb.addOutput(changeAddress, change);
|
|
|
|
}
|
2023-01-05 18:48:18 +00:00
|
|
|
|
|
|
|
txb.sign(
|
|
|
|
vin: 0,
|
|
|
|
keyPair: myKeyPair,
|
|
|
|
);
|
|
|
|
|
2023-01-06 19:55:47 +00:00
|
|
|
// sign rest of possible inputs
|
|
|
|
for (var i = 1; i < utxosToUse.length - 1; i++) {
|
|
|
|
final txid = utxosToUse[i].txid;
|
|
|
|
txb.sign(
|
|
|
|
vin: i,
|
2023-01-10 23:55:19 +00:00
|
|
|
keyPair: utxoSigningData[txid]["keyPair"] as btc_dart.ECPair,
|
2023-01-06 19:55:47 +00:00
|
|
|
// witnessValue: utxosToUse[i].value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-05 18:48:18 +00:00
|
|
|
final builtTx = txb.build();
|
|
|
|
|
2023-01-06 19:55:47 +00:00
|
|
|
return Tuple2(builtTx.toHex(), builtTx.virtualSize());
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:31:36 +00:00
|
|
|
Future<String> confirmNotificationTx(
|
|
|
|
{required Map<String, dynamic> preparedTx}) async {
|
|
|
|
try {
|
|
|
|
Logging.instance.log("confirmNotificationTx txData: $preparedTx",
|
|
|
|
level: LogLevel.Info);
|
|
|
|
final txHash = await electrumXClient.broadcastTransaction(
|
|
|
|
rawTx: preparedTx["hex"] as String);
|
|
|
|
Logging.instance.log("Sent txHash: $txHash", level: LogLevel.Info);
|
|
|
|
|
|
|
|
await updatePaynymNotificationInfo(
|
|
|
|
txid: txHash,
|
|
|
|
confirmed: false,
|
2023-01-09 21:42:37 +00:00
|
|
|
paymentCodeString: preparedTx["address"] as String,
|
2023-01-06 22:31:36 +00:00
|
|
|
);
|
|
|
|
return txHash;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("Exception rethrown from confirmSend(): $e\n$s",
|
|
|
|
level: LogLevel.Error);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 19:55:47 +00:00
|
|
|
|
2023-01-06 22:31:36 +00:00
|
|
|
// Future<bool> hasConfirmedNotificationTxSentTo(
|
|
|
|
// String paymentCodeString) async {
|
|
|
|
// final targetPaymentCode =
|
|
|
|
// PaymentCode.fromPaymentCode(paymentCodeString, network);
|
|
|
|
// final targetNotificationAddress = targetPaymentCode.notificationAddress();
|
|
|
|
//
|
|
|
|
// final myTxHistory = (await transactionData)
|
|
|
|
// .getAllTransactions()
|
|
|
|
// .entries
|
|
|
|
// .map((e) => e.value)
|
|
|
|
// .where((e) =>
|
|
|
|
// e.txType == "Sent" && e.address == targetNotificationAddress);
|
|
|
|
//
|
|
|
|
// return myTxHistory.isNotEmpty;
|
|
|
|
// }
|
|
|
|
|
|
|
|
bool hasConnected(String paymentCodeString) {
|
|
|
|
return getPaynymNotificationTxInfo()
|
2023-01-09 21:42:37 +00:00
|
|
|
.values
|
2023-01-06 22:31:36 +00:00
|
|
|
.where((e) => e["paymentCodeString"] == paymentCodeString)
|
|
|
|
.isNotEmpty;
|
|
|
|
}
|
2023-01-06 19:55:47 +00:00
|
|
|
|
2023-01-06 22:31:36 +00:00
|
|
|
bool hasConnectedConfirmed(String paymentCodeString) {
|
|
|
|
return getPaynymNotificationTxInfo()
|
2023-01-09 21:42:37 +00:00
|
|
|
.values
|
2023-01-06 22:31:36 +00:00
|
|
|
.where((e) =>
|
|
|
|
e["paymentCodeString"] == paymentCodeString &&
|
|
|
|
e["confirmed"] == true)
|
|
|
|
.isNotEmpty;
|
2023-01-06 19:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch paynym notification tx meta data
|
2023-01-09 21:42:37 +00:00
|
|
|
Map<String, dynamic> getPaynymNotificationTxInfo() {
|
|
|
|
final map = DB.instance.get<dynamic>(
|
|
|
|
boxName: walletId, key: "paynymNotificationTxInfo") as Map? ??
|
2023-01-06 19:55:47 +00:00
|
|
|
{};
|
|
|
|
|
2023-01-09 21:42:37 +00:00
|
|
|
return Map<String, dynamic>.from(map);
|
2023-01-06 19:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add/update paynym notification tx meta data entry
|
|
|
|
Future<void> updatePaynymNotificationInfo({
|
|
|
|
required String txid,
|
|
|
|
required bool confirmed,
|
|
|
|
required String paymentCodeString,
|
|
|
|
}) async {
|
|
|
|
final data = getPaynymNotificationTxInfo();
|
2023-01-09 21:42:37 +00:00
|
|
|
data[txid] = {
|
2023-01-06 19:55:47 +00:00
|
|
|
"txid": txid,
|
|
|
|
"confirmed": confirmed,
|
|
|
|
"paymentCodeString": paymentCodeString,
|
2023-01-09 21:42:37 +00:00
|
|
|
};
|
2023-01-06 19:55:47 +00:00
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: walletId,
|
|
|
|
key: "paynymNotificationTxInfo",
|
|
|
|
value: data,
|
|
|
|
);
|
2023-01-05 18:48:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:25:09 +00:00
|
|
|
Future<Tuple3<Transaction, List<Output>, List<Input>>> parseTransaction(
|
2023-01-05 18:48:18 +00:00
|
|
|
Map<String, dynamic> txData,
|
|
|
|
dynamic electrumxClient,
|
2023-01-10 23:55:19 +00:00
|
|
|
List<Address> myAddresses,
|
2023-01-05 18:48:18 +00:00
|
|
|
Coin coin,
|
|
|
|
int minConfirms,
|
|
|
|
) async {
|
2023-01-13 19:25:09 +00:00
|
|
|
final transactionAddress = txData["address"] as Address;
|
|
|
|
|
2023-01-10 23:55:19 +00:00
|
|
|
Set<String> receivingAddresses = myAddresses
|
|
|
|
.where((e) => e.subType == AddressSubType.receiving)
|
|
|
|
.map((e) => e.value)
|
|
|
|
.toSet();
|
|
|
|
Set<String> changeAddresses = myAddresses
|
|
|
|
.where((e) => e.subType == AddressSubType.change)
|
|
|
|
.map((e) => e.value)
|
|
|
|
.toSet();
|
|
|
|
|
2023-01-05 18:48:18 +00:00
|
|
|
Set<String> inputAddresses = {};
|
|
|
|
Set<String> outputAddresses = {};
|
|
|
|
|
|
|
|
int totalInputValue = 0;
|
|
|
|
int totalOutputValue = 0;
|
|
|
|
|
|
|
|
int amountSentFromWallet = 0;
|
|
|
|
int amountReceivedInWallet = 0;
|
2023-01-10 23:55:19 +00:00
|
|
|
int changeAmount = 0;
|
2023-01-05 18:48:18 +00:00
|
|
|
|
|
|
|
// parse inputs
|
|
|
|
for (final input in txData["vin"] as List) {
|
|
|
|
final prevTxid = input["txid"] as String;
|
|
|
|
final prevOut = input["vout"] as int;
|
|
|
|
|
|
|
|
// fetch input tx to get address
|
|
|
|
final inputTx = await electrumxClient.getTransaction(
|
|
|
|
txHash: prevTxid,
|
|
|
|
coin: coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
for (final output in inputTx["vout"] as List) {
|
|
|
|
// check matching output
|
|
|
|
if (prevOut == output["n"]) {
|
|
|
|
// get value
|
|
|
|
final value = Format.decimalAmountToSatoshis(
|
|
|
|
Decimal.parse(output["value"].toString()),
|
|
|
|
coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
// add value to total
|
|
|
|
totalInputValue += value;
|
|
|
|
|
|
|
|
// get input(prevOut) address
|
|
|
|
final address = output["scriptPubKey"]?["addresses"]?[0] as String? ??
|
|
|
|
output["scriptPubKey"]?["address"] as String?;
|
2023-01-09 21:42:37 +00:00
|
|
|
|
2023-01-05 18:48:18 +00:00
|
|
|
if (address != null) {
|
|
|
|
inputAddresses.add(address);
|
|
|
|
|
|
|
|
// if input was from my wallet, add value to amount sent
|
2023-01-10 23:55:19 +00:00
|
|
|
if (receivingAddresses.contains(address) ||
|
|
|
|
changeAddresses.contains(address)) {
|
2023-01-05 18:48:18 +00:00
|
|
|
amountSentFromWallet += value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse outputs
|
|
|
|
for (final output in txData["vout"] as List) {
|
|
|
|
// get value
|
|
|
|
final value = Format.decimalAmountToSatoshis(
|
|
|
|
Decimal.parse(output["value"].toString()),
|
|
|
|
coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
// add value to total
|
|
|
|
totalOutputValue += value;
|
|
|
|
|
|
|
|
// get output address
|
2023-01-09 21:42:37 +00:00
|
|
|
final address = output["scriptPubKey"]?["addresses"]?[0] as String? ??
|
|
|
|
output["scriptPubKey"]?["address"] as String?;
|
2023-01-05 18:48:18 +00:00
|
|
|
if (address != null) {
|
|
|
|
outputAddresses.add(address);
|
|
|
|
|
2023-01-10 23:55:19 +00:00
|
|
|
// if output was to my wallet, add value to amount received
|
|
|
|
if (receivingAddresses.contains(address)) {
|
2023-01-05 18:48:18 +00:00
|
|
|
amountReceivedInWallet += value;
|
2023-01-10 23:55:19 +00:00
|
|
|
} else if (changeAddresses.contains(address)) {
|
|
|
|
changeAmount += value;
|
2023-01-05 18:48:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 23:55:19 +00:00
|
|
|
final mySentFromAddresses = [
|
|
|
|
...receivingAddresses.intersection(inputAddresses),
|
|
|
|
...changeAddresses.intersection(inputAddresses)
|
|
|
|
];
|
|
|
|
final myReceivedOnAddresses =
|
|
|
|
receivingAddresses.intersection(outputAddresses);
|
|
|
|
final myChangeReceivedOnAddresses =
|
|
|
|
changeAddresses.intersection(outputAddresses);
|
2023-01-05 18:48:18 +00:00
|
|
|
|
|
|
|
final fee = totalInputValue - totalOutputValue;
|
|
|
|
|
2023-01-10 23:55:19 +00:00
|
|
|
final tx = Transaction();
|
|
|
|
tx.txid = txData["txid"] as String;
|
|
|
|
tx.timestamp = txData["blocktime"] as int? ??
|
2023-01-05 18:48:18 +00:00
|
|
|
(DateTime.now().millisecondsSinceEpoch ~/ 1000);
|
2023-01-10 23:55:19 +00:00
|
|
|
|
2023-01-13 17:11:41 +00:00
|
|
|
// this should be the address we used to originally fetch the tx so we should
|
|
|
|
// be able to easily figure out if the tx is a send or receive
|
2023-01-13 19:25:09 +00:00
|
|
|
tx.address.value = transactionAddress;
|
2023-01-13 17:11:41 +00:00
|
|
|
|
2023-01-05 18:48:18 +00:00
|
|
|
if (mySentFromAddresses.isNotEmpty && myReceivedOnAddresses.isNotEmpty) {
|
|
|
|
// tx is sent to self
|
2023-01-10 23:57:00 +00:00
|
|
|
tx.type = TransactionType.sentToSelf;
|
2023-01-10 23:55:19 +00:00
|
|
|
tx.amount =
|
|
|
|
amountSentFromWallet - amountReceivedInWallet - fee - changeAmount;
|
2023-01-05 18:48:18 +00:00
|
|
|
} else if (mySentFromAddresses.isNotEmpty) {
|
|
|
|
// outgoing tx
|
2023-01-10 23:55:19 +00:00
|
|
|
tx.type = TransactionType.outgoing;
|
|
|
|
tx.amount = amountSentFromWallet - changeAmount - fee;
|
2023-01-05 18:48:18 +00:00
|
|
|
} else {
|
|
|
|
// incoming tx
|
2023-01-10 23:55:19 +00:00
|
|
|
tx.type = TransactionType.incoming;
|
|
|
|
tx.amount = amountReceivedInWallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: other subtypes
|
|
|
|
tx.subType = TransactionSubType.none;
|
|
|
|
|
|
|
|
tx.fee = fee;
|
2023-01-13 17:11:41 +00:00
|
|
|
|
2023-01-13 19:25:09 +00:00
|
|
|
log("transactionAddress: $transactionAddress");
|
2023-01-13 17:11:41 +00:00
|
|
|
log("mySentFromAddresses: $mySentFromAddresses");
|
|
|
|
log("myReceivedOnAddresses: $myReceivedOnAddresses");
|
|
|
|
log("myChangeReceivedOnAddresses: $myChangeReceivedOnAddresses");
|
2023-01-10 23:55:19 +00:00
|
|
|
|
2023-01-13 19:25:09 +00:00
|
|
|
List<Output> outs = [];
|
|
|
|
List<Input> ins = [];
|
|
|
|
|
2023-01-10 23:55:19 +00:00
|
|
|
for (final json in txData["vin"] as List) {
|
|
|
|
bool isCoinBase = json['coinbase'] != null;
|
|
|
|
final input = Input();
|
|
|
|
input.txid = json['txid'] as String;
|
|
|
|
input.vout = json['vout'] as int? ?? -1;
|
|
|
|
input.scriptSig = json['scriptSig']?['hex'] as String?;
|
|
|
|
input.scriptSigAsm = json['scriptSig']?['asm'] as String?;
|
|
|
|
input.isCoinbase = isCoinBase ? isCoinBase : json['is_coinbase'] as bool?;
|
|
|
|
input.sequence = json['sequence'] as int?;
|
|
|
|
input.innerRedeemScriptAsm = json['innerRedeemscriptAsm'] as String?;
|
2023-01-13 19:25:09 +00:00
|
|
|
ins.add(input);
|
2023-01-05 18:48:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 23:55:19 +00:00
|
|
|
for (final json in txData["vout"] as List) {
|
|
|
|
final output = Output();
|
|
|
|
output.scriptPubKey = json['scriptPubKey']?['hex'] as String?;
|
|
|
|
output.scriptPubKeyAsm = json['scriptPubKey']?['asm'] as String?;
|
|
|
|
output.scriptPubKeyType = json['scriptPubKey']?['type'] as String?;
|
|
|
|
output.scriptPubKeyAddress =
|
|
|
|
json["scriptPubKey"]?["addresses"]?[0] as String? ??
|
|
|
|
json['scriptPubKey']['type'] as String;
|
|
|
|
output.value = Format.decimalAmountToSatoshis(
|
|
|
|
Decimal.parse(json["value"].toString()),
|
|
|
|
coin,
|
|
|
|
);
|
2023-01-13 19:25:09 +00:00
|
|
|
outs.add(output);
|
2023-01-10 23:55:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:14:04 +00:00
|
|
|
tx.height = txData["height"] as int?;
|
2023-01-10 23:55:19 +00:00
|
|
|
|
|
|
|
//TODO: change these for epic (or other coins that need it)
|
2023-01-12 03:14:04 +00:00
|
|
|
tx.isCancelled = false;
|
2023-01-10 23:55:19 +00:00
|
|
|
tx.slateId = null;
|
|
|
|
tx.otherData = null;
|
2023-01-12 04:45:59 +00:00
|
|
|
tx.isLelantus = null;
|
2023-01-05 18:48:18 +00:00
|
|
|
|
2023-01-13 19:25:09 +00:00
|
|
|
return Tuple3(tx, outs, ins);
|
2022-12-20 23:00:03 +00:00
|
|
|
}
|