Ejemplo n.º 1
0
        private TransactionOutput ReadTransactionOutput(LoggedBinaryReader binaryReader)
        {
            var amount       = binaryReader.ReadBytes(OutputAmountLength);
            var scriptPubKey = _hexReader.ReadVarString(binaryReader);

            return(new TransactionOutput(amount, scriptPubKey));
        }
Ejemplo n.º 2
0
        private TransactionInput ReadTransactionInput(LoggedBinaryReader binaryReader)
        {
            var transactionHash = binaryReader.ReadBytes(InputTransactionHashLength);
            var outputIndex     = binaryReader.ReadUInt32();
            var scriptSig       = _hexReader.ReadVarString(binaryReader);
            var sequence        = binaryReader.ReadUInt32();

            return(new TransactionInput(transactionHash, outputIndex, scriptSig, sequence));
        }
Ejemplo n.º 3
0
        public Transaction ReadTransaction(LoggedBinaryReader binaryReader)
        {
            var version = binaryReader.ReadUInt32();

            var countTransactionInputs = _hexReader.ReadVarInt(binaryReader, Endian.Little, out _);
            var hasWitness             = countTransactionInputs == 0;

            if (hasWitness)
            {
                // todo: use SegWit
                var __ = binaryReader.ReadByte();

                countTransactionInputs = _hexReader.ReadVarInt(binaryReader, Endian.Little, out _);
            }

            // todo: use transaction input pool here
            var transactionInputs = new TransactionInput[countTransactionInputs];

            for (var i = 0; i < countTransactionInputs; i++)
            {
                transactionInputs[i] = ReadTransactionInput(binaryReader);
            }

            var countTransactionOutputs = _hexReader.ReadVarInt(binaryReader, Endian.Little, out _);

            // todo: use transaction output pool here
            var transactionOutputs = new TransactionOutput[countTransactionOutputs];

            for (var i = 0; i < countTransactionOutputs; i++)
            {
                transactionOutputs[i] = ReadTransactionOutput(binaryReader);
            }

            var witnessAsciiStrings = new List <AsciiString>();

            if (hasWitness)
            {
                for (var i = 0; i < countTransactionInputs; i++)
                {
                    var countWitnessStackItems = _hexReader.ReadVarInt(binaryReader, Endian.Little, out _);
                    for (var j = 0; j < countWitnessStackItems; j++)
                    {
                        witnessAsciiStrings.Add(_hexReader.ReadVarString(binaryReader));
                    }
                }
            }

            var lockTime = binaryReader.ReadUInt32();

            var txId = GetTrxId(binaryReader.AsSpan());

            return(new Transaction(txId, version, transactionInputs, transactionOutputs, lockTime, hasWitness, witnessAsciiStrings));
        }
Ejemplo n.º 4
0
        public Block ReadBlock(LoggedBinaryReader binaryReader)
        {
            var blockHeader    = binaryReader.ReadBytes(80);
            var nbTransactions = _hexReader.ReadVarInt(binaryReader, Endian.Little, out var sizeOfNbTransactions);

            // todo: use transactions pool here.
            var transactions = new Transaction[nbTransactions];

            for (var i = 0; i < nbTransactions; i++)
            {
                binaryReader.Reset();
                transactions[i] = ReadTransaction(binaryReader);
            }

            return(new Block(blockHeader, sizeOfNbTransactions, transactions));
        }