CTxIn and CTxOut constructors, some interoperability improvements
[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                 private ulong nValue;
16
17                 /// <summary>
18                 /// Second half of script which contains spending instructions.
19                 /// </summary>
20                 private 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         /// Parse input byte sequence and initialize new CTxOut instance.
34         /// </summary>
35         /// <param name="bytes">Byte sequence.</param>
36         public CTxOut(IList<byte> bytes)
37         {
38             WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
39             
40             nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8));
41             int spkLength = (int)VarInt.ReadVarInt(wBytes);
42
43             scriptPubKey = wBytes.GetItems(spkLength);
44         }
45
46         /// <summary>
47         /// Get raw bytes representation of our output.
48         /// </summary>
49         /// <returns>Byte sequence.</returns>
50         public IList<byte> ToBytes()
51         {
52             List<byte> resultBytes = new List<byte>();
53
54             resultBytes.AddRange(Interop.LEBytes(nValue)); // txout value
55             resultBytes.AddRange(VarInt.EncodeVarInt(scriptPubKey.LongLength)); // scriptPubKey length
56             resultBytes.AddRange(scriptPubKey); // scriptPubKey
57
58             return resultBytes;
59         }
60
61                 public override string ToString ()
62                 {
63                         StringBuilder sb = new StringBuilder ();
64                         sb.AppendFormat ("CTxOut(nValue={0},scriptPubKey={1}", nValue, scriptPubKey.ToString());
65
66                         return sb.ToString ();
67                 }
68         }
69 }
70