Given a NAME_IMPORT nameop, see if we can import it.
    * the name must be well-formed
    * the namespace must be revealed, but not ready
    * the name cannot have been imported yet
    * the sender must be the same as the namespace's sender

    Set the __preorder__ and __prior_history__ fields, since this
    is a state-creating operation.

    Return True if accepted
    Return False if not

def check( state_engine, nameop, block_id, checked_ops ):
    """
    Given a NAME_IMPORT nameop, see if we can import it.
    * the name must be well-formed
    * the namespace must be revealed, but not ready
    * the name cannot have been imported yet
    * the sender must be the same as the namespace's sender

    Set the __preorder__ and __prior_history__ fields, since this
    is a state-creating operation.

    Return True if accepted
    Return False if not
    """

    from ..nameset import BlockstackDB

    name = str(nameop['name'])
    sender = str(nameop['sender'])
    sender_pubkey = None
    recipient = str(nameop['recipient'])
    recipient_address = str(nameop['recipient_address'])

    preorder_hash = hash_name( nameop['name'], sender, recipient_address )
    log.debug("preorder_hash = %s (%s, %s, %s)" % (preorder_hash, nameop['name'], sender, recipient_address))

    preorder_block_number = block_id
    name_block_number = block_id 
    name_first_registered = block_id
    name_last_renewed = block_id
    # transfer_send_block_id = None

    if not nameop.has_key('sender_pubkey'):
        log.warning("Name import requires a sender_pubkey (i.e. use of a p2pkh transaction)")
        return False

    # name must be well-formed
    if not is_name_valid( name ):
        log.warning("Malformed name '%s'" % name)
        return False

    name_without_namespace = get_name_from_fq_name( name )
    namespace_id = get_namespace_from_name( name )

    # namespace must be revealed, but not ready
    if not state_engine.is_namespace_revealed( namespace_id ):
        log.warning("Namespace '%s' is not revealed" % namespace_id )
        return False

    namespace = state_engine.get_namespace_reveal( namespace_id )

    # sender p2pkh script must use a public key derived from the namespace revealer's public key
    sender_pubkey_hex = str(nameop['sender_pubkey'])
    sender_pubkey = virtualchain.BitcoinPublicKey( str(sender_pubkey_hex) )
    sender_address = sender_pubkey.address()

    import_addresses = BlockstackDB.load_import_keychain( state_engine.working_dir, namespace['namespace_id'] )
    if import_addresses is None:

        # the first name imported must be the revealer's address
        if sender_address != namespace['recipient_address']:
            log.warning("First NAME_IMPORT must come from the namespace revealer's address")
            return False

        # need to generate a keyring from the revealer's public key
        log.warning("Generating %s-key keychain for '%s'" % (NAME_IMPORT_KEYRING_SIZE, namespace_id))
        import_addresses = BlockstackDB.build_import_keychain( state_engine.working_dir, namespace['namespace_id'], sender_pubkey_hex )

    # sender must be the same as the the person who revealed the namespace
    # (i.e. sender's address must be from one of the valid import addresses)
    if sender_address not in import_addresses:
        log.warning("Sender address '%s' is not in the import keychain" % (sender_address))
        return False

    # we can overwrite, but emit a warning
    # search *current* block as well as last block
    prev_name_rec = get_prev_imported( state_engine, checked_ops, name )
    if prev_name_rec is not None and is_earlier_than( prev_name_rec, block_id, nameop['vtxindex'] ):

        log.warning("Overwriting already-imported name '%s'" % name)

        # propagate preorder block number and hash...
        preorder_block_number = prev_name_rec['preorder_block_number']
        name_block_number = prev_name_rec['block_number']
        name_first_registered = prev_name_rec['first_registered']
        name_last_renewed = prev_name_rec['last_renewed']

        log.debug("use previous preorder_hash = %s" % prev_name_rec['preorder_hash'])
        preorder_hash = prev_name_rec['preorder_hash']

    # can never have been preordered
    state_create_put_preorder( nameop, None )

    # carry out the transition 
    del nameop['recipient']
    del nameop['recipient_address']

    # set op_fee for BTC
    # set token_fee otherwise
    bitcoin_price = 0
    stacks_price = 0

    if namespace['version'] == NAMESPACE_VERSION_PAY_WITH_STACKS:
        # make sure we're in the right epoch 
        epoch_features = get_epoch_features(block_id)
        if EPOCH_FEATURE_STACKS_BUY_NAMESPACES not in epoch_features or EPOCH_FEATURE_NAMEOPS_COST_TOKENS not in epoch_features:
            log.fatal('Have a namespace with STACKs enabled, but we\'re in the wrong epoch!')
            os.abort()

        stacks_price = price_name(name_without_namespace, namespace, block_id)
        stacks_price = int(stacks_price)

    else:
        # QUIRK: keep this as a float due to backwards-compatibility
        bitcoin_price = price_name(name_without_namespace, namespace, block_id)
        bitcoin_price = float(bitcoin_price)

    nameop['sender'] = recipient
    nameop['address'] = recipient_address
    nameop['importer'] = sender
    nameop['importer_address'] = sender_address
    nameop['op_fee'] = bitcoin_price
    nameop['token_fee'] = '{}'.format(stacks_price)
    nameop['namespace_block_number'] = namespace['block_number']
    nameop['consensus_hash'] = None 
    nameop['preorder_hash'] = preorder_hash
    nameop['block_number'] = name_block_number
    nameop['first_registered'] = name_first_registered
    nameop['last_renewed'] = name_last_renewed
    nameop['preorder_block_number'] = preorder_block_number
    nameop['opcode'] = "NAME_IMPORT"

    # not required for consensus, but for SNV
    nameop['last_creation_op'] = NAME_IMPORT
    
    # good!
    return True