2026-06-11 10:47:40 +02:00
|
|
|
using NBitcoin;
|
|
|
|
|
using NBitcoin.Policy;
|
|
|
|
|
using PalladiumWallet.Core.Chain;
|
|
|
|
|
using PalladiumWallet.Core.Crypto;
|
|
|
|
|
using PalladiumWallet.Core.Storage;
|
|
|
|
|
|
|
|
|
|
namespace PalladiumWallet.Core.Wallet;
|
|
|
|
|
|
2026-06-16 14:40:06 +02:00
|
|
|
/// <summary>Result of a transaction build operation.</summary>
|
2026-06-11 10:47:40 +02:00
|
|
|
public sealed class BuiltTransaction
|
|
|
|
|
{
|
|
|
|
|
public required Transaction Transaction { get; init; }
|
|
|
|
|
public required Money Fee { get; init; }
|
|
|
|
|
public required FeeRate FeeRate { get; init; }
|
|
|
|
|
public required bool Signed { get; init; }
|
|
|
|
|
|
2026-06-16 14:40:06 +02:00
|
|
|
/// <summary>PSBT for watch-only/air-gapped/multisig flows (§6.5).</summary>
|
2026-06-11 10:47:40 +02:00
|
|
|
public required PSBT Psbt { get; init; }
|
|
|
|
|
|
|
|
|
|
public string ToHex() => Transaction.ToHex();
|
|
|
|
|
public string Txid => Transaction.GetHash().ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-06-16 14:40:06 +02:00
|
|
|
/// Transaction construction and signing (blueprint §6) on top of NBitcoin primitives:
|
|
|
|
|
/// coin selection (manual or automatic), fixed fee rate, send-all with fee subtracted,
|
|
|
|
|
/// change on the internal chain, RBF on by default.
|
|
|
|
|
/// With a watch-only account produces an unsigned PSBT (§6.5).
|
2026-06-11 10:47:40 +02:00
|
|
|
/// </summary>
|
2026-06-15 14:20:10 +02:00
|
|
|
public sealed class TransactionFactory(IWalletAccount account)
|
2026-06-11 10:47:40 +02:00
|
|
|
{
|
|
|
|
|
private Network Network => PalladiumNetworks.For(account.Profile.Kind);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-06-16 14:40:06 +02:00
|
|
|
/// Builds (and signs if possible) a transaction.
|
2026-06-11 10:47:40 +02:00
|
|
|
/// </summary>
|
2026-06-16 14:40:06 +02:00
|
|
|
/// <param name="utxos">Selected UTXOs (coin control §6.2) or all spendable ones.</param>
|
|
|
|
|
/// <param name="transactions">Source transactions for the UTXOs (txid → tx), from sync.</param>
|
|
|
|
|
/// <param name="destination">Recipient address.</param>
|
|
|
|
|
/// <param name="amountSats">Amount; ignored when <paramref name="sendAll"/> is true.</param>
|
|
|
|
|
/// <param name="feeRateSatPerVByte">Fixed fee rate in sat/vByte (§6.4).</param>
|
|
|
|
|
/// <param name="changeIndex">Index of the next change address (internal chain).</param>
|
|
|
|
|
/// <param name="sendAll">Send all: fee subtracted from the amount (§6.1).</param>
|
2026-06-11 10:47:40 +02:00
|
|
|
public BuiltTransaction Build(
|
|
|
|
|
IReadOnlyList<CachedUtxo> utxos,
|
|
|
|
|
IReadOnlyDictionary<string, Transaction> transactions,
|
|
|
|
|
BitcoinAddress destination,
|
|
|
|
|
long amountSats,
|
|
|
|
|
decimal feeRateSatPerVByte,
|
|
|
|
|
int changeIndex,
|
|
|
|
|
bool sendAll = false)
|
|
|
|
|
{
|
2026-06-16 14:40:06 +02:00
|
|
|
// Only confirmed UTXOs are spent: mempool funds appear in the "pending"
|
|
|
|
|
// balance but are not spendable until confirmed.
|
2026-06-11 11:34:13 +02:00
|
|
|
var spendable = utxos.Where(u => !u.Frozen && u.Height > 0).ToList();
|
2026-06-11 10:47:40 +02:00
|
|
|
if (spendable.Count == 0)
|
2026-06-11 11:34:13 +02:00
|
|
|
{
|
|
|
|
|
var pending = utxos.Where(u => !u.Frozen && u.Height <= 0).Sum(u => u.ValueSats);
|
|
|
|
|
throw new WalletSpendException(pending > 0
|
2026-06-16 14:40:06 +02:00
|
|
|
? $"No confirmed funds: {CoinAmount.Format(pending)} pending confirmation (not spendable)."
|
|
|
|
|
: "No spendable UTXOs selected.");
|
2026-06-11 11:34:13 +02:00
|
|
|
}
|
2026-06-11 10:47:40 +02:00
|
|
|
|
|
|
|
|
var coins = spendable.Select(u => new Coin(
|
|
|
|
|
new OutPoint(uint256.Parse(u.Txid), (uint)u.Vout),
|
|
|
|
|
transactions[u.Txid].Outputs[u.Vout])).ToList();
|
|
|
|
|
|
|
|
|
|
var feeRate = new FeeRate(Money.Satoshis(feeRateSatPerVByte * 1000m), 1000);
|
|
|
|
|
var builder = Network.CreateTransactionBuilder();
|
|
|
|
|
builder.SetVersion(2);
|
2026-06-16 14:40:06 +02:00
|
|
|
// RBF sequence to allow fee bumping (§6.6).
|
2026-06-11 10:47:40 +02:00
|
|
|
builder.OptInRBF = true;
|
|
|
|
|
builder.AddCoins(coins);
|
|
|
|
|
builder.SetChange(account.GetChangeAddress(changeIndex));
|
|
|
|
|
builder.SendEstimatedFees(feeRate);
|
|
|
|
|
|
|
|
|
|
if (sendAll)
|
|
|
|
|
builder.Send(destination, coins.Sum(c => (Money)c.Amount)).SubtractFees();
|
|
|
|
|
else
|
|
|
|
|
builder.Send(destination, Money.Satoshis(amountSats));
|
|
|
|
|
|
|
|
|
|
if (!account.IsWatchOnly)
|
|
|
|
|
{
|
|
|
|
|
builder.AddKeys(spendable
|
2026-06-15 14:20:10 +02:00
|
|
|
.Select(u => account.GetPrivateKey(u.IsChange, u.AddressIndex))
|
|
|
|
|
.OfType<Key>()
|
2026-06-11 10:47:40 +02:00
|
|
|
.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Transaction tx;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
tx = builder.BuildTransaction(sign: !account.IsWatchOnly);
|
|
|
|
|
}
|
|
|
|
|
catch (NotEnoughFundsException ex)
|
|
|
|
|
{
|
2026-06-16 14:40:06 +02:00
|
|
|
throw new WalletSpendException($"Insufficient funds: {ex.Message}");
|
2026-06-11 10:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!account.IsWatchOnly)
|
|
|
|
|
{
|
|
|
|
|
if (!builder.Verify(tx, out TransactionPolicyError[] errors))
|
|
|
|
|
throw new WalletSpendException(
|
2026-06-16 14:40:06 +02:00
|
|
|
"Invalid transaction: " + string.Join("; ", errors.Select(e => e.ToString())));
|
2026-06-11 10:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new BuiltTransaction
|
|
|
|
|
{
|
|
|
|
|
Transaction = tx,
|
|
|
|
|
Fee = GetFee(tx, coins),
|
|
|
|
|
FeeRate = feeRate,
|
|
|
|
|
Signed = !account.IsWatchOnly,
|
|
|
|
|
Psbt = builder.BuildPSBT(sign: !account.IsWatchOnly),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Money GetFee(Transaction tx, IReadOnlyList<Coin> coins)
|
|
|
|
|
{
|
|
|
|
|
var spentOutpoints = tx.Inputs.Select(i => i.PrevOut).ToHashSet();
|
|
|
|
|
var inputSum = coins.Where(c => spentOutpoints.Contains(c.Outpoint))
|
|
|
|
|
.Sum(c => (Money)c.Amount);
|
|
|
|
|
return inputSum - tx.Outputs.Sum(o => o.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 14:40:06 +02:00
|
|
|
/// <summary>Error during transaction construction/signing (funds, policy, parameters).</summary>
|
2026-06-11 10:47:40 +02:00
|
|
|
public sealed class WalletSpendException(string message) : Exception(message);
|