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 outpoint using provided value and script.
44 /// <param name="nValue">Input value</param>
45 /// <param name="scriptPubKey">Spending instructions.</param>
46 public CTxOut(ulong nValue, CScript scriptPubKey)
49 this.scriptPubKey = scriptPubKey;
53 /// Initialize new CTxOut instance as a copy of another instance.
55 /// <param name="o">CTxOut instance.</param>
56 public CTxOut(CTxOut o)
59 scriptPubKey = o.scriptPubKey;
63 /// Initialize an empty instance of CTxOut class
71 /// Read vout list from byte sequence.
73 /// <param name="wBytes">Reference to binary reader</param>
74 /// <returns>Outputs array</returns>
75 internal static CTxOut[] ReadTxOutList(ref BinaryReader reader)
77 int nOutputs = (int)VarInt.ReadVarInt(ref reader);
78 var vout =new CTxOut[nOutputs];
80 for (int nIndex = 0; nIndex < nOutputs; nIndex++)
83 vout[nIndex] = new CTxOut();
84 vout[nIndex].nValue = reader.ReadUInt64();
86 int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
87 vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
94 /// Deserialize outputs array.
96 /// <param name="outBytes">Byte array</param>
97 /// <returns>Outputs array</returns>
98 public static CTxOut[] DeserializeOutputsArray(byte[] outBytes)
100 var stream = new MemoryStream(outBytes);
101 var reader = new BinaryReader(stream);
103 CTxOut[] result = ReadTxOutList(ref reader);
111 /// Create serialized representation of outputs array.
113 /// <param name="vout">Outputs array</param>
114 /// <returns>Byte array</returns>
115 public static byte[] SerializeOutputsArray(CTxOut[] vout)
117 var stream = new MemoryStream();
118 var writer = new BinaryWriter(stream);
120 writer.Write(VarInt.EncodeVarInt(vout.Length));
122 foreach (var o in vout)
127 var resultBytes = stream.ToArray();
136 /// Get raw bytes representation of our output.
138 /// <returns>Byte sequence.</returns>
139 public static implicit operator byte[] (CTxOut output)
141 var stream = new MemoryStream();
142 var writer = new BinaryWriter(stream);
144 writer.Write(output.nValue); // txout value
145 writer.Write(VarInt.EncodeVarInt(output.scriptPubKey.Size)); // scriptPubKey length
146 writer.Write(output.scriptPubKey); // scriptPubKey
148 var resultBytes = stream.ToArray();
156 /// Null prevouts have -1 value
158 public void SetNull()
160 nValue = ulong.MaxValue;
161 scriptPubKey = new CScript();
165 /// Empty outputs have zero value and empty scriptPubKey
167 public void SetEmpty()
170 scriptPubKey = new CScript();
175 get { return (nValue == ulong.MaxValue); }
180 get { return nValue == 0 && scriptPubKey.IsNull; }
190 var nScriptSize = scriptPubKey.Size;
191 return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
195 public override string ToString ()
197 var sb = new StringBuilder ();
198 sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
200 return sb.ToString ();