Exemplo n.º 1
0
        public TransactionPayload(byte[] bytes)
        {
            Version = BitConverter.ToInt32(bytes, 0);
            var remaining = bytes.Skip(4);

            var txInCount = new IntegerPayload(remaining.ToArray());

            remaining = remaining.Skip(txInCount.ToBytes().Length);

            TxInputs = new List <TxInputPayload>();
            for (UInt64 i = 0; i < txInCount.Integer; i++)
            {
                var txInput = new TxInputPayload(remaining.ToArray());
                remaining = remaining.Skip(txInput.ToBytes().Length);
                TxInputs.Add(txInput);
            }

            var txOutCount = new IntegerPayload(remaining.ToArray());

            remaining = remaining.Skip(txOutCount.ToBytes().Length);

            TxOutputs = new List <TxOutputPayload>();
            for (UInt64 i = 0; i < txOutCount.Integer; i++)
            {
                var txOutput = new TxOutputPayload(remaining.ToArray());
                remaining = remaining.Skip(txOutput.ToBytes().Length);
                TxOutputs.Add(txOutput);
            }

            LockTime = BitConverter.ToUInt32(remaining.ToArray(), 0);
        }
Exemplo n.º 2
0
        public override byte[] ToBytes()
        {
            var bytes = new Byte[0].AsEnumerable();

            bytes = bytes.Concat(BitConverter.GetBytes(Version));

            var txInCount = new IntegerPayload((UInt64)TxInputs.Count);

            bytes = bytes.Concat(txInCount.ToBytes());
            foreach (TxInputPayload input in TxInputs)
            {
                bytes = bytes.Concat(input.ToBytes());
            }

            var txOutCount = new IntegerPayload((UInt64)TxOutputs.Count);

            bytes = bytes.Concat(txOutCount.ToBytes());
            foreach (TxOutputPayload output in TxOutputs)
            {
                bytes = bytes.Concat(output.ToBytes());
            }

            bytes = bytes.Concat(BitConverter.GetBytes(LockTime));

            return(bytes.ToArray());
        }
Exemplo n.º 3
0
        public override byte[] ToBytes()
        {
            var bytes = new Byte[0].AsEnumerable();

            bytes = bytes.Concat(BitConverter.GetBytes(Version));
            bytes = bytes.Concat(PreviousBlockHash);
            bytes = bytes.Concat(MerkleRoot);
            bytes = bytes.Concat(BitConverter.GetBytes(TimeStamp));
            bytes = bytes.Concat(BitConverter.GetBytes(Bits));
            bytes = bytes.Concat(BitConverter.GetBytes(Nonce));

            var pcmBytes = PrimeChainMultiplier.ToByteArray();
            var pcmCount = new IntegerPayload((UInt64)pcmBytes.Length);

            bytes = bytes.Concat(pcmCount.ToBytes()).Concat(pcmBytes);

            var txCount = new IntegerPayload((UInt64)Transactions.Count);

            bytes = bytes.Concat(txCount.ToBytes());
            foreach (TransactionPayload tx in Transactions)
            {
                bytes = bytes.Concat(tx.ToBytes());
            }

            return(bytes.ToArray());
        }
Exemplo n.º 4
0
        public override byte[] ToBytes()
        {
            var strBytes  = Encoding.ASCII.GetBytes(String);
            var sizeBytes = new IntegerPayload((UInt64)strBytes.Length).ToBytes();

            return(sizeBytes.Concat(strBytes).ToArray());
        }
Exemplo n.º 5
0
        public override byte[] ToBytes()
        {
            var amount       = BitConverter.GetBytes(Amount);
            var script       = Script.ToBytes();
            var scriptLength = new IntegerPayload((UInt64)script.Length);

            return(amount
                   .Concat(scriptLength.ToBytes())
                   .Concat(script)
                   .ToArray());
        }
Exemplo n.º 6
0
        public TxOutputPayload(byte[] bytes)
        {
            Amount = BitConverter.ToInt64(bytes, 0);
            var remaining = bytes.Skip(8);

            var scriptLength = new IntegerPayload(remaining.ToArray());

            remaining = remaining.Skip(scriptLength.ToBytes().Length);

            Script = new UnknownPayload(remaining.Take((Int32)scriptLength.Integer).ToArray());
        }
Exemplo n.º 7
0
        public override Byte[] ToBytes()
        {
            var count = new IntegerPayload((UInt64)Entries.Count);
            var bytes = count.ToBytes().AsEnumerable();

            for (Int32 i = 0; i < Entries.Count; i++)
            {
                bytes = bytes.Concat(Entries[i].ToBytes());
            }

            return(bytes.ToArray());
        }
Exemplo n.º 8
0
        public InvPayload(byte[] bytes)
        {
            var count     = new IntegerPayload(bytes);
            var remaining = bytes.Skip(count.ToBytes().Length);

            Entries = new List <InvEntryPayload>();
            for (UInt64 i = 0; i < count.Integer; i++)
            {
                var invEntry = new InvEntryPayload(remaining.ToArray());
                Entries.Add(invEntry);
                remaining = remaining.Skip(invEntry.ToBytes().Length);
            }
        }
Exemplo n.º 9
0
        public override byte[] ToBytes()
        {
            var txOutPointBytes   = PreviousTransaction.ToBytes();
            var scriptBytes       = Script.ToBytes();
            var scriptLengthBytes = new IntegerPayload((UInt64)scriptBytes.Length).ToBytes();
            var sequenceBytes     = BitConverter.GetBytes(Sequence);

            return(txOutPointBytes
                   .Concat(scriptLengthBytes)
                   .Concat(scriptBytes)
                   .Concat(sequenceBytes)
                   .ToArray());
        }
Exemplo n.º 10
0
        public TxInputPayload(byte[] bytes)
        {
            PreviousTransaction = new TxOutPointPayload(bytes);
            var remaining = bytes.Skip(PreviousTransaction.ToBytes().Length);

            var scriptLength = new IntegerPayload(remaining.ToArray());

            remaining = remaining.Skip(scriptLength.ToBytes().Length);

            Script    = new UnknownPayload(remaining.Take((Int32)scriptLength.Integer).ToArray());
            remaining = remaining.Skip(Script.ToBytes().Length);

            Sequence = BitConverter.ToUInt32(remaining.ToArray(), 0);
        }
Exemplo n.º 11
0
        public BlockPayload(byte[] bytes)
        {
            Version = BitConverter.ToInt32(bytes, 0);
            var remaining = bytes.Skip(4);

            PreviousBlockHash = remaining.Take(32).ToArray();
            remaining         = remaining.Skip(32);

            var merkleRoot = remaining.Take(32).ToArray();

            remaining = remaining.Skip(32);

            TimeStamp = BitConverter.ToUInt32(remaining.ToArray(), 0);
            remaining = remaining.Skip(4);

            Bits      = BitConverter.ToUInt32(remaining.ToArray(), 0);
            remaining = remaining.Skip(4);

            Nonce     = BitConverter.ToUInt32(remaining.ToArray(), 0);
            remaining = remaining.Skip(4);

            // Handle PrimeChainMultiplier...
            var pcmCount = new IntegerPayload(remaining.ToArray());

            remaining = remaining.Skip(pcmCount.ToBytes().Length);
            var pcmBytes = remaining.Take((Int32)pcmCount.Integer).ToArray();

            remaining            = remaining.Skip(pcmBytes.Length);
            PrimeChainMultiplier = new BigInteger(pcmBytes);

            var txCount = new IntegerPayload(remaining.ToArray());

            remaining = remaining.Skip(txCount.ToBytes().Length);

            Transactions = new List <TransactionPayload>();
            for (UInt64 i = 0; i < txCount.Integer; i++)
            {
                var tx = new TransactionPayload(remaining.ToArray());
                remaining = remaining.Skip(tx.ToBytes().Length);
                Transactions.Add(tx);
            }

            if (!merkleRoot.SequenceEqual(MerkleRoot))
            {
                throw new Exception("MerkleRoot incorrect!");
            }
        }
Exemplo n.º 12
0
        public StringPayload(Byte[] bytes)
        {
            if (bytes.Length == 0)
            {
                throw new ArgumentException("StringPayload from bytes requires at least one byte.");
            }

            if (bytes[0] == 0x00)
            {
                String = "";
            }
            else
            {
                var size   = new IntegerPayload(bytes);
                var offset = size.ToBytes().Length;
                String = Encoding.ASCII.GetString(bytes, offset, (Int32)size.Integer);
            }
        }
Exemplo n.º 13
0
        public Byte[] Hash()
        {
            var version   = BitConverter.GetBytes(Version);
            var timeStamp = BitConverter.GetBytes(TimeStamp);
            var bits      = BitConverter.GetBytes(Bits);
            var nonce     = BitConverter.GetBytes(Nonce);
            var pcmBytes  = PrimeChainMultiplier.ToByteArray();
            var pcmCount  = new IntegerPayload((UInt64)pcmBytes.Length);
            var pcm       = pcmCount.ToBytes().Concat(pcmBytes);
            var bytes     = version
                            .Concat(PreviousBlockHash)
                            .Concat(MerkleRoot)
                            .Concat(timeStamp)
                            .Concat(bits)
                            .Concat(nonce)
                            .Concat(pcm)
                            .ToArray();

            SHA256 sha256 = SHA256Managed.Create();

            return(sha256.ComputeHash(sha256.ComputeHash(bytes)));
        }