Example #1
0
        public bool TryReadBlock(ref ByteSequenceReader r)
        {
            if (!TryReadBlockHeader(ref r))
            {
                goto fail;
            }

            if (!r.TryReadVariant(out var count))
            {
                goto fail;
            }

            Txs = new Transaction[count];

            for (var i = 0L; i < count; i++)
            {
                var t = new Transaction();
                Txs[i] = t;
                if (!t.TryReadTransaction(ref r))
                {
                    goto fail;
                }
            }

            if (!VerifyMerkleRoot())
            {
                goto fail;
            }

            return(true);

fail:
            return(false);
        }
Example #2
0
        public bool TryParseTransaction(ref ByteSequenceReader r, IBlockParser bp)
        {
            var offset = r.Data.Consumed;
            var start  = r.Data.Position;

            if (!r.TryReadLittleEndian(out _version))
            {
                return(false);
            }
            if (!r.TryReadVariant(out var countIn))
            {
                return(false);
            }

            bp.TxStart(this, offset);

            Inputs = new TxIn[countIn];
            for (var i = 0L; i < countIn; i++)
            {
                ref var txIn = ref Inputs[i];
                if (!txIn.TryParseTxIn(ref r, bp))
                {
                    return(false);
                }
            }
Example #3
0
        public bool TryParseTxIn(ref ByteSequenceReader r, IBlockParser bp)
        {
            if (!_prevOutPoint.TryReadOutPoint(ref r))
            {
                goto fail;
            }

            bp.TxInStart(this, r.Data.Consumed);

            if (!_scriptSig.TryParseScript(ref r, bp))
            {
                goto fail;
            }
            if (!r.TryReadLittleEndian(out _sequence))
            {
                goto fail;
            }

            bp.TxInParsed(this, r.Data.Consumed);

            return(true);

fail:
            return(false);
        }
Example #4
0
        public bool TryParseScript(ref ByteSequenceReader reader, IBlockParser bp, bool withoutLength = false)
        {
            var length = reader.Data.Remaining;

            if (!withoutLength && !reader.TryReadVariant(out length))
            {
                goto fail;
            }

            bp.ScriptStart(this, reader.Data.Consumed);

            if (reader.Data.Remaining < length)
            {
                goto fail;
            }

            _script = (VarType)(ReadOnlyByteSequence)reader.Data.Sequence.Slice(reader.Data.Position, length);
            reader.Data.Advance(length);

            bp.ScriptParsed(this, reader.Data.Consumed);

            return(true);

fail:
            return(false);
        }
Example #5
0
        public static (bool ok, Script script) ParseHex(string rawScriptHex, bool withoutLength = false)
        {
            var bytes = rawScriptHex.HexToBytes();
            var s     = new Script();
            var ros   = new ReadOnlyByteSequence(bytes);
            var sr    = new ByteSequenceReader(ros);

            return(s.TryReadScript(ref sr, withoutLength), s);
        }
Example #6
0
        public bool TryReadBlock(ref ReadOnlyByteSequence ros)
        {
            var r = new ByteSequenceReader(ros);

            if (!TryReadBlock(ref r))
            {
                return(false);
            }
            ros = ros.Data.Slice(r.Data.Consumed);
            return(true);
        }
Example #7
0
        public bool TryParseBlock(ref ReadOnlyByteSequence ros, int height, IBlockParser bp)
        {
            var r = new ByteSequenceReader(ros);

            if (!TryParseBlock(ref r, height, bp))
            {
                return(false);
            }
            ros = ros.Data.Slice(r.Data.Consumed);
            return(true);
        }
Example #8
0
        public bool TryReadOutPoint(ref ByteSequenceReader r)
        {
            var txHash = TxId;

            if (!r.TryReadUInt256(ref txHash) || !r.TryReadLittleEndian(out int index))
            {
                return(false);
            }

            TxId  = txHash;
            Index = index;
            return(true);
        }
Example #9
0
        public bool TryReadTransaction(ref ReadOnlyByteSequence ros)
        {
            var r = new ByteSequenceReader(ros);

            if (!TryReadTransaction(ref r))
            {
                goto fail;
            }

            ros = ros.Data.Slice(r.Data.Consumed);

            return(true);

fail:
            return(false);
        }
Example #10
0
        public bool TryReadBlockHeader(ref ByteSequenceReader r)
        {
            if (r.Data.Remaining < BlockHeaderSize)
            {
                return(false);
            }

            var start = r.Data.Position;

            if (!r.TryReadLittleEndian(out _version))
            {
                return(false);
            }
            if (!r.TryReadUInt256(ref _hashPrevBlock))
            {
                return(false);
            }
            if (!r.TryReadUInt256(ref _hashMerkleRoot))
            {
                return(false);
            }
            if (!r.TryReadLittleEndian(out _time))
            {
                return(false);
            }
            if (!r.TryReadLittleEndian(out _bits))
            {
                return(false);
            }
            if (!r.TryReadLittleEndian(out _nonce))
            {
                return(false);
            }

            var end = r.Data.Position;

            var blockBytes = r.Data.Sequence.Slice(start, end).ToArray();

            using var sha256 = SHA256.Create();
            var hash1 = sha256.ComputeHash(blockBytes);
            var hash2 = sha256.ComputeHash(hash1);

            hash2.CopyTo(_hash.Span);
            return(true);
        }
Example #11
0
        public bool TryParseBlock(ref ByteSequenceReader r, int height, IBlockParser bp)
        {
            var offset = r.Data.Consumed;

            if (!TryReadBlockHeader(ref r))
            {
                goto fail;
            }

            Height = height;

            bp.BlockStart(this, offset);

            if (!r.TryReadVariant(out var count))
            {
                goto fail;
            }

            Txs = new Transaction[count];

            for (var i = 0L; i < count; i++)
            {
                var t = new Transaction();
                Txs[i] = t;
                if (!t.TryParseTransaction(ref r, bp))
                {
                    goto fail;
                }
            }

            if (!VerifyMerkleRoot())
            {
                goto fail;
            }

            bp.BlockParsed(this, r.Data.Consumed);

            return(true);

fail:
            return(false);
        }
Example #12
0
        public bool TryReadTxIn(ref ByteSequenceReader r)
        {
            if (!_prevOutPoint.TryReadOutPoint(ref r))
            {
                goto fail;
            }
            if (!_scriptSig.TryReadScript(ref r))
            {
                goto fail;
            }
            if (!r.TryReadLittleEndian(out _sequence))
            {
                goto fail;
            }

            return(true);

fail:
            return(false);
        }
Example #13
0
        public bool TryReadScript(ref ByteSequenceReader r, bool withoutLength = false)
        {
            var length = r.Data.Remaining;

            if (!withoutLength && !r.TryReadVariant(out length))
            {
                goto fail;
            }

            if (r.Data.Remaining < length)
            {
                goto fail;
            }

            _script = (VarType)(ReadOnlyByteSequence)r.Data.Sequence.Slice(r.Data.Position, length);
            r.Data.Advance(length);

            return(true);

fail:
            return(false);
        }