stratum - Connecting to stratumv2 from python

stratum – Connecting to stratumv2 from python


so I’ve put together the following example to connect to a stratum v2 pool, but it’s timeouting every time. It’s basically just modified from the readme of, but using the Noise_NX_25519_ChaChaPoly_BLAKE2s, which stratum v2 is supposedly using.

Client (works on localhost with the server snippet below, timeouts on a real stratum v2 server like slushpool’s):

import socket
from noise.connection import NoiseConnection, Keypair

sock = socket.socket()

# stratum2
# sock.connect(('localhost', 2000))
sock.connect(('', 3336))

# Create instance of NoiseConnection, set up to use NN handshake pattern, Curve25519 for
# elliptic curve keypair, ChaCha20Poly1305 as cipher function and SHA256 for hashing.  

proto = NoiseConnection.from_name(b'Noise_NX_25519_ChaChaPoly_BLAKE2s')

# Set role in this connection as initiator

# Enter handshake mode

# Perform handshake - as we are the initiator, we need to generate first message. 
# We don't provide any payload (although we could, but it would be cleartext for this pattern).
message = proto.write_message()
# Send the message to the responder - you may simply use sockets or any other way 
# to exchange bytes between communicating parties. 

# Receive the message from the responder 
received = sock.recv(2048)

# Feed the received message into
payload = proto.read_message(received)

# As of now, the handshake should be finished (as we are using NN pattern). 
# Any further calls to write_message or read_message would raise NoiseHandshakeError exception.
# We can use encrypt/decrypt methods of NoiseConnection now for encryption and decryption of messages.
encrypted_message = proto.encrypt(b'This is an example payload')

ciphertext = sock.recv(2048)
plaintext = proto.decrypt(ciphertext)

Server to run on localhost, to test the above code:

import socket
from itertools import cycle
from noise.connection import NoiseConnection, Keypair
import base64

if __name__ == '__main__':
    s = socket.socket()
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(('localhost', 2000))

    conn, addr = s.accept()
    print('Accepted connection from', addr)

    noise = NoiseConnection.from_name(b'Noise_NX_25519_ChaChaPoly_BLAKE2s')

    our_private = base64.b64decode('WAmgVYXkbT2bCtdcDwolI88/iVi/aV3/PHcUBTQSYmo=')
    noise.set_keypair_from_private_bytes(Keypair.STATIC, our_private)


    # Perform handshake. Break when finished
    for action in cycle(['receive', 'send']):
        if noise.handshake_finished:
        elif action == 'send':
            ciphertext = noise.write_message()
        elif action == 'receive':
            data = conn.recv(2048)
            plaintext = noise.read_message(data)

    # Endless loop "echoing" received data
    while True:
        data = conn.recv(2048)
        if not data:
        received = noise.decrypt(data)

Basically, socket just gets closed, there’s no message back from server, nothing. Not sure if I gravely misunderstood their documentation (, or what, because I can’t get past the handshake.

Please help.

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *