diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 729174c..5cc4f49 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -1602,23 +1602,44 @@ class BasicSwap(BaseApp): session.remove() self.mxDB.release() - def list_bid_events(self, bid_id): + def getXmrBidAndOffer(self, bid_id, list_events=True): self.mxDB.acquire() - events = [] try: session = scoped_session(self.session_factory) - query_str = 'SELECT created_at, event_type, event_msg FROM eventlog ' + \ - 'WHERE linked_type = {} AND linked_id = x\'{}\' '.format(TableTypes.BID, bid_id.hex()) - q = self.engine.execute(query_str) + xmr_swap = None + offer = None + xmr_offer = None + events = [] - for row in q: - events.append({'at': row[0], 'desc': describeEventEntry(row[1], row[2])}) - return events + bid = session.query(Bid).filter_by(bid_id=bid_id).first() + if bid: + offer = session.query(Offer).filter_by(offer_id=bid.offer_id).first() + if offer and offer.swap_type == SwapTypes.XMR_SWAP: + xmr_swap = session.query(XmrSwap).filter_by(bid_id=bid.bid_id).first() + xmr_offer = session.query(XmrOffer).filter_by(offer_id=bid.offer_id).first() + self.loadBidTxns(bid, session) + if list_events: + events = self.list_bid_events(bid.bid_id, session) + else: + bid.initiate_tx = session.query(SwapTx).filter(sa.and_(SwapTx.bid_id == bid_id, SwapTx.tx_type == TxTypes.ITX)).first() + bid.participate_tx = session.query(SwapTx).filter(sa.and_(SwapTx.bid_id == bid_id, SwapTx.tx_type == TxTypes.PTX)).first() + + return bid, xmr_swap, offer, xmr_offer, events finally: session.close() session.remove() self.mxDB.release() + def list_bid_events(self, bid_id, session): + session = scoped_session(self.session_factory) + query_str = 'SELECT created_at, event_type, event_msg FROM eventlog ' + \ + 'WHERE linked_type = {} AND linked_id = x\'{}\' '.format(TableTypes.BID, bid_id.hex()) + q = self.engine.execute(query_str) + events = [] + for row in q: + events.append({'at': row[0], 'desc': describeEventEntry(row[1], row[2])}) + return events + def acceptBid(self, bid_id): self.log.info('Accepting bid %s', bid_id.hex()) @@ -4269,7 +4290,7 @@ class BasicSwap(BaseApp): xmr_swap.a_lock_spend_tx, xmr_swap.al_lock_spend_tx_esig, xmr_swap.pkal, xmr_swap.pkasf, 0, xmr_swap.a_lock_tx_script, bid.amount) assert(v), 'verifyTxOtVES failed' - except Exception as e: + except Exception as ex: if self.debug: traceback.print_exc() self.setBidError(bid_id, bid, str(ex)) @@ -4422,7 +4443,7 @@ class BasicSwap(BaseApp): else: self.log.debug('TODO - determine in-progress for manualBidUpdate') if offer.swap_type == SwapTypes.XMR_SWAP: - if bid.state and bid.state in (BidStates.XMR_SWAP_LOCK_RELEASED, BidStates.XMR_SWAP_NOSCRIPT_TX_REDEEMED): + if bid.state and bid.state in (BidStates.XMR_SWAP_SCRIPT_COIN_LOCKED, BidStates.XMR_SWAP_LOCK_RELEASED, BidStates.XMR_SWAP_NOSCRIPT_TX_REDEEMED): activate_bid = True if activate_bid: diff --git a/basicswap/chainparams.py b/basicswap/chainparams.py index 77e247f..27eee4b 100644 --- a/basicswap/chainparams.py +++ b/basicswap/chainparams.py @@ -7,7 +7,8 @@ from enum import IntEnum from .util import ( COIN, - format_amount + format_amount, + make_int ) XMR_COIN = 10 ** 12 @@ -202,7 +203,11 @@ class CoinInterface: def __init__(self): self._unknown_wallet_seed = True - def format_amount(self, amount_int): + def make_int(self, amount_in): + return make_int(amount_in, self.exp()) + + def format_amount(self, amount_in, conv_int=False): + amount_int = make_int(amount_in, self.exp()) if conv_int else amount_in return format_amount(amount_int, self.exp()) def coin_name(self): diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 814abc2..349a83e 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -526,6 +526,7 @@ class HttpHandler(BaseHTTPRequestHandler): messages = [] show_txns = False edit_bid = False + view_tx_ind = None form_data = self.checkForm(post_string, 'bid', messages) if form_data: if b'abandon_bid' in form_data: @@ -553,11 +554,14 @@ class HttpHandler(BaseHTTPRequestHandler): messages.append('Bid edited') except Exception as ex: messages.append('Edit failed ' + str(ex)) + elif b'view_tx_submit' in form_data: + show_txns = True + view_tx_ind = form_data[b'view_tx'][0].decode('utf-8') - bid, offer = swap_client.getBidAndOffer(bid_id) + bid, xmr_swap, offer, xmr_offer, events = swap_client.getXmrBidAndOffer(bid_id) assert(bid), 'Unknown bid ID' - data = describeBid(swap_client, bid, offer, edit_bid, show_txns) + data = describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, events, edit_bid, show_txns, view_tx_ind) old_states = [] num_states = len(bid.states) // 12 diff --git a/basicswap/interface_btc.py b/basicswap/interface_btc.py index 231c14c..5e9b193 100644 --- a/basicswap/interface_btc.py +++ b/basicswap/interface_btc.py @@ -48,15 +48,10 @@ from .contrib.test_framework.messages import ( from .contrib.test_framework.script import ( CScript, CScriptOp, - CScriptNum, OP_IF, OP_ELSE, OP_ENDIF, OP_0, OP_2, - OP_16, - OP_EQUALVERIFY, OP_CHECKSIG, - OP_SIZE, - OP_SHA256, OP_CHECKMULTISIG, OP_CHECKSEQUENCEVERIFY, OP_DROP, diff --git a/basicswap/js_server.py b/basicswap/js_server.py index 13cb75d..34bc910 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -127,12 +127,12 @@ def js_bids(self, url_split, post_string): if b'accept' in post_data: swap_client.acceptBid(bid_id) - bid, offer = swap_client.getBidAndOffer(bid_id) + bid, xmr_swap, offer, xmr_offer, events = swap_client.getXmrBidAndOffer(bid_id) assert(bid), 'Unknown bid ID' edit_bid = False show_txns = False - data = describeBid(swap_client, bid, offer, edit_bid, show_txns) + data = describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, events, edit_bid, show_txns) return bytes(json.dumps(data), 'UTF-8') diff --git a/basicswap/network.py b/basicswap/network.py index b2706cc..87701ce 100644 --- a/basicswap/network.py +++ b/basicswap/network.py @@ -38,5 +38,4 @@ class Network: readable, writable, errored = select.select([self._socket], [], []) for s in readable: client_socket, address = self._socket.accept() - read_list.append(client_socket) - logging.info('Connection from %s', address) + logging.info('Connection from %s', address) diff --git a/basicswap/script.py b/basicswap/script.py index 1beca9d..1418aac 100644 --- a/basicswap/script.py +++ b/basicswap/script.py @@ -11,6 +11,7 @@ class OpCodes(IntEnum): OP_0 = 0x00, OP_PUSHDATA1 = 0x4c, OP_1 = 0x51, + OP_16 = 0x60, OP_IF = 0x63, OP_ELSE = 0x67, OP_ENDIF = 0x68, diff --git a/basicswap/templates/bid_xmr.html b/basicswap/templates/bid_xmr.html index e361f38..dbb3412 100644 --- a/basicswap/templates/bid_xmr.html +++ b/basicswap/templates/bid_xmr.html @@ -21,16 +21,6 @@ Received{{ data.was_received }} -{% if data.show_txns %} -

Transactions

- - -{% for tx in data.txns %} - -{% endfor %} -
Tx TypeTx IDBlocks Deep
{{ tx.type }}{{ tx.txid }}{{ tx.confirms }}
-{% endif %} -
{% if edit_bid %}

Edit Bid

@@ -57,6 +47,29 @@ {% endif %} + +{% if data.show_txns %} +

Transactions

+ + +{% for tx in data.txns %} + +{% endfor %} +
Tx TypeTx IDBlocks Deep
{{ tx.type }}{{ tx.txid }}{{ tx.confirms }}
+ + +
View Transaction +
+ + +{% if data.view_tx_hex %} +

{{ data.view_tx_hex }}

+{% endif %} +{% endif %}
diff --git a/basicswap/ui.py b/basicswap/ui.py index ea72a34..cc0bb47 100644 --- a/basicswap/ui.py +++ b/basicswap/ui.py @@ -87,7 +87,7 @@ def listBidStates(): return rv -def describeBid(swap_client, bid, offer, edit_bid, show_txns): +def describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, bid_events, edit_bid, show_txns, view_tx_ind=None): ci_from = swap_client.ci(Coins(offer.coin_from)) ci_to = swap_client.ci(Coins(offer.coin_to)) ticker_from = ci_from.ticker() @@ -165,7 +165,13 @@ def describeBid(swap_client, bid, offer, edit_bid, show_txns): txns.append({'type': 'Chain B Lock', 'txid': bid.xmr_b_lock_tx.txid.hex(), 'confirms': confirms}) if bid.xmr_b_lock_tx and bid.xmr_b_lock_tx.spend_txid: txns.append({'type': 'Chain B Lock Spend', 'txid': bid.xmr_b_lock_tx.spend_txid.hex()}) + if xmr_swap.a_lock_refund_tx: + txns.append({'type': strTxType(TxTypes.XMR_SWAP_A_LOCK_REFUND), 'txid': xmr_swap.a_lock_refund_tx_id.hex()}) + if xmr_swap.a_lock_refund_spend_tx: + txns.append({'type': strTxType(TxTypes.XMR_SWAP_A_LOCK_REFUND_SPEND), 'txid': xmr_swap.a_lock_refund_spend_tx_id.hex()}) for tx_type, tx in bid.txns.items(): + if tx_type in (TxTypes.XMR_SWAP_A_LOCK_REFUND, TxTypes.XMR_SWAP_A_LOCK_REFUND_SPEND): + continue txns.append({'type': strTxType(tx_type), 'txid': tx.txid.hex()}) data['txns'] = txns else: @@ -175,6 +181,18 @@ def describeBid(swap_client, bid, offer, edit_bid, show_txns): data['participate_tx_spend'] = getTxSpendHex(bid, TxTypes.PTX) if offer.swap_type == SwapTypes.XMR_SWAP: - data['events'] = swap_client.list_bid_events(bid.bid_id) + if view_tx_ind: + data['view_tx_ind'] = view_tx_ind + view_tx_id = bytes.fromhex(view_tx_ind) + + if xmr_swap: + if view_tx_id == xmr_swap.a_lock_tx_id and xmr_swap.a_lock_tx: + data['view_tx_hex'] = xmr_swap.a_lock_tx.hex() + if view_tx_id == xmr_swap.a_lock_refund_tx_id and xmr_swap.a_lock_refund_tx: + data['view_tx_hex'] = xmr_swap.a_lock_refund_tx.hex() + if view_tx_id == xmr_swap.a_lock_refund_spend_tx_id and xmr_swap.a_lock_refund_spend_tx: + data['view_tx_hex'] = xmr_swap.a_lock_refund_spend_tx.hex() + + data['events'] = bid_events return data diff --git a/basicswap/util.py b/basicswap/util.py index ca38354..d5f0c14 100644 --- a/basicswap/util.py +++ b/basicswap/util.py @@ -7,10 +7,11 @@ import json import decimal import hashlib + +from .script import OpCodes from .contrib.segwit_addr import bech32_decode, convertbits, bech32_encode -OP_1 = 0x51 -OP_16 = 0x60 + COIN = 100000000 @@ -195,8 +196,8 @@ def DeserialiseNum(b, o=0): def decodeScriptNum(script_bytes, o): v = 0 num_len = script_bytes[o] - if num_len >= OP_1 and num_len <= OP_16: - return((num_len - OP_1) + 1, 1) + if num_len >= OpCodes.OP_1 and num_len <= OpCodes.OP_16: + return((num_len - OpCodes.OP_1) + 1, 1) if num_len > 4: raise ValueError('Bad scriptnum length') # Max 4 bytes diff --git a/tests/basicswap/test_other.py b/tests/basicswap/test_other.py index 724a3c3..c82f379 100644 --- a/tests/basicswap/test_other.py +++ b/tests/basicswap/test_other.py @@ -205,7 +205,6 @@ class Test(unittest.TestCase): assert(len(sig) == 64) ci.verifyCompact(pk, 'test signing message', sig) - def test_dleag(self): coin_settings = {'rpcport': 0, 'walletrpcport': 0, 'walletrpcauth': 'none', 'blocks_confirmed': 1, 'conf_target': 1} ci = XMRInterface(coin_settings, 'regtest')