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;
26 public class TxInConstructorException : Exception
28 public TxInConstructorException()
32 public TxInConstructorException(string message)
37 public TxInConstructorException(string message, Exception inner)
38 : base(message, inner)
44 /// Transaction input.
49 /// Previous input data
51 public COutPoint prevout;
54 /// First half of script, signatures for the scriptPubKey
56 public CScript scriptSig;
59 /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
61 public uint nSequence = uint.MaxValue;
64 /// Initialize new CTxIn instance as copy of another one.
66 /// <param name="i">CTxIn instance.</param>
69 prevout = new COutPoint(i.prevout);
70 scriptSig = i.scriptSig;
71 nSequence = i.nSequence;
75 /// Initialize an empty instance of CTxIn class
79 prevout = new COutPoint();
80 scriptSig = new CScript();
84 /// Read vin list from byte sequence.
86 /// <param name="wBytes">Reference to byte sequence</param>
87 /// <returns>Inputs array</returns>
88 public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
93 int nInputs = (int)wBytes.GetVarInt();
94 var vin = new CTxIn[nInputs];
96 for (int nIndex = 0; nIndex < nInputs; nIndex++)
99 vin[nIndex] = new CTxIn();
100 vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
101 vin[nIndex].scriptSig = new CScript(wBytes.Get((int)wBytes.GetVarInt()));
102 vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
105 // Return inputs array
110 throw new TxInConstructorException("Desirealization failed.", e);
120 int nSize = 40; // COutPoint, nSequence
121 nSize += VarInt.GetEncodedSize(scriptSig.Size);
122 nSize += scriptSig.Size;
129 /// Get raw bytes representation of our input.
131 /// <returns>Byte sequence.</returns>
132 public static implicit operator byte[] (CTxIn input)
134 var inputBytes = new List<byte>();
136 inputBytes.AddRange((byte[])input.prevout); // prevout
138 var s = (byte[])input.scriptSig;
139 inputBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptSig length
140 inputBytes.AddRange(s); // scriptSig
141 inputBytes.AddRange(BitConverter.GetBytes(input.nSequence)); // Sequence
143 return inputBytes.ToArray();
148 get { return (nSequence == uint.MaxValue); }
150 public override string ToString ()
152 StringBuilder sb = new StringBuilder ();
154 sb.AppendFormat("CTxIn(");
155 sb.Append(prevout.ToString());
159 sb.AppendFormat(", coinbase={0}", Interop.ToHex((byte[])scriptSig));
163 sb.AppendFormat(", scriptsig={0}", scriptSig.ToString());
166 if (nSequence != uint.MaxValue)
168 sb.AppendFormat(", nSequence={0}", nSequence);
174 return sb.ToString ();