3f63423ae61323de0097514241ec5ee4141cfc4e
[NovacoinLibrary.git] / Novacoin / CTxOut.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Text;
21 using System.Collections.Generic;
22
23 namespace Novacoin
24 {
25         /// <summary>
26         /// Transaction output.
27         /// </summary>
28         public class CTxOut
29         {
30                 /// <summary>
31                 /// Input value.
32                 /// </summary>
33         public ulong nValue = ulong.MaxValue;
34
35                 /// <summary>
36                 /// Second half of script which contains spending instructions.
37                 /// </summary>
38         public CScript scriptPubKey;
39
40         /// <summary>
41         /// Initialize new CTxOut instance as a copy of another instance.
42         /// </summary>
43         /// <param name="o">CTxOut instance.</param>
44         public CTxOut(CTxOut o)
45         {
46             nValue = o.nValue;
47             scriptPubKey = o.scriptPubKey;
48         }
49
50         /// <summary>
51         /// Initialize an empty instance of CTxOut class
52         /// </summary>
53         public CTxOut()
54         {
55             SetEmpty();
56         }
57
58         /// <summary>
59         /// Read vout list from byte sequence.
60         /// </summary>
61         /// <param name="wBytes">Reference to byte sequence</param>
62         /// <returns>Outputs array</returns>
63         public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes)
64         {
65             int nOutputs = (int)wBytes.GetVarInt();
66             var vout =new CTxOut[nOutputs];
67
68             for (int nIndex = 0; nIndex < nOutputs; nIndex++)
69             {
70                 // Fill outputs array
71                 vout[nIndex] = new CTxOut();
72                 vout[nIndex].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
73
74                 int nScriptPKLen = (int)wBytes.GetVarInt();
75                 vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
76             }
77
78             return vout;
79         }
80
81         /// <summary>
82         /// Get raw bytes representation of our output.
83         /// </summary>
84         /// <returns>Byte sequence.</returns>
85         public static implicit operator byte[] (CTxOut output)
86         {
87                 var resultBytes = new List<byte>();
88
89                 resultBytes.AddRange(BitConverter.GetBytes(output.nValue)); // txout value
90
91                 byte[] s = output.scriptPubKey;
92                 resultBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptPubKey length
93                 resultBytes.AddRange(s); // scriptPubKey
94
95                 return resultBytes.ToArray();
96         }
97
98         /// <summary>
99         /// Null prevouts have -1 value
100         /// </summary>
101         public void SetNull()
102         {
103             nValue = ulong.MaxValue;
104             scriptPubKey = new CScript();
105         }
106
107         /// <summary>
108         /// Empty outputs have zero value and empty scriptPubKey
109         /// </summary>
110         public void SetEmpty()
111         {
112             nValue = 0;
113             scriptPubKey = new CScript();
114         }
115
116         public bool IsNull
117         {
118             get { return (nValue == ulong.MaxValue); }
119         }
120
121         public bool IsEmpty
122         {
123            get { return nValue == 0 && scriptPubKey.IsNull; }
124         }
125
126         /// <summary>
127         /// Serialized size
128         /// </summary>
129         public int Size
130         {
131             get
132             {
133                 var nScriptSize = scriptPubKey.Size;
134                 return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
135             }
136         }
137
138         public override string ToString ()
139                 {
140                         var sb = new StringBuilder ();
141                         sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
142
143                         return sb.ToString ();
144                 }
145         }
146 }
147