Beispiel #1
0
    def test_register_piece_with_editions(self):
        # create piece
        piece, editions = self.create_piece(self.user1,
                                            self.digitalwork_user1,
                                            self.thumbnail_user1,
                                            num_editions=10)

        # check edition address
        for edition in editions:
            self.assertTrue(
                self._check_address(edition.bitcoin_path, self.user1,
                                    self.password))

        # retrieve transactions
        btc_txs = BitcoinTransaction.objects.filter(ownership__piece=piece)
        btc_tx = BitcoinTransaction.objects.get(
            ownership__type='OwnershipEditions')

        # piece + editions
        self.assertEqual(len(btc_txs), 2)

        self.assertEqual(btc_tx.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx.outputs,
            [(BitcoinService.minDustSize, piece.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, piece.hash_as_address()),
             (BitcoinService.minDustSize, piece.bitcoin_id)])

        self.assertEqual(btc_tx.spoolverb, 'ASCRIBESPOOL01EDITIONS10')
        self.assertEqual(btc_tx.status, TX_PENDING)
Beispiel #2
0
    def test_register_piece_no_editions(self):
        # create piece
        piece, editions = self.create_piece(self.user1,
                                            self.digitalwork_user1,
                                            self.thumbnail_user1,
                                            num_editions=0)

        # check piece address
        self.assertTrue(
            self._check_address(piece.bitcoin_path, self.user1, self.password))

        # retrieve transaction
        btc_tx = BitcoinTransaction.objects.get(ownership__piece=piece)

        self.assertEqual(btc_tx.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx.outputs[0],
            (BitcoinService.minDustSize, piece.hash_as_address_no_metada()))
        self.assertEqual(btc_tx.outputs[1],
                         (BitcoinService.minDustSize, piece.hash_as_address()))
        self.assertEqual(btc_tx.outputs[2],
                         (BitcoinService.minDustSize, piece.bitcoin_id))

        self.assertEqual(btc_tx.spoolverb, 'ASCRIBESPOOL01PIECE')
        self.assertEqual(btc_tx.status, TX_PENDING)
Beispiel #3
0
    def test_consigned_registration(self):
        piece, editions = self.create_consigned_registration(
            self.user1,
            self.digitalwork_user1,
            self.thumbnail_user1,
            num_editions=10)
        btc_txs = BitcoinTransaction.objects.filter(ownership__piece=piece)
        btc_tx = BitcoinTransaction.objects.get(
            ownership__type='ConsignedRegistration')

        # consigned registration + editions
        self.assertEqual(len(btc_txs), 2)

        for edition in editions:
            self.assertTrue(
                self._check_address(edition.bitcoin_path, self.user1,
                                    self.password))

        self.assertEqual(btc_tx.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx.outputs,
            [(BitcoinService.minDustSize, piece.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, piece.hash_as_address()),
             (BitcoinService.minDustSize, piece.bitcoin_id)])

        self.assertEqual(btc_tx.spoolverb,
                         'ASCRIBESPOOL01CONSIGNEDREGISTRATION')
        self.assertEqual(btc_tx.status, TX_PENDING)
Beispiel #4
0
    def test_register_number_editions(self):
        # create piece
        piece, editions = self.create_piece(self.user1,
                                            self.digitalwork_user1,
                                            self.thumbnail_user1,
                                            num_editions=0)
        self.assertFalse(
            BitcoinTransaction.objects.filter(
                ownership__type='OwnershipEditions').exists())

        # create editions
        edition_task = self.create_editions(self.user1, piece, 10)
        btc_tx = BitcoinTransaction.objects.get(
            ownership__type='OwnershipEditions')

        self.assertEqual(btc_tx.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx.outputs,
            [(BitcoinService.minDustSize, piece.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, piece.hash_as_address()),
             (BitcoinService.minDustSize, piece.bitcoin_id)])

        self.assertEqual(btc_tx.spoolverb, 'ASCRIBESPOOL01EDITIONS10')
        self.assertEqual(btc_tx.status, TX_PENDING)
Beispiel #5
0
def refill_main_wallet():
    inputs = refill_main_wallet.select_chunk()
    outputs = [{
        'address': BitcoinWallet.mainAdminBtcAddress(),
        'value': settings.BTC_FEE
    }] * 50
    outputs += [{
        'address': BitcoinWallet.mainAdminBtcAddress(),
        'value': settings.BTC_TOKEN
    }] * 150
    unsigned_tx = refill_main_wallet.transactions.build_transaction(
        inputs, outputs)
    signed_tx = refill_main_wallet.transactions.sign_transaction(
        unsigned_tx, settings.BTC_REFILL_PASSWORD)
    txid = refill_main_wallet.transactions.push(signed_tx)
    logger.info('Refilling main wallet {}'.format(txid))

    return txid
Beispiel #6
0
    def test_migration_transfer(self):
        piece, editions = self.create_piece(self.user1,
                                            self.digitalwork_user1,
                                            self.thumbnail_user1,
                                            num_editions=10)
        edition = editions[0]
        self.request_reset_password(self.user1)
        self.reset_password(self.user1, self.new_password)
        self.create_transfer(self.user1, self.user2.email, edition.bitcoin_id,
                             self.new_password)

        btc_txs = BitcoinTransaction.objects.filter(ownership__piece=piece)
        btc_tx_migrate = BitcoinTransaction.objects.get(
            ownership__type='OwnershipMigration')
        btc_tx_transfer = BitcoinTransaction.objects.get(
            ownership__type='OwnershipTransfer')
        ownership_migration = OwnershipMigration.objects.get(edition=edition)
        ownership_transfer = OwnershipTransfer.objects.get(edition=edition)

        # piece, editions, register, migration, transfer
        self.assertEqual(len(btc_txs), 5)

        # check migration
        new_btc_path_migration = ownership_migration.new_btc_address
        _, new_address_migration = new_btc_path_migration.split(':')

        self.assertTrue(
            self._check_address(new_btc_path_migration, self.user1,
                                self.new_password))

        self.assertEqual(btc_tx_migrate.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx_migrate.outputs,
            [(BitcoinService.minDustSize, edition.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, edition.bitcoin_id),
             (BitcoinService.minDustSize, new_address_migration)])

        self.assertEqual(
            btc_tx_migrate.spoolverb,
            'ASCRIBESPOOL01MIGRATE{}'.format(edition.edition_number))
        self.assertEqual(btc_tx_migrate.status, TX_PENDING)

        # check transfer
        new_btc_path_transfer = ownership_transfer.new_btc_address
        _, new_address_transfer = new_btc_path_transfer.split(':')

        self.assertEqual(btc_tx_transfer.from_address, new_btc_path_migration)
        self.assertEqual(
            btc_tx_transfer.outputs,
            [(BitcoinService.minDustSize, edition.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, new_address_transfer)])

        self.assertEqual(
            btc_tx_transfer.spoolverb,
            'ASCRIBESPOOL01TRANSFER{}'.format(edition.edition_number))
        self.assertEqual(btc_tx_transfer.status, TX_PENDING)
Beispiel #7
0
def rescan():
    print 'Rescanning the blockchain. This may take several minutes...'
    transactions = Transactions(service=settings.BTC_SERVICE,
                                testnet=settings.BTC_TESTNET,
                                username=settings.BTC_USERNAME,
                                password=settings.BTC_PASSWORD,
                                host=settings.BTC_HOST,
                                port=settings.BTC_PORT)
    main_address = BitcoinWallet.mainAdminBtcAddress()
    print 'Sending rescan command to {} main address {}'.format(
        settings.BTC_HOST, main_address)
    transactions.import_address(main_address, 'mainaddress', rescan=True)
Beispiel #8
0
def wallet_status():
    print 'Checking Federation wallet status...'
    transactions = Transactions(service=settings.BTC_SERVICE, testnet=settings.BTC_TESTNET,
                                username=settings.BTC_USERNAME, password=settings.BTC_PASSWORD,
                                host=settings.BTC_HOST, port=settings.BTC_PORT)
    main_address = BitcoinWallet.mainAdminBtcAddress()
    unspents = transactions.get(main_address, min_confirmations=1)['unspents']
    fees = 0
    tokens = 0
    for u in unspents:
        if u['amount'] == settings.BTC_TOKEN:
            tokens += 1
        elif u['amount'] == settings.BTC_FEE:
            fees += 1

    print "Wallet {} has {} tokens and {} fees".format(main_address, tokens, fees)
Beispiel #9
0
    def test_transfer_edition(self):
        piece, editions = self.create_piece(self.user1,
                                            self.digitalwork_user1,
                                            self.thumbnail_user1,
                                            num_editions=10)
        edition = editions[0]
        self.create_transfer(self.user1, self.user2.email, edition.bitcoin_id,
                             self.password)

        btc_txs = BitcoinTransaction.objects.filter(ownership__piece=piece)
        btc_tx_transfer = BitcoinTransaction.objects.get(
            ownership__type='OwnershipTransfer')
        btc_tx_register = BitcoinTransaction.objects.get(
            ownership__type='OwnershipRegistration')

        # piece + editions + register + transfer
        self.assertEqual(len(btc_txs), 4)

        # check registration
        self.assertEqual(btc_tx_register.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx_register.outputs,
            [(BitcoinService.minDustSize, edition.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, edition.hash_as_address()),
             (BitcoinService.minDustSize, edition.bitcoin_id)])

        self.assertEqual(
            btc_tx_register.spoolverb,
            'ASCRIBESPOOL01REGISTER{}'.format(edition.edition_number))
        self.assertEqual(btc_tx_register.status, TX_PENDING)

        # check transfer
        self.assertTrue(
            self._check_address(edition.btc_owner_address, self.user2,
                                self.password2))

        self.assertEqual(btc_tx_transfer.from_address, edition.bitcoin_path)
        self.assertEqual(
            btc_tx_transfer.outputs,
            [(BitcoinService.minDustSize, edition.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, edition.btc_owner_address_noprefix)])

        self.assertEqual(
            btc_tx_transfer.spoolverb,
            'ASCRIBESPOOL01TRANSFER{}'.format(edition.edition_number))
        self.assertEqual(btc_tx_transfer.status, TX_PENDING)
Beispiel #10
0
    def select_inputs(self, address, nfees, ntokens, min_confirmations=6):
        # select inputs from the federation wallet
        if address == BitcoinWallet.mainAdminBtcAddress():
            cursor = connection.cursor()
            cursor.execute(query_sql_main % (ntokens, nfees))
            desc = cursor.description
            unspents = [
                dict(zip([col[0] for col in desc], row))
                for row in cursor.fetchall()
            ]
            fees = filter(lambda d: d['amount'] == settings.BTC_FEE, unspents)
            tokens = filter(lambda d: d['amount'] == settings.BTC_TOKEN,
                            unspents)
            if len(fees) != nfees or len(tokens) != ntokens:
                raise SpoolFundsError('Not enough unspents for transaction')

            return fees + tokens
        else:
            # select inputs from the user HD wallet
            return super(BackendSpool, self).select_inputs(address,
                                                           nfees,
                                                           ntokens,
                                                           min_confirmations=1)
Beispiel #11
0
def test_register_editions(registered_piece_alice):
    from bitcoin.bitcoin_service import BitcoinService
    from bitcoin.models import BitcoinTransaction, BitcoinWallet, TX_PENDING
    from ownership.models import OwnershipEditions
    from acl.models import ActionControl
    from ..models import Edition
    from ..tasks import register_editions

    # Test can be used with any number of editions.
    # We use 1 for speed reasons.
    num_editions = 1
    alice = registered_piece_alice.user_registered
    piece_alice = registered_piece_alice

    editions_tasks = register_editions(piece_alice, alice,
                                       num_editions).delay()
    assert editions_tasks.failed() is False

    # We test if the editions returned
    # were correctly inserted into the database
    editions = editions_tasks.get()

    assert all(
        Edition.objects.filter(parent=piece_alice,
                               bitcoin_path__icontains=e.bitcoin_id).exists()
        for e in editions)
    assert piece_alice.num_editions == num_editions
    assert len(editions) == num_editions

    # We test if the ACLs for the respective user
    # and their editions have been set appropriately
    acls = ActionControl.objects.filter(user=alice,
                                        piece=piece_alice,
                                        edition__in=editions)
    for acl in acls:
        assert acl.acl_view is True
        assert acl.acl_edit is True
        assert acl.acl_download is True
        assert acl.acl_delete is True
        assert acl.acl_create_editions is False
        assert acl.acl_share is True
        assert acl.acl_unshare is False
        assert acl.acl_transfer is True
        assert acl.acl_withdraw_transfer is False
        assert acl.acl_consign is True
        assert acl.acl_withdraw_consign is False
        assert acl.acl_unconsign is False
        assert acl.acl_request_unconsign is False
        assert acl.acl_loan is True
        assert acl.acl_coa is True

    # We test if an OwnershipEditions object and
    # a related BitcoinTransaction were created
    editions_ownership = OwnershipEditions.objects.get(piece=piece_alice,
                                                       new_owner=alice)
    assert editions_ownership.piece == piece_alice

    btc_tx = BitcoinTransaction.objects.get(pk=editions_ownership.btc_tx.id)
    assert btc_tx.from_address == BitcoinWallet.mainAdminBtcAddress()
    assert btc_tx.outputs == [
        (BitcoinService.minDustSize, piece_alice.hash_as_address_no_metada()),
        (BitcoinService.minDustSize, piece_alice.hash_as_address()),
        (BitcoinService.minDustSize, piece_alice.bitcoin_id)
    ]
    assert btc_tx.spoolverb == 'ASCRIBESPOOL01EDITIONS{}'.format(num_editions)
    assert btc_tx.status == TX_PENDING
Beispiel #12
0
    def test_migration_loan_piece_after_password_change(self):
        piece, editions = self.create_piece(self.user1,
                                            self.digitalwork_user1,
                                            self.thumbnail_user1,
                                            num_editions=0)

        # change password
        self.request_reset_password(self.user1)
        self.reset_password(self.user1, self.new_password)

        # create loan
        startdate = datetime.utcnow().date()
        enddate = datetime.utcnow().date() + timedelta(days=1)
        self.create_loan_piece(self.user1,
                               self.user2.email,
                               piece.id,
                               startdate=startdate,
                               enddate=enddate,
                               password=self.new_password)
        # confirm loan
        self.confirm_loan_piece(self.user2, piece.id)

        btc_txs = BitcoinTransaction.objects.filter(ownership__piece=piece)
        btc_tx_migrate = BitcoinTransaction.objects.get(
            ownership__type='OwnershipMigration')
        btc_tx_loan = BitcoinTransaction.objects.get(
            ownership__type='LoanPiece')
        ownership_migration = OwnershipMigration.objects.get(piece=piece)
        ownership_loan = LoanPiece.objects.get(piece=piece)

        # piece, migrate, loan
        self.assertEqual(len(btc_txs), 3)

        # check migration
        new_btc_path_migration = ownership_migration.new_btc_address
        _, new_address_migration = new_btc_path_migration.split(':')

        self.assertTrue(
            self._check_address(new_btc_path_migration, self.user1,
                                self.new_password))

        self.assertEqual(btc_tx_migrate.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx_migrate.outputs,
            [(BitcoinService.minDustSize, piece.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, piece.bitcoin_id),
             (BitcoinService.minDustSize, new_address_migration)])

        self.assertEqual(btc_tx_migrate.spoolverb, 'ASCRIBESPOOL01MIGRATE')
        self.assertEqual(btc_tx_migrate.status, TX_PENDING)

        # check loan
        new_btc_path_loan = ownership_loan.new_btc_address
        _, new_address_loan = new_btc_path_loan.split(':')

        self.assertEqual(btc_tx_loan.from_address, new_btc_path_migration)
        self.assertEqual(
            btc_tx_loan.outputs,
            [(BitcoinService.minDustSize, piece.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, new_address_loan)])

        self.assertEqual(
            btc_tx_loan.spoolverb,
            'ASCRIBESPOOL01LOAN/{}{}'.format(startdate.strftime('%y%m%d'),
                                             enddate.strftime('%y%m%d')))
        self.assertEqual(btc_tx_loan.status, TX_PENDING)
Beispiel #13
0
    def test_migration_unconsign(self):
        piece, editions = self.create_piece(self.user1,
                                            self.digitalwork_user1,
                                            self.thumbnail_user1,
                                            num_editions=10)
        edition = editions[0]
        self.create_consign(self.user1, self.user2.email, edition.bitcoin_id,
                            self.password)
        self.confirm_consign(self.user2, edition.bitcoin_id)

        # reset password user2
        self.request_reset_password(self.user2)
        self.reset_password(self.user2, self.new_password)

        # create unconsign
        self.create_unconsign(self.user2, edition.bitcoin_id,
                              self.new_password)

        btc_txs = BitcoinTransaction.objects.filter(ownership__piece=piece)
        btc_tx_migrate = BitcoinTransaction.objects.get(
            ownership__type='OwnershipMigration')
        btc_tx_unconsign = BitcoinTransaction.objects.get(
            ownership__type='UnConsignment')
        ownership_migration = OwnershipMigration.objects.get(edition=edition)
        ownership_consign = Consignment.objects.get(edition=edition)

        # piece, editions, register, consign, migrate, unconsign
        self.assertEqual(len(btc_txs), 6)

        # check migration
        new_btc_path_migration = ownership_migration.new_btc_address
        _, new_address_migration = new_btc_path_migration.split(':')

        _, consign_address = ownership_consign.new_btc_address.split(':')

        self.assertTrue(
            self._check_address(new_btc_path_migration, self.user2,
                                self.new_password))

        self.assertEqual(btc_tx_migrate.from_address,
                         BitcoinWallet.mainAdminBtcAddress())
        self.assertEqual(
            btc_tx_migrate.outputs,
            [(BitcoinService.minDustSize, edition.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, consign_address),
             (BitcoinService.minDustSize, new_address_migration)])

        self.assertEqual(
            btc_tx_migrate.spoolverb,
            'ASCRIBESPOOL01MIGRATE{}'.format(edition.edition_number))
        self.assertEqual(btc_tx_migrate.status, TX_PENDING)

        # check unconsign
        self.assertEqual(btc_tx_unconsign.from_address, new_btc_path_migration)
        self.assertEqual(
            btc_tx_unconsign.outputs,
            [(BitcoinService.minDustSize, edition.hash_as_address_no_metada()),
             (BitcoinService.minDustSize, edition.bitcoin_id)])

        self.assertEqual(
            btc_tx_unconsign.spoolverb,
            'ASCRIBESPOOL01UNCONSIGN{}'.format(edition.edition_number))
        self.assertEqual(btc_tx_unconsign.status, TX_PENDING)