feat: p2tr spend

This commit is contained in:
Rafael Saes 2023-11-18 16:38:00 -03:00
parent 2f6ad5248d
commit ae6c72909a
2 changed files with 175 additions and 178 deletions

View file

@ -189,234 +189,235 @@ abstract class ElectrumWalletBase
@override @override
Future<PendingTransaction> createTransaction(Object credentials) async { Future<PendingTransaction> createTransaction(Object credentials) async {
const minAmount = 546; try {
final transactionCredentials = credentials as BitcoinTransactionCredentials; const minAmount = 546;
final inputs = <BitcoinUnspent>[]; final transactionCredentials = credentials as BitcoinTransactionCredentials;
final outputs = transactionCredentials.outputs; final inputs = <BitcoinUnspent>[];
final hasMultiDestination = outputs.length > 1; final outputs = transactionCredentials.outputs;
var allInputsAmount = 0; final hasMultiDestination = outputs.length > 1;
var allInputsAmount = 0;
if (unspentCoins.isEmpty) { if (unspentCoins.isEmpty) {
await updateUnspent(); await updateUnspent();
}
for (final utx in unspentCoins) {
if (utx.isSending) {
allInputsAmount += utx.value;
inputs.add(utx);
}
}
if (inputs.isEmpty) {
throw BitcoinTransactionNoInputsException();
}
final allAmountFee = 222;
final allAmount = allInputsAmount - allAmountFee;
var credentialsAmount = 0;
var amount = 0;
var fee = 0;
if (hasMultiDestination) {
if (outputs.any((item) => item.sendAll || item.formattedCryptoAmount! <= 0)) {
throw BitcoinTransactionWrongBalanceException(currency);
} }
credentialsAmount = outputs.fold(0, (acc, value) => acc + value.formattedCryptoAmount!); for (int i = 0; i < unspentCoins.length; i++) {
final utx = unspentCoins[i];
if (allAmount - credentialsAmount < minAmount) { if (utx.isSending) {
throw BitcoinTransactionWrongBalanceException(currency); allInputsAmount += utx.value;
inputs.add(utx);
}
} }
amount = credentialsAmount; if (inputs.isEmpty) {
throw BitcoinTransactionNoInputsException();
}
if (transactionCredentials.feeRate != null) { final allAmountFee = 222;
fee = calculateEstimatedFeeWithFeeRate(transactionCredentials.feeRate!, amount,
outputsCount: outputs.length + 1); final allAmount = allInputsAmount - allAmountFee;
var credentialsAmount = 0;
var amount = 0;
var fee = 0;
if (hasMultiDestination) {
if (outputs.any((item) => item.sendAll || item.formattedCryptoAmount! <= 0)) {
throw BitcoinTransactionWrongBalanceException(currency);
}
credentialsAmount = outputs.fold(0, (acc, value) => acc + value.formattedCryptoAmount!);
if (allAmount - credentialsAmount < minAmount) {
throw BitcoinTransactionWrongBalanceException(currency);
}
amount = credentialsAmount;
if (transactionCredentials.feeRate != null) {
fee = calculateEstimatedFeeWithFeeRate(transactionCredentials.feeRate!, amount,
outputsCount: outputs.length + 1);
} else {
fee = calculateEstimatedFee(transactionCredentials.priority, amount,
outputsCount: outputs.length + 1);
}
} else { } else {
fee = calculateEstimatedFee(transactionCredentials.priority, amount, final output = outputs.first;
outputsCount: outputs.length + 1); credentialsAmount = !output.sendAll ? output.formattedCryptoAmount! : 0;
}
} else {
final output = outputs.first;
credentialsAmount = !output.sendAll ? output.formattedCryptoAmount! : 0;
if (credentialsAmount > allAmount) { if (credentialsAmount > allAmount) {
throw BitcoinTransactionWrongBalanceException(currency); throw BitcoinTransactionWrongBalanceException(currency);
}
amount = output.sendAll || allAmount - credentialsAmount < minAmount
? allAmount
: credentialsAmount;
if (output.sendAll || amount == allAmount) {
fee = allAmountFee;
} else if (transactionCredentials.feeRate != null) {
fee = calculateEstimatedFeeWithFeeRate(transactionCredentials.feeRate!, amount);
} else {
fee = calculateEstimatedFee(transactionCredentials.priority, amount);
}
} }
amount = output.sendAll || allAmount - credentialsAmount < minAmount if (fee == 0 && networkType == bitcoin.bitcoin) {
? allAmount // throw BitcoinTransactionWrongBalanceException(currency);
: credentialsAmount;
if (output.sendAll || amount == allAmount) {
fee = allAmountFee;
} else if (transactionCredentials.feeRate != null) {
fee = calculateEstimatedFeeWithFeeRate(transactionCredentials.feeRate!, amount);
} else {
fee = calculateEstimatedFee(transactionCredentials.priority, amount);
} }
}
if (fee == 0 && networkType == bitcoin.bitcoin) { final totalAmount = amount + fee;
// throw BitcoinTransactionWrongBalanceException(currency);
}
final totalAmount = amount + fee; if (totalAmount > balance[currency]!.confirmed || totalAmount > allInputsAmount) {
// throw BitcoinTransactionWrongBalanceException(currency);
}
if (totalAmount > balance[currency]!.confirmed || totalAmount > allInputsAmount) { final changeAddress = await walletAddresses.getChangeAddress();
// throw BitcoinTransactionWrongBalanceException(currency); var leftAmount = totalAmount;
} var totalInputAmount = 0;
final txb = bitcoin.TransactionBuilder(network: networkType); final txb = bitcoin.TransactionBuilder(network: networkType, version: 1);
final changeAddress = await walletAddresses.getChangeAddress();
var leftAmount = totalAmount;
var totalInputAmount = 0;
inputs.clear(); List<bitcoin.PrivateKeyInfo> inputPrivKeys = [];
List<bitcoin.Outpoint> outpoints = [];
for (final utx in unspentCoins) { for (int i = 0; i < inputs.length; i++) {
if (utx.isSending) { final utx = inputs[i];
leftAmount = leftAmount - utx.value; leftAmount = utx.value - leftAmount;
totalInputAmount += utx.value; totalInputAmount += utx.value;
inputs.add(utx);
if (leftAmount <= 0) { if (leftAmount <= 0) {
break; break;
} }
}
}
if (inputs.isEmpty) { final isSilentPayment = utx.bitcoinAddressRecord.silentPaymentTweak != null;
throw BitcoinTransactionNoInputsException();
}
if (amount <= 0 || totalInputAmount < totalAmount) { outpoints.add(bitcoin.Outpoint(txid: utx.hash, index: utx.vout));
// throw BitcoinTransactionWrongBalanceException(currency);
}
txb.setVersion(1); if (isSilentPayment) {
List<bitcoin.PrivateKeyInfo> inputPrivKeys = []; // https://github.com/bitcoin/bips/blob/c55f80c53c98642357712c1839cfdc0551d531c4/bip-0352.mediawiki#user-content-Spending
List<bitcoin.Outpoint> outpoints = []; final d = bitcoin.PrivateKey.fromHex(bitcoin.getSecp256k1(),
inputs.forEach((input) { walletAddresses.silentAddress!.spendPrivkey.toCompressedHex())
final isSilentPayment = input.bitcoinAddressRecord.silentPaymentTweak != null; .tweakAdd(utx.bitcoinAddressRecord.silentPaymentTweak!.bigint)!;
outpoints.add(bitcoin.Outpoint(txid: input.hash, index: input.vout)); inputPrivKeys.add(bitcoin.PrivateKeyInfo(d, true));
if (isSilentPayment) { final point = bitcoin.ECPublic.fromHex(d.publicKey.toHex()).toTapPoint();
// https://github.com/bitcoin/bips/blob/c55f80c53c98642357712c1839cfdc0551d531c4/bip-0352.mediawiki#user-content-Spending final p2tr = bitcoin.P2trAddress(program: point);
final d = walletAddresses.silentAddress!.spendPrivkey
.tweakAdd(input.bitcoinAddressRecord.silentPaymentTweak!.bigint)!;
inputPrivKeys.add(bitcoin.PrivateKeyInfo(d, true)); bitcoin.ECPair keyPair = bitcoin.ECPair.fromPrivateKey(d.toCompressedHex().fromHex,
compressed: true, network: networkType);
print(["output", d]); txb.addInput(
utx.hash, utx.vout, null, p2tr.toScriptPubKey().toBytes(), keyPair, utx.value);
continue;
}
final p2tr = bitcoin
.P2TR(
data: bitcoin.PaymentData(pubkey: d.publicKey.toCompressedHex().fromHex),
network: networkType)
.data;
print(["output", p2tr.output]);
txb.addInput(input.hash, input.vout, null, p2tr.output);
} else {
inputPrivKeys.add(bitcoin.PrivateKeyInfo( inputPrivKeys.add(bitcoin.PrivateKeyInfo(
bitcoin.PrivateKey.fromHex( bitcoin.PrivateKey.fromHex(
bitcoin.getSecp256k1(), bitcoin.getSecp256k1(),
generateKeyPair( generateKeyPair(
hd: input.bitcoinAddressRecord.isHidden hd: utx.bitcoinAddressRecord.isHidden
? walletAddresses.sideHd ? walletAddresses.sideHd
: walletAddresses.mainHd, : walletAddresses.mainHd,
index: input.bitcoinAddressRecord.index, index: utx.bitcoinAddressRecord.index,
network: networkType) network: networkType)
.privateKey! .privateKey!
.hex), .hex),
false)); false));
if (input.isP2wpkh) { bitcoin.ECPair keyPair = generateKeyPair(
hd: utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd,
index: utx.bitcoinAddressRecord.index,
network: networkType);
if (utx.isP2wpkh) {
final p2wpkh = bitcoin final p2wpkh = bitcoin
.P2WPKH( .P2WPKH(
data: generatePaymentData( data: generatePaymentData(
hd: input.bitcoinAddressRecord.isHidden hd: utx.bitcoinAddressRecord.isHidden
? walletAddresses.sideHd ? walletAddresses.sideHd
: walletAddresses.mainHd, : walletAddresses.mainHd,
index: input.bitcoinAddressRecord.index), index: utx.bitcoinAddressRecord.index),
network: networkType) network: networkType)
.data; .data;
txb.addInput(input.hash, input.vout, null, p2wpkh.output); txb.addInput(utx.hash, utx.vout, null, p2wpkh.output, keyPair, utx.value);
} else { continue;
txb.addInput(input.hash, input.vout);
} }
txb.addInput(utx.hash, utx.vout, null, null, keyPair, utx.value);
} }
});
List<bitcoin.SilentPaymentDestination> silentAddresses = []; if (txb.inputs.isEmpty) {
outputs.forEach((item) { throw BitcoinTransactionNoInputsException();
final outputAmount = hasMultiDestination ? item.formattedCryptoAmount : amount;
final outputAddress = item.isParsedAddress ? item.extractedAddress! : item.address;
if (outputAddress.startsWith('tsp1')) {
silentAddresses
.add(bitcoin.SilentPaymentDestination.fromAddress(outputAddress, outputAmount!));
} else {
txb.addOutput(addressToOutputScript(outputAddress, networkType), outputAmount!);
} }
});
if (silentAddresses.isNotEmpty) { if (amount <= 0 || totalInputAmount < totalAmount) {
final outpointsHash = bitcoin.SilentPayment.hashOutpoints(outpoints); // throw BitcoinTransactionWrongBalanceException(currency);
}
final aSum = bitcoin.SilentPayment.getSumInputPrivKeys(inputPrivKeys); List<bitcoin.SilentPaymentDestination> silentPaymentDestinations = [];
outputs.forEach((item) {
final outputAmount = hasMultiDestination ? item.formattedCryptoAmount : amount;
final outputAddress = item.isParsedAddress ? item.extractedAddress! : item.address;
if (outputAddress.startsWith('tsp1')) {
silentPaymentDestinations
.add(bitcoin.SilentPaymentDestination.fromAddress(outputAddress, outputAmount!));
} else {
txb.addOutput(addressToOutputScript(outputAddress, networkType), outputAmount!);
}
});
final generatedOutputs = bitcoin.SilentPayment.generateMultipleRecipientPubkeys( if (silentPaymentDestinations.isNotEmpty) {
aSum, outpointsHash, silentAddresses); final outpointsHash = bitcoin.SilentPayment.hashOutpoints(outpoints);
final aSum = bitcoin.SilentPayment.getSumInputPrivKeys(inputPrivKeys);
final generatedOutputs = bitcoin.SilentPayment.generateMultipleRecipientPubkeys(
aSum, outpointsHash, silentPaymentDestinations);
generatedOutputs.forEach((recipientSilentAddress, generatedOutput) { generatedOutputs.forEach((recipientSilentAddress, generatedOutput) {
generatedOutput.forEach((output) { generatedOutput.forEach((output) {
final generatedPubkey = output.$1.toHex(); final generatedPubkey = output.$1.toHex();
// TODO: pubkeyToOutputScript (?) // TODO: DRY code: pubkeyToOutputScript (?)
final point = bitcoin.ECPublic.fromHex(generatedPubkey).toTapPoint(); final point = bitcoin.ECPublic.fromHex(generatedPubkey).toTapPoint();
final p2tr = bitcoin.P2trAddress(program: point); final p2tr = bitcoin.P2trAddress(program: point);
txb.addOutput(p2tr.toScriptPubKey().toBytes(), amount); txb.addOutput(p2tr.toScriptPubKey().toBytes(), amount);
});
}); });
}); }
final estimatedSize = estimatedTransactionSize(inputs.length, outputs.length + 1);
var feeAmount = 222;
// if (transactionCredentials.feeRate != null) {
// feeAmount = transactionCredentials.feeRate! * estimatedSize;
// } else {
// feeAmount = feeRate(transactionCredentials.priority!) * estimatedSize;
// }
final changeValue = totalInputAmount - amount - feeAmount;
if (changeValue > minAmount) {
txb.addOutput(changeAddress, changeValue);
}
final amounts = txb.inputs.map((utx) => utx.value!).toList();
final scriptPubKeys = txb.inputs.map((utx) => utx.prevOutScript!).toList();
for (var i = 0; i < inputs.length; i++) {
txb.sign(vin: i, amounts: amounts, scriptPubKeys: scriptPubKeys, inputs: inputs);
}
return PendingBitcoinTransaction(txb.build(), type,
electrumClient: electrumClient, amount: amount, fee: fee)
..addListener((transaction) async {
transactionHistory.addOne(transaction);
await updateBalance();
});
} catch (e, stacktrace) {
print(stacktrace);
print(e.toString());
rethrow;
} }
final estimatedSize = estimatedTransactionSize(inputs.length, outputs.length + 1);
var feeAmount = 222;
// if (transactionCredentials.feeRate != null) {
// feeAmount = transactionCredentials.feeRate! * estimatedSize;
// } else {
// feeAmount = feeRate(transactionCredentials.priority!) * estimatedSize;
// }
final changeValue = totalInputAmount - amount - feeAmount;
if (changeValue > minAmount) {
txb.addOutput(changeAddress, changeValue);
}
for (var i = 0; i < inputs.length; i++) {
final input = inputs[i];
final keyPair = generateKeyPair(
hd: input.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd,
index: input.bitcoinAddressRecord.index,
network: networkType);
final witnessValue = input.isP2wpkh ? input.value : null;
txb.sign(vin: i, keyPair: keyPair, witnessValue: witnessValue);
}
return PendingBitcoinTransaction(txb.build(), type,
electrumClient: electrumClient, amount: amount, fee: fee)
..addListener((transaction) async {
transactionHistory.addOne(transaction);
await updateBalance();
});
} }
String toJSON() => json.encode({ String toJSON() => json.encode({
@ -712,9 +713,7 @@ abstract class ElectrumWalletBase
final normalizedHistories = <Map<String, dynamic>>[]; final normalizedHistories = <Map<String, dynamic>>[];
walletAddresses.addresses.forEach((addressRecord) { walletAddresses.addresses.forEach((addressRecord) {
if (addressRecord.address == if (addressRecord.address ==
"tb1pch9qmsq87wy4my4akd60x2r2yt784zfmfwqeuk7w7g7u45za4ktq9pdnmf") { "tb1pch9qmsq87wy4my4akd60x2r2yt784zfmfwqeuk7w7g7u45za4ktq9pdnmf") {}
print(["during fetch txs", addressRecord.address, addressRecord.silentPaymentTweak]);
}
final sh = scriptHash(addressRecord.address, networkType: networkType); final sh = scriptHash(addressRecord.address, networkType: networkType);
addressHashes[sh] = addressRecord; addressHashes[sh] = addressRecord;
}); });

View file

@ -9,9 +9,7 @@ import 'package:cw_core/wallet_type.dart';
class PendingBitcoinTransaction with PendingTransaction { class PendingBitcoinTransaction with PendingTransaction {
PendingBitcoinTransaction(this._tx, this.type, PendingBitcoinTransaction(this._tx, this.type,
{required this.electrumClient, {required this.electrumClient, required this.amount, required this.fee})
required this.amount,
required this.fee})
: _listeners = <void Function(ElectrumTransactionInfo transaction)>[]; : _listeners = <void Function(ElectrumTransactionInfo transaction)>[];
final WalletType type; final WalletType type;
@ -36,8 +34,9 @@ class PendingBitcoinTransaction with PendingTransaction {
@override @override
Future<void> commit() async { Future<void> commit() async {
print(["hex", _tx.txHex ?? _tx.toHex()]);
final result = final result =
await electrumClient.broadcastTransaction(transactionRaw: _tx.toHex()); await electrumClient.broadcastTransaction(transactionRaw: _tx.txHex ?? _tx.toHex());
if (result.isEmpty) { if (result.isEmpty) {
throw BitcoinCommitTransactionException(); throw BitcoinCommitTransactionException();
@ -46,8 +45,7 @@ class PendingBitcoinTransaction with PendingTransaction {
_listeners?.forEach((listener) => listener(transactionInfo())); _listeners?.forEach((listener) => listener(transactionInfo()));
} }
void addListener( void addListener(void Function(ElectrumTransactionInfo transaction) listener) =>
void Function(ElectrumTransactionInfo transaction) listener) =>
_listeners.add(listener); _listeners.add(listener);
ElectrumTransactionInfo transactionInfo() => ElectrumTransactionInfo(type, ElectrumTransactionInfo transactionInfo() => ElectrumTransactionInfo(type,