proto.btc.tx.completed: move check_swap_memo() to parent class

This commit is contained in:
The MMGen Project 2025-03-21 11:42:58 +03:00
commit 324d17deb5
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 27 additions and 22 deletions

View file

@ -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)

View file

@ -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')

View file

@ -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'))