'Extract address and script type from transaction output script.'

def script_to_pubkey_id(store, chain, script):

    (script_type, data) = chain.parse_txout_script(script)
    if (script_type in (Chain.SCRIPT_TYPE_ADDRESS, Chain.SCRIPT_TYPE_P2SH)):
        return store.pubkey_hash_to_id(data)
    if (script_type == Chain.SCRIPT_TYPE_PUBKEY):
        return store.pubkey_to_id(chain, data)
    if (script_type == Chain.SCRIPT_TYPE_MULTISIG):
        script_hash = chain.script_hash(script)
        multisig_id = store._pubkey_id(script_hash, script)
        if (not store.selectrow('SELECT    1    FROM    multisig_pubkey    WHERE    multisig_id    =    ?', (multisig_id,))):
            for pubkey in set(data['pubkeys']):
                pubkey_id = store.pubkey_to_id(chain, pubkey)
                store.sql('\n                                                                                                INSERT    INTO    multisig_pubkey    (multisig_id,    pubkey_id)\n                                                                                                VALUES    (?,    ?)', (multisig_id, pubkey_id))
        return multisig_id
    if (script_type == Chain.SCRIPT_TYPE_BURN):
        return PUBKEY_ID_NETWORK_FEE
    return None
