-\feffusing System;
+\feff/**
+ * Novacoin classes library
+ * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
using System.Text;
using System.Collections.Generic;
+using System.IO;
namespace Novacoin
{
/// <summary>
/// Input value.
/// </summary>
- private ulong nValue;
+ public ulong nValue = ulong.MaxValue;
/// <summary>
/// Second half of script which contains spending instructions.
/// </summary>
- private byte[] scriptPubKey;
+ public CScript scriptPubKey;
+
+ /// <summary>
+ /// Initialize new outpoint using provided value and script.
+ /// </summary>
+ /// <param name="nValue">Input value</param>
+ /// <param name="scriptPubKey">Spending instructions.</param>
+ public CTxOut(ulong nValue, CScript scriptPubKey)
+ {
+ this.nValue = nValue;
+ this.scriptPubKey = scriptPubKey;
+ }
/// <summary>
/// Initialize new CTxOut instance as a copy of another instance.
}
/// <summary>
- /// Parse input byte sequence and initialize new CTxOut instance.
+ /// Initialize an empty instance of CTxOut class
+ /// </summary>
+ public CTxOut()
+ {
+ SetEmpty();
+ }
+
+ /// <summary>
+ /// Read vout list from byte sequence.
+ /// </summary>
+ /// <param name="wBytes">Reference to binary reader</param>
+ /// <returns>Outputs array</returns>
+ internal static CTxOut[] ReadTxOutList(ref BinaryReader reader)
+ {
+ int nOutputs = (int)VarInt.ReadVarInt(ref reader);
+ var vout =new CTxOut[nOutputs];
+
+ for (int nIndex = 0; nIndex < nOutputs; nIndex++)
+ {
+ // Fill outputs array
+ vout[nIndex] = new CTxOut();
+ vout[nIndex].nValue = reader.ReadUInt64();
+
+ int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
+ vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
+ }
+
+ return vout;
+ }
+
+ /// <summary>
+ /// Deserialize outputs array.
/// </summary>
- /// <param name="bytes">Byte sequence.</param>
- public CTxOut(IList<byte> bytes)
+ /// <param name="outBytes">Byte array</param>
+ /// <returns>Outputs array</returns>
+ public static CTxOut[] DeserializeOutputsArray(byte[] outBytes)
{
- WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
-
- nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8));
- int spkLength = (int)VarInt.ReadVarInt(wBytes);
+ var stream = new MemoryStream(outBytes);
+ var reader = new BinaryReader(stream);
+
+ CTxOut[] result = ReadTxOutList(ref reader);
- scriptPubKey = wBytes.GetItems(spkLength);
+ reader.Close();
+
+ return result;
}
/// <summary>
+ /// Create serialized representation of outputs array.
+ /// </summary>
+ /// <param name="vout">Outputs array</param>
+ /// <returns>Byte array</returns>
+ public static byte[] SerializeOutputsArray(CTxOut[] vout)
+ {
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
+
+ writer.Write(VarInt.EncodeVarInt(vout.Length));
+
+ foreach (var o in vout)
+ {
+ writer.Write(o);
+ }
+
+ var resultBytes = stream.ToArray();
+
+ writer.Close();
+
+ return resultBytes;
+ }
+
+
+ /// <summary>
/// Get raw bytes representation of our output.
/// </summary>
/// <returns>Byte sequence.</returns>
- public IList<byte> ToBytes()
+ public static implicit operator byte[] (CTxOut output)
{
- List<byte> resultBytes = new List<byte>();
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
+
+ writer.Write(output.nValue); // txout value
+ writer.Write(VarInt.EncodeVarInt(output.scriptPubKey.Size)); // scriptPubKey length
+ writer.Write(output.scriptPubKey); // scriptPubKey
+
+ var resultBytes = stream.ToArray();
- resultBytes.AddRange(Interop.LEBytes(nValue)); // txout value
- resultBytes.AddRange(VarInt.EncodeVarInt(scriptPubKey.LongLength)); // scriptPubKey length
- resultBytes.AddRange(scriptPubKey); // scriptPubKey
+ writer.Close();
return resultBytes;
}
- public override string ToString ()
+ /// <summary>
+ /// Null prevouts have -1 value
+ /// </summary>
+ public void SetNull()
+ {
+ nValue = ulong.MaxValue;
+ scriptPubKey = new CScript();
+ }
+
+ /// <summary>
+ /// Empty outputs have zero value and empty scriptPubKey
+ /// </summary>
+ public void SetEmpty()
+ {
+ nValue = 0;
+ scriptPubKey = new CScript();
+ }
+
+ public bool IsNull
+ {
+ get { return (nValue == ulong.MaxValue); }
+ }
+
+ public bool IsEmpty
+ {
+ get { return nValue == 0 && scriptPubKey.IsNull; }
+ }
+
+ /// <summary>
+ /// Serialized size
+ /// </summary>
+ public uint Size
+ {
+ get
+ {
+ var nScriptSize = scriptPubKey.Size;
+ return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
+ }
+ }
+
+ public override string ToString ()
{
- StringBuilder sb = new StringBuilder ();
- sb.AppendFormat ("CTxOut(nValue={0},scriptPubKey={1}", nValue, scriptPubKey.ToString());
+ var sb = new StringBuilder ();
+ sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
return sb.ToString ();
}