# Path: jmbase/jmbase/support.py
# def bintohex(b):
#     """Convert bytes to a hex string"""
#     return binascii.hexlify(b).decode('utf8')
# 
# def utxo_to_utxostr(u):
#     if not isinstance(u, tuple):
#         return (False, "utxo is not a tuple.")
#     if not len(u) == 2:
#         return (False, "utxo should have two elements.")
#     if not isinstance(u[0], bytes):
#         return (False, "txid should be bytes.")
#     if not isinstance(u[1], int):
#         return (False, "index should be int.")
#     if u[1] < 0:
#         return (False, "index must be a positive integer.")
#     if not len(u[0]) == 32:
#         return (False, "txid must be 32 bytes.")
#     txid = binascii.hexlify(u[0]).decode("ascii")
#     return (True, txid + ":" + str(u[1]))


import random
import json
import hashlib
from jmbitcoin.secp256k1_main import *
from jmbase import bintohex, utxo_to_utxostr
from bitcointx.core import (CMutableTransaction, CTxInWitness,
                            CMutableOutPoint, CMutableTxIn, CTransaction,
                            CMutableTxOut, CTxIn, CTxOut, ValidationError)
from bitcointx.core.script import *
from bitcointx.wallet import (P2WPKHCoinAddress, CCoinAddress, P2PKHCoinAddress,
                              CCoinAddressError)
from bitcointx.core.scripteval import (VerifyScript, SCRIPT_VERIFY_WITNESS,
                                       SCRIPT_VERIFY_P2SH,
                                       SCRIPT_VERIFY_STRICTENC,
                                       SIGVERSION_WITNESS_V0)
    'p2pkh': {
        'p2wpkh': -3,
        'p2sh-p2wpkh': -2,
        'p2wsh': 9
    },
    'p2wpkh': {
        'p2pkh': 3,
        'p2sh-p2wpkh': 1,
        'p2wsh': 12
    },
    'p2sh-p2wpkh': {
        'p2pkh': 2,
        'p2wpkh': -1,
        'p2wsh': 11
    }
}

def human_readable_transaction(tx, jsonified=True):
    """ Given a CTransaction object, output a human
    readable json-formatted string (suitable for terminal
    output or large GUI textbox display) containing
    all details of that transaction.
    If `jsonified` is False, the dict is returned, instead
    of the json string.
    """
    assert isinstance(tx, CTransaction)
    outdict = {}
    outdict["hex"] = bintohex(tx.serialize())
    outdict["inputs"]=[]
    outdict["outputs"]=[]

# Path: jmbase/jmbase/support.py
# def bintohex(b):
#     """Convert bytes to a hex string"""
#     return binascii.hexlify(b).decode('utf8')
# 
# def utxo_to_utxostr(u):
#     if not isinstance(u, tuple):
#         return (False, "utxo is not a tuple.")
#     if not len(u) == 2:
#         return (False, "utxo should have two elements.")
#     if not isinstance(u[0], bytes):
#         return (False, "txid should be bytes.")
#     if not isinstance(u[1], int):
#         return (False, "index should be int.")
#     if u[1] < 0:
#         return (False, "index must be a positive integer.")
#     if not len(u[0]) == 32:
#         return (False, "txid must be 32 bytes.")
#     txid = binascii.hexlify(u[0]).decode("ascii")
#     return (True, txid + ":" + str(u[1]))


# Path: jmbitcoin/jmbitcoin/secp256k1_transaction.py
import random
import json
import hashlib
from jmbitcoin.secp256k1_main import *
from jmbase import bintohex, utxo_to_utxostr
from bitcointx.core import (CMutableTransaction, CTxInWitness,
                            CMutableOutPoint, CMutableTxIn, CTransaction,
                            CMutableTxOut, CTxIn, CTxOut, ValidationError)
from bitcointx.core.script import *
from bitcointx.wallet import (P2WPKHCoinAddress, CCoinAddress, P2PKHCoinAddress,
                              CCoinAddressError)
from bitcointx.core.scripteval import (VerifyScript, SCRIPT_VERIFY_WITNESS,
                                       SCRIPT_VERIFY_P2SH,
                                       SCRIPT_VERIFY_STRICTENC,
                                       SIGVERSION_WITNESS_V0)

    'p2pkh': {
        'p2wpkh': -3,
        'p2sh-p2wpkh': -2,
        'p2wsh': 9
    },
    'p2wpkh': {
        'p2pkh': 3,
        'p2sh-p2wpkh': 1,
        'p2wsh': 12
    },
    'p2sh-p2wpkh': {
        'p2pkh': 2,
        'p2wpkh': -1,
        'p2wsh': 11
    }
}

def human_readable_transaction(tx, jsonified=True):
    """ Given a CTransaction object, output a human
    readable json-formatted string (suitable for terminal
    output or large GUI textbox display) containing
    all details of that transaction.
    If `jsonified` is False, the dict is returned, instead
    of the json string.
    """
    assert isinstance(tx, CTransaction)
    outdict = {}
    outdict["hex"] = bintohex(tx.serialize())
    outdict["inputs"]=[]
    outdict["outputs"]=[]

    outdict["txid"]= bintohex(tx.GetTxid()[::-1])