main_seedjoin.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen/main_seedjoin: Regenerate an MMGen deterministic wallet from seed shares
  20. created by 'mmgen-seedsplit'
  21. """
  22. from .common import *
  23. from .obj import MasterShareIdx,SeedSplitIDString,MMGenWalletLabel
  24. from .seed import Seed,SeedShareMasterJoining
  25. from .wallet import Wallet
  26. opts_data = {
  27. 'text': {
  28. 'desc': """Regenerate an MMGen deterministic wallet from seed shares
  29. created by 'mmgen-seedsplit'""",
  30. 'usage': '[options] share1 share2 [...shareN]',
  31. 'options': """
  32. -h, --help Print this help message
  33. --, --longhelp Print help message for long options (common options)
  34. -d, --outdir= d Output file to directory 'd' instead of working dir
  35. -e, --echo-passphrase Echo passphrases and other user input to screen
  36. -i, --id-str= s ID String of split (required for master share join only)
  37. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  38. 'f' at offset 'o' (comma-separated). NOTE: only the
  39. first share may be in hidden incognito format!
  40. -J, --hidden-incog-output-params=f,o Write hidden incognito data to file
  41. 'f' at offset 'o' (comma-separated). File 'f' will be
  42. created if necessary and filled with random data.
  43. -o, --out-fmt= f Output to wallet format 'f' (see FMT CODES below)
  44. -O, --old-incog-fmt Specify old-format incognito input
  45. -L, --label= l Specify a label 'l' for output wallet
  46. -M, --master-share=i Use a master share with index 'i' (min:{ms_min}, max:{ms_max})
  47. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  48. for password hashing (default: '{g.hash_preset}')
  49. -z, --show-hash-presets Show information on available hash presets
  50. -P, --passwd-file= f Get wallet passphrase from file 'f'
  51. -q, --quiet Produce quieter output; suppress some warnings
  52. -r, --usr-randchars=n Get 'n' characters of additional randomness from user
  53. (min={g.min_urandchars}, max={g.max_urandchars}, default={g.usr_randchars})
  54. -S, --stdout Write wallet data to stdout instead of file
  55. -v, --verbose Produce more verbose output
  56. """,
  57. 'notes': """
  58. COMMAND NOTES:
  59. When joining with a master share, the master share must be listed first.
  60. The remaining shares may be listed in any order.
  61. The --id-str option is required only for master share joins. For ordinary
  62. joins it will be ignored.
  63. For usage examples, see the help screen for the 'mmgen-seedsplit' command.
  64. {n_pw}
  65. FMT CODES:
  66. {f}
  67. """
  68. },
  69. 'code': {
  70. 'options': lambda s: s.format(
  71. ms_min=MasterShareIdx.min_val,
  72. ms_max=MasterShareIdx.max_val,
  73. g=g,
  74. ),
  75. 'notes': lambda s: s.format(
  76. f='\n '.join(Wallet.format_fmt_codes().splitlines()),
  77. n_pw=help_notes('passwd'),
  78. )
  79. }
  80. }
  81. def print_shares_info():
  82. si,out = 0,'\nComputed shares:\n'
  83. if opt.master_share:
  84. fs = '{:3}: {}->{} ' + yellow('(master share #{}, split id ') + '{}' + yellow(', share count {})\n')
  85. out += fs.format(
  86. 1,
  87. shares[0].sid,
  88. share1.sid,
  89. master_idx,
  90. id_str.hl(encl="''"),
  91. len(shares) )
  92. si = 1
  93. for n,s in enumerate(shares[si:],si+1):
  94. out += '{:3}: {}\n'.format(n,s.sid)
  95. qmsg(out)
  96. cmd_args = opts.init(opts_data)
  97. if len(cmd_args) + bool(opt.hidden_incog_input_params) < 2:
  98. opts.usage()
  99. if opt.label:
  100. opt.label = MMGenWalletLabel(opt.label,msg="Error in option '--label'")
  101. if opt.master_share:
  102. master_idx = MasterShareIdx(opt.master_share)
  103. id_str = SeedSplitIDString(opt.id_str or 'default')
  104. if opt.id_str and not opt.master_share:
  105. die(1,'--id-str option meaningless in context of non-master-share join')
  106. for arg in cmd_args:
  107. check_wallet_extension(arg)
  108. check_infile(arg)
  109. do_license_msg()
  110. qmsg('Input files:\n {}\n'.format('\n '.join(cmd_args)))
  111. shares = [Wallet().seed] if opt.hidden_incog_input_params else []
  112. shares += [Wallet(fn).seed for fn in cmd_args]
  113. if opt.master_share:
  114. share1 = SeedShareMasterJoining(master_idx,shares[0],id_str,len(shares)).derived_seed
  115. else:
  116. share1 = shares[0]
  117. print_shares_info()
  118. msg_r('Joining {n}-of-{n} XOR split...'.format(n=len(shares)))
  119. seed_out = Seed.join_shares([share1]+shares[1:])
  120. msg('OK\nJoined Seed ID: {}'.format(seed_out.sid.hl()))
  121. Wallet(seed=seed_out).write_to_file()