Improve empty constructors behavior
[NovacoinLibrary.git] / Novacoin / CTxOut.cs
1 \feffusing System;
2 using System.Text;
3 using System.Collections.Generic;
4
5 namespace Novacoin
6 {
7         /// <summary>
8         /// Transaction output.
9         /// </summary>
10         public class CTxOut
11         {
12                 /// <summary>
13                 /// Input value.
14                 /// </summary>
15         public ulong nValue;
16
17                 /// <summary>
18                 /// Second half of script which contains spending instructions.
19                 /// </summary>
20         public CScript scriptPubKey;
21
22         /// <summary>
23         /// Initialize new CTxOut instance as a copy of another instance.
24         /// </summary>
25         /// <param name="o">CTxOut instance.</param>
26         public CTxOut(CTxOut o)
27         {
28             nValue = o.nValue;
29             scriptPubKey = o.scriptPubKey;
30         }
31
32         /// <summary>
33         /// Initialize an empty instance of CTxOut class
34         /// </summary>
35         public CTxOut()
36         {
37             scriptPubKey = new CScript();
38         }
39
40         /// <summary>
41         /// Read vout list from byte sequence.
42         /// </summary>
43         /// <param name="wBytes">Reference to byte sequence</param>
44         /// <returns>Outputs array</returns>
45         public static CTxOut[] ReadTxOutList(ref WrappedList<byte> wBytes)
46         {
47             int nOutputs = (int)VarInt.ReadVarInt(ref wBytes);
48             CTxOut[] vout =new CTxOut[nOutputs];
49
50             for (int nIndex = 0; nIndex < nOutputs; nIndex++)
51             {
52                 // Fill outputs array
53                 vout[nIndex] = new CTxOut();
54                 vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.GetItems(8), 0);
55
56                 int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
57                 vout[nIndex].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
58             }
59
60             return vout;
61         }
62
63         /// <summary>
64         /// Get raw bytes representation of our output.
65         /// </summary>
66         /// <returns>Byte sequence.</returns>
67         public IList<byte> Bytes
68         {
69             get
70             {
71                 List<byte> resultBytes = new List<byte>();
72
73                 resultBytes.AddRange(BitConverter.GetBytes(nValue)); // txout value
74
75                 List<byte> s = new List<byte>(scriptPubKey.Bytes);
76
77                 resultBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptPubKey length
78                 resultBytes.AddRange(s); // scriptPubKey
79
80                 return resultBytes;
81             }
82         }
83
84                 public override string ToString ()
85                 {
86                         StringBuilder sb = new StringBuilder ();
87                         sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
88
89                         return sb.ToString ();
90                 }
91         }
92 }
93