Add license header.
[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 long nValue = -1;
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             CTxOut[] 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.ToUInt32(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 IList<byte> Bytes
86         {
87             get
88             {
89                 List<byte> resultBytes = new List<byte>();
90
91                 resultBytes.AddRange(BitConverter.GetBytes(nValue)); // txout value
92
93                 List<byte> s = new List<byte>(scriptPubKey.Bytes);
94
95                 resultBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptPubKey length
96                 resultBytes.AddRange(s); // scriptPubKey
97
98                 return resultBytes;
99             }
100         }
101
102         /// <summary>
103         /// Null prevouts have -1 value
104         /// </summary>
105         public void SetNull()
106         {
107             nValue = -1;
108             scriptPubKey = new CScript();
109         }
110
111         /// <summary>
112         /// Empty outputs have zero value and empty scriptPubKey
113         /// </summary>
114         public void SetEmpty()
115         {
116             nValue = 0;
117             scriptPubKey = new CScript();
118         }
119
120         public bool IsNull
121         {
122             get { return (nValue == -1); }
123         }
124
125         public bool IsEmpty
126         {
127            get { return nValue == 0 && scriptPubKey.IsNull; }
128         }
129
130                 public override string ToString ()
131                 {
132                         StringBuilder sb = new StringBuilder ();
133                         sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
134
135                         return sb.ToString ();
136                 }
137         }
138 }
139