Example #1
0
def on_unconsignment_create(sender, instance, password, *args, **kwargs):
    # Create bitcoin unconsign transaction
    unconsign = BitcoinTransaction.unconsign(instance)

    # before pushing the transaction we need to check:
    # 1. check if the edition needs migration
    # 2. the edition address is refilled

    # check if edition needs migration
    migration = check_migration(instance)
    if migration:
        instance.prev_btc_address = migration.new_btc_address
        instance.btc_tx = None
        instance.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(instance, password)
        instance.save()

        # delete old btc_tx which has the wrong addresses and create a new one
        unconsign.delete()
        unconsign = BitcoinTransaction.unconsign(instance)

    # refill the edition address
    # create the transaction
    refill = BitcoinTransaction.refill(instance)
    # set the unconsign as the dependent transaction so that it is sent after the refill by the
    # transaction_monitor
    refill.dependent_tx = unconsign
    refill.save()
    tasks.refill.delay(refill.id, util.mainAdminPassword())
Example #2
0
def on_migration_created(sender, instance, created, *args, **kwargs):
    if created:
        # Create bitcoin transaction
        # we need to check if its for an edition or piece
        if instance.edition:
            transaction = BitcoinTransaction.migrate(instance)
        else:
            transaction = BitcoinTransaction.migrate_piece(instance)

        # migration of a piece or edition
        tasks.migrate.delay(transaction.id, util.mainAdminPassword())
Example #3
0
def on_ownership_registration_create(sender, instance, created, *args, **kwargs):
    if created:
        # Create bitcoin transaction
        transaction = BitcoinTransaction.register(instance)

        # register the edition
        tasks.register.delay(transaction.id, util.mainAdminPassword())
Example #4
0
def on_ownership_editions_create(sender, instance, created, *args, **kwargs):
    if created:
        # Create bitcoin transaction
        transaction = BitcoinTransaction.editions(instance)

        # register number of editions
        tasks.editions.delay(transaction.id, util.mainAdminPassword())
Example #5
0
def on_ownership_piece_create(sender, instance, created, *args, **kwargs):
    if created:
        # Create bitcoin transaction
        transaction = BitcoinTransaction.register_piece(instance)

        # register piece
        tasks.register_piece.delay(transaction.id, util.mainAdminPassword())
Example #6
0
def on_consigned_registration_create(sender, instance, created, *args, **kwargs):
    if created:
        # Create bitcoin transaction
        transaction = BitcoinTransaction.consigned_registration(instance)

        # consigned registeration of a piece
        tasks.consigned_registration.delay(transaction.id, util.mainAdminPassword())
Example #7
0
def on_loan_piece_confirmed(sender, instance, *args, **kwargs):
    # Create bitcoin consign transaction
    loan_piece = BitcoinTransaction.loan_piece(instance)

    # before pushing the transaction we need to check:
    # 1. the piece is already registered
    # 2. the piece address is refilled

    # check if edition is registered
    # unlike editions a piece is registered at the time of creation

    # refill the piece address
    # create the transaction
    refill = BitcoinTransaction.refill(instance)
    # set the loan as the dependent transaction so that it is sent after the refill by the
    # transaction_monitor
    refill.dependent_tx = loan_piece
    refill.save()
    tasks.refill.delay(refill.id, util.mainAdminPassword())
Example #8
0
def on_ownership_transfer_create(sender, instance, password, *args, **kwargs):
    # Create bitcoin transfer transaction
    transfer = BitcoinTransaction.transfer(instance)

    # if a user needs to register and there is no wallet yet no transaction will be created
    if transfer:

        # before pushing the transaction we need to check:
        # 1. the edition is already registered (because of lazy editions)
        # 2. check if an edition needs migration (due to a password change)
        # 3. the edition address is refilled

        # check if edition is registered
        registration = ownership_models.OwnershipRegistration.objects.filter(edition=instance.edition)
        if not registration:
            registration = ownership_models.OwnershipRegistration.create(edition=instance.edition,
                                                                         new_owner=instance.edition.owner)
            registration.save()

        # check if edition needs migration
        migration = check_migration(instance)
        if migration:
            instance.prev_btc_address = migration.new_btc_address
            instance.btc_tx = None
            instance.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(instance, password)
            instance.save()

            # delete old btc_tx which has the wrong addresses and create a new one
            transfer.delete()
            transfer = BitcoinTransaction.transfer(instance)

        # refill the edition address
        # create the transaction
        refill = BitcoinTransaction.refill(instance)
        # set the transfer as the dependent transaction so that it is sent after the refill by the
        # transaction_monitor
        refill.dependent_tx = transfer
        refill.save()
        tasks.refill.delay(refill.id, util.mainAdminPassword())
Example #9
0
def execute_pending_actions(sender, user, *args, **kwargs):
    # check for pending actions when a user logins for the first time
    ownership_transfers = ownership_models.OwnershipTransfer.objects.filter(new_owner=user)

    for ownership_transfer in ownership_transfers:
        ownership_transfer.edition.pending_new_owner = None
        ownership_transfer.edition.owner = ownership_transfer.new_owner
        ownership_transfer.edition.save()
        ownership_transfer.save()
        acl = ActionControl.objects.get(user=ownership_transfer.prev_owner,
                                        piece=ownership_transfer.edition.parent,
                                        edition=ownership_transfer.edition)
        acl.acl_withdraw_transfer = False
        acl.acl_unshare = True
        acl.save()

        # create the transaction
        transfer = BitcoinTransaction.transfer(ownership_transfer)

        # before pushing the transaction we need to check:
        # 1. the edition is already registered (because of lazy editions)
        # 2. the edition address is refilled

        # check if edition is registered
        registration = ownership_models.OwnershipRegistration.objects.filter(edition=ownership_transfer.edition)
        if not registration:
            registration = ownership_models.OwnershipRegistration.create(edition=ownership_transfer.edition,
                                                                         new_owner=ownership_transfer.edition.owner)
            registration.save()

        # refill the edition address
        # create the transaction
        refill = BitcoinTransaction.refill(ownership_transfer)
        # set the transfer as the dependent transaction so that it is sent after the refill by the
        # transaction_monitor
        refill.dependent_tx = transfer
        refill.save()
        tasks.refill.delay(refill.id, util.mainAdminPassword())
Example #10
0
def on_loan_edition_confirmed(sender, instance, *args, **kwargs):
    # Create bitcoin consign transaction
    loan = BitcoinTransaction.loan(instance)

    # before pushing the transaction we need to check:
    # 1. the edition is already registered (because of lazy editions)
    # 2. the edition address is refilled

    # check if edition is registered
    registration = ownership_models.OwnershipRegistration.objects.filter(edition=instance.edition)
    if not registration:
        registration = ownership_models.OwnershipRegistration.create(edition=instance.edition,
                                                                     new_owner=instance.edition.owner)
        registration.save()

    # refill the edition address
    # create the transaction
    refill = BitcoinTransaction.refill(instance)
    # set the loan as the dependent transaction so that it is sent after the refill by the
    # transaction_monitor
    refill.dependent_tx = loan
    refill.save()
    tasks.refill.delay(refill.id, util.mainAdminPassword())
Example #11
0
    def testCreateBitcoinTransaction(self):
        user = self.user1
        from_wallet = BitcoinWallet.create(self.user1, password=self.password)
        from_wallet.save()

        save_bitcointransaction = BitcoinTransaction.create(
            user=user,
            from_address=from_wallet.address,
            outputs=[(int(1 * BitcoinService.minTransactionSize),
                      BTC_TEST_ADDRESS)],
            spoolverb='ascribespoolverbtest')
        save_bitcointransaction.save()

        find_bitcointransaction = BitcoinTransaction.objects.get(
            id=save_bitcointransaction.id)
        self.assertTrue(save_bitcointransaction == find_bitcointransaction)
Example #12
0
    def test_acl_edit_of_a_retrieved_transferred_edition(self):
        from dynamicfixtures import (
            _djroot_user,
            _alice,
            _bob,
            _bob_bitcoin_wallet,
            _registered_edition_pair_alice,
            _whitelabel_merlin,
        )
        from bitcoin import tasks
        from bitcoin.models import BitcoinTransaction, BitcoinWallet
        from ownership.models import OwnershipRegistration, OwnershipTransfer
        from util import util
        _djroot_user()
        alice, bob = _alice(), _bob()
        _bob_bitcoin_wallet()
        edition_one, edition_two = _registered_edition_pair_alice()

        # TODO Extract, and/or simplify to the essentials.
        #
        # What are the essentials?
        # - Having two editions.
        # - The two editions belong to the same piece.
        # - The piece has been registered by alice.
        # - One edition has been transferred to bob.
        # - The transferred edition should have its acl_edit set accordingly.
        #
        # So, it may very well be possible to avoid going through all the
        # transfer related operations. Waht matters is that the transferred
        # edition has its fields set like it would have if it would have been
        # transferred. Related objects, which are created and/or modified
        # during a transfer may alos need to be created.
        OwnershipRegistration.objects.create(
            edition=edition_one,
            new_owner=edition_one.owner,
            piece=edition_one.parent,
            type=OwnershipRegistration.__name__,
        )
        transfer = OwnershipTransfer(
            edition=edition_one,
            prev_owner=edition_one.owner,
            new_owner=bob,
            prev_btc_address=None,
            piece=edition_one.parent,
            type=OwnershipTransfer.__name__,
        )
        transfer.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(
            transfer,
            'alice-secret',
        )
        transfer.save()
        transfer_tx = BitcoinTransaction.transfer(transfer)
        refill = BitcoinTransaction.refill(transfer)
        refill.dependent_tx = transfer_tx
        refill.save()
        tasks.refill(refill.id, util.mainAdminPassword())
        edition_one.owner = bob
        edition_one.save()
        # END of transfer

        whitelabel = _whitelabel_merlin()
        subdomain = whitelabel.subdomain
        view = MarketEditionEndpoint.as_view({'get': 'retrieve'})

        url = reverse(
            'api:whitelabel:market:edition-detail',
            kwargs={'domain_pk': subdomain, 'pk': edition_two.bitcoin_id},
        )
        factory = APIRequestFactory()
        request = factory.get(url)
        force_authenticate(request, user=alice)

        response = view(
            request, pk=edition_two.bitcoin_id, domain_pk=subdomain)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertFalse(response.data['edition']['acl']['acl_edit'])
        self.assertTrue(response.data['edition']['acl']['acl_wallet_submit'])