2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 using System.Collections.Generic;
27 public class TxInConstructorException : Exception
29 public TxInConstructorException()
33 public TxInConstructorException(string message)
38 public TxInConstructorException(string message, Exception inner)
39 : base(message, inner)
45 /// Transaction input.
50 /// Previous input data
52 public COutPoint prevout;
55 /// First half of script, signatures for the scriptPubKey
57 public CScript scriptSig;
60 /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
62 public uint nSequence = uint.MaxValue;
65 /// Initialize new CTxIn instance as copy of another one.
67 /// <param name="i">CTxIn instance.</param>
70 prevout = new COutPoint(i.prevout);
71 scriptSig = i.scriptSig;
72 nSequence = i.nSequence;
76 /// Initialize an empty instance of CTxIn class
80 prevout = new COutPoint();
81 scriptSig = new CScript();
85 /// Read vin list from byte sequence.
87 /// <param name="wBytes">Reference to binary reader</param>
88 /// <returns>Inputs array</returns>
89 internal static CTxIn[] ReadTxInList(ref BinaryReader reader)
94 int nInputs = (int)VarInt.ReadVarInt(ref reader);
95 var vin = new CTxIn[nInputs];
97 for (int nIndex = 0; nIndex < nInputs; nIndex++)
100 vin[nIndex] = new CTxIn();
101 vin[nIndex].prevout = new COutPoint(reader.ReadBytes(36));
102 vin[nIndex].scriptSig = new CScript(reader.ReadBytes((int)VarInt.ReadVarInt(ref reader)));
103 vin[nIndex].nSequence = reader.ReadUInt32();
106 // Return inputs array
111 throw new TxInConstructorException("Desirealization failed.", e);
121 uint nSize = 40; // COutPoint, nSequence
122 nSize += VarInt.GetEncodedSize(scriptSig.Size);
123 nSize += scriptSig.Size;
130 /// Get raw bytes representation of our input.
132 /// <returns>Byte sequence.</returns>
133 public static implicit operator byte[] (CTxIn input)
135 var stream = new MemoryStream();
136 var writer = new BinaryWriter(stream);
138 writer.Write(input.prevout); // prevout
139 writer.Write(VarInt.EncodeVarInt(input.scriptSig.Size)); // scriptSig length
140 writer.Write(input.scriptSig); // scriptSig
141 writer.Write(input.nSequence); // nSequence
143 var inputBytes = stream.ToArray();
150 get { return (nSequence == uint.MaxValue); }
152 public override string ToString ()
154 StringBuilder sb = new StringBuilder ();
156 sb.AppendFormat("CTxIn(");
157 sb.Append(prevout.ToString());
161 sb.AppendFormat(", coinbase={0}", Interop.ToHex((byte[])scriptSig));
165 sb.AppendFormat(", scriptsig={0}", scriptSig);
168 if (nSequence != uint.MaxValue)
170 sb.AppendFormat(", nSequence={0}", nSequence);
176 return sb.ToString ();