def test_btc_wallet(self):
        """
        Test the creating, opening, transactions and balance query of a Bitcoin wallet
        """
        wallet = BitcoinWallet(self.session_base_dir, testnet=True)

        def on_wallet_transactions(transactions):
            self.assertFalse(transactions)

            wallet.get_transactions = lambda: succeed([{"id": "abc"}])
            return wallet.monitor_transaction("abc")

        def on_wallet_balance(balance):
            self.assertDictEqual(balance, {
                'available': 0,
                'pending': 0,
                'currency': 'BTC'
            })
            return wallet.get_transactions().addCallback(
                on_wallet_transactions)

        def on_wallet_created(_):
            self.assertIsNotNone(wallet.wallet)
            self.assertTrue(wallet.get_address())

            _ = BitcoinWallet(self.session_base_dir, testnet=True)
            wallet.set_wallet_password('abc')
            self.assertRaises(Exception,
                              BitcoinWallet,
                              self.session_base_dir,
                              testnet=True)

            return wallet.get_balance().addCallback(on_wallet_balance)

        return wallet.create_wallet('tribler').addCallback(on_wallet_created)
    def test_btc_wallet_transfer_no_funds(self):
        """
        Test that the transfer method of a BTC wallet raises an error when we don't have enough funds
        """
        test_deferred = Deferred()

        wallet = BitcoinWallet(self.session_base_dir)
        mock_daemon = MockObject()
        wallet.get_daemon = lambda: mock_daemon

        wallet.transfer(
            3, 'abacd').addErrback(lambda _: test_deferred.callback(None))
        return test_deferred
    def test_btc_balance_no_wallet(self):
        """
        Test the retrieval of the balance of a BTC wallet that is not created yet
        """
        def on_wallet_balance(balance):
            self.assertDictEqual(balance, {
                'available': 0,
                'pending': 0,
                'currency': 'BTC'
            })

        wallet = BitcoinWallet(self.session_base_dir)
        return wallet.get_balance().addCallback(on_wallet_balance)
    def load_market_community_in_session(self, session, market_member,
                                         mc_community):
        """
        Load the market community and tradechain community in a given session.
        """
        wallets = {
            'BTC':
            BitcoinWallet(
                os.path.join(session.config.get_state_dir(), 'wallet')),
            'MC':
            TrustchainWallet(mc_community),
            'DUM1':
            DummyWallet1(),
            'DUM2':
            DummyWallet2()
        }
        wallets['MC'].check_negative_balance = False

        dispersy = session.get_dispersy_instance()

        # Load MarketCommunity
        market_kargs = {'tribler_session': session, 'wallets': wallets}
        self.market_communities[session] = dispersy.define_auto_load(
            MarketCommunityTests, market_member, kargs=market_kargs,
            load=True)[0]
        return self.market_communities[session]
 def test_get_transactions(self):
     wallet = BitcoinWallet(self.session_base_dir)
     mock_daemon = MockObject()
     mock_server = MockObject()
     transactions = [{
         'value': -1,
         'txid': 'a',
         'timestamp': 1
     }, {
         'value': 1,
         'txid': 'a',
         'timestamp': 1
     }]
     mock_server.run_cmdline = lambda _: transactions
     mock_daemon.get_server = lambda _: mock_server
     wallet.get_daemon = lambda: mock_daemon
     return wallet.get_transactions()
    def test_btc_wallet_transfer(self):
        """
        Test that the transfer method of a BTC wallet
        """
        def mocked_run_cmdline(request):
            if request['cmd'] == 'payto':
                return {'hex': 'abcd'}
            elif request['cmd'] == 'broadcast':
                return True, 'abcd'

        wallet = BitcoinWallet(self.session_base_dir)
        mock_daemon = MockObject()
        mock_server = MockObject()
        mock_server.run_cmdline = mocked_run_cmdline
        mock_daemon.get_server = lambda _: mock_server
        wallet.get_daemon = lambda: mock_daemon
        wallet.get_balance = lambda: succeed({'available': 5})

        return wallet.transfer(3, 'abacd')
        def on_wallet_created(_):
            self.assertIsNotNone(wallet.wallet)
            self.assertTrue(wallet.get_address())

            _ = BitcoinWallet(self.session_base_dir, testnet=True)
            wallet.set_wallet_password('abc')
            self.assertRaises(Exception,
                              BitcoinWallet,
                              self.session_base_dir,
                              testnet=True)

            return wallet.get_balance().addCallback(on_wallet_balance)
Example #8
0
    def load_communities(self):
        self._logger.info("tribler: Preparing communities...")
        now_time = timemod.time()
        default_kwargs = {'tribler_session': self.session}

        # Search Community
        if self.session.config.get_torrent_search_enabled():
            from Tribler.community.search.community import SearchCommunity
            self.dispersy.define_auto_load(SearchCommunity,
                                           self.session.dispersy_member,
                                           load=True,
                                           kargs=default_kwargs)

        # AllChannel Community
        if self.session.config.get_channel_search_enabled():
            from Tribler.community.allchannel.community import AllChannelCommunity
            self.dispersy.define_auto_load(AllChannelCommunity,
                                           self.session.dispersy_member,
                                           load=True,
                                           kargs=default_kwargs)

        # Channel Community
        if self.session.config.get_channel_community_enabled():
            from Tribler.community.channel.community import ChannelCommunity
            self.dispersy.define_auto_load(ChannelCommunity,
                                           self.session.dispersy_member,
                                           load=True,
                                           kargs=default_kwargs)

        # PreviewChannel Community
        if self.session.config.get_preview_channel_community_enabled():
            from Tribler.community.channel.preview import PreviewChannelCommunity
            self.dispersy.define_auto_load(PreviewChannelCommunity,
                                           self.session.dispersy_member,
                                           kargs=default_kwargs)

        # Tunnel Community
        mc_community = None
        if self.session.config.get_tunnel_community_enabled():
            tunnel_settings = TunnelSettings(tribler_session=self.session)
            tunnel_kwargs = {
                'tribler_session': self.session,
                'settings': tunnel_settings
            }

            if self.session.config.get_trustchain_enabled():
                trustchain_kwargs = {'tribler_session': self.session}

                # If the trustchain is enabled, we use the permanent trustchain keypair
                # for both the trustchain and the tunnel community
                keypair = self.session.trustchain_keypair
                dispersy_member = self.dispersy.get_member(
                    private_key=keypair.key_to_bin())

                from Tribler.community.triblerchain.community import TriblerChainCommunity
                mc_community = self.dispersy.define_auto_load(
                    TriblerChainCommunity,
                    dispersy_member,
                    load=True,
                    kargs=trustchain_kwargs)[0]

            else:
                keypair = self.dispersy.crypto.generate_key(u"curve25519")
                dispersy_member = self.dispersy.get_member(
                    private_key=self.dispersy.crypto.key_to_bin(keypair))

            from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity
            self.tunnel_community = self.dispersy.define_auto_load(
                HiddenTunnelCommunity,
                dispersy_member,
                load=True,
                kargs=tunnel_kwargs)[0]

            # We don't want to automatically load other instances of this community with other master members.
            self.dispersy.undefine_auto_load(HiddenTunnelCommunity)

        # Use the permanent TrustChain ID for Market community/TradeChain if it's available
        if self.session.config.get_market_community_enabled():
            wallets = {}
            btc_wallet = BitcoinWallet(
                os.path.join(self.session.config.get_state_dir(), 'wallet'),
                testnet=self.session.config.get_btc_testnet())
            wallets[btc_wallet.get_identifier()] = btc_wallet

            mc_wallet = TrustchainWallet(mc_community)
            wallets[mc_wallet.get_identifier()] = mc_wallet

            if self.session.config.get_dummy_wallets_enabled():
                # For debugging purposes, we create dummy wallets
                dummy_wallet1 = DummyWallet1()
                wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

                dummy_wallet2 = DummyWallet2()
                wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

            from Tribler.community.market.community import MarketCommunity
            keypair = self.session.tradechain_keypair
            dispersy_member = self.dispersy.get_member(
                private_key=keypair.key_to_bin())

            market_kwargs = {
                'tribler_session': self.session,
                'wallets': wallets
            }
            self.market_community = self.dispersy.define_auto_load(
                MarketCommunity,
                dispersy_member,
                load=True,
                kargs=market_kwargs)[0]

        self.session.config.set_anon_proxy_settings(
            2,
            ("127.0.0.1",
             self.session.config.get_tunnel_community_socks5_listen_ports()))

        self._logger.info("tribler: communities are ready in %.2f seconds",
                          timemod.time() - now_time)
 def test_btc_wallet_unit(self):
     """
     Test the mininum unit of a Bitcoin wallet
     """
     wallet = BitcoinWallet(self.session_base_dir)
     self.assertEqual(wallet.min_unit(), 0.0001)
Example #10
0
 def test_btc_wallet_address(self):
     """
     Test the address of a Bitcoin wallet
     """
     wallet = BitcoinWallet(self.session_base_dir)
     self.assertEqual(wallet.get_address(), '')
Example #11
0
 def test_btc_wallet_identfier(self):
     """
     Test the identifier of a Bitcoin wallet
     """
     wallet = BitcoinWallet(self.session_base_dir)
     self.assertEqual(wallet.get_identifier(), 'BTC')
Example #12
0
 def test_btc_wallet_name(self):
     """
     Test the name of a Bitcoin wallet
     """
     wallet = BitcoinWallet(self.session_base_dir)
     self.assertEqual(wallet.get_name(), 'Bitcoin')