/// <summary> /// /// </summary> /// <param name="senderAddress">node's public key</param> /// <param name="receiverAddress">the receiver node's public key</param> /// <param name="privateKeyCointainer">i.e the private key of the sender to sign the tx ins</param> /// <param name="amount"></param> /// <param name="unspentTxOuts"></param> /// <param name="pool"></param> /// <returns></returns> public static Transaction CreateTransaction(string senderAddress, string receiverAddress, CspParameters privateKeyCointainer, int amount, IEnumerable <UnspentTxOut> unspentTxOuts, TransactionPool pool) { List <UnspentTxOut> myUnspentTxOuts = unspentTxOuts.Where(uto => uto.address == senderAddress).ToList(); // Remove unspent tx outs already used in any unverified transaction of the pool List <TxIn> poolTxIns = pool.GetTransactions().SelectMany(t => t.txIns).ToList(); myUnspentTxOuts = myUnspentTxOuts.Where(uto => !poolTxIns.Any(ti => ti.txOutTxId == uto.txId && ti.txOutIndex == uto.txOutIndex)).ToList(); // Find unspent tx outs to fund the transaction List <Transaction.UnspentTxOut> unspentTxOutsToUse = new List <Transaction.UnspentTxOut>(); int currentAmount = 0; for (int i = 0; i < myUnspentTxOuts.Count() && currentAmount < amount; i++) { unspentTxOutsToUse.Add(myUnspentTxOuts.ElementAt(i)); currentAmount += myUnspentTxOuts.ElementAt(i).amount; } if (currentAmount < amount) { Debug.LogError("Insufficiant balance"); return(null); } int leftOver = currentAmount - amount; TxIn[] txIns = unspentTxOutsToUse.Select(uto => new TxIn(uto.txId, uto.txOutIndex)).ToArray(); // Create corresponding tx outs (with eventual leftover sent back to sender TxOut[] txOuts; if (leftOver == 0) { txOuts = new TxOut[] { new TxOut(receiverAddress, amount) } } ; else { txOuts = new TxOut[] { new TxOut(receiverAddress, amount), new TxOut(senderAddress, leftOver) } }; Transaction transaction = new Transaction(); transaction.txIns = txIns; transaction.txOuts = txOuts; transaction.ComputeAndSetId(); transaction.SignTxIns(privateKeyCointainer, unspentTxOuts); return(transaction); }
public Transaction[] GetPooledTransactions() { return(transactionPool.GetTransactions()); }