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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python
  2. # Copyright (c) 2014 Wladmir J. van der Laan
  3. # Distributed under the MIT/X11 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 __future__ import print_function, division
  28. from base64 import b32decode
  29. from binascii import a2b_hex
  30. import sys, os
  31. import re
  32. # ipv4 in ipv6 prefix
  33. pchIPv4 = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff])
  34. # tor-specific ipv6 prefix
  35. pchOnionCat = bytearray([0xFD,0x87,0xD8,0x7E,0xEB,0x43])
  36. def name_to_ipv6(addr):
  37. if len(addr)>6 and addr.endswith('.onion'):
  38. vchAddr = b32decode(addr[0:-6], True)
  39. if len(vchAddr) != 16-len(pchOnionCat):
  40. raise ValueError('Invalid onion %s' % s)
  41. return pchOnionCat + vchAddr
  42. elif '.' in addr: # IPv4
  43. return pchIPv4 + bytearray((int(x) for x in addr.split('.')))
  44. elif ':' in addr: # IPv6
  45. sub = [[], []] # prefix, suffix
  46. x = 0
  47. addr = addr.split(':')
  48. for i,comp in enumerate(addr):
  49. if comp == '':
  50. if i == 0 or i == (len(addr)-1): # skip empty component at beginning or end
  51. continue
  52. x += 1 # :: skips to suffix
  53. assert(x < 2)
  54. else: # two bytes per component
  55. val = int(comp, 16)
  56. sub[x].append(val >> 8)
  57. sub[x].append(val & 0xff)
  58. nullbytes = 16 - len(sub[0]) - len(sub[1])
  59. assert((x == 0 and nullbytes == 0) or (x == 1 and nullbytes > 0))
  60. return bytearray(sub[0] + ([0] * nullbytes) + sub[1])
  61. elif addr.startswith('0x'): # IPv4-in-little-endian
  62. return pchIPv4 + bytearray(reversed(a2b_hex(addr[2:])))
  63. else:
  64. raise ValueError('Could not parse address %s' % addr)
  65. def parse_spec(s, defaultport):
  66. match = re.match('\[([0-9a-fA-F:]+)\](?::([0-9]+))?$', s)
  67. if match: # ipv6
  68. host = match.group(1)
  69. port = match.group(2)
  70. else:
  71. (host,_,port) = s.partition(':')
  72. if not port:
  73. port = defaultport
  74. else:
  75. port = int(port)
  76. host = name_to_ipv6(host)
  77. return (host,port)
  78. def process_nodes(g, f, structname, defaultport):
  79. g.write('static SeedSpec6 %s[] = {\n' % structname)
  80. first = True
  81. for line in f:
  82. comment = line.find('#')
  83. if comment != -1:
  84. line = line[0:comment]
  85. line = line.strip()
  86. if not line:
  87. continue
  88. if not first:
  89. g.write(',\n')
  90. first = False
  91. (host,port) = parse_spec(line, defaultport)
  92. hoststr = ','.join(('0x%02x' % b) for b in host)
  93. g.write(' {{%s}, %i}' % (hoststr, port))
  94. g.write('\n};\n')
  95. def main():
  96. if len(sys.argv)<2:
  97. print(('Usage: %s <path_to_nodes_txt>' % sys.argv[0]), file=sys.stderr)
  98. exit(1)
  99. g = sys.stdout
  100. indir = sys.argv[1]
  101. g.write('#ifndef H_CHAINPARAMSSEEDS\n')
  102. g.write('#define H_CHAINPARAMSSEEDS\n')
  103. g.write('// List of fixed seed nodes for the bitcoin network\n')
  104. g.write('// AUTOGENERATED by contrib/devtools/generate-seeds.py\n\n')
  105. g.write('// Each line contains a 16-byte IPv6 address and a port.\n')
  106. g.write('// IPv4 as well as onion addresses are wrapped inside a IPv6 address accordingly.\n')
  107. with open(os.path.join(indir,'nodes_main.txt'),'r') as f:
  108. process_nodes(g, f, 'pnSeed6_main', 8333)
  109. g.write('\n')
  110. with open(os.path.join(indir,'nodes_test.txt'),'r') as f:
  111. process_nodes(g, f, 'pnSeed6_test', 18333)
  112. g.write('#endif\n')
  113. if __name__ == '__main__':
  114. main()