COutPoint class, coinbase/coinstake properties
[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             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 void SetNull()
85         {
86             nValue = -1;
87             scriptPubKey.SetNullDestination();
88         }
89
90         public void SetEmpty()
91         {
92             nValue = 0;
93             scriptPubKey.SetNullDestination();
94         }
95
96         public bool IsNull
97         {
98             get { return (nValue == -1); }
99         }
100
101         public bool IsEmpty
102         {
103            get { return nValue == 0 && scriptPubKey.IsNull; }
104         }
105
106                 public override string ToString ()
107                 {
108                         StringBuilder sb = new StringBuilder ();
109                         sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
110
111                         return sb.ToString ();
112                 }
113         }
114 }
115