def check(state_engine):

    global wallet_keys, error

    if error:
        print "Key operation failed."
        return False

    # not revealed, but ready
    ns = state_engine.get_namespace_reveal("test")
    if ns is not None:
        print "namespace not ready"
        return False

    ns = state_engine.get_namespace("test")
    if ns is None:
        print "no namespace"
        return False

    if ns['namespace_id'] != 'test':
        print "wrong namespace"
        return False

    # not preordered
    names = ['foo.test']
    wallet_keys_list = [wallet_keys]
    test_proxy = testlib.TestAPIProxy()

    for i in xrange(0, len(names)):
        name = names[i]
        wallet_payer = 3 * (i + 1) - 1
        wallet_owner = 3 * (i + 1)
        wallet_data_pubkey = 3 * (i + 1) + 1
        wallet_keys = wallet_keys_list[i]

        preorder = state_engine.get_name_preorder(
            name,
            pybitcoin.make_pay_to_address_script(wallets[wallet_payer].addr),
            wallets[wallet_owner].addr)
        if preorder is not None:
            print "still have preorder"
            return False

        # registered
        name_rec = state_engine.get_name(name)
        if name_rec is None:
            print "name does not exist"
            return False

        # owned
        if name_rec['address'] != wallets[wallet_owner].addr or name_rec[
                'sender'] != pybitcoin.make_pay_to_address_script(
                    wallets[wallet_owner].addr):
            print "name has wrong owner"
            return False

        # only serviceFoo exists
        accounts = testlib.blockstack_cli_list_accounts("foo.test")
        if len(accounts) != 1:
            print "wrong number of accounts"
            print json.dumps(accounts, indent=4, sort_keys=True)
            return False

        account = accounts[0]
        on_file_account = testlib.blockstack_cli_get_account(
            "foo.test", "serviceFoo", "serviceFooID")
        if 'error' in on_file_account:
            print json.dumps(on_file_account, sort_keys=True, indent=4)
            return False

        if account != on_file_account:
            print "wrong service\nexpected:\n%s\n\ngot:\n%s\n" % \
                    (json.dumps(account, indent=4, sort_keys=True),
                     json.dumps(on_file_account, indent=4, sort_keys=True))

            return False

        if account['identifier'] != 'serviceFooID':
            print "wrong identifier: %s" % account
            return False

        if account['contentUrl'] != 'foo://bar.com':
            print "wrong URL: %s" % account
            return False

        if account['service'] != 'serviceFoo':
            print "wrong service: %s" % account
            return False

    return True
def check( state_engine ):

    global wallet_keys, error

    if error:
        print "Key operation failed."
        return False

    # not revealed, but ready 
    ns = state_engine.get_namespace_reveal( "test" )
    if ns is not None:
        print "namespace not ready"
        return False 

    ns = state_engine.get_namespace( "test" )
    if ns is None:
        print "no namespace"
        return False 

    if ns['namespace_id'] != 'test':
        print "wrong namespace"
        return False 

    # not preordered
    names = ['foo.test']
    wallet_keys_list = [wallet_keys]
    test_proxy = testlib.TestAPIProxy()

    for i in xrange(0, len(names)):
        name = names[i]
        wallet_payer = 3 * (i+1) - 1
        wallet_owner = 3 * (i+1)
        wallet_data_pubkey = 3 * (i+1) + 1
        wallet_keys = wallet_keys_list[i]

        preorder = state_engine.get_name_preorder( name, pybitcoin.make_pay_to_address_script(wallets[wallet_payer].addr), wallets[wallet_owner].addr )
        if preorder is not None:
            print "still have preorder"
            return False
    
        # registered 
        name_rec = state_engine.get_name( name )
        if name_rec is None:
            print "name does not exist"
            return False 

        # owned 
        if name_rec['address'] != wallets[wallet_owner].addr or name_rec['sender'] != pybitcoin.make_pay_to_address_script(wallets[wallet_owner].addr):
            print "name has wrong owner"
            return False 

        # only serviceFoo exists
        accounts = testlib.blockstack_cli_list_accounts( "foo.test" )
        if len(accounts) != 1:
            print "wrong number of accounts"
            print json.dumps(accounts, indent=4, sort_keys=True)
            return False 

        account = accounts[0]
        on_file_account = testlib.blockstack_cli_get_account( "foo.test", "serviceFoo", "serviceFooID" )
        if 'error' in on_file_account:
            print json.dumps(on_file_account, sort_keys=True, indent=4)
            return False 

        if account != on_file_account:
            print "wrong service\nexpected:\n%s\n\ngot:\n%s\n" % \
                    (json.dumps(account, indent=4, sort_keys=True),
                     json.dumps(on_file_account, indent=4, sort_keys=True))

            return False
            
        if account['identifier'] != 'serviceFooID':
            print "wrong identifier: %s" % account
            return False 

        if account['contentUrl'] != 'foo://bar.com':
            print "wrong URL: %s" % account
            return False 

        if account['service'] != 'serviceFoo':
            print "wrong service: %s" % account
            return False

    return True