private void SerializeTxn(BitcoinStream stream) { var version = this.Version; stream.ReadWrite(ref version); // POS Timestamp var time = this.Time; stream.ReadWrite(ref time); TxInList vin = this.Inputs; stream.ReadWrite(ref vin); vin.Transaction = this; TxOutList vout = this.Outputs; stream.ReadWrite(ref vout); vout.Transaction = this; LockTime lockTime = this.LockTime; stream.ReadWriteStruct(ref lockTime); }
public ICoinsView OutPoints(TxInList txIns) { var outPointSet = txIns.Select(x => x.PrevOut).ToHashSet(); var smartCoins = Coins.Where(x => outPointSet.Contains(x.OutPoint)); return(new CoinsView(smartCoins)); }
private void DeserializeTxn(BitcoinStream stream) { UInt32 nVersionTemp = 0; stream.ReadWrite(ref nVersionTemp); // POS time stamp UInt32 nTimeTemp = 0; stream.ReadWrite(ref nTimeTemp); TxInList vinTemp = null; TxOutList voutTemp = null; // Try to read the vin. stream.ReadWrite(ref vinTemp); vinTemp.Transaction = this; // Assume a normal vout follows. stream.ReadWrite(ref voutTemp); voutTemp.Transaction = this; LockTime lockTimeTemp = LockTime.Zero; stream.ReadWriteStruct(ref lockTimeTemp); this.Version = nVersionTemp; this.Time = nTimeTemp; // POS Timestamp vinTemp.ForEach(i => this.Inputs.Add(i)); voutTemp.ForEach(i => this.Outputs.Add(i)); this.LockTime = lockTimeTemp; }
private List <string> GetSendersAddressByInputs(TxInList inputs) { var senders = new List <string>(); foreach (var input in inputs) { var signer = input.GetSigner(); var addr = signer.ScriptPubKey.GetDestinationAddress(NetworkOperator.Instance.Network); senders.Add(addr.ToString()); } return(senders.Distinct().ToList()); }
private void SerializeTxn(BitcoinStream stream, bool witSupported) { byte flags = 0; var version = (witSupported && (this.Inputs.Count == 0 && this.Outputs.Count > 0)) ? this.Version | NoDummyInput : this.Version; stream.ReadWrite(ref version); // POS Timestamp var time = this.Time; stream.ReadWrite(ref time); if (witSupported) { // Check whether witnesses need to be serialized. if (HasWitness) { flags |= 1; } } if (flags != 0) { // Use extended format in case witnesses are to be serialized. TxInList vinDummy = new TxInList(); stream.ReadWrite <TxInList, TxIn>(ref vinDummy); stream.ReadWrite(ref flags); } TxInList vin = this.Inputs; stream.ReadWrite <TxInList, TxIn>(ref vin); vin.Transaction = this; TxOutList vout = this.Outputs; stream.ReadWrite <TxOutList, TxOut>(ref vout); vout.Transaction = this; if ((flags & 1) != 0) { StratisWitness wit = new StratisWitness(this.Inputs); wit.ReadWrite(stream); } LockTime lockTime = this.LockTime; stream.ReadWriteStruct(ref lockTime); }
public ElementsWitness(TxInList inputs, TxOutList outputs) { _Inputs = inputs; _Outputs = outputs; }
private void DeserializeTxn(BitcoinStream stream, bool witSupported) { byte flags = 0; UInt32 nVersionTemp = 0; stream.ReadWrite(ref nVersionTemp); // POS time stamp uint nTimeTemp = 0; stream.ReadWrite(ref nTimeTemp); TxInList vinTemp = new TxInList(); TxOutList voutTemp = new TxOutList(); // Try to read the vin. In case the dummy is there, this will be read as an empty vector. stream.ReadWrite <TxInList, TxIn>(ref vinTemp); var hasNoDummy = (nVersionTemp & NoDummyInput) != 0 && vinTemp.Count == 0; if (witSupported && hasNoDummy) { nVersionTemp = nVersionTemp & ~NoDummyInput; } if (vinTemp.Count == 0 && witSupported && !hasNoDummy) { // We read a dummy or an empty vin. stream.ReadWrite(ref flags); if (flags != 0) { // Assume we read a dummy and a flag. stream.ReadWrite <TxInList, TxIn>(ref vinTemp); vinTemp.Transaction = this; stream.ReadWrite <TxOutList, TxOut>(ref voutTemp); voutTemp.Transaction = this; } else { // Assume read a transaction without output. voutTemp = new TxOutList(); voutTemp.Transaction = this; } } else { // We read a non-empty vin. Assume a normal vout follows. stream.ReadWrite <TxOutList, TxOut>(ref voutTemp); voutTemp.Transaction = this; } if (((flags & 1) != 0) && witSupported) { // The witness flag is present, and we support witnesses. flags ^= 1; StratisWitness wit = new StratisWitness(vinTemp); wit.ReadWrite(stream); } if (flags != 0) { // Unknown flag in the serialization throw new FormatException("Unknown transaction optional data"); } LockTime lockTimeTemp = LockTime.Zero; stream.ReadWriteStruct(ref lockTimeTemp); this.Version = nVersionTemp; this.Time = nTimeTemp; // POS Timestamp vinTemp.ForEach(i => this.AddInput(i)); voutTemp.ForEach(i => this.AddOutput(i)); this.LockTime = lockTimeTemp; }
public StratisWitness(TxInList inputs) { _Inputs = inputs; }
public static void Execute() { string TxnHex = "f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94"; //Transaction tx = new Transaction(TxnHex); // Query the transaction by id: // Create a client QBitNinjaClient client = new QBitNinjaClient(Network.Main); // Parse transaction id to NBitcoin.uint256 so the client can eat it var transactionId = uint256.Parse(TxnHex); // Query the transaction GetTransactionResponse transactionResponse = client.GetTransaction(transactionId).Result; Transaction transaction = transactionResponse.Transaction; Console.WriteLine("TransactionId: " + transactionResponse.TransactionId); Console.WriteLine("Transaction Hash: " + transaction.GetHash()); // RECEIVED COINS using QBitNinja List <ICoin> receivedCoins = transactionResponse.ReceivedCoins; foreach (ICoin coin in receivedCoins) { Money amount = (Money)coin.Amount; Script paymentScript = coin.TxOut.ScriptPubKey; BitcoinAddress address = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(); Console.WriteLine("amount: " + amount.ToDecimal(MoneyUnit.BTC)); Console.WriteLine("paymentScript: " + paymentScript); // It's the ScriptPubKey Console.WriteLine("address: " + address); // 1HfbwN6Lvma9eDsv7mdwp529tgiyfNr7jc } // Each input shows you which previous out has been spent TxInList inputs = transaction.Inputs; foreach (TxIn input in inputs) { OutPoint previousOutpoint = input.PrevOut; Console.WriteLine(); Console.WriteLine("previousOutpoint hash: " + previousOutpoint.Hash); // hash of prev tx Console.WriteLine("previousOutpoint indx: " + previousOutpoint.N); // idx of out from prev tx, that has been spent in the current tx } // TxOut represents an amount of bitcoin and a ScriptPubKey. (Recipient) Money twentyOneBtc = new Money(21, MoneyUnit.BTC); Script scriptPubKey = transaction.Outputs.First().ScriptPubKey; TxOut txOut = new TxOut(twentyOneBtc, scriptPubKey); // The Outpoint of the TxOut with 13.19683492 BTC in our transaction is OutPoint firstOutPoint = receivedCoins.First().Outpoint; Console.WriteLine("firstOutPoint hash: " + firstOutPoint.Hash); // f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94 Console.WriteLine("firstOutPoint indx: " + firstOutPoint.N); // 0 // TxIn is composed of the Outpoint of the TxOut being spent and // of the ScriptSig (Proof of Ownership) OutPoint firstPrevOutPt = transaction.Inputs.First().PrevOut; Transaction firstPrevTxn = client.GetTransaction(firstPrevOutPt.Hash).Result.Transaction; Console.WriteLine("Txn input count: " + transaction.Inputs.Count); // 9 Console.WriteLine("IsCoinBase Txn: " + firstPrevTxn.IsCoinBase); // False // the outputs were for a total of 13.19703492 BTC Money spentAmount = Money.Zero; List <ICoin> spentCoins = transactionResponse.SpentCoins; foreach (var spentCoin in spentCoins) { spentAmount = (Money)spentCoin.Amount.Add(spentAmount); } Console.WriteLine("spentAmount: " + spentAmount.ToDecimal(MoneyUnit.BTC)); // 13.19703492 // Get Transaction Fees or Miner’s Fees Money fee = transaction.GetFee(spentCoins.ToArray()); Console.WriteLine("Txn fee: " + fee.ToString()); // coinbase Txn rule // sum of output's value = (transaction fees in the block + the mining reward) Console.ReadLine(); }
public BitcoinplusWitness(TxInList inputs) { _Inputs = inputs; }
public override void ReadWrite(BitcoinStream stream) { bool witSupported = (((uint)stream.TransactionOptions & (uint)TransactionOptions.Witness) != 0) && stream.ProtocolVersion >= ProtocolVersion.WITNESS_VERSION; byte flags = 0; if (!stream.Serializing) { stream.ReadWrite(ref this.nVersion); // POS time stamp stream.ReadWrite(ref this.nTime); /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */ stream.ReadWrite <TxInList, TxIn>(ref this.vin); bool hasNoDummy = (this.nVersion & NoDummyInput) != 0 && this.vin.Count == 0; if (witSupported && hasNoDummy) { this.nVersion = this.nVersion & ~NoDummyInput; } if (this.vin.Count == 0 && witSupported && !hasNoDummy) { /* We read a dummy or an empty vin. */ stream.ReadWrite(ref flags); if (flags != 0) { /* Assume we read a dummy and a flag. */ stream.ReadWrite <TxInList, TxIn>(ref this.vin); this.vin.Transaction = this; stream.ReadWrite <TxOutList, TxOut>(ref this.vout); this.vout.Transaction = this; } else { /* Assume read a transaction without output. */ this.vout = new TxOutList(); this.vout.Transaction = this; } } else { /* We read a non-empty vin. Assume a normal vout follows. */ stream.ReadWrite <TxOutList, TxOut>(ref this.vout); this.vout.Transaction = this; } if (((flags & 1) != 0) && witSupported) { /* The witness flag is present, and we support witnesses. */ flags ^= 1; var wit = new Witness(this.Inputs); wit.ReadWrite(stream); } if (flags != 0) { /* Unknown flag in the serialization */ throw new FormatException("Unknown transaction optional data"); } } else { uint version = (witSupported && (this.vin.Count == 0 && this.vout.Count > 0)) ? this.nVersion | NoDummyInput : this.nVersion; stream.ReadWrite(ref version); // the POS time stamp stream.ReadWrite(ref this.nTime); if (witSupported) { /* Check whether witnesses need to be serialized. */ if (this.HasWitness) { flags |= 1; } } if (flags != 0) { /* Use extended format in case witnesses are to be serialized. */ var vinDummy = new TxInList(); stream.ReadWrite <TxInList, TxIn>(ref vinDummy); stream.ReadWrite(ref flags); } stream.ReadWrite <TxInList, TxIn>(ref this.vin); this.vin.Transaction = this; stream.ReadWrite <TxOutList, TxOut>(ref this.vout); this.vout.Transaction = this; if ((flags & 1) != 0) { var wit = new Witness(this.Inputs); wit.ReadWrite(stream); } } stream.ReadWriteStruct(ref this.nLockTime); }
public ICoinsView OutPoints(TxInList txIns) => AsCoinsView().OutPoints(txIns);
private static void RecursivelyInspectForCoinbaseTransaction(QBitNinjaClient client, NBitcoin.Transaction transaction, TxInList inputs) { foreach (TxIn input in transaction.Inputs) { uint256 previousTransactionId = input.PrevOut.Hash; GetTransactionResponse previousTransactionResponse = client.GetTransaction(previousTransactionId).Result; NBitcoin.Transaction previousTransaction = previousTransactionResponse.Transaction; var previousTransactionOutputs = previousTransaction.Outputs; foreach (TxOut previousTransactionOutput in previousTransactionOutputs) { Money amount = previousTransactionOutput.Value; Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC)); var paymentScript = previousTransactionOutput.ScriptPubKey; Console.WriteLine(paymentScript); // It's the ScriptPubKey var address = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(address); Console.WriteLine(); } } }
public ICoinsView SpentBy(TxInList txIns) => OutPoints(txIns.Select(x => x.PrevOut));
private static void Main(string[] args) { // create client var client = new QBitNinjaClient(Network.Main); // parse tx id to NBitcoin.uint256 for client var transactionId = uint256.Parse("f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94"); // query tx GetTransactionResponse transactionResponse = client.GetTransaction(transactionId).Result; // get NBitcoin.Transaction type NBitcoin.Transaction nBitcoinTransaction = transactionResponse.Transaction; var fromTransactionClass = nBitcoinTransaction.GetHash(); var fromGetTransactionResponseClass = transactionResponse.TransactionId; Console.WriteLine(fromTransactionClass == transactionId && fromGetTransactionResponseClass == transactionId); List <ICoin> receivedCoins = transactionResponse.ReceivedCoins; // received coins Console.WriteLine("Begin received coins"); foreach (var coin in receivedCoins) { Money amount = coin.Amount as Money; Console.WriteLine(amount?.ToDecimal(MoneyUnit.BTC)); var paymentScript = coin.TxOut.ScriptPubKey; Console.WriteLine(paymentScript); var address = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(address); } // outputs TxOutList outputs = nBitcoinTransaction.Outputs; Console.WriteLine("Begin outputs"); foreach (var output in outputs) { Money amount = output.Value; Console.WriteLine(amount.ToDecimal(MoneyUnit.BTC)); var paymentScript = output.ScriptPubKey; Console.WriteLine(paymentScript); var address = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(address); } // inputs TxInList inputs = nBitcoinTransaction.Inputs; Console.WriteLine("Begin inputs"); foreach (var input in inputs) { OutPoint previousOutPoint = input.PrevOut; Console.WriteLine(previousOutPoint); Console.WriteLine(previousOutPoint.Hash); // hash of prev tx Console.WriteLine(previousOutPoint.N); // index of out from prev tx, that has been spent in current tx } // The terms TxOut, Output and out are synonymous. // create a txout with 21 bitcoin from the first ScriptPubKey in our current transaction: Console.WriteLine("Begin txout"); Money twentyBtc = new Money(21, MoneyUnit.BTC); var scriptPubKey = nBitcoinTransaction.Outputs.FirstOrDefault()?.ScriptPubKey; TxOut txOut = new TxOut(twentyBtc, scriptPubKey); Console.WriteLine(txOut.Value.ToDecimal(MoneyUnit.BTC)); Console.WriteLine(txOut.ScriptPubKey); // every TxOut is uniq addressed by at the blockchain level by the id of the tx, including the index // e.g., OutPoint of TxOut is Console.WriteLine("Begin outpoint"); OutPoint firstOutPoint = receivedCoins.FirstOrDefault()?.Outpoint; Console.WriteLine(firstOutPoint?.Hash); Console.WriteLine(firstOutPoint?.N); Console.WriteLine("Begin txin"); // TxIn is composed of the Outpoint of the TxOut being spent and of the ScriptSig (we can see the ScriptSig as the “Proof of Ownership”). // with the previous transaction ID, we can review the info associated with that tx OutPoint firstPreviousOutPoint = nBitcoinTransaction.Inputs.FirstOrDefault()?.PrevOut; if (firstPreviousOutPoint != null) { var firstPreviousTransaction = client.GetTransaction(firstPreviousOutPoint.Hash).Result.Transaction; Console.WriteLine(firstPreviousTransaction.IsCoinBase); } Console.WriteLine("begin coin base hunt"); //TraceCoinBase(nBitcoinTransaction, client); just done as an exercise, do not use Console.WriteLine("begin total output value count"); var spentCoins = transactionResponse.SpentCoins; var spentAmount = spentCoins.Aggregate(Money.Zero, (money, next) => (Money)next.Amount.Add(money)).ToDecimal(MoneyUnit.BTC); Console.WriteLine(spentAmount); var receivedAmount = receivedCoins.Aggregate(Money.Zero, (money, next) => (Money)next.Amount.Add(money)).ToDecimal(MoneyUnit.BTC); Console.WriteLine(receivedAmount); Console.WriteLine(spentAmount - receivedAmount); // fee var fee = nBitcoinTransaction.GetFee(spentCoins.ToArray()).ToDecimal(MoneyUnit.BTC); Console.WriteLine(fee); // note: there are no fee in a coinbase transaction Console.ReadLine(); }