SetEmpty on construct
[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 long nValue = -1;
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             SetEmpty();
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         /// <summary>
85         /// Null prevouts have -1 value
86         /// </summary>
87         public void SetNull()
88         {
89             nValue = -1;
90         }
91
92         /// <summary>
93         /// Empty outputs have zero value and empty scriptPubKey
94         /// </summary>
95         public void SetEmpty()
96         {
97             nValue = 0;
98             scriptPubKey.SetNullDestination();
99         }
100
101         public bool IsNull
102         {
103             get { return (nValue == -1); }
104         }
105
106         public bool IsEmpty
107         {
108            get { return nValue == 0 && scriptPubKey.IsNull; }
109         }
110
111                 public override string ToString ()
112                 {
113                         StringBuilder sb = new StringBuilder ();
114                         sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
115
116                         return sb.ToString ();
117                 }
118         }
119 }
120