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 https://github.com/plizonczyk/noiseprotocol, 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(('v2.stratum.slushpool.com', 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
proto.set_as_initiator()

# Enter handshake mode
proto.start_handshake()

# 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. 
sock.sendall(message)

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

# Feed the received message into noiseasia1.ethpool.org
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')
sock.sendall(encrypted_message)

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

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))
    s.listen(1)

    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)

    noise.set_as_responder()
    noise.start_handshake()


    # Perform handshake. Break when finished
    for action in cycle(['receive', 'send']):
        if noise.handshake_finished:
            break
        elif action == 'send':
            ciphertext = noise.write_message()
            conn.sendall(ciphertext)
        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:
            break
        received = noise.decrypt(data)
        conn.sendall(noise.encrypt(received))

Basically, socket just gets closed, there’s no message back from server, nothing. Not sure if I gravely misunderstood their documentation (https://docs.google.com/document/d/1FadCWj-57dvhxsnFM_7X806qyvhR0u3i85607bGHxvg/edit#), 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 *