コード例 #1
0
        private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            _timer.Interval = RandomizeDelay().TotalMilliseconds;
            _logger.Debug($"Generating a test transaction for testing ({_count}).");

            Transaction tx = new Transaction();

            tx.GasPrice = 1;
            tx.GasLimit = 21000;
            tx.To       = new Address(0x0f.ToBigEndianByteArray().PadLeft(20));
            tx.Nonce    = 0;
            tx.Value    = 1;
            tx.Data     = new byte[0];
            tx.Nonce    = _count++;
            _signer.Sign(_privateKey, tx, 0);
            Address address = _signer.RecoverAddress(tx, 0);

            if (address != SenderAddress)
            {
                _logger.Debug($"Signature mismatch in tests generator (EIP?).");
            }

            tx.Hash = Transaction.CalculateHash(tx);

            _store.AddPending(tx);
            _logger.Debug($"Generated a test transaction for testing ({_count - 1}).");
        }
コード例 #2
0
        private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            _timer.Interval = RandomizeDelay().TotalMilliseconds;
            _logger.Debug($"Generating a test transaction for testing ({_count}).");

            Transaction tx = new Transaction();

            tx.GasPrice      = 1;
            tx.GasLimit      = 21000;
            tx.To            = new Address(0x0f.ToBigEndianByteArray().PadLeft(20));
            tx.Nonce         = _nonce++;
            tx.Value         = 1;
            tx.Data          = new byte[0];
            tx.Nonce         = _count++;
            tx.SenderAddress = SenderAddress;
            _signer.Sign(_privateKey, tx, 1);
            Address address = _signer.RecoverAddress(tx, 1);

            if (address != tx.SenderAddress)
            {
                throw new InvalidDataException($"{nameof(TestTransactionsGenerator)} producing invalid transactions");
            }

            tx.Hash = Transaction.CalculateHash(tx);

            _transactionPool.AddTransaction(tx, 1);
            _logger.Debug($"Generated a test transaction for testing ({_count - 1}).");
        }
コード例 #3
0
        private void TestEncodeDecode(IEthereumSigner signer)
        {
            AuthEip8Message authMessage = new AuthEip8Message();

            authMessage.Nonce     = new byte[AuthMessageSerializer.NonceLength]; // sic!
            authMessage.Signature = signer.Sign(_privateKey, Keccak.Compute("anything"));
            authMessage.PublicKey = _privateKey.PublicKey;
            _random.NextBytes(authMessage.Nonce);
            byte[]          data  = _serializer.Serialize(authMessage);
            AuthEip8Message after = _serializer.Deserialize(data);

            Assert.AreEqual(authMessage.Signature, after.Signature);
            Assert.AreEqual(authMessage.PublicKey, after.PublicKey);
            Assert.True(Bytes.AreEqual(authMessage.Nonce, after.Nonce));
            Assert.AreEqual(authMessage.Version, after.Version);
        }
コード例 #4
0
        private Block Seal(Block block)
        {
            BlockHeader header = block.Header;

            // Sealing the genesis block is not supported
            UInt256 number = header.Number;

            if (number == 0)
            {
                throw new InvalidOperationException("Can't sign genesis block");
            }

            // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing)
            if (_config.BlockPeriod == 0 && block.Transactions.Length == 0)
            {
                // TODO: exception here?
                if (_logger.IsInfo)
                {
                    _logger.Info($"Not sealing empty block on 0-period chain {block.Number}");
                }
                return(null);
            }

            // Bail out if we're unauthorized to sign a block
            if (!CanSignBlock(block.Number, block.ParentHash))
            {
                if (_logger.IsInfo)
                {
                    _logger.Info($"Not authorized to seal the block {block.Number}");
                }
                return(null);
            }

            // Sign all the things!
            Keccak    headerHash = header.HashCliqueHeader();
            Signature signature  = _signer.Sign(_key, headerHash);

            // Copy signature bytes (R and S)
            byte[] signatureBytes = signature.Bytes;
            Array.Copy(signatureBytes, 0, header.ExtraData, header.ExtraData.Length - Clique.ExtraSealLength, signatureBytes.Length);
            // Copy signature's recovery id (V)
            byte recoveryId = signature.RecoveryId;

            header.ExtraData[header.ExtraData.Length - 1] = recoveryId;

            return(block);
        }
コード例 #5
0
        private void TestEncodeDecode(IEthereumSigner signer)
        {
            AuthMessage authMessage = new AuthMessage();
            authMessage.EphemeralPublicHash = new Keccak(new byte[AuthMessageSerializer.EphemeralHashLength]);
            authMessage.Nonce = new byte[AuthMessageSerializer.NonceLength];
            authMessage.Signature = signer.Sign(_privateKey, Keccak.Compute("anything"));
            _random.NextBytes(authMessage.EphemeralPublicHash.Bytes);
            authMessage.PublicKey = _privateKey.PublicKey;
            _random.NextBytes(authMessage.Nonce);
            authMessage.IsTokenUsed = true;
            byte[] bytes = _serializer.Serialize(authMessage);
            AuthMessage after = _serializer.Deserialize(bytes);

            Assert.AreEqual(authMessage.Signature, after.Signature);
            Assert.AreEqual(authMessage.EphemeralPublicHash, after.EphemeralPublicHash);
            Assert.AreEqual(authMessage.PublicKey, after.PublicKey);
            Assert.True(Bytes.AreEqual(authMessage.Nonce, after.Nonce));
            Assert.AreEqual(authMessage.IsTokenUsed, after.IsTokenUsed);
        }
コード例 #6
0
 public Signature Sign(PrivateKey privateKey, Keccak message)
 {
     return(_signer.Sign(privateKey, message));
 }
コード例 #7
0
 public TransactionBuilder Signed(IEthereumSigner signer, PrivateKey privateKey, UInt256 blockNumber)
 {
     signer.Sign(privateKey, TestObjectInternal, blockNumber);
     return(this);
 }
コード例 #8
0
 // TODO: auto create signer here
 public TransactionBuilder SignedAndResolved(IEthereumSigner signer, PrivateKey privateKey, UInt256 blockNumber)
 {
     signer.Sign(privateKey, TestObjectInternal, blockNumber);
     TestObjectInternal.SenderAddress = privateKey.Address;
     return(this);
 }
コード例 #9
0
 public Signature Sign(PrivateKey privateKey, Keccak message) => _signer.Sign(privateKey, message);