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/>.
25 /// Transaction output.
32 public long nValue = unchecked((long)0xffffffffffffffff);
35 /// Second half of script which contains spending instructions.
37 public CScript scriptPubKey;
40 /// Initialize new outpoint using provided value and script.
42 /// <param name="nValue">Input value</param>
43 /// <param name="scriptPubKey">Spending instructions.</param>
44 public CTxOut(long nValue, CScript scriptPubKey)
47 this.scriptPubKey = scriptPubKey;
51 /// Initialize new CTxOut instance as a copy of another instance.
53 /// <param name="o">CTxOut instance.</param>
54 public CTxOut(CTxOut o)
57 scriptPubKey = o.scriptPubKey;
61 /// Initialize an empty instance of CTxOut class
69 /// Read vout list from byte sequence.
71 /// <param name="wBytes">Reference to binary reader</param>
72 /// <returns>Outputs array</returns>
73 internal static CTxOut[] ReadTxOutList(ref BinaryReader reader)
75 int nOutputs = (int)VarInt.ReadVarInt(ref reader);
76 var vout =new CTxOut[nOutputs];
78 for (int nIndex = 0; nIndex < nOutputs; nIndex++)
81 vout[nIndex] = new CTxOut();
82 vout[nIndex].nValue = reader.ReadInt64();
84 int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
85 vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
92 /// Deserialize outputs array.
94 /// <param name="outBytes">Byte array</param>
95 /// <returns>Outputs array</returns>
96 public static CTxOut[] DeserializeOutputsArray(byte[] outBytes)
98 var stream = new MemoryStream(outBytes);
99 var reader = new BinaryReader(stream);
101 CTxOut[] result = ReadTxOutList(ref reader);
109 /// Create serialized representation of outputs array.
111 /// <param name="vout">Outputs array</param>
112 /// <returns>Byte array</returns>
113 public static byte[] SerializeOutputsArray(CTxOut[] vout)
115 var stream = new MemoryStream();
116 var writer = new BinaryWriter(stream);
118 writer.Write(VarInt.EncodeVarInt(vout.Length));
120 foreach (var o in vout)
125 var resultBytes = stream.ToArray();
134 /// Get raw bytes representation of our output.
136 /// <returns>Byte sequence.</returns>
137 public static implicit operator byte[] (CTxOut output)
139 var stream = new MemoryStream();
140 var writer = new BinaryWriter(stream);
142 writer.Write(output.nValue); // txout value
143 writer.Write(VarInt.EncodeVarInt(output.scriptPubKey.Size)); // scriptPubKey length
144 writer.Write(output.scriptPubKey); // scriptPubKey
146 var resultBytes = stream.ToArray();
154 /// Null prevouts have -1 value
156 public void SetNull()
158 nValue = unchecked((long)0xffffffffffffffff);
159 scriptPubKey = new CScript();
163 /// Empty outputs have zero value and empty scriptPubKey
165 public void SetEmpty()
168 scriptPubKey = new CScript();
173 get { return nValue == unchecked((long)0xffffffffffffffff); }
178 get { return nValue == 0 && scriptPubKey.IsNull; }
188 var nScriptSize = scriptPubKey.Size;
189 return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
193 public override string ToString ()
195 var sb = new StringBuilder ();
196 sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
198 return sb.ToString ();