Example #1
0
def check_address(address):
    """
    verify that a string is an address

    >>> check_address('16EMaNw3pkn3v6f2BgnSSs53zAKH4Q8YJg')
    True
    >>> check_address('16EMaNw3pkn3v6f2BgnSSs53zAKH4Q8YJh')
    False
    >>> check_address('mkkJsS22dnDJhD8duFkpGnHNr9uz3JEcWu')
    True
    >>> check_address('mkkJsS22dnDJhD8duFkpGnHNr9uz3JEcWv')
    False
    >>> check_address('MD8WooqTKmwromdMQfSNh8gPTPCSf8KaZj')
    True
    >>> check_address('SSXMcDiCZ7yFSQSUj7mWzmDcdwYhq97p2i')
    True
    >>> check_address('SSXMcDiCZ7yFSQSUj7mWzmDcdwYhq97p2j')
    False
    >>> check_address('16SuThrz')
    False
    >>> check_address('1TGKrgtrQjgoPjoa5BnUZ9Qu')
    False
    >>> check_address('1LPckRbeTfLjzrfTfnCtP7z2GxFTpZLafXi')
    True
    """
    if not check_string(
            address, min_length=26, max_length=35, pattern=OP_ADDRESS_PATTERN):
        return False

    try:
        keylib.b58check_decode(address)
        return True
    except:
        return False
Example #2
0
 def test_b58check_encode_then_decode(self):
     bin_private_key = self.ref['hex_private_key'].decode('hex')
     wif_private_key = b58check_encode(
         bin_private_key, version_byte=self.ref['wif_version_byte'])
     self.assertEqual(self.ref['wif_private_key'], wif_private_key)
     bin_private_key_verification = b58check_decode(wif_private_key)
     self.assertEqual(bin_private_key_verification, bin_private_key)
def addr_to_p2wpkh(addr):
    """
    Convert a p2pkh address into a p2wpkh script
    """
    if not virtualchain.btc_is_p2pkh_address(addr):
        raise ValueError("Not a valid p2pkh address: {}".format(addr))

    hash160 = keylib.b58check_decode(addr)
    return '0014' + hash160.encode('hex')
def scenario(wallets, **kw):

    start_subdomain_registrar()

    testlib.blockstack_namespace_preorder("test", wallets[1].addr,
                                          wallets[0].privkey)
    testlib.next_block(**kw)

    testlib.blockstack_namespace_reveal(
        "test", wallets[1].addr, 52595, 250, 4,
        [6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10, 10,
        wallets[0].privkey)
    testlib.next_block(**kw)

    testlib.blockstack_namespace_ready("test", wallets[1].privkey)
    testlib.next_block(**kw)

    testlib.blockstack_name_preorder("personal.test", wallets[2].privkey,
                                     wallets[3].addr)
    testlib.next_block(**kw)

    # make an initial subdomain so we record the registrar
    zf_subdomain_template = "$ORIGIN {}\n$TTL 3600\n{}"
    zf_subdomain_default_url = '_https._tcp URI 10 1 "https://raw.githubusercontent.com/nobody/content/profile.md"'

    first_subdomain_zf = zf_subdomain_template.format(
        "first.personal.test", zf_subdomain_default_url)
    first_subdomain_txt = subdomains.make_subdomain_txt(
        "first.personal.test", "personal.test",
        virtualchain.address_reencode(wallets[0].addr, network='mainnet'), 0,
        first_subdomain_zf, wallets[0].privkey)

    registrar_zf = '$ORIGIN personal.test\n$TTL 3600\n%s\n_registrar URI 10 1 "http://localhost:%s"\n_resolver URI 10 1 "http://localhost:%s"\n' % (
        first_subdomain_txt, registrar_port, registrar_port)
    registrar_zf_hash = storage.get_zonefile_data_hash(registrar_zf)

    testlib.blockstack_name_register("personal.test",
                                     wallets[2].privkey,
                                     wallets[3].addr,
                                     zonefile_hash=registrar_zf_hash)
    testlib.next_block(**kw)

    testlib.blockstack_put_zonefile(registrar_zf)
    testlib.next_block(**kw)

    zf_template = "$ORIGIN {}\n$TTL 3600\n{}"
    zf_default_url = '_https._tcp URI 10 1 "https://raw.githubusercontent.com/nobody/content/profile.md"'

    # request to register hello_XXX.personal.test
    for i in range(0, 50):
        sub_name = 'hello_{}.personal.test'.format(i)
        sub_zf = zf_template.format(sub_name, zf_default_url)

        addr = keylib.b58check_encode('{:040x}'.format(
            int(keylib.b58check_decode(wallets[0].addr).encode('hex'), 16) +
            i).decode('hex'),
                                      version_byte=0)

        req_json = {
            'name': 'hello_{}'.format(i),
            'owner_address': virtualchain.address_reencode(addr,
                                                           network='mainnet'),
            'zonefile': sub_zf,
        }

        resp = requests.post(
            'http://localhost:{}/register'.format(registrar_port),
            json=req_json,
            headers={'Authorization': 'bearer test_registrar'})
        if resp.status_code != 202:
            print 'did not accept {}'.format(sub_name)
            print resp.text
            return False

    # try to resolve each name on the subdomain registrar
    for i in range(0, 50):
        sub_name = 'hello_{}'.format(i)
        resp = requests.get('http://localhost:{}/status/{}'.format(
            registrar_port, sub_name))
        status = resp.json()

        print json.dumps(status, indent=4, sort_keys=True)

        if resp.status_code != 200:
            print 'not accepted: {}'.format(sub_name)
            return False

    # test /v1/names/{} emulation on the subdomain registrar
    for i in range(0, 50):
        sub_name = 'hello_{}.personal.test'.format(i)
        resp = requests.get('http://localhost:{}/v1/names/{}'.format(
            registrar_port, sub_name))
        status = resp.json()

        print json.dumps(status, indent=4, sort_keys=True)

        if 'pending_subdomain' != status['status']:
            print 'not pending 1.1: {}'.format(sub_name)
            return False

        if len(status['last_txid']) != 0:
            print 'not pending 1.2: {}'.format(sub_name)
            return False

    # test /v1/names/{} redirect from Blockstack Core
    for i in range(0, 50):
        sub_name = 'hello_{}.personal.test'.format(i)
        resp = requests.get('http://localhost:{}/v1/names/{}'.format(
            16268, sub_name))
        status = resp.json()

        print json.dumps(status, indent=4, sort_keys=True)

        if 'pending_subdomain' != status['status']:
            print 'not pending 2.1: {}'.format(sub_name)
            return False

        if len(status['last_txid']) != 0:
            print 'not pending 2.2: {}'.format(sub_name)
            return False

    # tell the registrar to flush the queue
    resp = requests.post(
        'http://localhost:{}/issue_batch/'.format(registrar_port),
        headers={'Authorization': 'bearer hello_world'})
    if resp.status_code >= 300:
        print 'issue_batch returned {}'.format(resp.status_code)
        return False

    print 'wait for subdomain registrar to send the tx'
    time.sleep(60)

    testlib.next_block(**kw)
    testlib.next_block(**kw)
    testlib.next_block(**kw)
    testlib.next_block(**kw)
    testlib.next_block(**kw)
    testlib.next_block(**kw)
    testlib.next_block(**kw)

    # tell the registrar to send the zonefiles
    resp = requests.post(
        'http://localhost:{}/check_zonefiles/'.format(registrar_port),
        headers={'Authorization': 'bearer hello_world'})
    if resp.status_code >= 300:
        print 'issue_batch returned {}'.format(resp.status_code)
        return False

    print 'wait for zonefile to propagate'
    time.sleep(5)
    testlib.next_block(**kw)

    # make sure that /v1/names/{} works without redirect
    for i in range(0, 3):
        sub_name = 'hello_{}.personal.test'.format(i)
        resp = requests.get('http://localhost:{}/v1/names/{}'.format(
            16268, sub_name))
        if resp.status_code != 200:
            print 'invalid status code {}'.format(resp.status_code)
            return False

        status = resp.json()

        print json.dumps(status, indent=4, sort_keys=True)

        if 'registered_subdomain' != status['status']:
            print 'still pending 3.1: {}'.format(sub_name)
            return False

        if len(status['last_txid']) == 0:
            print 'still pending 2.2: {}'.format(sub_name)
            return False