X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxOut.cs;h=eb24845cbd503f15d2c0b6714fdb512a496ba074;hb=be9d844557911f95165d2c9875c4f5b2822cfc92;hp=8acab9be307684e199cc3d528105d8f143267809;hpb=d594e4dba0ffa4414741069eb90a8754a119a819;p=NovacoinLibrary.git diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index 8acab9b..eb24845 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -1,6 +1,25 @@ -using System; +/** + * 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 . + */ + +using System; using System.Text; using System.Collections.Generic; +using System.IO; namespace Novacoin { @@ -12,7 +31,7 @@ namespace Novacoin /// /// Input value. /// - public long nValue = -1; + public ulong nValue = ulong.MaxValue; /// /// Second half of script which contains spending instructions. @@ -44,16 +63,16 @@ namespace Novacoin /// Outputs array public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes) { - int nOutputs = (int)VarInt.ReadVarInt(ref wBytes); - CTxOut[] vout =new CTxOut[nOutputs]; + int nOutputs = (int)wBytes.GetVarInt(); + 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 = BitConverter.ToUInt64(wBytes.Get(8), 0); - int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes); + int nScriptPKLen = (int)wBytes.GetVarInt(); vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen)); } @@ -64,21 +83,20 @@ namespace Novacoin /// Get raw bytes representation of our output. /// /// Byte sequence. - public IList Bytes + public static implicit operator byte[] (CTxOut output) { - get - { - List 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 - List s = new List(scriptPubKey.Bytes); + var resultBytes = stream.ToArray(); - resultBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptPubKey length - resultBytes.AddRange(s); // scriptPubKey + writer.Close(); - return resultBytes; - } + return resultBytes; } /// @@ -86,7 +104,7 @@ namespace Novacoin /// public void SetNull() { - nValue = -1; + nValue = ulong.MaxValue; scriptPubKey = new CScript(); } @@ -101,7 +119,7 @@ namespace Novacoin public bool IsNull { - get { return (nValue == -1); } + get { return (nValue == ulong.MaxValue); } } public bool IsEmpty @@ -109,9 +127,21 @@ 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 () { - StringBuilder sb = new StringBuilder (); + var sb = new StringBuilder (); sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString()); return sb.ToString ();