Beispiel #1
0
def test_maybe_schedule_xpub_derivation(task_manager, database):
    xpub = 'xpub68V4ZQQ62mea7ZUKn2urQu47Bdn2Wr7SxrBxBDDwE3kjytj361YBGSKDT4WoBrE5htrSB8eAMe59NPnKrcAbiv2veN5GQUmfdjRddD1Hxrk'  # noqa: E501
    xpub_data = XpubData(
        xpub=HDKey.from_xpub(xpub=xpub, path='m'),
        derivation_path='m/0/0',
    )
    database.add_bitcoin_xpub(xpub_data)
    task_manager.potential_tasks = [
        task_manager._maybe_schedule_xpub_derivation
    ]
    xpub_derive_patch = patch(
        'rotkehlchen.chain.bitcoin.xpub.XpubManager.check_for_new_xpub_addresses',
        return_value=None,
    )

    timeout = 4
    try:
        with gevent.Timeout(timeout):
            with xpub_derive_patch as xpub_mock:
                task_manager.schedule()
                while True:
                    if xpub_mock.call_count == 1:
                        break
                    gevent.sleep(.2)

    except gevent.Timeout as e:
        raise AssertionError(
            f'xpub derivation query was not scheduled within {timeout} seconds'
        ) from e  # noqa: E501
Beispiel #2
0
def test_xpub_data_comparison():
    hdkey1 = HDKey.from_xpub(
        'xpub6DCi5iJ57ZPd5qPzvTm5hUt6X23TJdh9H4NjNsNbt7t7UuTMJfawQWsdWRFhfLwkiMkB1rQ4ZJWLB9YBnzR7kbs9N8b2PsKZgKUHQm1X4or'
    )  # noqa: E501
    hdkey2 = HDKey.from_xpub(
        'xpub68V4ZQQ62mea7ZUKn2urQu47Bdn2Wr7SxrBxBDDwE3kjytj361YBGSKDT4WoBrE5htrSB8eAMe59NPnKrcAbiv2veN5GQUmfdjRddD1Hxrk'
    )  # noqa: E501
    xpubdata1 = XpubData(xpub=hdkey1)
    xpubdata2 = XpubData(xpub=hdkey2)
    mapping = {xpubdata1: 1}
    assert not (xpubdata1 == xpubdata2
                )  # there is a reason for both queries. In the first
    assert xpubdata1 != xpubdata2  # implementation they did not both work correctly
    assert xpubdata1 in mapping
    assert xpubdata2 not in mapping

    xpubdata1 = XpubData(xpub=hdkey1)
    xpubdata2 = XpubData(xpub=hdkey1)
    assert xpubdata1 == xpubdata2
    assert not (xpubdata1 != xpubdata2)

    xpubdata1 = XpubData(xpub=hdkey1, derivation_path='m')
    xpubdata2 = XpubData(xpub=hdkey1, derivation_path='m/0/0')
    assert xpubdata1 != xpubdata2
    assert not (xpubdata1 == xpubdata2)
Beispiel #3
0
 def patch(
     self,
     xpub: 'HDKey',
     derivation_path: Optional[str],
     label: Optional[str],
     tags: Optional[List[str]],
 ) -> Response:
     return self.rest_api.edit_xpub(xpub_data=XpubData(
         xpub=xpub,
         derivation_path=derivation_path,
         label=label,
         tags=tags,
     ), )
Beispiel #4
0
 def delete(
     self,
     xpub: 'HDKey',
     derivation_path: Optional[str],
     async_query: bool,
 ) -> Response:
     return self.rest_api.delete_xpub(
         xpub_data=XpubData(
             xpub=xpub,
             derivation_path=derivation_path,
             label=None,
             tags=None,
         ),
         async_query=async_query,
     )
Beispiel #5
0
 def put(
     self,
     xpub: 'HDKey',
     derivation_path: Optional[str],
     label: Optional[str],
     tags: Optional[List[str]],
     async_query: bool,
 ) -> Response:
     return self.rest_api.add_xpub(
         xpub_data=XpubData(
             xpub=xpub,
             derivation_path=derivation_path,
             label=label,
             tags=tags,
         ),
         async_query=async_query,
     )
Beispiel #6
0
def test_edit_bitcoin_xpub_not_existing_tag(setup_db_for_xpub_tests):
    """Test that edits bitcoin xpub label and tries to add non existing tag"""
    db, xpub, _, _, _ = setup_db_for_xpub_tests

    with pytest.raises(InputError):
        db.edit_bitcoin_xpub(
            XpubData(
                xpub=xpub.xpub,
                derivation_path=xpub.derivation_path,
                label='123',
                tags=['test'],
            ))

    result = db.get_bitcoin_xpub_data()

    assert result[1].xpub == xpub.xpub
    assert result[1].label == xpub.label
    assert result[1].derivation_path == xpub.derivation_path
    assert result[1].tags != {'test'}
Beispiel #7
0
def test_get_last_xpub_derived_indices(setup_db_for_xpub_tests):
    db, xpub1, xpub2, xpub3, _ = setup_db_for_xpub_tests
    # Get index from xpubs existing in the DB that have derived addresses
    receiving_idx, change_idx = db.get_last_xpub_derived_indices(xpub1)
    assert receiving_idx == 1
    assert change_idx == 0
    receiving_idx, change_idx = db.get_last_xpub_derived_indices(xpub2)
    assert receiving_idx == 0
    assert change_idx == 3
    # Get index from xpubs existing in the DB that have no derived addresses
    receiving_idx, change_idx = db.get_last_xpub_derived_indices(xpub3)
    assert receiving_idx == change_idx == 0
    # Get index from unknown xpub (not in DB)
    xpub = 'xpub6D1ZRhLSRWWGowFT22WJYYJx3GH5wxidsHcEm6NYeXfMAGxKWiQ5dQ8hSz7gdJsE86Lrjf1MN7SCKowZU8VxZ45Z1KeNP5CZ514JbCamRdC'  # noqa: E501
    derivation_path = 'm/0/0/0'
    xpub_data = XpubData(xpub=HDKey.from_xpub(xpub=xpub),
                         derivation_path=derivation_path)
    receiving_idx, change_idx = db.get_last_xpub_derived_indices(xpub_data)
    assert receiving_idx == change_idx == 0
Beispiel #8
0
def test_edit_bitcoin_xpub(setup_db_for_xpub_tests):
    """Test that editing bitcoin xpub label and tags"""
    db, xpub, _, _, _ = setup_db_for_xpub_tests

    db.add_tag('test',
               description="test",
               background_color='000000',
               foreground_color='111111')

    db.edit_bitcoin_xpub(
        XpubData(
            xpub=xpub.xpub,
            derivation_path=xpub.derivation_path,
            label='123',
            tags=['test'],
        ))

    result = db.get_bitcoin_xpub_data()

    assert result[1].xpub == xpub.xpub
    assert result[1].label == '123'
    assert result[1].derivation_path == xpub.derivation_path
    assert set(result[1].tags) == {'test'}
Beispiel #9
0
def setup_db_for_xpub_tests(data_dir, username):
    msg_aggregator = MessagesAggregator()
    data = DataHandler(data_dir, msg_aggregator)
    data.unlock(username, '123', create_new=True)

    data.db.add_tag('public', 'foooo', 'ffffff', '000000')
    data.db.add_tag('desktop', 'boooo', 'ffffff', '000000')

    xpub = 'xpub68V4ZQQ62mea7ZUKn2urQu47Bdn2Wr7SxrBxBDDwE3kjytj361YBGSKDT4WoBrE5htrSB8eAMe59NPnKrcAbiv2veN5GQUmfdjRddD1Hxrk'  # noqa: E501
    derivation_path = 'm/0/0/0'
    xpub_data1 = XpubData(
        xpub=HDKey.from_xpub(xpub=xpub, path='m'),
        derivation_path=derivation_path,
        label='xpub1',
        tags=['public', 'desktop'],
    )
    data.db.ensure_tags_exist([xpub_data1], action='adding', data_type='bitcoin_xpub')
    insert_tag_mappings(    # if we got tags add them to the xpub
        cursor=data.db.conn.cursor(),
        data=[xpub_data1],
        object_reference_keys=['xpub.xpub', 'derivation_path'],
    )

    data.db.add_bitcoin_xpub(xpub_data1)
    addr1 = '1LZypJUwJJRdfdndwvDmtAjrVYaHko136r'
    addr2 = '1MKSdDCtBSXiE49vik8xUG2pTgTGGh5pqe'
    addr3 = '12wxFzpjdymPk3xnHmdDLCTXUT9keY3XRd'
    addr4 = '16zNpyv8KxChtjXnE5nYcPqcXcrSQXX2JW'
    all_addresses = [addr1, addr2, addr3, addr4]
    account_data = [BlockchainAccountData(x) for x in [addr1, addr2, addr3, addr4]]
    data.db.add_blockchain_accounts(
        blockchain=SupportedBlockchain.BITCOIN,
        account_data=account_data,
    )
    insert_tag_mappings(    # if we got tags add them to the existing addresses too
        cursor=data.db.conn.cursor(),
        data=account_data,
        object_reference_keys=['address'],
    )
    data.db.ensure_xpub_mappings_exist(
        xpub=xpub,
        derivation_path=derivation_path,
        derived_addresses_data=[
            XpubDerivedAddressData(0, 0, addr1, ZERO),
            XpubDerivedAddressData(0, 1, addr2, ZERO),
        ],
    )

    xpub = 'zpub6quTRdxqWmerHdiWVKZdLMp9FY641F1F171gfT2RS4D1FyHnutwFSMiab58Nbsdu4fXBaFwpy5xyGnKZ8d6xn2j4r4yNmQ3Yp3yDDxQUo3q'  # noqa: E501
    derivation_path = 'm/0'
    xpub_data2 = XpubData(
        xpub=HDKey.from_xpub(xpub=xpub, path='m'),
        derivation_path=derivation_path,
    )
    data.db.add_bitcoin_xpub(xpub_data2)
    addr1 = 'bc1qc3qcxs025ka9l6qn0q5cyvmnpwrqw2z49qwrx5'
    addr2 = 'bc1qnus7355ecckmeyrmvv56mlm42lxvwa4wuq5aev'
    addr3 = 'bc1qup7f8g5k3h5uqzfjed03ztgn8hhe542w69wc0g'
    addr4 = 'bc1qr4r8vryfzexvhjrx5fh5uj0s2ead8awpqspqra'
    all_addresses.extend([addr1, addr2, addr3, addr4])
    data.db.add_blockchain_accounts(
        blockchain=SupportedBlockchain.BITCOIN,
        account_data=[BlockchainAccountData(x) for x in [addr1, addr2, addr3, addr4]],
    )
    data.db.ensure_xpub_mappings_exist(
        xpub=xpub,
        derivation_path=derivation_path,
        derived_addresses_data=[
            XpubDerivedAddressData(1, 0, addr1, ZERO),
            XpubDerivedAddressData(1, 1, addr2, ZERO),
            XpubDerivedAddressData(1, 2, addr3, ZERO),
            XpubDerivedAddressData(1, 3, addr4, ZERO),
        ],
    )

    # Finally also add the same xpub as xpub1 with no derivation path
    xpub = 'xpub68V4ZQQ62mea7ZUKn2urQu47Bdn2Wr7SxrBxBDDwE3kjytj361YBGSKDT4WoBrE5htrSB8eAMe59NPnKrcAbiv2veN5GQUmfdjRddD1Hxrk'  # noqa: E501
    derivation_path = None
    xpub_data3 = XpubData(
        xpub=HDKey.from_xpub(xpub=xpub, path='m'),
        derivation_path=derivation_path,
    )
    data.db.add_bitcoin_xpub(xpub_data3)

    return data.db, xpub_data1, xpub_data2, xpub_data3, all_addresses