Skip to content

Instantly share code, notes, and snippets.

@davidlj95
Last active September 26, 2018 12:27
Show Gist options
  • Select an option

  • Save davidlj95/c824896bef29e98e9c915671f49a2ddc to your computer and use it in GitHub Desktop.

Select an option

Save davidlj95/c824896bef29e98e9c915671f49a2ddc to your computer and use it in GitHub Desktop.
Converts an IPv4 address to a line of chainparamsseeds.h to check if your node is a seed of Bitcoin Core client
#!/usr/bin/python3
#
# Converts an IPv4 address to the format of the file ``chainparamsseeds.h``
# to know if your node is a seed in Bitcoin Core client's seeds.
#
# This file is automatically created by ``generate-seeds.py``
#
# Sources:
# https://github.com/bitcoin/bitcoin/blob/v0.16.3/src/chainparamsseeds.h
# https://github.com/bitcoin/bitcoin/tree/v0.16.3/contrib/seeds
# https://github.com/bitcoin/bitcoin/blob/v0.16.3/contrib/seeds/generate-seeds.py
#
# Usage:
# python ip2chainparamsseeds.py 0.0.0.0
import sys
FORMAT = "{{{{"+ "0x{:02x},"*15 + "0x{:02x}}}, {:d}}}"
def ip_to_seed(ip: str, port: int = 8333) -> str:
"""Converts an IP address and port into the ``chainparamsseeds.h`` format.
Just IPv4 dotted-quad addresses are understood.
"""
num_vals = [0 for _ in range(16)]
quad_vals = bytes(int(i) for i in ip.split("."))
num_vals[-6:-4] = 0xff, 0xff
num_vals[-4:] = quad_vals
return FORMAT.format(*num_vals, port)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage:")
print("ip2chainparamsseeds.py 0.0.0.0")
ip = sys.argv[1]
print(ip_to_seed(ip))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment