unsigned.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. proto.eth.tx.unsigned: Ethereum unsigned transaction class
  12. """
  13. import json
  14. from ....tx import unsigned as TxBase
  15. from ....util import msg, msg_r
  16. from ....obj import CoinTxID, ETHNonce, Int, HexStr
  17. from ....addr import CoinAddr, TokenAddr
  18. from ..contract import Token
  19. from .completed import Completed, TokenCompleted
  20. class Unsigned(Completed,TxBase.Unsigned):
  21. desc = 'unsigned transaction'
  22. def parse_txfile_serialized_data(self):
  23. d = json.loads(self.serialized)
  24. o = {
  25. 'from': CoinAddr(self.proto,d['from']),
  26. # NB: for token, 'to' is sendto address
  27. 'to': CoinAddr(self.proto,d['to']) if d['to'] else None,
  28. 'amt': self.proto.coin_amt(d['amt']),
  29. 'gasPrice': self.proto.coin_amt(d['gasPrice']),
  30. 'startGas': self.proto.coin_amt(d['startGas']),
  31. 'nonce': ETHNonce(d['nonce']),
  32. 'chainId': None if d['chainId'] == 'None' else Int(d['chainId']),
  33. 'data': HexStr(d['data']) }
  34. self.gas = o['startGas'] # approximate, but better than nothing
  35. self.txobj = o
  36. return d # 'token_addr','decimals' required by Token subclass
  37. async def do_sign(self,wif):
  38. o = self.txobj
  39. o_conv = {
  40. 'to': bytes.fromhex(o['to'] or ''),
  41. 'startgas': o['startGas'].toWei(),
  42. 'gasprice': o['gasPrice'].toWei(),
  43. 'value': o['amt'].toWei() if o['amt'] else 0,
  44. 'nonce': o['nonce'],
  45. 'data': bytes.fromhex(o['data'])}
  46. from ..pyethereum.transactions import Transaction
  47. etx = Transaction(**o_conv).sign(wif, o['chainId'])
  48. assert etx.sender.hex() == o['from'], (
  49. 'Sender address recovered from signature does not match true sender')
  50. from .. import rlp
  51. self.serialized = rlp.encode(etx).hex()
  52. self.coin_txid = CoinTxID(etx.hash.hex())
  53. if o['data']:
  54. if o['to']:
  55. assert self.txobj['token_addr'] == TokenAddr(self.proto,etx.creates.hex()),'Token address mismatch'
  56. else: # token- or contract-creating transaction
  57. self.txobj['token_addr'] = TokenAddr(self.proto,etx.creates.hex())
  58. async def sign(self,tx_num_str,keys): # return TX object or False; don't exit or raise exception
  59. from ....exception import TransactionChainMismatch
  60. try:
  61. self.check_correct_chain()
  62. except TransactionChainMismatch:
  63. return False
  64. msg_r(f'Signing transaction{tx_num_str}...')
  65. try:
  66. await self.do_sign(keys[0].sec.wif)
  67. msg('OK')
  68. from ....tx import SignedTX
  69. return await SignedTX(cfg=self.cfg, data=self.__dict__, automount=self.automount)
  70. except Exception as e:
  71. msg(f'{e}: transaction signing failed!')
  72. return False
  73. class TokenUnsigned(TokenCompleted,Unsigned):
  74. desc = 'unsigned transaction'
  75. def parse_txfile_serialized_data(self):
  76. d = Unsigned.parse_txfile_serialized_data(self)
  77. o = self.txobj
  78. o['token_addr'] = TokenAddr(self.proto,d['token_addr'])
  79. o['decimals'] = Int(d['decimals'])
  80. t = Token(self.cfg,self.proto,o['token_addr'],o['decimals'])
  81. o['data'] = t.create_data(o['to'],o['amt'])
  82. o['token_to'] = t.transferdata2sendaddr(o['data'])
  83. async def do_sign(self,wif):
  84. o = self.txobj
  85. t = Token(self.cfg,self.proto,o['token_addr'],o['decimals'])
  86. tx_in = t.make_tx_in(
  87. to_addr = o['to'],
  88. amt = o['amt'],
  89. start_gas = self.start_gas,
  90. gasPrice = o['gasPrice'],
  91. nonce = o['nonce'])
  92. (self.serialized,self.coin_txid) = await t.txsign(tx_in,wif,o['from'],chain_id=o['chainId'])
  93. class AutomountUnsigned(TxBase.AutomountUnsigned, Unsigned):
  94. pass
  95. class TokenAutomountUnsigned(TxBase.AutomountUnsigned, TokenUnsigned):
  96. pass