Implement ReadTxInList and ReadTxoutList as static 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 = Interop.LEBytesToUInt64(wBytes.GetItems(8));
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> ToBytes()
65         {
66             List<byte> resultBytes = new List<byte>();
67
68             resultBytes.AddRange(Interop.LEBytes(nValue)); // txout value
69             resultBytes.AddRange(VarInt.EncodeVarInt(scriptPubKey.LongLength)); // scriptPubKey length
70             resultBytes.AddRange(scriptPubKey); // scriptPubKey
71
72             return resultBytes;
73         }
74
75                 public override string ToString ()
76                 {
77                         StringBuilder sb = new StringBuilder ();
78                         sb.AppendFormat ("CTxOut(nValue={0},scriptPubKey={1})", nValue, (new CScript(scriptPubKey)).ToString());
79
80                         return sb.ToString ();
81                 }
82         }
83 }
84