Esempio n. 1
0
        public override void Deserialize(byte[] data)
        {
            // Decode our RLP item from data.
            RLPList rlpList = (RLPList)RLP.Decode(data);

            // Verify the sizes of all components.
            if (!rlpList.Items[0].IsByteArray)
            {
                throw new ArgumentException("RLPx EIP8 auth packet's first item (signature) was not a byte array.");
            }
            else if (((byte[])rlpList.Items[0]).Length != EthereumEcdsa.SIGNATURE_RSV_SIZE)
            {
                throw new ArgumentException("RLPx EIP8 auth packet's first item (signature) was the incorrect size.");
            }
            else if (!rlpList.Items[1].IsByteArray)
            {
                throw new ArgumentException("RLPx EIP8 auth packet's second item (public key) was not a byte array.");
            }
            else if (((byte[])rlpList.Items[1]).Length != EthereumEcdsa.PUBLIC_KEY_SIZE)
            {
                throw new ArgumentException("RLPx EIP8 auth packet's second item (public key) was the incorrect size.");
            }
            else if (!rlpList.Items[2].IsByteArray)
            {
                throw new ArgumentException("RLPx EIP8 auth packet's third item (nonce) was not a byte array.");
            }
            else if (((byte[])rlpList.Items[2]).Length != RLPxSession.NONCE_SIZE)
            {
                throw new ArgumentException("RLPx EIP8 auth packet's third item (nonce) was the incorrect size.");
            }
            else if (rlpList.Items.Count >= 4 && !rlpList.Items[3].IsByteArray)
            {
                throw new ArgumentException("RLPx EIP8 auth packet's fourth item (version) was not a byte array.");
            }

            // Obtain all components.
            Memory <byte> signature = rlpList.Items[0];

            R         = signature.Slice(0, 32).ToArray();
            S         = signature.Slice(32, 32).ToArray();
            V         = signature.Span[64];
            PublicKey = rlpList.Items[1];
            Nonce     = rlpList.Items[2];

            // Decode version if it's available.
            if (rlpList.Items.Count >= 4)
            {
                Version = RLP.ToInteger((RLPByteArray)rlpList.Items[3], 32, false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Deserializes the given RLP serialized receipt and sets all values accordingly.
        /// </summary>
        /// <param name="item">The RLP item to deserialize and obtain values from.</param>
        public void Deserialize(RLPItem item)
        {
            // Verify this is a list
            if (!item.IsList)
            {
                throw new ArgumentException();
            }

            // Verify it has 4 items.
            RLPList rlpReceipt = (RLPList)item;

            if (rlpReceipt.Items.Count != 4)
            {
                throw new ArgumentException();
            }

            // Verify the types of all items
            if (!rlpReceipt.Items[0].IsByteArray ||
                !rlpReceipt.Items[1].IsByteArray ||
                !rlpReceipt.Items[2].IsByteArray ||
                !rlpReceipt.Items[3].IsList)
            {
                throw new ArgumentException();
            }

            // Set our state root
            RLPByteArray rlpStateRoot = (RLPByteArray)rlpReceipt.Items[0];

            StateRoot = rlpStateRoot.Data.ToArray();

            // Set our gas used
            RLPByteArray rlpGasUsed = (RLPByteArray)rlpReceipt.Items[1];

            GasUsed = RLP.ToInteger(rlpGasUsed, EVMDefinitions.WORD_SIZE);

            // Set our bloom
            RLPByteArray rlpBloom = (RLPByteArray)rlpReceipt.Items[2];

            Bloom = RLP.ToInteger(rlpBloom, EVMDefinitions.BLOOM_FILTER_SIZE);

            // Obtain our logs
            RLPList rlpLogs = (RLPList)rlpReceipt.Items[3];

            Logs = new List <Log>();
            foreach (RLPItem rlpLog in rlpLogs.Items)
            {
                // Add our log
                Logs.Add(new Log(rlpLog));
            }
        }
Esempio n. 3
0
File: Log.cs Progetto: zutobg/Meadow
        /// <summary>
        /// Deserializes the given RLP serialized log and sets all values accordingly.
        /// </summary>
        /// <param name="item">The RLP item to deserialize and obtain values from.</param>
        public void Deserialize(RLPItem item)
        {
            // Verify this is a list
            if (!item.IsList)
            {
                throw new ArgumentException();
            }

            // Verify it has 3 items.
            RLPList rlpLog = (RLPList)item;

            if (rlpLog.Items.Count != 3)
            {
                throw new ArgumentException();
            }

            // Verify the types of all items
            if (!rlpLog.Items[0].IsByteArray ||
                !rlpLog.Items[1].IsList ||
                !rlpLog.Items[2].IsByteArray)
            {
                throw new ArgumentException();
            }

            // Set our address
            RLPByteArray rlpAddress = (RLPByteArray)rlpLog.Items[0];

            Address = new Address(rlpAddress.Data.Span);

            // Obtain our topics
            RLPList rlpTopicsList = (RLPList)rlpLog.Items[1];

            Topics = new List <BigInteger>();
            foreach (RLPItem rlpTopic in rlpTopicsList.Items)
            {
                // Verify all of our items are data
                if (rlpTopic.GetType() != typeof(RLPByteArray))
                {
                    throw new ArgumentException();
                }

                // Add our topic.
                Topics.Add(RLP.ToInteger((RLPByteArray)rlpTopic, EVMDefinitions.WORD_SIZE));
            }

            // Obtain our data
            RLPByteArray rlpData = (RLPByteArray)rlpLog.Items[2];

            Data = rlpData.Data.ToArray();
        }
Esempio n. 4
0
        /// <summary>
        /// Deserializes the given RLP serialized account and sets all values accordingly.
        /// </summary>
        /// <param name="item">The RLP item to deserialize and obtain values from.</param>
        public void Deserialize(RLPItem item)
        {
            // Verify this is a list
            if (!item.IsList)
            {
                throw new ArgumentException();
            }

            // Verify it has 4 items.
            RLPList rlpAccount = (RLPList)item;

            if (rlpAccount.Items.Count != 4)
            {
                throw new ArgumentException();
            }

            // Verify the types of all items
            if (!rlpAccount.Items[0].IsByteArray ||
                !rlpAccount.Items[1].IsByteArray ||
                !rlpAccount.Items[2].IsByteArray ||
                !rlpAccount.Items[3].IsByteArray)
            {
                throw new ArgumentException();
            }

            // Set our nonce, balance, storage, and code hash.
            Nonce       = RLP.ToInteger((RLPByteArray)rlpAccount.Items[0]);
            Balance     = RLP.ToInteger((RLPByteArray)rlpAccount.Items[1]);
            StorageRoot = rlpAccount.Items[2];
            CodeHash    = rlpAccount.Items[3];

            // Verify the length of our storage root and code hash.
            if (StorageRoot.Length != KeccakHash.HASH_SIZE || CodeHash.Length != KeccakHash.HASH_SIZE)
            {
                throw new ArgumentException();
            }

            // Initialize our storage change cache
            StorageCache = new Dictionary <Memory <byte>, byte[]>(new MemoryComparer <byte>());

            // Load our trie given our storage root
            StorageTrie = new Trie(Configuration.Database, StorageRoot);
        }
Esempio n. 5
0
        public override void Deserialize(byte[] data)
        {
            // Decode our RLP item from data.
            RLPList rlpList = (RLPList)RLP.Decode(data);

            // Verify the sizes of all components.
            if (!rlpList.Items[0].IsByteArray)
            {
                throw new ArgumentException("RLPx EIP8 auth-ack packet's first item (ephemeral public key) was not a byte array.");
            }
            else if (((byte[])rlpList.Items[0]).Length != EthereumEcdsa.PUBLIC_KEY_SIZE)
            {
                throw new ArgumentException("RLPx EIP8 auth-ack packet's first item (ephemeral public key) was not the correct size.");
            }
            else if (!rlpList.Items[1].IsByteArray)
            {
                throw new ArgumentException("RLPx EIP8 auth-ack packet's second item (nonce) was not a byte array.");
            }
            else if (((byte[])rlpList.Items[1]).Length != RLPxSession.NONCE_SIZE)
            {
                throw new ArgumentException("RLPx EIP8 auth-ack packet's second item (nonce) was not the correct size.");
            }
            else if (rlpList.Items.Count >= 3 && !rlpList.Items[2].IsByteArray)
            {
                throw new ArgumentException("RLPx EIP8 auth-ack packet's third item (version) was not a byte array.");
            }

            // Obtain all components.
            EphemeralPublicKey = rlpList.Items[0];
            Nonce = rlpList.Items[1];

            // Decode version if it's available.
            if (rlpList.Items.Count >= 3)
            {
                Version = RLP.ToInteger((RLPByteArray)rlpList.Items[2], 32, false);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Deserializes the given RLP serialized block header and sets all values accordingly.
        /// </summary>
        /// <param name="item">The RLP item to block header and obtain values from.</param>
        public void Deserialize(RLPItem item)
        {
            // Verify this is a list
            if (!item.IsList)
            {
                throw new ArgumentException();
            }

            // Verify it has 15 items.
            RLPList rlpBlockHeader = (RLPList)item;

            if (rlpBlockHeader.Items.Count != 15)
            {
                throw new ArgumentException();
            }

            // Verify the types of all items
            for (int i = 0; i < rlpBlockHeader.Items.Count; i++)
            {
                if (!rlpBlockHeader.Items[i].IsByteArray)
                {
                    throw new ArgumentException();
                }
            }

            // Verify our items are the correct length
            if (((RLPByteArray)rlpBlockHeader.Items[0]).Data.Length != KeccakHash.HASH_SIZE)
            {
                throw new ArgumentException();
            }

            if (((RLPByteArray)rlpBlockHeader.Items[1]).Data.Length != KeccakHash.HASH_SIZE)
            {
                throw new ArgumentException();
            }

            if (((RLPByteArray)rlpBlockHeader.Items[2]).Data.Length != Address.ADDRESS_SIZE)
            {
                throw new ArgumentException();
            }

            if (((RLPByteArray)rlpBlockHeader.Items[3]).Data.Length != KeccakHash.HASH_SIZE)
            {
                throw new ArgumentException();
            }

            if (((RLPByteArray)rlpBlockHeader.Items[4]).Data.Length != KeccakHash.HASH_SIZE)
            {
                throw new ArgumentException();
            }

            if (((RLPByteArray)rlpBlockHeader.Items[5]).Data.Length != KeccakHash.HASH_SIZE)
            {
                throw new ArgumentException();
            }

            if (((RLPByteArray)rlpBlockHeader.Items[6]).Data.Length != EVMDefinitions.BLOOM_FILTER_SIZE)
            {
                throw new ArgumentException();
            }


            // Obtain our items
            PreviousHash         = rlpBlockHeader.Items[0];
            UnclesHash           = rlpBlockHeader.Items[1];
            Coinbase             = RLP.ToInteger((RLPByteArray)rlpBlockHeader.Items[2], Address.ADDRESS_SIZE);
            StateRootHash        = rlpBlockHeader.Items[3];
            TransactionsRootHash = rlpBlockHeader.Items[4];
            ReceiptsRootHash     = rlpBlockHeader.Items[5];
            Bloom       = RLP.ToInteger((RLPByteArray)rlpBlockHeader.Items[6], EVMDefinitions.BLOOM_FILTER_SIZE);
            Difficulty  = RLP.ToInteger((RLPByteArray)rlpBlockHeader.Items[7]);
            BlockNumber = RLP.ToInteger((RLPByteArray)rlpBlockHeader.Items[8]);
            GasLimit    = RLP.ToInteger((RLPByteArray)rlpBlockHeader.Items[9]);
            GasUsed     = RLP.ToInteger((RLPByteArray)rlpBlockHeader.Items[10]);
            Timestamp   = RLP.ToInteger((RLPByteArray)rlpBlockHeader.Items[11]);
            ExtraData   = rlpBlockHeader.Items[12];
            MixHash     = rlpBlockHeader.Items[13];
            Nonce       = rlpBlockHeader.Items[14];
        }