# -*- 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, PackageLoader from . import __version__ from .util import ( COIN, format8, makeInt, dumpj, ) from .chainparams import ( chainparams, Coins, ) from .basicswap import ( SwapTypes, BidStates, TxStates, TxTypes, strOfferState, strBidState, strTxState, getLockName, SEQUENCE_LOCK_TIME, ABS_LOCK_TIME, ) def format_timestamp(value): return time.strftime('%Y-%m-%d %H:%M', time.localtime(value)) env = Environment(loader=PackageLoader('basicswap', 'templates')) env.filters['formatts'] = format_timestamp PAGE_LIMIT = 50 def getCoinName(c): return chainparams[c]['name'].capitalize() def listAvailableCoins(swap_client): coins = [] for k, v in swap_client.coin_clients.items(): if v['connection_type'] == 'rpc': coins.append((int(k), getCoinName(k))) return coins def extractDomain(url): return url.split('://', 1)[1].split('/', 1)[0] def listAvailableExplorers(swap_client): explorers = [] for c in Coins: for i, e in enumerate(swap_client.coin_clients[c]['explorers']): explorers.append(('{}_{}'.format(int(c), i), swap_client.coin_clients[c]['name'].capitalize() + ' - ' + extractDomain(e.base_url))) return explorers def listExplorerActions(swap_client): actions = [('height', 'Chain Height'), ('block', 'Get Block'), ('tx', 'Get Transaction'), ('balance', 'Address Balance'), ('unspent', 'List Unspent')] return actions def listBidStates(): rv = [] for s in BidStates: rv.append((int(s), strBidState(s))) return rv def getTxIdHex(bid, tx_type, prefix): if tx_type == TxTypes.ITX: obj = bid.initiate_tx elif tx_type == TxTypes.PTX: obj = bid.participate_tx else: return 'Unknown Type' if not obj: return 'None' if not obj.txid: return 'None' return obj.txid.hex() + prefix def getTxSpendHex(bid, tx_type): if tx_type == TxTypes.ITX: obj = bid.initiate_tx elif tx_type == TxTypes.PTX: obj = bid.participate_tx else: return 'Unknown Type' if not obj: return 'None' if not obj.spend_txid: return 'None' return obj.spend_txid.hex() + ' {}'.format(obj.spend_n) def validateAmountString(amount): if type(amount) != str: return ar = amount.split('.') if len(ar) > 1 and len(ar[1]) > 8: raise ValueError('Too many decimal places in amount {}'.format(amount)) def html_content_start(title, h2=None, refresh=None): content = '\n
' \ + '' \ + ('' if not refresh else ''.format(refresh)) \ + 'Info: ' + info_str + '
' \ + '' return bytes(content, 'UTF-8') def page_error(self, error_str): content = html_content_start('BasicSwap Error') \ + 'Error: ' + error_str + '
' \ + '