Ejemplo n.º 1
0
        public override void HandleBroadcast(IObserverDto <ProtocolMessage> messageDto)
        {
            Logger.Debug("received broadcast");

            var deserialised = messageDto.Payload.FromProtocolMessage <TransactionBroadcast>();

            _transactionReceivedEvent.OnTransactionReceived(messageDto.Payload);

            Logger.Debug("transaction signature is {0}", deserialised.PublicEntry.Signature);
        }
        protected override BroadcastRawTransactionResponse HandleRequest(BroadcastRawTransactionRequest messageDto,
                                                                         IChannelHandlerContext channelHandlerContext,
                                                                         PeerId senderPeerId,
                                                                         ICorrelationId correlationId)
        {
            var responseCode = _transactionReceivedEvent.OnTransactionReceived(messageDto.Transaction.ToProtocolMessage(senderPeerId, correlationId));

            return(new BroadcastRawTransactionResponse {
                ResponseCode = responseCode
            });
        }
Ejemplo n.º 3
0
        private IActionResult GenerateTweet(IPrivateKey privateKey, IPublicKey publicKey, string message)
        {
            if (String.IsNullOrEmpty(message))
            {
                return(BadRequest("Messages must contain text"));
            }

            if (message.Length > 240)
            {
                return(BadRequest("Messages have a maximum length of 240 characters."));
            }

            var publicKeyByteString = ByteString.CopyFrom(publicKey.Bytes);

            var transaction = new TransactionBroadcast();
            var publicEntry = new PublicEntry();

            publicEntry.Amount = ByteString.CopyFrom(0);

            var contractEntry = new ContractEntry();

            contractEntry.Amount = ByteString.CopyFrom(0);
            contractEntry.Data   = ByteString.CopyFromUtf8($"tw:{message}");

            var contractEntryBase = new BaseEntry();

            contractEntryBase.SenderPublicKey = publicKeyByteString;
            contractEntryBase.TransactionFees = ByteString.CopyFrom(0);

            contractEntry.Base = contractEntryBase;
            publicEntry.Base   = contractEntryBase;

            transaction.Timestamp = Timestamp.FromDateTime(DateTime.UtcNow);
            transaction.ContractEntries.Add(contractEntry);
            transaction.PublicEntries.Add(publicEntry);

            transaction.Signature = SignatureHelper.GenerateSignature(_cryptoWrapper, privateKey, transaction, _signingContext);

            _transactionReceivedEvent.OnTransactionReceived(transaction);

            return(Ok());
        }
        public void Can_Respond_With_Correct_Response(ResponseCode expectedResponse)
        {
            var channelContext = Substitute.For <IChannelHandlerContext>();
            var channel        = Substitute.For <IChannel>();

            channelContext.Channel.Returns(channel);

            _transactionReceivedEvent.OnTransactionReceived(Arg.Any <ProtocolMessage>())
            .Returns(expectedResponse);
            _broadcastRawTransactionRequestObserver
            .OnNext(new ObserverDto(channelContext,
                                    new BroadcastRawTransactionRequest {
                Transaction = new TransactionBroadcast()
            }.ToProtocolMessage(
                                        PeerIdHelper.GetPeerId("FakeSender"))));
            channelContext.Channel.Received(1)?.WriteAndFlushAsync(
                Arg.Is <object>(transactionObj =>
                                ((MessageDto)transactionObj)
                                .Content.FromProtocolMessage <BroadcastRawTransactionResponse>()
                                .ResponseCode == expectedResponse));
        }
Ejemplo n.º 5
0
        public Keccak SendTransaction(PublicEntry publicEntry)
        {
            TransactionBroadcast broadcast = new TransactionBroadcast
            {
                PublicEntry = publicEntry
            };

            _transactionReceived.OnTransactionReceived(broadcast.ToProtocolMessage(_peerId));

            byte[] kvmAddressBytes = Keccak.Compute(publicEntry.SenderAddress.ToByteArray()).Bytes.AsSpan(12).ToArray();
            string hex             = kvmAddressBytes.ToHexString() ?? throw new ArgumentNullException("kvmAddressBytes.ToHexString()");

            publicEntry.SenderAddress = kvmAddressBytes.ToByteString();

            if (publicEntry.ReceiverAddress.Length == 1)
            {
                publicEntry.ReceiverAddress = ByteString.Empty;
            }

            return(publicEntry.GetHash(HashProvider));
        }