2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
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.
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.
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/>.
21 using System.Collections.Generic;
27 /// Transaction output.
34 public ulong nValue = ulong.MaxValue;
37 /// Second half of script which contains spending instructions.
39 public CScript scriptPubKey;
42 /// Initialize new CTxOut instance as a copy of another instance.
44 /// <param name="o">CTxOut instance.</param>
45 public CTxOut(CTxOut o)
48 scriptPubKey = o.scriptPubKey;
52 /// Initialize an empty instance of CTxOut class
60 /// Read vout list from byte sequence.
62 /// <param name="wBytes">Reference to binary reader</param>
63 /// <returns>Outputs array</returns>
64 internal static CTxOut[] ReadTxOutList(ref BinaryReader reader)
66 int nOutputs = (int)VarInt.ReadVarInt(ref reader);
67 var vout =new CTxOut[nOutputs];
69 for (int nIndex = 0; nIndex < nOutputs; nIndex++)
72 vout[nIndex] = new CTxOut();
73 vout[nIndex].nValue = reader.ReadUInt64();
75 int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
76 vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
83 /// Deserialize outputs array.
85 /// <param name="outBytes">Byte array</param>
86 /// <returns>Outputs array</returns>
87 public static CTxOut[] DeserializeOutputsArray(byte[] outBytes)
89 var stream = new MemoryStream(outBytes);
90 var reader = new BinaryReader(stream);
92 CTxOut[] result = ReadTxOutList(ref reader);
100 /// Create serialized representation of outputs array.
102 /// <param name="vout">Outputs array</param>
103 /// <returns>Byte array</returns>
104 public static byte[] SerializeOutputsArray(CTxOut[] vout)
106 var stream = new MemoryStream();
107 var writer = new BinaryWriter(stream);
109 writer.Write(VarInt.EncodeVarInt(vout.Length));
111 foreach (var o in vout)
116 var resultBytes = stream.ToArray();
125 /// Get raw bytes representation of our output.
127 /// <returns>Byte sequence.</returns>
128 public static implicit operator byte[] (CTxOut output)
130 var stream = new MemoryStream();
131 var writer = new BinaryWriter(stream);
133 writer.Write(output.nValue); // txout value
134 writer.Write(VarInt.EncodeVarInt(output.scriptPubKey.Size)); // scriptPubKey length
135 writer.Write(output.scriptPubKey); // scriptPubKey
137 var resultBytes = stream.ToArray();
145 /// Null prevouts have -1 value
147 public void SetNull()
149 nValue = ulong.MaxValue;
150 scriptPubKey = new CScript();
154 /// Empty outputs have zero value and empty scriptPubKey
156 public void SetEmpty()
159 scriptPubKey = new CScript();
164 get { return (nValue == ulong.MaxValue); }
169 get { return nValue == 0 && scriptPubKey.IsNull; }
179 var nScriptSize = scriptPubKey.Size;
180 return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
184 public override string ToString ()
186 var sb = new StringBuilder ();
187 sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
189 return sb.ToString ();