Last active
February 19, 2018 15:05
-
-
Save 30mb1/22e87ed6556c931006c3a3ae53a8988d to your computer and use it in GitHub Desktop.
Simple program for connection to litecoin nodes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import socket | |
import struct | |
import random | |
import hashlib | |
import dns.resolver | |
import ipaddress | |
from messages import * | |
# list of hardcoded in chainparams.cpp dns seeders | |
litecoin_nodes = [ | |
'seed-a.litecoin.loshan.co.uk', | |
'dnsseed.thrasher.io', | |
'dnsseed.litecointools.com', | |
'dnsseed.litecoinpool.org', | |
'dnsseed.koin-project.com' | |
] | |
# get list of nodes | |
ip_list = [] | |
for node in litecoin_nodes: | |
try: | |
answers = dns.resolver.query(node) | |
for res in answers: | |
ip_list.append(res.address) | |
except: | |
continue | |
ip_list = list(set(ip_list)) | |
# trying to connect to every node | |
# sending "version" message to every node | |
for num, ip in enumerate(ip_list): | |
if type(ipaddress.ip_address(ip)) == ipaddress.IPv6Address: | |
addr_family = socket.AF_INET6 | |
else: | |
addr_family = socket.AF_INET | |
try: | |
with socket.socket(addr_family, socket.SOCK_STREAM) as sock: | |
sock.settimeout(5) | |
sock.connect((ip, 9333)) | |
sock.send(makeMessage("version", versionMessage())) | |
# catch version and verack message | |
sock.recv(1024) | |
except: | |
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import socket | |
import struct | |
import random | |
import hashlib | |
def makeMessage(cmd, payload): | |
magic = b'\xfb\xc0\xb6\xdb' # litecoin mainnet magic number | |
command = str.encode(cmd + (12 - len(cmd)) * "\00") | |
length = struct.pack("I", len(payload)) | |
check = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] | |
return magic + command + length + check + payload | |
def versionMessage(): | |
version = struct.pack("i", 70015) | |
services = struct.pack("Q", 0) | |
timestamp = struct.pack("q", int(time.time())) | |
addr_recv = struct.pack("Q", 0) | |
addr_recv += struct.pack(">16s", b"127.0.0.1") | |
addr_recv += struct.pack(">H", 9333) | |
addr_from = struct.pack("Q", 0) | |
addr_from += struct.pack(">16s", b"127.0.0.1") | |
addr_from += struct.pack(">H", 9333) | |
nonce = struct.pack("Q", random.getrandbits(64)) | |
user_agent = struct.pack("B", 0) # Anything | |
height = struct.pack("i", 0) # Block number, doesn't matter | |
payload = version + services + timestamp + addr_recv + addr_from + nonce + user_agent + height | |
return payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
I am interesting in your project. Could you make transactions with Litecoin? I tried the same way and I could not. I don`t know why because their have same protocols, it should be easy.