# Path: jmbase/jmbase/support.py
# def get_log():
#     """
#     provides joinmarket logging instance
#     :return: log instance
#     """
#     return log
# 
# def bintohex(b):
#     """Convert bytes to a hex string"""
#     return binascii.hexlify(b).decode('utf8')
# 
# EXIT_FAILURE = 1
# 
# def hexbin(func):
#     """ Decorator for functions of taker and maker receiving over
#     the wire AMP arguments that may be in hex or hextxid:n format
#     and converting all to binary.
#     Functions to which this decorator applies should have all arguments
#     be one of:
#     - hex string (keys), converted here to binary
#     - lists of keys or txid:n strings (converted here to binary, or
#       (txidbytes, n))
#     - lists of lists or dicts, to which these rules apply recursively.
#     - any other string (unchanged)
#     - dicts with keys as per above; values are altered recursively according
#       to the rules above.
#     """
#     @wraps(func)
#     def func_wrapper(inst, *args, **kwargs):
#         newargs = []
#         for arg in args:
#             if isinstance(arg, (list, tuple)):
#                 newargs.append(listchanger(arg))
#             elif isinstance(arg, dict):
#                 newargs.append(dictchanger(arg))
#             else:
#                 newargs.append(_convert(arg))
#         return func(inst, *newargs, **kwargs)
# 
#     return func_wrapper
# 
# Path: jmclient/jmclient/cryptoengine.py
# class EngineError(Exception):
#     pass


import base64
import sys
import abc
import atexit
import jmbitcoin as btc
from jmbase import bintohex, hexbin, get_log, EXIT_FAILURE
from jmclient.wallet_service import WalletService
from jmclient.configure import jm_single
from jmclient.support import calc_cj_fee
from jmclient.podle import verify_podle, PoDLE, PoDLEError
from twisted.internet import task
from .cryptoengine import EngineError

        # authorisation of taker passed
        # Find utxos for the transaction now:
        utxos, cj_addr, change_addr = self.oid_to_order(offer, amount)
        if not utxos:
            #could not find funds
            return (False,)
        # for index update persistence:
        self.wallet_service.save_wallet()
        # Construct data for auth request back to taker.
        # Need to choose an input utxo pubkey to sign with
        # Just choose the first utxo in utxos and retrieve key from wallet.
        auth_address = next(iter(utxos.values()))['address']
        auth_key = self.wallet_service.get_key_from_addr(auth_address)
        auth_pub = btc.privkey_to_pubkey(auth_key)
        # kphex was auto-converted by @hexbin but we actually need to sign the
        # hex version to comply with pre-existing JM protocol:
        btc_sig = btc.ecdsa_sign(bintohex(kphex), auth_key)
        return (True, utxos, auth_pub, cj_addr, change_addr, btc_sig)

    @hexbin
    def on_tx_received(self, nick, tx, offerinfo):
        """Called when the counterparty has sent an unsigned
        transaction. Sigs are created and returned if and only
        if the transaction passes verification checks (see
        verify_unsigned_tx()).
        """
        # special case due to cjfee passed as string: it can accidentally parse
        # as hex:
        if not isinstance(offerinfo["offer"]["cjfee"], str):

# Path: jmbase/jmbase/support.py
# def get_log():
#     """
#     provides joinmarket logging instance
#     :return: log instance
#     """
#     return log
# 
# def bintohex(b):
#     """Convert bytes to a hex string"""
#     return binascii.hexlify(b).decode('utf8')
# 
# EXIT_FAILURE = 1
# 
# def hexbin(func):
#     """ Decorator for functions of taker and maker receiving over
#     the wire AMP arguments that may be in hex or hextxid:n format
#     and converting all to binary.
#     Functions to which this decorator applies should have all arguments
#     be one of:
#     - hex string (keys), converted here to binary
#     - lists of keys or txid:n strings (converted here to binary, or
#       (txidbytes, n))
#     - lists of lists or dicts, to which these rules apply recursively.
#     - any other string (unchanged)
#     - dicts with keys as per above; values are altered recursively according
#       to the rules above.
#     """
#     @wraps(func)
#     def func_wrapper(inst, *args, **kwargs):
#         newargs = []
#         for arg in args:
#             if isinstance(arg, (list, tuple)):
#                 newargs.append(listchanger(arg))
#             elif isinstance(arg, dict):
#                 newargs.append(dictchanger(arg))
#             else:
#                 newargs.append(_convert(arg))
#         return func(inst, *newargs, **kwargs)
# 
#     return func_wrapper
# 
# Path: jmclient/jmclient/cryptoengine.py
# class EngineError(Exception):
#     pass


# Path: jmclient/jmclient/maker.py
import base64
import sys
import abc
import atexit
import jmbitcoin as btc
from jmbase import bintohex, hexbin, get_log, EXIT_FAILURE
from jmclient.wallet_service import WalletService
from jmclient.configure import jm_single
from jmclient.support import calc_cj_fee
from jmclient.podle import verify_podle, PoDLE, PoDLEError
from twisted.internet import task
from .cryptoengine import EngineError


        # authorisation of taker passed
        # Find utxos for the transaction now:
        utxos, cj_addr, change_addr = self.oid_to_order(offer, amount)
        if not utxos:
            #could not find funds
            return (False,)
        # for index update persistence:
        self.wallet_service.save_wallet()
        # Construct data for auth request back to taker.
        # Need to choose an input utxo pubkey to sign with
        # Just choose the first utxo in utxos and retrieve key from wallet.
        auth_address = next(iter(utxos.values()))['address']
        auth_key = self.wallet_service.get_key_from_addr(auth_address)
        auth_pub = btc.privkey_to_pubkey(auth_key)
        # kphex was auto-converted by @hexbin but we actually need to sign the
        # hex version to comply with pre-existing JM protocol:
        btc_sig = btc.ecdsa_sign(bintohex(kphex), auth_key)
        return (True, utxos, auth_pub, cj_addr, change_addr, btc_sig)

    @hexbin
    def on_tx_received(self, nick, tx, offerinfo):
        """Called when the counterparty has sent an unsigned
        transaction. Sigs are created and returned if and only
        if the transaction passes verification checks (see
        verify_unsigned_tx()).
        """
        # special case due to cjfee passed as string: it can accidentally parse
        # as hex:
        if not isinstance(offerinfo["offer"]["cjfee"], str):

            offerinfo["offer"]["cjfee"] = bintohex(offerinfo["offer"]["cjfee"])