public static byte[] ReadTransaction(BinaryReader reader)
{
var txBytes = new byte[1024];
var offset = 0;
// read version
reader.ReadExactly(txBytes, offset, 4);
offset += 4;
// read inputs
var inputCount = reader.ReadVarInt(ref txBytes, ref offset).ToIntChecked();
for (var i = 0; i < inputCount; i++)
{
// read prevTxHash and prevTxOutputIndex
SizeAtLeast(ref txBytes, offset + 36);
reader.ReadExactly(txBytes, offset, 36);
offset += 36;
// read scriptSignatureLength
var scriptSignatureLength = reader.ReadVarInt(ref txBytes, ref offset).ToIntChecked();
// read scriptSignature
SizeAtLeast(ref txBytes, offset + scriptSignatureLength);
reader.ReadExactly(txBytes, offset, scriptSignatureLength);
offset += scriptSignatureLength;
// read sequence
SizeAtLeast(ref txBytes, offset + 4);
reader.ReadExactly(txBytes, offset, 4);
offset += 4;
}
// read outputs
var outputCount = reader.ReadVarInt(ref txBytes, ref offset).ToIntChecked();
for (var i = 0; i < outputCount; i++)
{
// read value
SizeAtLeast(ref txBytes, offset + 8);
reader.ReadExactly(txBytes, offset, 8);
offset += 8;
// read scriptPublicKeyLength
var scriptPublicKeyLength = reader.ReadVarInt(ref txBytes, ref offset).ToIntChecked();
// read scriptPublicKey
SizeAtLeast(ref txBytes, offset + scriptPublicKeyLength);
reader.ReadExactly(txBytes, offset, scriptPublicKeyLength);
offset += scriptPublicKeyLength;
}
// read lockTime
SizeAtLeast(ref txBytes, offset + 4);
reader.ReadExactly(txBytes, offset, 4);
offset += 4;
// resize raw tx bytes to final size
Array.Resize(ref txBytes, offset);
return txBytes;
}