示例#1
0
def getnewaddress(request, label=None):
    """
    Returns a new bitcoin address for receiving payments.
    
    Arguments:

    - *account* -- If account is specified (recommended), it is added to the address book
      so that payments received with the address will be credited to it.

    """
    # Create the label for the address.
    if label is None:
        label = DEFAULT_ADDRESS_LABEL

    try:
        # Does this address already exist?
        # If so, we'll just return that object.
        # Note that this query is case-insensitive.
        addressObj = Address.objects.get(user=request.user,
                                         label__iexact=label)
    except ObjectDoesNotExist:
        try:
            # Create user's address in bitcoind.
            # Throws an exception if it fails.
            address = conn.getnewaddress(util.getaccount(request.user, label))

            # Create the corresponding Address object.
            addressObj = Address(user=request.user,
                                 address=address,
                                 label=label)

            # Does the user have any other addresses?
            # If not, this should be set as their primary.
            if Address.objects.filter(user=request.user).count() == 0:
                addressObj.primary = True

            # Save the new address to the database.
            addressObj.save()
        except JSONRPCException, e:
            raise _wrap_exception(e.error)
示例#2
0
    def save(self):
        username = self.cleaned_data["username"]
        email = self.cleaned_data["email"]
        password = self.cleaned_data["password1"]
        conn = bitcoind.connect_to_local()

        if self.cleaned_data["confirmation_key"]:
            from friends.models import JoinInvitation  # @@@ temporary fix for issue 93
            try:
                join_invitation = JoinInvitation.objects.get(
                    confirmation_key=self.cleaned_data["confirmation_key"])
                confirmed = True
            except JoinInvitation.DoesNotExist:
                confirmed = False
        else:
            confirmed = False

        # @@@ clean up some of the repetition below -- DRY!

        if confirmed:
            if email == join_invitation.contact.email:
                new_user = User.objects.create_user(username, email, password)
                join_invitation.accept(
                    new_user
                )  # should go before creation of EmailAddress below
                new_user.message_set.create(message=ugettext(
                    u"Your email address has already been verified"))
                # already verified so can just create
                EmailAddress(user=new_user,
                             email=email,
                             verified=True,
                             primary=True).save()
            else:
                new_user = User.objects.create_user(username, "", password)
                join_invitation.accept(
                    new_user
                )  # should go before creation of EmailAddress below
                if email:
                    new_user.message_set.create(message=ugettext(
                        u"Confirmation email sent to %(email)s") %
                                                {'email': email})
                    EmailAddress.objects.add_email(new_user, email)
        else:
            new_user = User.objects.create_user(username, "", password)
            if email:
                new_user.message_set.create(
                    message=ugettext(u"Confirmation email sent to %(email)s") %
                    {'email': email})
                EmailAddress.objects.add_email(new_user, email)

        if settings.ACCOUNT_EMAIL_VERIFICATION:
            new_user.is_active = False
            new_user.save()

        # Create their bitcoin account.
        # By default we will be creating an account with only their username.
        # Subsequent accounts made via the JSON-RPC interface will be in the
        # format username+label, where username is their username and label
        # is the label passed in by the JSON-RPC call.
        address = conn.getnewaddress(util.getaccount(username, ""))
        addressObj = Address(user=new_user,
                             address=address,
                             label="",
                             is_primary=True)
        addressObj.save()

        return username, password  # required for authenticate()