X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxOut.cs;h=3017feae4e8a21e925f7982e74dd82bf5db9b615;hb=ced87004f5e3d01ccb655b690289df227f3f87bf;hp=920fcd5f95f6b341067d8184c345d73c5d724874;hpb=3887c570113ea13d39c3e5c79d6adeda56b4b461;p=NovacoinLibrary.git diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index 920fcd5..3017fea 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 ulong nValue; + public ulong nValue = ulong.MaxValue; /// /// Second half of script which contains spending instructions. @@ -20,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. @@ -34,56 +64,137 @@ namespace Novacoin /// public CTxOut() { - scriptPubKey = new CScript(); + SetEmpty(); } /// /// Read vout list from byte sequence. /// - /// Reference to byte sequence + /// Reference to binary reader /// Outputs array - public static CTxOut[] ReadTxOutList(ref WrappedList wBytes) + internal static CTxOut[] ReadTxOutList(ref BinaryReader reader) { - int nOutputs = (int)VarInt.ReadVarInt(ref wBytes); - CTxOut[] vout =new CTxOut[nOutputs]; + 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.GetItems(8), 0); + vout[nIndex].nValue = reader.ReadUInt64(); - int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes); - vout[nIndex].scriptPubKey = new CScript(wBytes.GetItems(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 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; + } + + /// + /// Null prevouts have -1 value + /// + public void SetNull() + { + nValue = ulong.MaxValue; + scriptPubKey = new CScript(); + } + + /// + /// Empty outputs have zero value and empty scriptPubKey + /// + 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; } + } + + /// + /// Serialized size + /// + public int Size + { + get + { + var nScriptSize = scriptPubKey.Size; + return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize; } } - public override string ToString () + 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 ();