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