Exemple #1
0
        private async Task <string> SignAndSendTransactionAsync(TransactionInput transaction)
        {
            if (Client == null)
            {
                throw new NullReferenceException("Client not configured");
            }
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (!transaction.From.IsTheSameAddress(Account.Address))
            {
                throw new Exception("Invalid account used signing");
            }

            if (PrivateFor != null && PrivateFor.Count > 0)
            {
                var enclave = new QuorumEnclave(PrivateUrl);
                var key     = await enclave.StoreRawAsync(Convert.ToBase64String(transaction.Data.HexToByteArray()), PrivateFrom);

                transaction.Data = Convert.FromBase64String(key).ToHex();
            }

            var ethSendTransaction = new EthSendRawTransaction(Client);
            var signedTransaction  = await SignTransactionRetrievingNextNonceAsync(transaction).ConfigureAwait(false);

            return(await ethSendTransaction.SendRequestAsync(signedTransaction.EnsureHexPrefix()).ConfigureAwait(false));
        }
        /// <summary>
        /// Sends a message to an ethereum smart contract with the intent to change a part of the contract on the blockchain.
        /// </summary>
        /// <typeparam name="TFunc"> The type of the function to execute. </typeparam>
        /// <param name="function"> The function to execute. </param>
        /// <param name="privateKey"> The private key of the address executing the contract function. </param>
        /// <param name="contractAddress"> The address of the contract which will process the message. </param>
        /// <param name="gasPrice"> The gas price (in wei) to use to send the transaction. </param>
        /// <param name="gasLimit"> The gas limit (in wei) to use to send the transaction. </param>
        /// <param name="value"> The amount of eth (in wei) to send with the transaction. </param>
        /// <returns> Task which returns the TransactionPoller which will await the transaction result. </returns>
        public static async Task <TransactionPoller> SendContractMessage <TFunc>(
            TFunc function,
            string privateKey,
            string contractAddress,
            BigInteger gasPrice,
            BigInteger gasLimit,
            BigInteger value) where TFunc : FunctionMessage, new()
        {
            EthECKey ethECKey = new EthECKey(privateKey);

            function.SetDefaultFromAddressIfNotSet(ethECKey.GetPublicAddress());
            function.Gas      = gasLimit;
            function.GasPrice = gasPrice;

            InMemoryNonceService nonceService = new InMemoryNonceService(ethECKey.GetPublicAddress(), NetworkProvider.GetWeb3().Client);

            TransactionSigner signer       = new TransactionSigner();
            string            signedTxData = signer.SignTransaction(
                privateKey,
                NetworkProvider.GetActiveNetworkChain(),
                contractAddress,
                value,
                (await nonceService.GetNextNonceAsync()).Value,
                gasPrice,
                gasLimit,
                function.CreateTransactionInput(contractAddress).Data);

            EthSendRawTransaction rawTransaction = new EthSendRawTransaction(NetworkProvider.GetWeb3().Client);

            return(new TransactionPoller(await rawTransaction.SendRequestAsync(signedTxData)));
        }
        private async Task <string> SignAndSendTransactionAsync(TransactionInput transaction)
        {
            if (Client == null)
            {
                throw new NullReferenceException("Client not configured");
            }
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (transaction.From.EnsureHexPrefix().ToLower() != _account.Address.EnsureHexPrefix().ToLower())
            {
                throw new Exception("Invalid account used signing");
            }
            SetDefaultGasPriceAndCostIfNotSet(transaction);

            var ethSendTransaction = new EthSendRawTransaction(Client);
            var nonce = await GetNonceAsync(transaction);

            var gasPrice = transaction.GasPrice;
            var gasLimit = transaction.Gas;

            var value = transaction.Value;

            if (value == null)
            {
                value = new HexBigInteger(0);
            }

            var signedTransaction = _transactionSigner.SignTransaction(_account.PrivateKey, transaction.To, value.Value, nonce,
                                                                       gasPrice.Value, gasLimit.Value, transaction.Data);

            return(await ethSendTransaction.SendRequestAsync(signedTransaction.EnsureHexPrefix()).ConfigureAwait(false));
        }
        public async Task <string> SubmitSignedTransaction(string from, string signedTrHex)
        {
            bool isSignedRight = await _signatureChecker.CheckTransactionSign(from, signedTrHex);

            if (!isSignedRight)
            {
                throw new ClientSideException(ExceptionType.WrongSign, "WrongSign");
            }

            var    ethSendTransaction = new EthSendRawTransaction(_web3.Client);
            string transactionHex;

            try
            {
                transactionHex = await ethSendTransaction.SendRequestAsync(signedTrHex);
            }
            catch (Nethereum.JsonRpc.Client.RpcResponseException ex)
            {
                if (ex.Message == "intrinsic gas too low")
                {
                    throw new ClientSideException(ExceptionType.TransactionRequiresMoreGas, ex.Message);
                }

                throw ex;
            }

            return(transactionHex);
        }
Exemple #5
0
        public async Task <object> ExecuteTestAsync(IClient client)
        {
            var ethSendRawTransaction = new EthSendRawTransaction(client);

            return
                (await
                 ethSendRawTransaction.SendRequestAsync(
                     "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"));
        }
 public EthApiTransactionsService(IClient client) : base(client)
 {
     Call        = new EthCall(client);
     EstimateGas = new EthEstimateGas(client);
     GetTransactionByBlockHashAndIndex   = new EthGetTransactionByBlockHashAndIndex(client);
     GetTransactionByBlockNumberAndIndex = new EthGetTransactionByBlockNumberAndIndex(client);
     GetTransactionByHash  = new EthGetTransactionByHash(client);
     GetTransactionCount   = new EthGetTransactionCount(client);
     GetTransactionReceipt = new EthGetTransactionReceipt(client);
     SendRawTransaction    = new EthSendRawTransaction(client);
     SendTransaction       = new EthSendTransaction(client);
 }
 public EthTransactionsService(IClient client) : base(client)
 {
     Call = new EthCall(client);
     EstimateGas = new EthEstimateGas(client);
     GetTransactionByBlockHashAndIndex = new EthGetTransactionByBlockHashAndIndex(client);
     GetTransactionByBlockNumberAndIndex = new EthGetTransactionByBlockNumberAndIndex(client);
     GetTransactionByHash = new EthGetTransactionByHash(client);
     GetTransactionCount = new EthGetTransactionCount(client);
     GetTransactionReceipt = new EthGetTransactionReceipt(client);
     SendRawTransaction = new EthSendRawTransaction(client);
     SendTransaction = new EthSendTransaction(client);
 }
        public Task <string> SendRawTransactionAsync(string signedTransaction)
        {
            if (Client == null)
            {
                throw new NullReferenceException("Client not configured");
            }
            if (string.IsNullOrEmpty(signedTransaction))
            {
                throw new ArgumentNullException(nameof(signedTransaction));
            }
            var ethSendRawTransaction = new EthSendRawTransaction(Client);

            return(ethSendRawTransaction.SendRequestAsync(signedTransaction));
        }
Exemple #9
0
        public async Task <string> SendTransactionAsync <T>(T transaction) where T : TransactionInput
        {
            var ethSendTransaction = new EthSendRawTransaction(Client);

            var nonce = await GetNonceAsync(transaction);

            var value    = transaction.Value?.Value ?? 0;
            var gasPrice = await EstimateGasPrice();

            var gasValue = transaction.Gas?.Value ?? Transaction.DEFAULT_GAS_LIMIT;

            var tr       = new Transaction(transaction.To, value, nonce, gasPrice, gasValue, transaction.Data);
            var hex      = tr.GetRLPEncoded().ToHex();
            var response = await _signatureApi.SignTransaction(new SignRequest { From = transaction.From, Transaction = hex });

            return(await ethSendTransaction.SendRequestAsync(response.SignedTransaction.EnsureHexPrefix()).ConfigureAwait(false));
        }
Exemple #10
0
        private async Task <string> SignAndSendTransactionAsync(TransactionInput transaction)
        {
            if (Client == null)
            {
                throw new NullReferenceException("Client not configured");
            }
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (transaction.From.EnsureHexPrefix().ToLower() != Account.Address.EnsureHexPrefix().ToLower())
            {
                throw new Exception("Invalid account used signing");
            }

            var ethSendTransaction = new EthSendRawTransaction(Client);
            var signedTransaction  = await SignTransactionRetrievingNextNonceAsync(transaction);

            return(await ethSendTransaction.SendRequestAsync(signedTransaction.EnsureHexPrefix()).ConfigureAwait(false));
        }
Exemple #11
0
        public LykkeSignedTransactionManager(
            IBaseSettings baseSettings,
            INonceCalculator nonceCalculator,
            ILykkeSigningAPI signingApi,
            ITransactionRouter transactionRouter,
            Web3 web3,
            IGasPriceRepository gasPriceRepository)
        {
            _baseSettings       = baseSettings;
            _estimateGas        = new EthEstimateGas(web3.Client);
            _nonceCalculator    = nonceCalculator;
            _semaphores         = new ConcurrentDictionary <string, SemaphoreSlim>();
            _sendRawTransaction = new EthSendRawTransaction(web3.Client);
            _signingApi         = signingApi;
            _transactionRouter  = transactionRouter;
            _web3 = web3;
            _gasPriceRepository = gasPriceRepository;

            Client = web3.Client;
        }
Exemple #12
0
        private async Task <string> SignAndSendTransaction(TransactionInput transaction)
        {
            if (Client == null)
            {
                throw new NullReferenceException("Client not configured");
            }
            if (transaction.From.EnsureHexPrefix().ToLower() != _account.EnsureHexPrefix().ToLower())
            {
                throw new Exception("Invalid account used signing");
            }
            var ethSendTransaction = new EthSendRawTransaction(Client);
            var nonce = await GetNonceAsync(transaction);

            var gasPrice = transaction.GasPrice;

            if (gasPrice == null)
            {
                gasPrice = new HexBigInteger(Transaction.DEFAULT_GAS_PRICE);
            }

            var gasLimit = transaction.Gas;

            if (gasLimit == null)
            {
                gasLimit = new HexBigInteger(Transaction.DEFAULT_GAS_LIMIT);
            }

            var value = transaction.Value;

            if (value == null)
            {
                value = new HexBigInteger(0);
            }

            var signedTransaction = _transactionSigner.SignTransaction(_privateKey, transaction.To, value.Value, nonce,
                                                                       gasPrice.Value, gasLimit.Value, transaction.Data);

            return(await ethSendTransaction.SendRequestAsync(signedTransaction.EnsureHexPrefix()).ConfigureAwait(false));
        }
Exemple #13
0
        /// <summary>
        /// Sends a given amount of ether to an address.
        /// </summary>
        /// <param name="privateKey"> The private key of the address which is sending the ether. </param>
        /// <param name="addressTo"> The address the ether is being sent to. </param>
        /// <param name="amount"> The amount of ether being sent, in eth. (not wei) </param>
        /// <param name="gasPrice"> The gas price (in wei) to use to send the transaction. </param>
        /// <param name="gasLimit"> The gas limit (in wei) to use to send the transaction. </param>
        /// <returns> Task which returns the TransactionPoller which will await the transaction result. </returns>
        public static async Task <TransactionPoller> SendEther(string privateKey, string addressTo, decimal amount, BigInteger gasPrice, BigInteger gasLimit)
        {
            BigInteger value = SolidityUtils.ConvertToUInt(amount, 18);

            EthECKey ethECKey = new EthECKey(privateKey);

            InMemoryNonceService nonceService = new InMemoryNonceService(ethECKey.GetPublicAddress(), NetworkProvider.GetWeb3().Client);

            TransactionSigner signer       = new TransactionSigner();
            string            signedTxData = signer.SignTransaction(
                privateKey,
                NetworkProvider.GetActiveNetworkChain(),
                addressTo,
                value,
                (await nonceService.GetNextNonceAsync()).Value,
                gasPrice,
                gasLimit,
                string.Empty);

            EthSendRawTransaction rawTransaction = new EthSendRawTransaction(NetworkProvider.GetWeb3().Client);

            return(new TransactionPoller(await rawTransaction.SendRequestAsync(signedTxData)));
        }
        private async Task <string> SendViaRelay(
            string relayUrl,
            TransactionInput transaction,
            BigInteger txFee,
            BigInteger gasPrice,
            BigInteger gasLimit,
            BigInteger nonce,
            string relayHubAddress,
            string relayAddress,
            string signature,
            string approvalData,
            BigInteger relayMaxNonce)
        {
            var requestData = new RelayRequest
            {
                EncodedFunction = transaction.Data,
                Signature       = signature.HexToByteArray(),
                ApprovalData    = approvalData.HexToByteArray(),
                From            = transaction.From,
                To              = transaction.To,
                GasPrice        = gasPrice,
                GasLimit        = gasLimit,
                RelayFee        = txFee,
                RecipientNonce  = nonce,
                RelayMaxNonce   = relayMaxNonce,
                RelayHubAddress = relayHubAddress,
                UserAgent       = _options.UserAgent
            };

            RelayResponse relayResponse = await _relayClient.RelayAsync(new Uri(relayUrl), requestData)
                                          .ConfigureAwait(false);

            if (!string.IsNullOrEmpty(relayResponse.Error))
            {
                throw new Exception(relayResponse.Error);
            }

            if (relayResponse.Nonce.Value.IsZero)
            {
                throw new Exception("Empty body received from server, or neither 'error' nor 'nonce' fields present.");
            }

            ValidateTx(
                relayResponse,
                transaction,
                txFee,
                gasPrice,
                gasLimit,
                nonce,
                relayHubAddress,
                relayAddress);

            var    tx   = relayResponse.ToTransaction();
            string hash = string.Empty;
            var    ethSendTransaction = new EthSendRawTransaction(_client);

            try
            {
                hash = await ethSendTransaction.SendRequestAsync(tx.GetRLPEncoded().ToHex().EnsureHexPrefix())
                       .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("the tx doesn't have the correct nonce") &&
                    !ex.Message.Contains("known transaction") &&
                    !ex.Message.Contains("nonce too low"))
                {
                    throw ex;
                }
            }

            var txHash = relayResponse.Hash;

            if (!string.IsNullOrEmpty(hash) &&
                relayResponse.Hash.EnsureHexPrefix().ToLower() != hash.EnsureHexPrefix().ToLower())
            {
                txHash = hash;
            }

            return(txHash);
        }
 public async Task<dynamic> ExecuteTestAsync(RpcClient client)
 {
     var ethSendRawTransaction = new EthSendRawTransaction(client);
     return await ethSendRawTransaction.SendRequestAsync( "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675");
 }