X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTransaction.cs;h=e67ed459b6f64e1f8c7d8a7195e286c1d2ccc4cb;hb=161220f258cf9eb640d354951a48b45598301b1a;hp=d1de2656782f257c466473880cc50a0be1b9dd3e;hpb=6c6e3b0c68e764ea520dde48d5e44c757f1dabbb;p=NovacoinLibrary.git
diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs
index d1de265..e67ed45 100644
--- a/Novacoin/CTransaction.cs
+++ b/Novacoin/CTransaction.cs
@@ -19,6 +19,7 @@
using System;
using System.Text;
using System.Collections.Generic;
+using System.IO;
namespace Novacoin
{
@@ -268,7 +269,7 @@ namespace Novacoin
{
try
{
- var wBytes = new ByteQueue(txBytes);
+ var wBytes = new ByteQueue(ref txBytes);
nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
@@ -341,12 +342,12 @@ namespace Novacoin
///
/// Bytes sequence
/// Transactions array
- public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
+ internal static CTransaction[] ReadTransactionsList(ref BinaryReader reader)
{
try
{
// Read amount of transactions
- int nTransactions = (int)wTxBytes.GetVarInt();
+ int nTransactions = (int)VarInt.ReadVarInt(ref reader);
var tx = new CTransaction[nTransactions];
for (int nTx = 0; nTx < nTransactions; nTx++)
@@ -354,16 +355,16 @@ namespace Novacoin
// Fill the transactions array
tx[nTx] = new CTransaction();
- tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ tx[nTx].nVersion = reader.ReadUInt32();
+ tx[nTx].nTime = reader.ReadUInt32();
// Inputs array
- tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
+ tx[nTx].vin = CTxIn.ReadTxInList(ref reader);
// outputs array
- tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
+ tx[nTx].vout = CTxOut.ReadTxOutList(ref reader);
- tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ tx[nTx].nLockTime = reader.ReadUInt32();
}
return tx;
@@ -401,29 +402,34 @@ namespace Novacoin
///
public static implicit operator byte[] (CTransaction tx)
{
- var resultBytes = new List();
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
- resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion));
- resultBytes.AddRange(BitConverter.GetBytes(tx.nTime));
- resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength));
+ writer.Write(tx.nVersion);
+ writer.Write(tx.nTime);
+ writer.Write(VarInt.EncodeVarInt(tx.vin.LongLength));
foreach (var input in tx.vin)
{
- resultBytes.AddRange((byte[])input);
+ writer.Write(input);
}
- resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength));
+ writer.Write(VarInt.EncodeVarInt(tx.vout.LongLength));
foreach (var output in tx.vout)
{
- resultBytes.AddRange((byte[])output);
+ writer.Write(output);
}
- resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime));
+ writer.Write(tx.nLockTime);
+ var resultBytes = stream.ToArray();
+ writer.Close();
- return resultBytes.ToArray();
+ return resultBytes;
}
+
+
public override string ToString()
{
var sb = new StringBuilder();