X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxOut.cs;h=eb24845cbd503f15d2c0b6714fdb512a496ba074;hb=be9d844557911f95165d2c9875c4f5b2822cfc92;hp=e75579a9601bb40a6ab040cab1f2e09a808211c7;hpb=0aa2d5cb7cdb5813440e7afcdbe6014e9b8deeee;p=NovacoinLibrary.git diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index e75579a..eb24845 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. @@ -69,7 +70,7 @@ namespace Novacoin { // Fill outputs array vout[nIndex] = new CTxOut(); - vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.Get(8), 0); + vout[nIndex].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0); int nScriptPKLen = (int)wBytes.GetVarInt(); vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen)); @@ -82,20 +83,20 @@ namespace Novacoin /// Get raw bytes representation of our output. /// /// Byte sequence. - public IList Bytes + public static implicit operator byte[] (CTxOut output) { - get - { - var resultBytes = new List(); + var stream = new MemoryStream(); + var writer = new BinaryWriter(stream); - resultBytes.AddRange(BitConverter.GetBytes(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 = scriptPubKey.Bytes; - resultBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptPubKey length - resultBytes.AddRange(s); // scriptPubKey + var resultBytes = stream.ToArray(); - return resultBytes; - } + writer.Close(); + + return resultBytes; } /// @@ -103,7 +104,7 @@ namespace Novacoin /// public void SetNull() { - nValue = -1; + nValue = ulong.MaxValue; scriptPubKey = new CScript(); } @@ -118,7 +119,7 @@ namespace Novacoin public bool IsNull { - get { return (nValue == -1); } + get { return (nValue == ulong.MaxValue); } } public bool IsEmpty @@ -126,7 +127,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());