# -*- coding: utf-8 -*- # Copyright (c) 2019 tecnovert # Distributed under the MIT software license, see the accompanying # file LICENSE.txt or http://www.opensource.org/licenses/mit-license.php. import os import json import time import struct import traceback import threading import http.client import urllib.parse from http.server import BaseHTTPRequestHandler, HTTPServer from jinja2 import Environment, FileSystemLoader from .util import ( COIN, format8, ) from .chainparams import ( chainparams, Coins, ) from .basicswap import ( SwapTypes, BidStates, TxStates, getOfferState, getBidState, getTxState, getLockName, SEQUENCE_LOCK_TIME, ABS_LOCK_TIME, ) file_loader = FileSystemLoader('templates') env = Environment(loader=file_loader) def getCoinName(c): return chainparams[c]['name'].capitalize() def html_content_start(title, h2=None, refresh=None): content = '\n
' \ + '' \ + ('' if not refresh else ''.format(refresh)) \ + 'Error: ' + error_str + '
' \ + '' return bytes(content, 'UTF-8') def js_error(self, error_str): error_str_json = json.dumps({'error': error_str}) return bytes(error_str_json, 'UTF-8') def js_wallets(self, url_split): return bytes(json.dumps(self.server.swap_client.getWalletsInfo()), 'UTF-8') def js_offers(self, url_split): assert(False), 'TODO' return bytes(json.dumps(self.server.swap_client.listOffers()), 'UTF-8') def js_sentoffers(self, url_split): assert(False), 'TODO' return bytes(json.dumps(self.server.swap_client.listOffers(sent=True)), 'UTF-8') def js_bids(self, url_split): if len(url_split) > 3: bid_id = bytes.fromhex(url_split[3]) assert(len(bid_id) == 28) return bytes(json.dumps(self.server.swap_client.viewBid(bid_id)), 'UTF-8') return bytes(json.dumps(self.server.swap_client.listBids()), 'UTF-8') def js_sentbids(self, url_split): return bytes(json.dumps(self.server.swap_client.listBids(sent=True)), 'UTF-8') def js_index(self, url_split): return bytes(json.dumps(self.server.swap_client.getSummary()), 'UTF-8') def page_active(self, url_split, post_string): swap_client = self.server.swap_client active_swaps = swap_client.listSwapsInProgress() template = env.get_template('active.html') return bytes(template.render( title=self.server.title, refresh=30, h2=self.server.title, active_swaps=[(s[0].hex(), s[1], getBidState(s[2])) for s in active_swaps], ), 'UTF-8') def page_wallets(self, url_split, post_string): swap_client = self.server.swap_client content = html_content_start(self.server.title, self.server.title) \ + 'Prevented double submit for form {}.
'.format(form_id) else: self.server.last_form_id['wallets'] = form_id for c in Coins: cid = str(int(c)) if bytes('newaddr_' + cid, 'utf-8') in form_data: swap_client.cacheNewAddressForCoin(c) if bytes('withdraw_' + cid, 'utf-8') in form_data: value = form_data[bytes('amt_' + cid, 'utf-8')][0].decode('utf-8') address = form_data[bytes('to_' + cid, 'utf-8')][0].decode('utf-8') subfee = True if bytes('subfee_' + cid, 'utf-8') in form_data else False txid = swap_client.withdrawCoin(c, value, address, subfee) ticker = swap_client.getTicker(c) content += 'Withdrew {} {} to address {}
In txid: {}