1

This is the code that i have. I receive messages from the zeroMQ however a hashtx message for example looks like this --> "hashtx �����v����|(8d �!��v��&�-�k�". Any help in turning that into a real hashtx is appreciated :)

public static void main(String[] args) {
    ZMQ.Context context = ZMQ.context(1);

    // Connect our subscriber socket
    ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
    subscriber.connect("tcp://192.168.137.211:28332");

    subscriber.subscribe("rawblock".getBytes());
    subscriber.subscribe("hashblock".getBytes());
    subscriber.subscribe("rawtx".getBytes());
    subscriber.subscribe("hashtx".getBytes());

    while (!msg.equalsIgnoreCase("END")) {

        String printMe = new String(subscriber.recv());
        System.out.println(printMe);

    }

}
  • I wish I had an answer of the to of my head, but I can tell you this: zeromq isn't a format, it's a transport medium. It can transport any format, but you are aiming it's a string. It's not. You need to read the spec to find out how to interpret the bytes you receive. – Jestin Feb 2 '18 at 13:34
2

Bitcoin Core does not give you strings for the data from ZMQ. It provides the raw bytes of the transactions, blocks, and their hashes. You will need to interpret the received data as byte arrays and handle them appropriately.

1

Ok so i found the solution. The "hashtx, hashblock" etc messages are sent as raw binary bytes which then need to be converted to hexadecimal.

Here is my code.

public static void main(String[] args) {
    ZMQ.Context context = ZMQ.context(1);

    // Connect our subscriber socket
    ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
    // Synchronize with the publisher
    subscriber.connect("tcp://192.168.137.211:28332");

    //subscriber.subscribe("rawblock".getBytes());
    //subscriber.subscribe("rawtx".getBytes());
    //subscriber.subscribe("hashtx".getBytes());
    subscriber.subscribe("hashblock".getBytes());

    System.out.println("Subscribed.. Waiting for messages.");
    while (true) {

        ZMsg zMsg = ZMsg.recvMsg(subscriber);
        System.out.println("New message received!");
        int messageNumber = 0;
        for (ZFrame f: zMsg) {
            byte[] bytes = f.getData();
            System.out.println("Message number: " + messageNumber + " | Byte array length: " + bytes.length);
            if (messageNumber == 0){
                String messageType = new String(bytes);
                System.out.println("Message type: " + messageType);
            } else if (messageNumber == 1) {
                String message = Converter.bin2hex(bytes);
                System.out.println("Message: " + message);
            }
            messageNumber++;
        }

    }
}

and the binary to hex converter class

import java.util.Formatter;
public class Converter
{
private Converter() {
    throw new IllegalStateException("Utility class");
}

public static String bin2hex(byte[] bytes)
{
    Formatter f = new Formatter();
    try {
        for (byte c : bytes)
            f.format("%02X",c);

        return (f.toString().toLowerCase());
    } finally {
        f.close();
    }
}

public static String hex2bin(String hexString)
{
    if (!hexString.matches("^[0-9a-fA-F]+$")) {
        return null;
    }
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < hexString.length(); i+=2) {
        String str = hexString.substring(i, i+2);
        output.append((char)Integer.parseInt(str, 16));
    }
    return output.toString();
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.