unsigned.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2026 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. proto.vm.tx.unsigned: unsigned transaction methods for VM chains
  12. """
  13. from ....util import msg, msg_r, die
  14. class Unsigned:
  15. desc = 'unsigned transaction'
  16. # Return signed object or False. Don’t exit or raise exception:
  17. async def sign(self, keys, tx_num_str=''):
  18. from ....exception import TransactionChainMismatch
  19. try:
  20. self.check_correct_chain()
  21. except TransactionChainMismatch:
  22. return False
  23. o = self.txobj
  24. def do_mismatch_err(io, j, k, desc):
  25. m = 'A compromised online installation may have altered your serialized data!'
  26. fs = '\n{} mismatch!\n{}\n orig: {}\n serialized: {}'
  27. die(3, fs.format(desc.upper(), m, getattr(io[0], k), o[j]))
  28. if o['from'] != self.inputs[0].addr:
  29. do_mismatch_err(self.inputs, 'from', 'addr', 'from-address')
  30. if self.outputs:
  31. if o['to'] != self.outputs[0].addr:
  32. do_mismatch_err(self.outputs, 'to', 'addr', 'to-address')
  33. if o['amt'] != self.outputs[0].amt:
  34. do_mismatch_err(self.outputs, 'amt', 'amt', 'amount')
  35. msg_r(f'Signing transaction{tx_num_str}...')
  36. try:
  37. await self.do_sign(o, keys[0].sec.wif)
  38. msg('OK')
  39. from ....tx import SignedTX
  40. tx = SignedTX(cfg=self.cfg, data=self.__dict__, automount=self.automount)
  41. tx.check_serialized_integrity()
  42. return tx
  43. except Exception as e:
  44. msg(f'{e}: transaction signing failed!')
  45. return False