You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate-seeds.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2017 Wladimir J. van der Laan
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. '''
  6. Script to generate list of seed nodes for chainparams.cpp.
  7. This script expects two text files in the directory that is passed as an
  8. argument:
  9. nodes_main.txt
  10. nodes_test.txt
  11. These files must consist of lines in the format
  12. <ip>
  13. <ip>:<port>
  14. [<ipv6>]
  15. [<ipv6>]:<port>
  16. <onion>.onion
  17. 0xDDBBCCAA (IPv4 little-endian old pnSeeds format)
  18. The output will be two data structures with the peers in binary format:
  19. static SeedSpec6 pnSeed6_main[]={
  20. ...
  21. }
  22. static SeedSpec6 pnSeed6_test[]={
  23. ...
  24. }
  25. These should be pasted into `src/chainparamsseeds.h`.
  26. '''
  27. from base64 import b32decode
  28. from binascii import a2b_hex
  29. import sys, os
  30. import re
  31. # ipv4 in ipv6 prefix
  32. pchIPv4 = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff])
  33. # tor-specific ipv6 prefix
  34. pchOnionCat = bytearray([0xFD,0x87,0xD8,0x7E,0xEB,0x43])
  35. def name_to_ipv6(addr):
  36. if len(addr)>6 and addr.endswith('.onion'):
  37. vchAddr = b32decode(addr[0:-6], True)
  38. if len(vchAddr) != 16-len(pchOnionCat):
  39. raise ValueError('Invalid onion %s' % s)
  40. return pchOnionCat + vchAddr
  41. elif '.' in addr: # IPv4
  42. return pchIPv4 + bytearray((int(x) for x in addr.split('.')))
  43. elif ':' in addr: # IPv6
  44. sub = [[], []] # prefix, suffix
  45. x = 0
  46. addr = addr.split(':')
  47. for i,comp in enumerate(addr):
  48. if comp == '':
  49. if i == 0 or i == (len(addr)-1): # skip empty component at beginning or end
  50. continue
  51. x += 1 # :: skips to suffix
  52. assert(x < 2)
  53. else: # two bytes per component
  54. val = int(comp, 16)
  55. sub[x].append(val >> 8)
  56. sub[x].append(val & 0xff)
  57. nullbytes = 16 - len(sub[0]) - len(sub[1])
  58. assert((x == 0 and nullbytes == 0) or (x == 1 and nullbytes > 0))
  59. return bytearray(sub[0] + ([0] * nullbytes) + sub[1])
  60. elif addr.startswith('0x'): # IPv4-in-little-endian
  61. return pchIPv4 + bytearray(reversed(a2b_hex(addr[2:])))
  62. else:
  63. raise ValueError('Could not parse address %s' % addr)
  64. def parse_spec(s, defaultport):
  65. match = re.match('\[([0-9a-fA-F:]+)\](?::([0-9]+))?$', s)
  66. if match: # ipv6
  67. host = match.group(1)
  68. port = match.group(2)
  69. elif s.count(':') > 1: # ipv6, no port
  70. host = s
  71. port = ''
  72. else:
  73. (host,_,port) = s.partition(':')
  74. if not port:
  75. port = defaultport
  76. else:
  77. port = int(port)
  78. host = name_to_ipv6(host)
  79. return (host,port)
  80. def process_nodes(g, f, structname, defaultport):
  81. g.write('static SeedSpec6 %s[] = {\n' % structname)
  82. first = True
  83. for line in f:
  84. comment = line.find('#')
  85. if comment != -1:
  86. line = line[0:comment]
  87. line = line.strip()
  88. if not line:
  89. continue
  90. if not first:
  91. g.write(',\n')
  92. first = False
  93. (host,port) = parse_spec(line, defaultport)
  94. hoststr = ','.join(('0x%02x' % b) for b in host)
  95. g.write(' {{%s}, %i}' % (hoststr, port))
  96. g.write('\n};\n')
  97. def main():
  98. if len(sys.argv)<2:
  99. print(('Usage: %s <path_to_nodes_txt>' % sys.argv[0]), file=sys.stderr)
  100. exit(1)
  101. g = sys.stdout
  102. indir = sys.argv[1]
  103. g.write('#ifndef STARWELS_CHAINPARAMSSEEDS_H\n')
  104. g.write('#define STARWELS_CHAINPARAMSSEEDS_H\n')
  105. g.write('/**\n')
  106. g.write(' * List of fixed seed nodes for the starwels network\n')
  107. g.write(' * AUTOGENERATED by contrib/seeds/generate-seeds.py\n')
  108. g.write(' *\n')
  109. g.write(' * Each line contains a 16-byte IPv6 address and a port.\n')
  110. g.write(' * IPv4 as well as onion addresses are wrapped inside a IPv6 address accordingly.\n')
  111. g.write(' */\n')
  112. with open(os.path.join(indir,'nodes_main.txt'),'r') as f:
  113. process_nodes(g, f, 'pnSeed6_main', 8353)
  114. g.write('\n')
  115. with open(os.path.join(indir,'nodes_test.txt'),'r') as f:
  116. process_nodes(g, f, 'pnSeed6_test', 8343)
  117. g.write('#endif // STARWELS_CHAINPARAMSSEEDS_H\n')
  118. if __name__ == '__main__':
  119. main()