def scenario(wallets, **kw):
    resp = testlib.blockstack_cli_namespace_preorder("test",
                                                     wallets[0].privkey,
                                                     wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    resp = testlib.blockstack_cli_namespace_reveal(
        'test', wallets[0].privkey, wallets[1].privkey, 52560, 4, 4,
        '6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0', 10, 10)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    resp = testlib.blockstack_cli_namespace_ready('test', wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)
def scenario( wallets, **kw ):
    resp = testlib.blockstack_cli_namespace_preorder("test", wallets[0].privkey, wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    resp = testlib.blockstack_cli_namespace_reveal('test', wallets[0].privkey, wallets[1].privkey, 52560, 4, 4, '6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0', 10, 10)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    resp = testlib.blockstack_cli_namespace_ready('test', wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)
def scenario( wallets, **kw ):

    # has the side-effect of starting the API server
    resp = testlib.blockstack_client_initialize_wallet( "0123456789abcdef", wallets[2].privkey, wallets[3].privkey, wallets[4].privkey )
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_namespace_preorder("test", wallets[0].privkey, wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    resp = testlib.blockstack_cli_namespace_reveal('test', wallets[0].privkey, wallets[1].privkey, 52560, 4, 4, '6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0', 10, 10)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    private_keychain = keychain.PrivateKeychain.from_private_key( wallets[1].privkey )
    private_keys = [wallets[1].privkey]     # NOTE: always start with the reveal key, then use children
    for i in xrange(0, 4):
        import_key = private_keychain.child(i).private_key()

        print "fund {} (child {})".format(import_key, i)
        res = testlib.send_funds( wallets[1].privkey, 100000000, virtualchain.BitcoinPrivateKey(import_key).public_key().address() )
        if 'error' in res:
            print json.dumps(res, indent=4, sort_keys=True)
            return False

        testlib.next_block(**kw)
        private_keys.append(import_key)

    # should fail (first key must be revealer)
    resp = testlib.blockstack_cli_name_import("fail.test", wallets[2].addr, "Hello fail.test!", private_keys[1])
    if 'error' not in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    # should succeed
    resp = testlib.blockstack_cli_name_import("foo.test", wallets[2].addr, "Hello foo.test!", private_keys[0])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)
   
    # 3 in one block (zero-conf)
    resp = testlib.blockstack_cli_name_import("bar.test", wallets[3].addr, "Hello bar.test!", private_keys[1])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_name_import("baz.test", wallets[4].addr, "Hello baz.test!", private_keys[2])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_name_import("goo.test", wallets[5].addr, "Hello goo.test!", private_keys[3])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    # should fail (wrong key)
    resp = testlib.blockstack_cli_name_import("fail.test", wallets[5].addr, "Hello fail.test!", wallets[2].privkey)
    if 'error' not in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_namespace_ready('test', wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    for i in xrange(0, 10):
        testlib.next_block(**kw)

    print "Waiting 10 seconds for registrar to replicate zone files"
    time.sleep(10)
def scenario(wallets, **kw):

    # has the side-effect of starting the API server
    resp = testlib.blockstack_client_initialize_wallet("0123456789abcdef",
                                                       wallets[2].privkey,
                                                       wallets[3].privkey,
                                                       wallets[4].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_namespace_preorder("test",
                                                     wallets[0].privkey,
                                                     wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    resp = testlib.blockstack_cli_namespace_reveal(
        'test', wallets[0].privkey, wallets[1].privkey, 52560, 4, 4,
        '6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0', 10, 10)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    private_keychain = keychain.PrivateKeychain.from_private_key(
        wallets[1].privkey)
    private_keys = [
        wallets[1].privkey
    ]  # NOTE: always start with the reveal key, then use children
    for i in xrange(0, 4):
        import_key = private_keychain.child(i).private_key()

        print "fund {} (child {})".format(import_key, i)
        res = testlib.send_funds(
            wallets[1].privkey, 100000000,
            virtualchain.BitcoinPrivateKey(import_key).public_key().address())
        if 'error' in res:
            print json.dumps(res, indent=4, sort_keys=True)
            return False

        testlib.next_block(**kw)
        private_keys.append(import_key)

    # should fail (first key must be revealer)
    resp = testlib.blockstack_cli_name_import("fail.test", wallets[2].addr,
                                              "Hello fail.test!",
                                              private_keys[1])
    if 'error' not in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    # should succeed
    resp = testlib.blockstack_cli_name_import("foo.test", wallets[2].addr,
                                              "Hello foo.test!",
                                              private_keys[0])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    # 3 in one block (zero-conf)
    resp = testlib.blockstack_cli_name_import("bar.test", wallets[3].addr,
                                              "Hello bar.test!",
                                              private_keys[1])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_name_import("baz.test", wallets[4].addr,
                                              "Hello baz.test!",
                                              private_keys[2])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_name_import("goo.test", wallets[5].addr,
                                              "Hello goo.test!",
                                              private_keys[3])
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    testlib.next_block(**kw)

    # should fail (wrong key)
    resp = testlib.blockstack_cli_name_import("fail.test", wallets[5].addr,
                                              "Hello fail.test!",
                                              wallets[2].privkey)
    if 'error' not in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    resp = testlib.blockstack_cli_namespace_ready('test', wallets[1].privkey)
    if 'error' in resp:
        print json.dumps(resp, indent=4, sort_keys=True)
        return False

    for i in xrange(0, 10):
        testlib.next_block(**kw)

    print "Waiting 10 seconds for registrar to replicate zone files"
    time.sleep(10)
def scenario( wallets, **kw ):
   
    res = testlib.blockstack_cli_namespace_preorder('test', wallets[0].privkey, wallets[1].privkey)
    if 'error' in res:
        print res
        return False
    
    test_txid = res['transaction_hash']

    res = testlib.blockstack_cli_namespace_preorder('testm', wallets[5].privkey, wallets[2].privkey)
    if 'error' in res:
        print res
        return False

    testm_txid = res['transaction_hash']
    
    # should fail (wrong key)
    res = testlib.blockstack_cli_namespace_preorder('testf', wallets[3].privkey, wallets[6].privkey)
    if 'error' not in res:
        print res
        return False

    testlib.next_block(**kw)

    # should fail (wrong reveal key)
    res = testlib.blockstack_cli_namespace_reveal('test', wallets[0].privkey, wallets[2].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10)
    if 'error' not in res:
        print res
        return False
     
    # should fail (wrong reveal key)
    res = testlib.blockstack_cli_namespace_reveal('test', wallets[5].privkey, wallets[3].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10)
    if 'error' not in res:
        print res
        return False

    # should fail (wrong preorder key)
    res = testlib.blockstack_cli_namespace_reveal('test', wallets[2].privkey, wallets[1].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10)
    if 'error' not in res:
        print res
        return False
    
    # should fail (wrong preorder key)
    res = testlib.blockstack_cli_namespace_reveal('test', wallets[6].privkey, wallets[2].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10)
    if 'error' not in res:
        print res
        return False

    # should fail (wrong namespace)
    res = testlib.blockstack_cli_namespace_reveal('test2', wallets[0].privkey, wallets[1].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10)
    if 'error' not in res:
        print res
        return False

    # should fail (wrong namespace)
    res = testlib.blockstack_cli_namespace_reveal('test2', wallets[5].privkey, wallets[2].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10)
    if 'error' not in res:
        print res
        return False

    # should fail (wrong preorder txid)
    res = testlib.blockstack_cli_namespace_reveal('test', wallets[0].privkey, wallets[1].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10, preorder_txid=testm_txid)
    if 'error' not in res:
        print res
        return False

    # should fail (wrong preorder txid)
    res = testlib.blockstack_cli_namespace_reveal('test', wallets[5].privkey, wallets[2].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10, preorder_txid=test_txid)
    if 'error' not in res:
        print res
        return False

    # should all fail (wrong key order)
    for w in invalid_multisig_wallets:
        res = testlib.blockstack_cli_namespace_reveal('testm', w.privkey, wallets[2].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10)
        if 'error' not in res:
            print res
            return False
    
    # should succeed
    res = testlib.blockstack_cli_namespace_reveal('test', wallets[0].privkey, wallets[1].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10, preorder_txid=test_txid)
    if 'error' in res:
        print res
        return False
   
    # should succeed
    res = testlib.blockstack_cli_namespace_reveal('testm', wallets[5].privkey, wallets[2].privkey, 52595, 250, 4, "6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0", 10, 10, preorder_txid=testm_txid)
    if 'error' in res:
        print res
        return False

    testlib.next_block(**kw)

    # should fail (wrong key)
    res = testlib.blockstack_cli_namespace_ready('test', wallets[2].privkey)
    if 'error' not in res:
        print res
        return False

    # should fail (wrong key)
    res = testlib.blockstack_cli_namespace_ready('testm', wallets[1].privkey)
    if 'error' not in res:
        print res
        return False

    # should succeed
    res = testlib.blockstack_cli_namespace_ready('test', wallets[1].privkey)
    if 'error' in res:
        print res
        return False

    # should succeed
    res = testlib.blockstack_cli_namespace_ready('testm', wallets[2].privkey)
    if 'error' in res:
        print res
        return False

    testlib.next_block( **kw )