Given the hex representation of a script,
    turn it into a nice, easy-to-read dict.
    The dict will have:
    * asm: the disassembled script as a string
    * hex: the raw hex (given as an argument)
    * type: the type of script

    Optionally, it will have:
    * addresses: a list of addresses the script represents (if applicable)
    * reqSigs: the number of required signatures (if applicable)

def btc_tx_output_parse_script( scriptpubkey ):
    """
    Given the hex representation of a script,
    turn it into a nice, easy-to-read dict.
    The dict will have:
    * asm: the disassembled script as a string
    * hex: the raw hex (given as an argument)
    * type: the type of script

    Optionally, it will have:
    * addresses: a list of addresses the script represents (if applicable)
    * reqSigs: the number of required signatures (if applicable)
    """
    
    script_type = None
    reqSigs = None
    addresses = []
   
    script_type = btc_script_classify(scriptpubkey)
    script_tokens = btc_script_deserialize(scriptpubkey)

    if script_type in ['p2pkh']:
        script_type = "pubkeyhash"
        reqSigs = 1
        addr = btc_script_hex_to_address(scriptpubkey)
        if not addr:
            raise ValueError("Failed to parse scriptpubkey address")

        addresses = [addr]

    elif script_type in ['p2sh', 'p2sh-p2wpkh', 'p2sh-p2wsh']:
        script_type = "scripthash"
        reqSigs = 1
        addr = btc_script_hex_to_address(scriptpubkey)
        if not addr:
            raise ValueError("Failed to parse scriptpubkey address")

        addresses = [addr]

    elif script_type == 'p2pk':
        script_type = "pubkey"
        reqSigs = 1

    elif script_type is None:
        script_type = "nonstandard"

    ret = {
        "asm": btc_tx_script_to_asm(scriptpubkey),
        "hex": scriptpubkey,
        "type": script_type
    }

    if addresses is not None:
        ret['addresses'] = addresses

    if reqSigs is not None:
        ret['reqSigs'] = reqSigs

    # print 'parse script {}: {}'.format(scriptpubkey, ret)

    return ret