brain.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. wallet.brain: brainwallet wallet class
  12. """
  13. from ..util import msg
  14. from ..color import yellow
  15. from .enc import wallet
  16. from .seed import Seed
  17. class wallet(wallet):
  18. stdin_ok = True
  19. desc = 'brainwallet'
  20. # brainwallet warning message? TODO
  21. def get_bw_params(self):
  22. # already checked
  23. a = self.cfg.brain_params.split(',')
  24. return int(a[0]), a[1]
  25. def _deformat(self):
  26. self.brainpasswd = ' '.join(self.fmt_data.split())
  27. return True
  28. def _decrypt(self):
  29. d = self.ssdata
  30. if self.cfg.brain_params:
  31. bw_seed_len, d.hash_preset = self.get_bw_params()
  32. else:
  33. if not self.cfg.seed_len:
  34. self.cfg._util.qmsg(f'Using default seed length of {yellow(str(Seed.dfl_len))} bits\n'
  35. + 'If this is not what you want, use the --seed-len option')
  36. self._get_hash_preset()
  37. bw_seed_len = self.cfg.seed_len or Seed.dfl_len
  38. self.cfg._util.qmsg_r('Hashing brainwallet data. Please wait...')
  39. # Use buflen arg of scrypt.hash() to get seed of desired length
  40. seed = self.crypto.scrypt_hash_passphrase(
  41. self.brainpasswd.encode(),
  42. b'',
  43. d.hash_preset,
  44. buflen = bw_seed_len // 8)
  45. self.cfg._util.qmsg('Done')
  46. self.seed = Seed(self.cfg, seed)
  47. msg(f'Seed ID: {self.seed.sid}')
  48. self.cfg._util.qmsg('Check this value against your records')
  49. return True
  50. def _format(self):
  51. raise NotImplementedError('Brainwallet not supported as an output format')
  52. def _encrypt(self):
  53. raise NotImplementedError('Brainwallet not supported as an output format')