Use getters instead of Satoshi-style access methods
[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 byte[] 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         }
38
39         /// <summary>
40         /// Read vout list from byte sequence.
41         /// </summary>
42         /// <param name="wBytes">Reference to byte sequence</param>
43         /// <returns>Outputs array</returns>
44         public static CTxOut[] ReadTxOutList(ref WrappedList<byte> wBytes)
45         {
46             int nOutputs = (int)VarInt.ReadVarInt(ref wBytes);
47             CTxOut[] vout =new CTxOut[nOutputs];
48
49             for (int nIndex = 0; nIndex < nOutputs; nIndex++)
50             {
51                 // Fill outputs array
52                 vout[nIndex] = new CTxOut();
53                 vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.GetItems(8), 0);
54                 vout[nIndex].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
55             }
56
57             return vout;
58         }
59
60         /// <summary>
61         /// Get raw bytes representation of our output.
62         /// </summary>
63         /// <returns>Byte sequence.</returns>
64         public IList<byte> Bytes
65         {
66             get
67             {
68                 List<byte> resultBytes = new List<byte>();
69
70                 resultBytes.AddRange(BitConverter.GetBytes(nValue)); // txout value
71                 resultBytes.AddRange(VarInt.EncodeVarInt(scriptPubKey.LongLength)); // scriptPubKey length
72                 resultBytes.AddRange(scriptPubKey); // scriptPubKey
73
74                 return resultBytes;
75             }
76         }
77
78                 public override string ToString ()
79                 {
80                         StringBuilder sb = new StringBuilder ();
81                         sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, (new CScript(scriptPubKey)).ToString());
82
83                         return sb.ToString ();
84                 }
85         }
86 }
87