public async Task given_valid_consumer_address_delivery_receipt_should_be_processed() { _signer.RecoverPublicKey(Arg.Any <Signature>(), Arg.Any <Keccak>()).ReturnsForAnyArgs(_publicKey); _peer.ConsumerAddress.Returns(_publicKey.Address); var currentReceiptRequestUnitsRange = new UnitsRange(0, 9); var receiptRequest = new DataDeliveryReceiptRequest(1, Keccak.Zero, currentReceiptRequestUnitsRange); var deliveryReceipt = new DataDeliveryReceipt(StatusCodes.Ok, 0, 0, new Signature(new byte[65])); var wasProcessed = await _processor.TryProcessAsync(_session, _publicKey.Address, _peer, receiptRequest, deliveryReceipt); wasProcessed.Should().BeTrue(); _session.DataAvailability.Should().NotBe(DataAvailability.DataDeliveryReceiptInvalid); }
public void handling_hi_message_should_succeed() { _verifySignature = true; InitSubprotocol(); var hiMessage = new HiMessage(1, TestItem.AddressC, TestItem.AddressD, TestItem.PublicKeyA, new Signature(1, 1, 27)); var hiPacket = new Packet(hiMessage.Protocol, hiMessage.PacketType, _messageSerializationService.Serialize(hiMessage)); _messageSerializationService.Deserialize <HiMessage>(hiPacket.Data).Returns(hiMessage); var hash = Keccak.Compute(hiMessage.NodeId.Bytes); _ecdsa.RecoverPublicKey(hiMessage.Signature, hash).Returns(TestItem.PublicKeyA); _subprotocol.HandleMessage(hiPacket); _ecdsa.Received().RecoverPublicKey(hiMessage.Signature, hash); _subprotocol.ProviderAddress.Should().Be(hiMessage.ProviderAddress); _subprotocol.ConsumerAddress.Should().Be(hiMessage.ConsumerAddress); _consumerService.Received().AddProviderPeer(_subprotocol); var getDataAssetsMessage = new GetDataAssetsMessage(); _messageSerializationService.Serialize(getDataAssetsMessage).Returns(Array.Empty <byte>()); var getDataAssetsPacket = new Packet(getDataAssetsMessage.Protocol, getDataAssetsMessage.PacketType, _messageSerializationService.Serialize(getDataAssetsMessage)); _messageSerializationService.Deserialize <GetDataAssetsMessage>(getDataAssetsPacket.Data) .Returns(getDataAssetsMessage); Received.InOrder(() => { _session.DeliverMessage(Arg.Any <GetDataAssetsMessage>()); _session.DeliverMessage(Arg.Any <GetDepositApprovalsMessage>()); }); }
public ResultWrapper <Address> personal_ecRecover(byte[] message, byte[] signature) { message = ToEthSignedMessage(message); Keccak msgHash = Keccak.Compute(message); PublicKey publicKey = _ecdsa.RecoverPublicKey(new Signature(signature), msgHash); return(ResultWrapper <Address> .Success(publicKey.Address)); }
public ResultWrapper <ParityTransaction[]> parity_pendingTransactions() => ResultWrapper <ParityTransaction[]> .Success(_txPool.GetPendingTransactions().Where(pt => pt.SenderAddress != null) .Select(t => new ParityTransaction(t, Rlp.Encode(t).Bytes, t.IsSigned ? _ecdsa.RecoverPublicKey(t.Signature, t.Hash) : null)).ToArray());
public Packet Ack(EncryptionHandshake handshake, Packet auth) { handshake.AuthPacket = auth; AuthMessageBase authMessage; bool preEip8Format = false; byte[] plainText = null; try { if (_logger.IsTrace) { _logger.Trace($"Trying to decrypt an old version of {nameof(AuthMessage)}"); } (preEip8Format, plainText) = _eciesCipher.Decrypt(_privateKey, auth.Data); } catch (Exception ex) { if (_logger.IsTrace) { _logger.Trace($"Exception when decrypting ack {ex.Message}"); } } if (preEip8Format) { authMessage = _messageSerializationService.Deserialize <AuthMessage>(plainText); } else { if (_logger.IsTrace) { _logger.Trace($"Trying to decrypt version 4 of {nameof(AuthEip8Message)}"); } byte[] sizeData = auth.Data.Slice(0, 2); (_, plainText) = _eciesCipher.Decrypt(_privateKey, auth.Data.Slice(2), sizeData); authMessage = _messageSerializationService.Deserialize <AuthEip8Message>(plainText); } var nodeId = authMessage.PublicKey; if (_logger.IsTrace) { _logger.Trace($"Received AUTH v{authMessage.Version} from {nodeId}"); } handshake.RemoteNodeId = nodeId; handshake.RecipientNonce = _cryptoRandom.GenerateRandomBytes(32); handshake.EphemeralPrivateKey = _ephemeralGenerator.Generate(); handshake.InitiatorNonce = authMessage.Nonce; byte[] staticSharedSecret = Proxy.EcdhSerialized(handshake.RemoteNodeId.Bytes, _privateKey.KeyBytes); byte[] forSigning = staticSharedSecret.Xor(handshake.InitiatorNonce); handshake.RemoteEphemeralPublicKey = _ecdsa.RecoverPublicKey(authMessage.Signature, new Keccak(forSigning)); byte[] data; if (preEip8Format) { if (_logger.IsTrace) { _logger.Trace($"Building an {nameof(AckMessage)}"); } AckMessage ackMessage = new AckMessage(); ackMessage.EphemeralPublicKey = handshake.EphemeralPrivateKey.PublicKey; ackMessage.Nonce = handshake.RecipientNonce; var ackData = _messageSerializationService.Serialize(ackMessage); data = _eciesCipher.Encrypt(handshake.RemoteNodeId, ackData, Array.Empty <byte>()); } else { if (_logger.IsTrace) { _logger.Trace($"Building an {nameof(AckEip8Message)}"); } AckEip8Message ackMessage = new AckEip8Message(); ackMessage.EphemeralPublicKey = handshake.EphemeralPrivateKey.PublicKey; ackMessage.Nonce = handshake.RecipientNonce; var ackData = _messageSerializationService.Serialize(ackMessage); int size = ackData.Length + 32 + 16 + 65; // data + MAC + IV + pub byte[] sizeBytes = size.ToBigEndianByteArray().Slice(2, 2); data = Bytes.Concat(sizeBytes, _eciesCipher.Encrypt(handshake.RemoteNodeId, ackData, sizeBytes)); } handshake.AckPacket = new Packet(data); SetSecrets(handshake, HandshakeRole.Recipient); return(handshake.AckPacket); }
public PublicKey GetNodeId(byte[] signature, int recoveryId, Span <byte> typeAndData) { return(_ecdsa.RecoverPublicKey(new Signature(signature, recoveryId), Keccak.Compute(typeAndData))); }
public PublicKey GetNodeId(byte[] signature, int recoveryId, byte[] messageType, byte[] data) { return(_ecdsa.RecoverPublicKey(new Signature(signature, recoveryId), Keccak.Compute(Bytes.Concat(messageType, data)))); }
public async Task <bool> TryProcessAsync(ProviderSession session, Address consumer, INdmProviderPeer peer, DataDeliveryReceiptRequest receiptRequest, DataDeliveryReceipt deliveryReceipt) { var depositId = session.DepositId; var unitsRange = receiptRequest.UnitsRange; var abiHash = _abiEncoder.Encode(AbiEncodingStyle.Packed, _dataDeliveryReceiptAbiSig, receiptRequest.DepositId.Bytes, new[] { unitsRange.From, unitsRange.To }); var address = _ecdsa.RecoverPublicKey(deliveryReceipt.Signature, Keccak.Compute(abiHash)).Address; if (!consumer.Equals(address)) { if (_logger.IsWarn) { _logger.Warn($"Recovered an invalid address: '{address}' (should be: '{consumer}') for delivery receipt for deposit: '{depositId}', consumer: '{session.ConsumerAddress}', session: '{session.Id}'."); } session.SetDataAvailability(DataAvailability.DataDeliveryReceiptInvalid); await _sessionRepository.UpdateAsync(session); peer.SendDataAvailability(depositId, DataAvailability.DataDeliveryReceiptInvalid); return(false); } var paidUnits = unitsRange.To - unitsRange.From + 1; if (_logger.IsInfo) { _logger.Info($"Consumer: '{consumer}' has provided a valid receipt for deposit: '{receiptRequest.DepositId}', range: [{unitsRange.From}, {unitsRange.To}], paid units: {paidUnits}"); } if (receiptRequest.ReceiptsToMerge.Any()) { if (_logger.IsInfo) { _logger.Info($"Processing a merged receipt request for consumer: {session.ConsumerAddress}, session: '{session.Id} - units will not be updated."); } } else { if (_logger.IsInfo) { _logger.Info($"Processing a receipt request for deposit: '{receiptRequest.DepositId}', consumer: {session.ConsumerAddress}, session: '{session.Id} - units will be updated."); } var unpaidUnits = session.UnpaidUnits > paidUnits ? session.UnpaidUnits - paidUnits : 0; session.SetUnpaidUnits(unpaidUnits); session.AddPaidUnits(paidUnits); if (receiptRequest.IsSettlement) { session.SetPaidUnits(paidUnits); session.SettleUnits(paidUnits); if (_logger.IsInfo) { _logger.Info($"Settled {paidUnits} units for deposit: '{receiptRequest.DepositId}', consumer: {session.ConsumerAddress}, session: '{session.Id}'."); } } await _sessionRepository.UpdateAsync(session); } var dataAvailability = session.DataAvailability; if (dataAvailability == DataAvailability.DataDeliveryReceiptInvalid || dataAvailability == DataAvailability.DataDeliveryReceiptNotProvided) { session.SetDataAvailability(DataAvailability.Available); await _sessionRepository.UpdateAsync(session); if (_logger.IsInfo) { _logger.Info($"Updated previously set data availability: '{dataAvailability}' -> '{DataAvailability.Available}', consumer: {session.ConsumerAddress}, session: '{session.Id}'."); } } return(true); }