Use CScript for vin and vout,
[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         }
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
55                 int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
56                 vout[nIndex].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
57             }
58
59             return vout;
60         }
61
62         /// <summary>
63         /// Get raw bytes representation of our output.
64         /// </summary>
65         /// <returns>Byte sequence.</returns>
66         public IList<byte> Bytes
67         {
68             get
69             {
70                 List<byte> resultBytes = new List<byte>();
71
72                 resultBytes.AddRange(BitConverter.GetBytes(nValue)); // txout value
73
74                 List<byte> s = new List<byte>(scriptPubKey.Bytes);
75
76                 resultBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptPubKey length
77                 resultBytes.AddRange(s); // scriptPubKey
78
79                 return resultBytes;
80             }
81         }
82
83                 public override string ToString ()
84                 {
85                         StringBuilder sb = new StringBuilder ();
86                         sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
87
88                         return sb.ToString ();
89                 }
90         }
91 }
92