CTxIn and CTxOut constructors, some interoperability improvements
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
1 \feffusing System;
2 using System.Text;
3 using System.Collections.Generic;
4
5 namespace Novacoin
6 {
7         /// <summary>
8         /// Transaction input.
9         /// </summary>
10         public class CTxIn
11         {
12                 /// <summary>
13                 /// Hash of parent transaction.
14                 /// </summary>
15                 private Hash256 txID = new Hash256();
16
17                 /// <summary>
18                 /// Parent input number.
19                 /// </summary>
20                 private uint nInput = 0;
21
22                 /// <summary>
23                 /// First half of script, signatures for the scriptPubKey
24                 /// </summary>
25                 private byte[] scriptSig;
26
27                 /// <summary>
28                 /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
29                 /// </summary>
30                 private uint nSequence = 0xffffffff;
31
32         /// <summary>
33         /// Initialize new CTxIn instance as copy of another one.
34         /// </summary>
35         /// <param name="i">CTxIn instance.</param>
36         public CTxIn(CTxIn i)
37         {
38             txID = i.txID;
39             nInput = i.nInput;
40             scriptSig = i.scriptSig;
41             nSequence = i.nSequence;
42         }
43
44         /// <summary>
45         /// Decode byte sequence and initialize new instance of CTxIn class.
46         /// </summary>
47         /// <param name="bytes">Byte sequence</param>
48                 public CTxIn (IList<byte> bytes)
49                 {
50             WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
51
52             txID = new Hash256(wBytes.GetItems(32));
53             nInput = Interop.LEBytesToUInt32(wBytes.GetItems(4));
54             int ssLength = (int)VarInt.ReadVarInt(wBytes);
55             scriptSig = wBytes.GetItems(ssLength);
56             nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
57                 }
58
59         /// <summary>
60         /// Get raw bytes representation of our input.
61         /// </summary>
62         /// <returns>Byte sequence.</returns>
63         public IList<byte> ToBytes()
64         {
65             List<byte> inputBytes = new List<byte>();
66
67             inputBytes.AddRange(txID.hashBytes); // Input transaction id
68             inputBytes.AddRange(Interop.LEBytes(nInput)); // Input number
69             inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // Scriptsig length
70             inputBytes.AddRange(scriptSig); // ScriptSig
71             inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
72
73             return inputBytes;
74         }
75
76                 public override string ToString ()
77                 {
78                         StringBuilder sb = new StringBuilder ();
79                         sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2}", nInput, nInput, scriptSig.ToString());
80
81                         return sb.ToString ();
82                 }
83
84         }
85 }
86