X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxIn.cs;h=067ad4639dabd9b3c46f9df6287e8c30473c4bce;hb=be9d844557911f95165d2c9875c4f5b2822cfc92;hp=4b88d15b855553f7b65e142ed34881b9e316c7d9;hpb=0aa2d5cb7cdb5813440e7afcdbe6014e9b8deeee;p=NovacoinLibrary.git diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs index 4b88d15..067ad46 100644 --- a/Novacoin/CTxIn.cs +++ b/Novacoin/CTxIn.cs @@ -19,13 +19,32 @@ using System; using System.Text; using System.Collections.Generic; +using System.IO; namespace Novacoin { - /// - /// Transaction input. - /// - public class CTxIn + [Serializable] + public class TxInConstructorException : Exception + { + public TxInConstructorException() + { + } + + public TxInConstructorException(string message) + : base(message) + { + } + + public TxInConstructorException(string message, Exception inner) + : base(message, inner) + { + } + } + + /// + /// Transaction input. + /// + public class CTxIn { /// /// Previous input data @@ -69,42 +88,61 @@ namespace Novacoin /// Inputs array public static CTxIn[] ReadTxInList(ref ByteQueue wBytes) { - // Get amount - int nInputs = (int)wBytes.GetVarInt(); - var vin = new CTxIn[nInputs]; - - for (int nIndex = 0; nIndex < nInputs; nIndex++) + try + { + // Get amount + int nInputs = (int)wBytes.GetVarInt(); + var vin = new CTxIn[nInputs]; + + for (int nIndex = 0; nIndex < nInputs; nIndex++) + { + // Fill inputs array + vin[nIndex] = new CTxIn(); + vin[nIndex].prevout = new COutPoint(wBytes.Get(36)); + vin[nIndex].scriptSig = new CScript(wBytes.Get((int)wBytes.GetVarInt())); + vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0); + } + + // Return inputs array + return vin; + } + catch (Exception e) { - // Fill inputs array - vin[nIndex] = new CTxIn(); - vin[nIndex].prevout = new COutPoint(wBytes.Get(36)); - vin[nIndex].scriptSig = new CScript(wBytes.Get((int)wBytes.GetVarInt())); - vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0); + throw new TxInConstructorException("Desirealization failed.", e); } + } + + /// + /// Serialized size + /// + public int Size + { + get { + int nSize = 40; // COutPoint, nSequence + nSize += VarInt.GetEncodedSize(scriptSig.Size); + nSize += scriptSig.Size; - // Return inputs array - return vin; + return nSize; + } } /// /// Get raw bytes representation of our input. /// /// Byte sequence. - public IList Bytes + public static implicit operator byte[] (CTxIn input) { - get - { - var inputBytes = new List(); - - inputBytes.AddRange(prevout.Bytes); // prevout + var stream = new MemoryStream(); + var writer = new BinaryWriter(stream); - var s = scriptSig.Bytes; - inputBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptSig length - inputBytes.AddRange(s); // scriptSig - inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence + writer.Write(input.prevout); // prevout + writer.Write(VarInt.EncodeVarInt(input.scriptSig.Size)); // scriptSig length + writer.Write(input.scriptSig); // scriptSig + writer.Write(input.nSequence); // nSequence - return inputBytes; - } + var inputBytes = stream.ToArray(); + writer.Close(); + return inputBytes; } public bool IsFinal @@ -120,7 +158,7 @@ namespace Novacoin if(prevout.IsNull) { - sb.AppendFormat(", coinbase={0}", Interop.ToHex(scriptSig.Bytes)); + sb.AppendFormat(", coinbase={0}", Interop.ToHex((byte[])scriptSig)); } else { @@ -140,4 +178,3 @@ namespace Novacoin } } -