create-bip-hd-chain-params.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. import json
  3. from bip_utils.slip.slip44 import Slip44
  4. from bip_utils.bip.conf.bip44.bip44_conf import Bip44Conf
  5. from bip_utils.bip.conf.bip49.bip49_conf import Bip49Conf
  6. from bip_utils.bip.conf.bip84.bip84_conf import Bip84Conf
  7. from bip_utils.bip.conf.bip86.bip86_conf import Bip86Conf
  8. import script_init
  9. from mmgen.cfg import Config
  10. from mmgen.main import launch
  11. opts_data = {
  12. 'text': {
  13. 'desc': 'Aggregate bip_lib and SLIP-44 data into a bip_hd chainparams table',
  14. 'usage':'[opts] infile',
  15. 'options': """
  16. -h, --help Print this help message.
  17. """,
  18. 'notes': """
  19. source: https://github.com/MetaMask/slip44/blob/main/slip44.json
  20. """
  21. }
  22. }
  23. cfg = Config(opts_data=opts_data)
  24. def curve_clsname_abbr(s):
  25. return {
  26. 'Bip32Slip10Secp256k1': 'x',
  27. 'Bip32Slip10Ed25519': 'edw',
  28. 'Bip32Slip10Ed25519Blake2b': 'blk',
  29. 'Bip32Slip10Nist256p1': 'nist',
  30. 'Bip32KholawEd25519': 'khol',
  31. }.get(s,s)
  32. fs2 = '{:5} {:2} {:16} {:8} {:8} {:6} {:8} {:8}'
  33. hdr2 = fs2.format('CURVE','NW','ADDR_CLS','VB_PRV','VB_PUB','VB_WIF','VB_ADDR','DFL_PATH')
  34. dfl_vb_prv = '0488ade4'
  35. dfl_vb_pub = '0488b21e'
  36. dfl_curve = 'secp'
  37. dfl_dfl_path = "0'/0/0"
  38. def get_bip_utils_data(bipnum,n):
  39. name,v = bip_utils_data[bipnum][n]
  40. #pexit(v.__dict__)
  41. vb_prv = v.m_key_net_ver.m_priv_net_ver.hex()
  42. vb_pub = v.m_key_net_ver.m_pub_net_ver.hex()
  43. ap = v.m_addr_params
  44. return fs2.format(
  45. curve_clsname_abbr(v.m_bip32_cls.__name__),
  46. 'T' if v.m_is_testnet else 'm',
  47. v.m_addr_cls.__name__.removesuffix('AddrEncoder'),
  48. 'x' if vb_prv == dfl_vb_prv else vb_prv,
  49. 'x' if vb_pub == dfl_vb_pub else vb_pub,
  50. v.m_wif_net_ver.hex() if isinstance(v.m_wif_net_ver,bytes) else '-',
  51. ap['net_ver'].hex() if 'net_ver' in ap else 'h:'+ap['hrp'] if 'hrp' in ap else 'spec' if ap else '-',
  52. 'x' if v.m_def_path == dfl_dfl_path else v.m_def_path,
  53. )
  54. def gen():
  55. def format_data(bipnum,n,sym,name):
  56. return fs.format(
  57. n,
  58. sym if sym else '---',
  59. get_bip_utils_data(bipnum,n) if bipnum else '-',
  60. name if name else '---')
  61. fs = '{:<6} {:6} {:1} {}'
  62. yield f'[defaults]'
  63. yield fs.format('IDX','CHAIN',hdr2,'NAME')
  64. yield fs.format('0', '-', fs2.format(dfl_curve, '-', '-', dfl_vb_prv, dfl_vb_pub, '-', '-', dfl_dfl_path), '-')
  65. yield f'\n[bip-44]'
  66. yield fs.format('IDX','CHAIN',hdr2,'NAME')
  67. for k,v in slip44_data.items():
  68. if int(k) in bip_utils_data[44]:
  69. yield format_data(44,int(k),v['symbol'],v['name'])
  70. for bipnum in (49, 84, 86):
  71. yield f'\n[bip-{bipnum}]'
  72. yield fs.format('IDX','CHAIN',hdr2,'NAME')
  73. for n,v in sorted(bip_utils_data[bipnum].items()):
  74. nd = v[1].m_coin_names
  75. yield format_data(bipnum,n,nd.m_abbr,nd.m_name)
  76. yield f'\n[bip-44-unsupported]'
  77. yield fs.format('IDX','CHAIN','','NAME')
  78. for k,v in slip44_data.items():
  79. if not int(k) in bip_utils_data[44]:
  80. yield format_data(None,int(k),v['symbol'],v['name'])
  81. def main():
  82. global slip44_data, bip_utils_data
  83. if len(cfg._args) != 1:
  84. cfg._opts.usage()
  85. with open(cfg._args[0]) as fh:
  86. slip44_data = json.loads(fh.read())
  87. bip_utils_data = {
  88. n:{v.m_coin_idx:(k,v) for k,v in globals()[f'Bip{n}Conf'].__dict__.items() if not k.startswith('_')}
  89. for n in (44, 49, 84, 86)
  90. }
  91. print('\n'.join(gen()))
  92. launch(func=main)