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;
26 /// Transaction output.
33 public long nValue = -1;
36 /// Second half of script which contains spending instructions.
38 public CScript scriptPubKey;
41 /// Initialize new CTxOut instance as a copy of another instance.
43 /// <param name="o">CTxOut instance.</param>
44 public CTxOut(CTxOut o)
47 scriptPubKey = o.scriptPubKey;
51 /// Initialize an empty instance of CTxOut class
59 /// Read vout list from byte sequence.
61 /// <param name="wBytes">Reference to byte sequence</param>
62 /// <returns>Outputs array</returns>
63 public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes)
65 int nOutputs = (int)wBytes.GetVarInt();
66 var vout =new CTxOut[nOutputs];
68 for (int nIndex = 0; nIndex < nOutputs; nIndex++)
71 vout[nIndex] = new CTxOut();
72 vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.Get(8), 0);
74 int nScriptPKLen = (int)wBytes.GetVarInt();
75 vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
82 /// Get raw bytes representation of our output.
84 /// <returns>Byte sequence.</returns>
85 public static implicit operator byte[] (CTxOut output)
87 var resultBytes = new List<byte>();
89 resultBytes.AddRange(BitConverter.GetBytes(output.nValue)); // txout value
91 byte[] s = output.scriptPubKey;
92 resultBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptPubKey length
93 resultBytes.AddRange(s); // scriptPubKey
95 return resultBytes.ToArray();
99 /// Null prevouts have -1 value
101 public void SetNull()
104 scriptPubKey = new CScript();
108 /// Empty outputs have zero value and empty scriptPubKey
110 public void SetEmpty()
113 scriptPubKey = new CScript();
118 get { return (nValue == -1); }
123 get { return nValue == 0 && scriptPubKey.IsNull; }
133 var nScriptSize = scriptPubKey.Size;
134 return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
138 public override string ToString ()
140 var sb = new StringBuilder ();
141 sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
143 return sb.ToString ();