X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxOut.cs;h=3017feae4e8a21e925f7982e74dd82bf5db9b615;hb=ced87004f5e3d01ccb655b690289df227f3f87bf;hp=c5adf1c6a0ac01f1c7547945e1840e94ee05bc4f;hpb=0279648337efe9bbe32b5204e247529243805484;p=NovacoinLibrary.git
diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs
index c5adf1c..3017fea 100644
--- a/Novacoin/CTxOut.cs
+++ b/Novacoin/CTxOut.cs
@@ -19,6 +19,7 @@
using System;
using System.Text;
using System.Collections.Generic;
+using System.IO;
namespace Novacoin
{
@@ -30,7 +31,7 @@ namespace Novacoin
///
/// Input value.
///
- public long nValue = -1;
+ public ulong nValue = ulong.MaxValue;
///
/// Second half of script which contains spending instructions.
@@ -38,6 +39,17 @@ namespace Novacoin
public CScript scriptPubKey;
///
+ /// Initialize new outpoint using provided value and script.
+ ///
+ /// Input value
+ /// Spending instructions.
+ public CTxOut(ulong nValue, CScript scriptPubKey)
+ {
+ this.nValue = nValue;
+ this.scriptPubKey = scriptPubKey;
+ }
+
+ ///
/// Initialize new CTxOut instance as a copy of another instance.
///
/// CTxOut instance.
@@ -58,41 +70,86 @@ namespace Novacoin
///
/// Read vout list from byte sequence.
///
- /// Reference to byte sequence
+ /// Reference to binary reader
/// Outputs array
- 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;
}
///
+ /// Deserialize outputs array.
+ ///
+ /// Byte array
+ /// Outputs array
+ 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;
+ }
+
+ ///
+ /// Create serialized representation of outputs array.
+ ///
+ /// Outputs array
+ /// Byte array
+ 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;
+ }
+
+
+ ///
/// Get raw bytes representation of our output.
///
/// Byte sequence.
public static implicit operator byte[] (CTxOut output)
{
- var resultBytes = new List();
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
- resultBytes.AddRange(BitConverter.GetBytes(output.nValue)); // txout value
+ writer.Write(output.nValue); // txout value
+ writer.Write(VarInt.EncodeVarInt(output.scriptPubKey.Size)); // scriptPubKey length
+ writer.Write(output.scriptPubKey); // scriptPubKey
- var s = (byte[])output.scriptPubKey;
- resultBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptPubKey length
- resultBytes.AddRange(s); // scriptPubKey
+ var resultBytes = stream.ToArray();
- return resultBytes.ToArray();
+ writer.Close();
+
+ return resultBytes;
}
///
@@ -100,7 +157,7 @@ namespace Novacoin
///
public void SetNull()
{
- nValue = -1;
+ nValue = ulong.MaxValue;
scriptPubKey = new CScript();
}
@@ -115,7 +172,7 @@ namespace Novacoin
public bool IsNull
{
- get { return (nValue == -1); }
+ get { return (nValue == ulong.MaxValue); }
}
public bool IsEmpty
@@ -123,7 +180,19 @@ namespace Novacoin
get { return nValue == 0 && scriptPubKey.IsNull; }
}
- public override string ToString ()
+ ///
+ /// Serialized size
+ ///
+ public int Size
+ {
+ get
+ {
+ var nScriptSize = scriptPubKey.Size;
+ return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
+ }
+ }
+
+ public override string ToString ()
{
var sb = new StringBuilder ();
sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());