From 324d17deb5e8807364d47b23c3c37435453e7d4f Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 21 Mar 2025 11:42:58 +0300 Subject: [PATCH] proto.btc.tx.completed: move `check_swap_memo()` to parent class --- mmgen/proto/btc/tx/completed.py | 27 ++++++++------------------- mmgen/proto/eth/tx/completed.py | 3 --- mmgen/tx/completed.py | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/mmgen/proto/btc/tx/completed.py b/mmgen/proto/btc/tx/completed.py index 5a09a82c..16b0baa4 100755 --- a/mmgen/proto/btc/tx/completed.py +++ b/mmgen/proto/btc/tx/completed.py @@ -14,12 +14,19 @@ proto.btc.tx.completed: Bitcoin completed transaction class from ....tx import completed as TxBase from ....obj import HexStr -from ....util import msg, ymsg, die +from ....util import msg, die from .base import Base, decodeScriptPubKey class Completed(Base, TxBase.Completed): fn_fee_unit = 'satoshi' + def decode_tx_usr_data(self): + if o := self.data_output: + try: + return o.data.decode() + except: + pass + # check signature and witness data def check_sigs(self): # return True if sigs found, False otherwise; raise exception on error txins = self.deserialized.txins @@ -43,24 +50,6 @@ class Completed(Base, TxBase.Completed): assert (200 < len(ti['scriptSig']) < 300), 'malformed scriptSig' # VERY rough check return True - def check_swap_memo(self): - if o := self.data_output: - from ....swap.proto.thorchain.memo import Memo - if Memo.is_partial_memo(o.data): - from ....protocol import init_proto - p = Memo.parse(o.data) - assert p.function == 'SWAP', f'‘{p.function}’: unsupported function in swap memo ‘{o.data}’' - assert p.chain == p.asset, f'{p.chain} != {p.asset}: chain/asset mismatch in swap memo ‘{o.data}’' - proto = init_proto(self.cfg, p.asset, network=self.cfg.network, need_amt=True) - if self.swap_recv_addr_mmid: - mmid = self.swap_recv_addr_mmid - elif self.cfg.allow_non_wallet_swap: - ymsg('Warning: allowing swap to non-wallet address (--allow-non-wallet-swap)') - mmid = None - else: - raise ValueError('Swap to non-wallet address forbidden (override with --allow-non-wallet-swap)') - return self.Output(proto, addr=p.address, mmid=mmid, amt=proto.coin_amt('0')) - def check_pubkey_scripts(self): for n, i in enumerate(self.inputs, 1): ds = decodeScriptPubKey(self.proto, i.scriptPubKey) diff --git a/mmgen/proto/eth/tx/completed.py b/mmgen/proto/eth/tx/completed.py index a79f8c73..a66080db 100755 --- a/mmgen/proto/eth/tx/completed.py +++ b/mmgen/proto/eth/tx/completed.py @@ -18,9 +18,6 @@ from .base import Base, TokenBase class Completed(Base, TxBase.Completed): fn_fee_unit = 'Mwei' - def check_swap_memo(self): - pass - @property def send_amt(self): return self.outputs[0].amt if self.outputs else self.proto.coin_amt('0') diff --git a/mmgen/tx/completed.py b/mmgen/tx/completed.py index 7fb3998b..1a735c37 100755 --- a/mmgen/tx/completed.py +++ b/mmgen/tx/completed.py @@ -68,3 +68,22 @@ class Completed(Base): for cls in (Signed, AutomountSigned): if ext == getattr(cls, 'ext'): return cls + + def check_swap_memo(self): + if text := self.decode_tx_usr_data(): + from ..swap.proto.thorchain.memo import Memo + if Memo.is_partial_memo(text): + from ..protocol import init_proto + p = Memo.parse(text) + assert p.function == 'SWAP', f'‘{p.function}’: unsupported function in swap memo ‘{text}’' + assert p.chain == p.asset, f'{p.chain} != {p.asset}: chain/asset mismatch in swap memo ‘{text}’' + proto = init_proto(self.cfg, p.asset, network=self.cfg.network, need_amt=True) + if self.swap_recv_addr_mmid: + mmid = self.swap_recv_addr_mmid + elif self.cfg.allow_non_wallet_swap: + from ..util import ymsg + ymsg('Warning: allowing swap to non-wallet address (--allow-non-wallet-swap)') + mmid = None + else: + raise ValueError('Swap to non-wallet address forbidden (override with --allow-non-wallet-swap)') + return self.Output(proto, addr=p.address, mmid=mmid, amt=proto.coin_amt('0'))