Exemplo n.º 1
0
        // Sends a test message to a specified friend
        static public bool sendTestMessage(TestFriend friend)
        {
            // Search for the relay ip
            string relayip = friend.searchForRelay();

            if (relayip == null)
            {
                Logging.error("No relay ip found.");
                return(false);
            }

            Logging.info(String.Format("Relay: {0}", relayip));

            // Check if we're connected to the relay node
            TestStreamClient stream_client = TestStreamClientManager.isConnectedTo(relayip);

            if (stream_client == null)
            {
                // In a normal client, this should be done in a different thread and wait for the
                // connection to be established
                stream_client = TestStreamClientManager.connectTo(relayip);

                if (stream_client == null)
                {
                    Logging.error(string.Format("Error sending message. Could not connect to stream node: {0}", relayip));
                }
            }

            // Generate encryption keys
            byte[] keys_data = friend.generateKeys();

            // Generate the transaction
            Transaction transaction = new Transaction((int)Transaction.Type.Normal);

            transaction.amount = CoreConfig.relayPriceInitial;
            transaction.toList.Add(friend.relayWallet, transaction.amount);
            transaction.fee = CoreConfig.transactionPrice;
            transaction.fromList.Add(new byte[1] {
                0
            }, transaction.amount + transaction.fee);
            transaction.blockHeight = Node.blockHeight;
            transaction.pubKey      = Node.walletStorage.getPrimaryPublicKey(); // TODO: check if it's in the walletstate already
            transaction.checksum    = Transaction.calculateChecksum(transaction);

            // Prepare the stream message
            StreamMessage message = new StreamMessage();

            message.recipient   = friend.walletAddress;
            message.sender      = Node.walletStorage.getPrimaryAddress();
            message.transaction = transaction.getBytes();


            // Encrypt the message
            byte[] text_message = Encoding.UTF8.GetBytes("Hello Ixian World!");
            message.encryptMessage(text_message, friend.aesPassword, friend.chachaKey);

            // Encrypt the transaction signature
            byte[] tx_signature = transaction.getSignature(transaction.checksum);
            message.encryptSignature(tx_signature, friend.aesPassword, friend.chachaKey);

            stream_client.sendData(ProtocolMessageCode.s2data, message.getBytes());

            return(true);
        }