using System;
using System.Text;
using System.Collections.Generic;
+using System.IO;
namespace Novacoin
{
/// <summary>
/// Input value.
/// </summary>
- public long nValue = -1;
+ public ulong nValue = ulong.MaxValue;
/// <summary>
/// Second half of script which contains spending instructions.
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>
/// <param name="o">CTxOut instance.</param>
/// <summary>
/// Read vout list from byte sequence.
/// </summary>
- /// <param name="wBytes">Reference to byte sequence</param>
+ /// <param name="wBytes">Reference to binary reader</param>
/// <returns>Outputs array</returns>
- public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes)
+ internal static CTxOut[] ReadTxOutList(ref BinaryReader reader)
{
- int nOutputs = (int)wBytes.GetVarInt();
+ 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 = BitConverter.ToUInt32(wBytes.Get(8), 0);
+ vout[nIndex].nValue = reader.ReadUInt64();
- int nScriptPKLen = (int)wBytes.GetVarInt();
- vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+ int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
+ vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
}
return vout;
}
/// <summary>
+ /// Deserialize outputs array.
+ /// </summary>
+ /// <param name="outBytes">Byte array</param>
+ /// <returns>Outputs array</returns>
+ public static CTxOut[] DeserializeOutputsArray(byte[] outBytes)
+ {
+ var stream = new MemoryStream(outBytes);
+ var reader = new BinaryReader(stream);
+
+ CTxOut[] result = ReadTxOutList(ref reader);
+
+ 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 static implicit operator byte[] (CTxOut output)
{
- var 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
- resultBytes.AddRange(BitConverter.GetBytes(output.nValue)); // txout value
+ var resultBytes = stream.ToArray();
- byte[] s = output.scriptPubKey;
- resultBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptPubKey length
- resultBytes.AddRange(s); // scriptPubKey
+ writer.Close();
- return resultBytes.ToArray();
+ return resultBytes;
}
/// <summary>
/// </summary>
public void SetNull()
{
- nValue = -1;
+ nValue = ulong.MaxValue;
scriptPubKey = new CScript();
}
public bool IsNull
{
- get { return (nValue == -1); }
+ get { return (nValue == ulong.MaxValue); }
}
public bool IsEmpty
/// <summary>
/// Serialized size
/// </summary>
- public int Size
+ public uint Size
{
get
{