public ResultWrapper <byte[]> eth_sign(Address addressData, byte[] message) { Signature sig; try { Address address = addressData; string messageText = _messageEncoding.GetString(message); const string signatureTemplate = "\x19Ethereum Signed Message:\n{0}{1}"; string signatureText = string.Format(signatureTemplate, messageText.Length, messageText); sig = _blockchainBridge.Sign(address, Keccak.Compute(signatureText)); } catch (SecurityException e) { return(ResultWrapper <byte[]> .Fail(e.Message, ErrorCodes.AccountLocked)); } catch (Exception) { return(ResultWrapper <byte[]> .Fail($"Unable to sign as {addressData}")); } if (_logger.IsTrace) { _logger.Trace($"eth_sign request {addressData}, {message}, result: {sig}"); } return(ResultWrapper <byte[]> .Success(sig.Bytes)); }
public ResultWrapper <byte[]> eth_sign(Address addressData, byte[] message) { try { _readerWriterLockSlim.EnterReadLock(); Signature sig; try { Address address = addressData; var messageText = _messageEncoding.GetString(message); var signatureText = string.Format(SignatureTemplate, messageText.Length, messageText); sig = _blockchainBridge.Sign(address, Keccak.Compute(signatureText)); } catch (Exception) { return(ResultWrapper <byte[]> .Fail($"Unable to sign as {addressData}")); } if (Logger.IsTrace) { Logger.Trace($"eth_sign request {addressData}, {message}, result: {sig}"); } return(ResultWrapper <byte[]> .Success(sig.Bytes)); } finally { _readerWriterLockSlim.ExitReadLock(); } }
public ResultWrapper <Data> eth_sign(Data address, Data message) { //TODO check how to deal with password SecureString secureString = new SecureString(); secureString.AppendChar('?'); var privateKey = _blockchainBridge.GetKey(new Address(address.Value), secureString); if (privateKey.Item2.ResultType == ResultType.Failure) { return(ResultWrapper <Data> .Fail("Incorrect address")); } var messageText = Encoding.GetEncoding(ConfigurationProvider.MessageEncoding).GetString(message.Value); var signatureText = string.Format(ConfigurationProvider.SignatureTemplate, messageText.Length, messageText); var signature = _blockchainBridge.Sign(privateKey.Item1, Keccak.Compute(signatureText)); Logger.Debug($"eth_sign request {address.ToJson()}, {message.ToJson()}, result: {signature}"); return(ResultWrapper <Data> .Success(new Data(signature.Bytes))); }
public ResultWrapper <Data> eth_sign(Data addressData, Data message) { Signature sig; try { Address address = new Address(addressData.Value); var messageText = Encoding.GetEncoding(JsonRpcConfig.MessageEncoding).GetString(message.Value); var signatureText = string.Format(JsonRpcConfig.SignatureTemplate, messageText.Length, messageText); sig = _blockchainBridge.Sign(address, Keccak.Compute(signatureText)); } catch (Exception) { return(ResultWrapper <Data> .Fail($"Unable to sign as {addressData}")); } if (Logger.IsTrace) { Logger.Trace($"eth_sign request {addressData.ToJson()}, {message.ToJson()}, result: {sig}"); } return(ResultWrapper <Data> .Success(new Data(sig.Bytes))); }
public async Task <bool> TryRequestEthAsync(string host, Address address, UInt256 value) { if (!_enabled) { if (_logger.IsInfo) { _logger.Info("NDM Faucet is disabled"); } return(false); } if (_faucetAddress is null || _faucetAddress == Address.Zero) { if (_logger.IsWarn) { _logger.Warn("NDM Faucet address is not set"); } return(false); } if (string.IsNullOrWhiteSpace(host) || address is null || address == Address.Zero || _faucetAddress == address || value == 0) { if (_logger.IsInfo) { _logger.Info("Invalid NDM Faucet ETH request"); } return(false); } if (value > _maxValue) { if (_logger.IsInfo) { _logger.Info($"ETH request from: {host} has too big value: {value} wei > {_maxValue} wei"); } return(false); } if (_pendingRequests.TryGetValue(host, out _)) { if (_logger.IsInfo) { _logger.Info($"ETH request from: {host} is already being processed."); } return(false); } if (_logger.IsInfo) { _logger.Info($"Received ETH request from: {host}, address: {address}, value: {value} wei"); } var latestRequest = await _requestRepository.GetLatestAsync(host); var requestedAt = _timestamp.UtcNow; if (!(latestRequest is null) && latestRequest.RequestedAt.Date >= requestedAt.Date) { if (_logger.IsInfo) { _logger.Info($"ETH request from: {host} was already processed today at: {latestRequest.RequestedAt}"); } return(false); } if (!_pendingRequests.TryAdd(host, true)) { if (_logger.IsWarn) { _logger.Warn($"Couldn't start processing ETH request from: {host}"); } return(false); } if (_logger.IsInfo) { _logger.Info($"Processing ETH request for: {host}, address: {address}, value: {value} wei"); } try { var faucetAccount = _blockchainBridge.GetAccount(_faucetAddress); var transaction = new Transaction { Value = value, GasLimit = 21000, GasPrice = 20.GWei(), To = address, SenderAddress = _faucetAddress, Nonce = faucetAccount?.Nonce ?? 0 }; _blockchainBridge.Sign(transaction); var transactionHash = _blockchainBridge.SendTransaction(transaction); if (latestRequest is null) { var requestId = Keccak.Compute(Rlp.Encode(Rlp.Encode(host))); await _requestRepository.AddAsync(new EthRequest(requestId, host, address, value, requestedAt, transactionHash)); } else { latestRequest.UpdateRequestDate(requestedAt); await _requestRepository.UpdateAsync(latestRequest); } if (_logger.IsInfo) { _logger.Info($"ETH request was successfully processed for: {host}, address: {address}, value: {value} wei"); } return(true); } catch (Exception e) { if (_logger.IsError) { _logger.Error(e.Message, e); } return(false); } finally { _pendingRequests.TryRemove(host, out _); } }
public async Task <FaucetResponse> TryRequestEthAsync(string node, Address address, UInt256 value) { if (!_enabled) { if (_logger.IsInfo) { _logger.Info("NDM Faucet is disabled."); } return(FaucetResponse.FaucetDisabled); } if (!_initialized) { if (_logger.IsInfo) { _logger.Info("NDM Faucet is not initialized."); } return(FaucetResponse.FaucetDisabled); } if (_today.Date != _timestamper.UtcNow.Date) { lock (_locker) { _today = _timestamper.UtcNow; _todayRequestsTotalValueWei = 0; } if (_logger.IsInfo) { _logger.Info($"NDM Faucet has updated its today's date ({_today.Date:d}) and reset the total requests value."); } } if (_faucetAddress is null || _faucetAddress == Address.Zero) { if (_logger.IsWarn) { _logger.Warn("NDM Faucet address is not set."); } return(FaucetResponse.FaucetAddressNotSet); } if (string.IsNullOrWhiteSpace(node) || address is null || address == Address.Zero) { if (_logger.IsInfo) { _logger.Info("Invalid NDM Faucet request."); } return(FaucetResponse.InvalidNodeAddress); } if (_faucetAddress == address) { if (_logger.IsInfo) { _logger.Info("NDM Faucet request cannot be processed for the same address as NDM Faucet."); } return(FaucetResponse.SameAddressAsFaucet); } if (value == 0) { if (_logger.IsInfo) { _logger.Info("NDM Faucet request cannot be processed for the zero value."); } return(FaucetResponse.ZeroValue); } if (value > _maxValue) { if (_logger.IsInfo) { _logger.Info($"NDM Faucet request from: {node} has too big value: {value} wei > {_maxValue} wei."); } return(FaucetResponse.TooBigValue); } if (_logger.IsInfo) { _logger.Info($"Received NDM Faucet request from: {node}, address: {address}, value: {value} wei."); } if (_pendingRequests.TryGetValue(node, out _)) { if (_logger.IsInfo) { _logger.Info($"NDM Faucet request from: {node} is already being processed."); } return(FaucetResponse.RequestAlreadyProcessing); } var latestRequest = await _requestRepository.GetLatestAsync(node); var requestedAt = _timestamper.UtcNow; if (!(latestRequest is null) && latestRequest.RequestedAt.Date >= requestedAt.Date) { if (_logger.IsInfo) { _logger.Info($"NDM Faucet request from: {node} was already processed today at: {latestRequest.RequestedAt}."); } return(FaucetResponse.RequestAlreadyProcessedToday(FaucetRequestDetails.From(latestRequest))); } if (!_pendingRequests.TryAdd(node, true)) { if (_logger.IsWarn) { _logger.Warn($"Couldn't start processing NDM Faucet request from: {node}."); } return(FaucetResponse.RequestError); } lock (_locker) { _todayRequestsTotalValueWei += value; if (_logger.IsInfo) { _logger.Info($"Increased NDM Faucet total value of today's ({_today.Date:d}) requests to {_todayRequestsTotalValueWei} wei."); } } if (_todayRequestsTotalValueWei > _dailyRequestsTotalValueWei) { if (_logger.IsInfo) { _logger.Info($"Daily ({_today.Date:d}) requests value for NDM Faucet was reached ({_dailyRequestsTotalValueWei} wei)."); } return(FaucetResponse.DailyRequestsTotalValueReached); } if (_logger.IsInfo) { _logger.Info($"NDM Faucet is processing request for: {node}, address: {address}, value: {value} wei."); } try { var faucetAccount = _blockchainBridge.GetAccount(_faucetAddress); var transaction = new Transaction { Value = value, GasLimit = 21000, GasPrice = 20.GWei(), To = address, SenderAddress = _faucetAddress, Nonce = faucetAccount?.Nonce ?? 0 }; _blockchainBridge.Sign(transaction); var transactionHash = _blockchainBridge.SendTransaction(transaction, true); if (latestRequest is null) { var requestId = Keccak.Compute(Rlp.Encode(Rlp.Encode(node))); latestRequest = new EthRequest(requestId, node, address, value, requestedAt, transactionHash); await _requestRepository.AddAsync(latestRequest); } else { latestRequest.UpdateRequestDetails(address, value, requestedAt, transactionHash); await _requestRepository.UpdateAsync(latestRequest); } if (_logger.IsInfo) { _logger.Info($"NDM Faucet has successfully processed request for: {node}, address: {address}, value: {value} wei."); } return(FaucetResponse.RequestCompleted(FaucetRequestDetails.From(latestRequest))); } catch (Exception ex) { if (_logger.IsError) { _logger.Error(ex.Message, ex); } lock (_locker) { _todayRequestsTotalValueWei -= value; if (_logger.IsInfo) { _logger.Info($"Decreased NDM Faucet total value of today's ({_today.Date:d}) requests to {_todayRequestsTotalValueWei} wei."); } } return(FaucetResponse.ProcessingRequestError); } finally { _pendingRequests.TryRemove(node, out _); } }