Exemple #1
0
def listtransactions(request, label, count=10):
    """
    Returns a list of the last transactions for an account.
    
    Each transaction is represented with a dictionary.
    
    Arguments:
    
    - *minconf* -- Minimum number of confirmations before payments are included.
    - *count* -- Number of transactions to return.

    """
    try:
        clean_transactions = []
        if (label == "*"):
            transactions = []
            addresses = Address.objects.filter(user=request.user)
            for address in addresses:
                my_transactions = conn.listtransactions(util.getaccount(request.user, address.label), count)
                transactions.extend(my_transactions)
        else:
            transactions = conn.listtransactions(util.getaccount(request.user, label), count)
            
        for transaction in transactions:
            transaction.account = util.getdisplayname(transaction.account)
            if hasattr(transaction, "otheraccount"):
                transaction.otheraccount = util.getdisplayname(transaction.otheraccount)
                
            clean_transactions.append(transaction.__dict__)
        return clean_transactions
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #2
0
def listtransactions(request, label, count=10):
    """
    Returns a list of the last transactions for an account.
    
    Each transaction is represented with a dictionary.
    
    Arguments:
    
    - *minconf* -- Minimum number of confirmations before payments are included.
    - *count* -- Number of transactions to return.

    """
    try:
        clean_transactions = []
        if (label == "*"):
            transactions = []
            addresses = Address.objects.filter(user=request.user)
            for address in addresses:
                my_transactions = conn.listtransactions(
                    util.getaccount(request.user, address.label), count)
                transactions.extend(my_transactions)
        else:
            transactions = conn.listtransactions(
                util.getaccount(request.user, label), count)

        for transaction in transactions:
            transaction.account = util.getdisplayname(transaction.account)
            if hasattr(transaction, "otheraccount"):
                transaction.otheraccount = util.getdisplayname(
                    transaction.otheraccount)

            clean_transactions.append(transaction.__dict__)
        return clean_transactions
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #3
0
def move(request, fromlabel, tolabel, amount, minconf=1, comment=None):
    """
    Move from one account in your wallet to another.
    
    Arguments:
    
    - *fromlabel* -- Source account name.
    - *tolabel* -- Destination account name.
    - *amount* -- Amount to transfer.
    - *minconf* -- Minimum number of confirmations required for transferred balance.
    - *comment* -- Comment to add to transaction log.
    
    """
    try:
        # Make sure the requested accounts exist.
        fromaccount = util.getaccount(request.user, fromlabel)
        try:
            fromaddress = Address.objects.get(user=request.user, label=fromlabel)
        except ObjectDoesNotExist:
            raise _wrap_exception("Could not find account \"%s\"" % fromlabel)
        
        
        toaccount = util.getaccount(request.user, tolabel)
        try:
            toaddress = Address.objects.get(user=request.user, label=tolabel)
        except ObjectDoesNotExist:
            raise _wrap_exception("Could not find account \"%s\"" % tolabel)
        
        if comment is None:
            return conn.move(fromaccount, toaccount, amount, minconf)
        else:
            return conn.move(fromaccount, toaccount, amount, minconf, comment)
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #4
0
def move(request, fromlabel, tolabel, amount, minconf=1, comment=None):
    """
    Move from one account in your wallet to another.
    
    Arguments:
    
    - *fromlabel* -- Source account name.
    - *tolabel* -- Destination account name.
    - *amount* -- Amount to transfer.
    - *minconf* -- Minimum number of confirmations required for transferred balance.
    - *comment* -- Comment to add to transaction log.
    
    """
    try:
        # Make sure the requested accounts exist.
        fromaccount = util.getaccount(request.user, fromlabel)
        try:
            fromaddress = Address.objects.get(user=request.user,
                                              label=fromlabel)
        except ObjectDoesNotExist:
            raise _wrap_exception("Could not find account \"%s\"" % fromlabel)

        toaccount = util.getaccount(request.user, tolabel)
        try:
            toaddress = Address.objects.get(user=request.user, label=tolabel)
        except ObjectDoesNotExist:
            raise _wrap_exception("Could not find account \"%s\"" % tolabel)

        if comment is None:
            return conn.move(fromaccount, toaccount, amount, minconf)
        else:
            return conn.move(fromaccount, toaccount, amount, minconf, comment)
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #5
0
def sendfrom(request,
             fromlabel,
             tobitcoinaddress,
             amount,
             minconf=1,
             comment=None,
             comment_to=None):
    """
    Sends amount from account's balance to bitcoinaddress. This method will fail 
    if there is less than amount bitcoins with minconf confirmations in the account's 
    balance (unless account is the empty-string-named default account; it 
    behaves like the sendtoaddress method). Returns transaction ID on success.
    
    Arguments:
    
    - *fromaccount* -- Account to send from.
    - *tobitcoinaddress* -- Bitcoin address to send to.
    - *amount* -- Amount to send (float, rounded to the nearest 0.01).
    - *minconf* -- Minimum number of confirmations required for transferred balance.
    - *comment* -- Comment for transaction.
    - *comment_to* -- Comment for to-address.

    """
    # See if the address we are sending to exists in our database.
    # If so, use move. If not, use the requested method.
    if Address.objects.filter(address=tobitcoinaddress).count() > 0:
        # Increase the balance of the address we're sending to
        # immediately, since it's on our server.
        toaddress = Address.objects.get(address=tobitcoinaddress)
        toaccount = util.getaccount(toaddress.user, toaddress.label)

        fromaccount = util.getaccount(request.user, fromlabel)
        try:
            fromaddress = Address.objects.get(user=request.user,
                                              label=fromlabel)
        except ObjectDoesNotExist:
            raise _wrap_exception("Could not find account \"%s\"" % fromlabel)

        # Use the "move" method instead.
        if comment is None:
            return conn.move(fromaccount, toaccount, amount, minconf)
        else:
            return conn.move(fromaccount, toaccount, amount, minconf, comment)
    else:
        try:
            if comment is None:
                return conn.sendfrom(fromaccount, tobitcoinaddress, amount,
                                     minconf)
            elif comment_to is None:
                return conn.sendfrom(fromaccount, tobitcoinaddress, amount,
                                     minconf, comment)
            else:
                return conn.sendfrom(fromaccount, tobitcoinaddress, amount,
                                     minconf, comment, comment_to)
        except JSONRPCException, e:
            raise _wrap_exception(e.error)
Exemple #6
0
def sendtoaddress(request, bitcoinaddress, amount, comment=None, comment_to=None, minconf=0):
    """
    Sends *amount* from the server's available balance to *bitcoinaddress*.
    
    Arguments:
    
    - *bitcoinaddress* -- Bitcoin address to send to.
    - *amount* -- Amount to send (float, rounded to the nearest 0.01).
    - *comment* -- Comment for transaction.
    - *comment_to* -- Comment for to-address.

    """
    # Set the "toaccount" to None. It will only
    # get set to a value if the user is doing a
    # local transfer.
    toaccount = None
    
    # Get the user's primary bitcoin address.
    fromaddress = Address.objects.get(user=request.user, is_primary=True)
    fromaccount = util.getaccount(request.user, fromaddress.label)
    
    # See if the user is using a username or account indicator to
    # send the bitcoins. If so, resolve the address using that.
    if len(bitcoinaddress) <= MAX_USERNAME_LENGTH or bitcoinaddress.find("+") > -1:
        username, label = util.getusername_and_label(bitcoinaddress)
        toaccount = util.getaccount(username, label)
    
    # See if the address we are sending to exists in our database.
    # If so, use move. If not, use the requested method. 
    if Address.objects.filter(address=bitcoinaddress).count() > 0:
        # Increase the balance of the address we're sending to
        # immediately, since it's on our server.
        toaddress = Address.objects.get(address=bitcoinaddress)
        toaccount = util.getaccount(toaddress.user, toaddress.label)
        
    if toaccount != None:
        # Use the "move" method instead.
        if comment is None:
            return conn.move(fromaccount, toaccount, amount, minconf)
        else:
            return conn.move(fromaccount, toaccount, amount, minconf, comment)
    else:
        # We don't want to actually "sendtoaddress" since that would result in
        # an amount being moved from some unknown account.
        try:
            if comment is None:
                return conn.sendfrom(fromaccount, bitcoinaddress, amount, minconf)
            elif comment_to is None:
                return conn.sendfrom(fromaccount, bitcoinaddress, amount, minconf, comment)
            else:
                return conn.sendfrom(fromaccount, bitcoinaddress, amount, minconf, comment, comment_to)
        except JSONRPCException, e:
            raise _wrap_exception(e.error)
Exemple #7
0
def listaccounts(request):
    result = {}
    for address in Address.objects.filter(user=request.user):
        result[address.label] = conn.getbalance(
            util.getaccount(request.user, address.label))

    return result
Exemple #8
0
def sendfrom(request, fromlabel, tobitcoinaddress, amount, minconf=1, comment=None, comment_to=None):
    """
    Sends amount from account's balance to bitcoinaddress. This method will fail 
    if there is less than amount bitcoins with minconf confirmations in the account's 
    balance (unless account is the empty-string-named default account; it 
    behaves like the sendtoaddress method). Returns transaction ID on success.
    
    Arguments:
    
    - *fromaccount* -- Account to send from.
    - *tobitcoinaddress* -- Bitcoin address to send to.
    - *amount* -- Amount to send (float, rounded to the nearest 0.01).
    - *minconf* -- Minimum number of confirmations required for transferred balance.
    - *comment* -- Comment for transaction.
    - *comment_to* -- Comment for to-address.

    """ 
    # See if the address we are sending to exists in our database.
    # If so, use move. If not, use the requested method. 
    if Address.objects.filter(address=tobitcoinaddress).count() > 0:
        # Increase the balance of the address we're sending to
        # immediately, since it's on our server.
        toaddress = Address.objects.get(address=tobitcoinaddress)
        toaccount = util.getaccount(toaddress.user, toaddress.label)
        
        fromaccount = util.getaccount(request.user, fromlabel)
        try:
            fromaddress = Address.objects.get(user=request.user, label=fromlabel)
        except ObjectDoesNotExist:
            raise _wrap_exception("Could not find account \"%s\"" % fromlabel)
        
        # Use the "move" method instead.
        if comment is None:
            return conn.move(fromaccount, toaccount, amount, minconf)
        else:
            return conn.move(fromaccount, toaccount, amount, minconf, comment)
    else:
        try:
            if comment is None:
                return conn.sendfrom(fromaccount, tobitcoinaddress, amount, minconf)
            elif comment_to is None:
                return conn.sendfrom(fromaccount, tobitcoinaddress, amount, minconf, comment)
            else:
                return conn.sendfrom(fromaccount, tobitcoinaddress, amount, minconf, comment, comment_to)
        except JSONRPCException, e:
            raise _wrap_exception(e.error)
Exemple #9
0
def getbalance(request, label=None, minconf=0):
    """
    Get the current balance, either for an account or the total server balance.
    
    Arguments:
    - *account* -- If this parameter is specified, returns the balance in the account.

    """
    try:
        return str(conn.getbalance(util.getaccount(request.user, label)))
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #10
0
def getbalance(request, label=None, minconf=0):
    """
    Get the current balance, either for an account or the total server balance.
    
    Arguments:
    - *account* -- If this parameter is specified, returns the balance in the account.

    """
    try:
        return str(conn.getbalance(util.getaccount(request.user, label)))
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #11
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()
Exemple #12
0
def getreceivedbyaccount(request, label, minconf=1):
    """
    Returns the total amount received by addresses with an account in transactions with 
    at least a certain number of confirmations.
    
    Arguments:
    
    - *account* -- Account to query for total amount.
    - *minconf* -- Number of confirmations to require, defaults to 1.

    """
    try:
        account = util.getaccount(request.user, label)
        return str(conn.getreceivedbyaccount(account, minconf))
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #13
0
def getreceivedbyaccount(request, label, minconf=1):
    """
    Returns the total amount received by addresses with an account in transactions with 
    at least a certain number of confirmations.
    
    Arguments:
    
    - *account* -- Account to query for total amount.
    - *minconf* -- Number of confirmations to require, defaults to 1.

    """
    try:
        account = util.getaccount(request.user, label)
        return str(conn.getreceivedbyaccount(account, minconf))
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #14
0
def getaccountaddress(request, label=""):
    """
    Returns the current bitcoin address for receiving payments to an account.
    
    Arguments:
    
    - *account* -- Account for which the address should be returned.

    """
    # To make sure this isn't redundant
    # with getnewaddress, we'll let
    # this throw an exception if the
    # account does not exist.
    try:
        return conn.getaccountaddress(util.getaccount(request.user, label))
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #15
0
def getaccountaddress(request, label=""):
    """
    Returns the current bitcoin address for receiving payments to an account.
    
    Arguments:
    
    - *account* -- Account for which the address should be returned.

    """
    # To make sure this isn't redundant
    # with getnewaddress, we'll let
    # this throw an exception if the
    # account does not exist.
    try:
        return conn.getaccountaddress(util.getaccount(request.user, label))
    except JSONRPCException, e:
        raise _wrap_exception(e.error)
Exemple #16
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)
Exemple #17
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)
Exemple #18
0
 def accountName(self):
     if self.label == "":
         return getaccount(self.user, self.label)
Exemple #19
0
def listaccounts(request):
    result = {}
    for address in Address.objects.filter(user=request.user):
        result[address.label] = conn.getbalance(util.getaccount(request.user, address.label))
        
    return result
Exemple #20
0
 def accountName(self):
     if self.label == "":
         return getaccount(self.user, self.label)
Exemple #21
0
    # Grab the address object associated with bitcoinaddress.
    try:
        address = Address.objects.get(user=request.user,
                                      address=bitcoinaddress)
    except ObjectDoesNotExist, e:
        raise _wrap_exception("You are not the known owner of this address.")

    # Make sure there aren't any other addresses trying to
    # use this label. This may not be orthogonal to the
    # bitcoin json-rpc API, but it seems to fit tcatm's
    # js-remote client, which is all we're worried about
    # for this first release.
    if Address.objects.filter(user=request.user, label=label).count() == 0:
        # Throws an exception if it fails.
        result = conn.setaccount(bitcoinaddress,
                                 util.getaccount(request.user, label))

        # Looks like the update went well on
        # bitcoind's side. Update our db object.
        address.label = label
        address.save()
    else:
        raise _wrap_exception('Address with label already exists!')

    return result


@basicauth()
@jsonrpc_method('getaccount')
def getaccount(request, bitcoinaddress):
    """
Exemple #22
0
def sendtoaddress(request,
                  bitcoinaddress,
                  amount,
                  comment=None,
                  comment_to=None,
                  minconf=0):
    """
    Sends *amount* from the server's available balance to *bitcoinaddress*.
    
    Arguments:
    
    - *bitcoinaddress* -- Bitcoin address to send to.
    - *amount* -- Amount to send (float, rounded to the nearest 0.01).
    - *comment* -- Comment for transaction.
    - *comment_to* -- Comment for to-address.

    """
    # Set the "toaccount" to None. It will only
    # get set to a value if the user is doing a
    # local transfer.
    toaccount = None

    # Get the user's primary bitcoin address.
    fromaddress = Address.objects.get(user=request.user, is_primary=True)
    fromaccount = util.getaccount(request.user, fromaddress.label)

    # See if the user is using a username or account indicator to
    # send the bitcoins. If so, resolve the address using that.
    if len(bitcoinaddress) <= MAX_USERNAME_LENGTH or bitcoinaddress.find(
            "+") > -1:
        username, label = util.getusername_and_label(bitcoinaddress)
        toaccount = util.getaccount(username, label)

    # See if the address we are sending to exists in our database.
    # If so, use move. If not, use the requested method.
    if Address.objects.filter(address=bitcoinaddress).count() > 0:
        # Increase the balance of the address we're sending to
        # immediately, since it's on our server.
        toaddress = Address.objects.get(address=bitcoinaddress)
        toaccount = util.getaccount(toaddress.user, toaddress.label)

    if toaccount != None:
        # Use the "move" method instead.
        if comment is None:
            return conn.move(fromaccount, toaccount, amount, minconf)
        else:
            return conn.move(fromaccount, toaccount, amount, minconf, comment)
    else:
        # We don't want to actually "sendtoaddress" since that would result in
        # an amount being moved from some unknown account.
        try:
            if comment is None:
                return conn.sendfrom(fromaccount, bitcoinaddress, amount,
                                     minconf)
            elif comment_to is None:
                return conn.sendfrom(fromaccount, bitcoinaddress, amount,
                                     minconf, comment)
            else:
                return conn.sendfrom(fromaccount, bitcoinaddress, amount,
                                     minconf, comment, comment_to)
        except JSONRPCException, e:
            raise _wrap_exception(e.error)
Exemple #23
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()
Exemple #24
0
    # We're going to ignore use case #2.
    
    # Grab the address object associated with bitcoinaddress.
    try:
        address = Address.objects.get(user=request.user, address=bitcoinaddress)
    except ObjectDoesNotExist, e:
        raise _wrap_exception("You are not the known owner of this address.");
    
    # Make sure there aren't any other addresses trying to
    # use this label. This may not be orthogonal to the
    # bitcoin json-rpc API, but it seems to fit tcatm's
    # js-remote client, which is all we're worried about
    # for this first release.
    if Address.objects.filter(user=request.user, label=label).count() == 0:
        # Throws an exception if it fails.
        result = conn.setaccount(bitcoinaddress, util.getaccount(request.user, label))
        
        # Looks like the update went well on
        # bitcoind's side. Update our db object.
        address.label = label
        address.save()
    else:
        raise _wrap_exception('Address with label already exists!')
    
    return result
    
@basicauth()
@jsonrpc_method('getaccount')
def getaccount(request, bitcoinaddress):
    """
    Returns the account associated with the given address.