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)
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)
def setaccount(request, bitcoinaddress, label): """ Sets the account associated with the given address. Arguments: - *bitcoinaddress* -- Bitcoin address to associate. - *account* -- Account to associate the address to. """ # Two use cases here, but only one that # we're going to honor. # 1) The user provides an address they control # and a new label to give it. In this case # we will simply update the Address object. # 2) The user provides a label they control. # The problem here is that they could give # a bitcoin address belonging to someone else # which could lead to confusion when the # address acts like an implicit joint account. # This does, of course, assume the bitcoin # client would let us do that; a likely # possibility since the backend in most cases # will probably be a single bitcoind instance. # # 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.");
def sendtoaddress(self, bitcoinaddress, amount, comment=None, comment_to=None): """ 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). - *minconf* -- Minimum number of confirmations required for transferred balance. - *comment* -- Comment for transaction. - *comment_to* -- Comment for to-address. """ try: if comment is None: return self.proxy.sendtoaddress(bitcoinaddress, amount) elif comment_to is None: return self.proxy.sendtoaddress(bitcoinaddress, amount, comment) else: return self.proxy.sendtoaddress(bitcoinaddress, amount, comment, comment_to) except JSONRPCException, e: raise _wrap_exception(e.error)
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)
def setaccount(request, bitcoinaddress, label): """ Sets the account associated with the given address. Arguments: - *bitcoinaddress* -- Bitcoin address to associate. - *account* -- Account to associate the address to. """ # Two use cases here, but only one that # we're going to honor. # 1) The user provides an address they control # and a new label to give it. In this case # we will simply update the Address object. # 2) The user provides a label they control. # The problem here is that they could give # a bitcoin address belonging to someone else # which could lead to confusion when the # address acts like an implicit joint account. # This does, of course, assume the bitcoin # client would let us do that; a likely # possibility since the backend in most cases # will probably be a single bitcoind instance. # # 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.")
def listreceivedbyaddress(request, minconf=1, includeempty=False): """ Returns a list of addresses. Each address is represented with a dictionary. Arguments: - *minconf* -- Minimum number of confirmations before payments are included. - *includeempty* -- Whether to include addresses that haven't received any payments. """ try: addresses = [] for address in Address.objects.filter(user=request.user): amount = conn.getreceivedbyaddress(address.address, minconf=minconf) if includeempty or amount > 0: addresses.append({ "address": address.address, "account": address.label, "amount": amount }) return addresses except JSONRPCException, e: raise _wrap_exception(e.error)
def sendfrom(self, fromaccount, 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. """ try: if comment is None: return self.proxy.sendfrom(fromaccount, tobitcoinaddress, amount, minconf) elif comment_to is None: return self.proxy.sendfrom(fromaccount, tobitcoinaddress, amount, minconf, comment) else: return self.proxy.sendfrom(fromaccount, tobitcoinaddress, amount, minconf, comment, comment_to) except JSONRPCException,e: raise _wrap_exception(e.error)
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)
def getinfo(self): """ Returns an :class:`~bitcoin.data.ServerInfo` object containing various state info. """ try: return ServerInfo(**self.proxy.getinfo()) except JSONRPCException,e: raise _wrap_exception(e.error)
def gethashespersec(self): """ Returns a recent hashes per second performance measurement while generating. """ try: return self.proxy.gethashespersec() except JSONRPCException,e: raise _wrap_exception(e.error)
def gethashespersec(self): """ Returns a recent hashes per second performance measurement while generating. """ try: return self.proxy.gethashespersec() except JSONRPCException, e: raise _wrap_exception(e.error)
def getconnectioncount(self): """ Returns the number of connections to other nodes. """ try: return self.proxy.getconnectioncount() except JSONRPCException,e: raise _wrap_exception(e.error)
def getblockcount(self): """ Returns the number of blocks in the longest block chain. """ try: return self.proxy.getblockcount() except JSONRPCException,e: raise _wrap_exception(e.error)
def getblocknumber(self): """ Returns the block number of the latest block in the longest block chain. """ try: return self.proxy.getblocknumber() except JSONRPCException,e: raise _wrap_exception(e.error)
def getblocknumber(self): """ Returns the block number of the latest block in the longest block chain. """ try: return self.proxy.getblocknumber() except JSONRPCException, e: raise _wrap_exception(e.error)
def stop(self): """ Stop bitcoin server. """ try: self.proxy.stop() except JSONRPCException,e: raise _wrap_exception(e.error)
def getblockcount(self): """ Returns the number of blocks in the longest block chain. """ try: return self.proxy.getblockcount() except JSONRPCException, e: raise _wrap_exception(e.error)
def getgenerate(self): """ Returns :const:`True` or :const:`False`, depending on whether generation is enabled. """ try: return self.proxy.getgenerate() except JSONRPCException,e: raise _wrap_exception(e.error)
def getconnectioncount(self): """ Returns the number of connections to other nodes. """ try: return self.proxy.getconnectioncount() except JSONRPCException, e: raise _wrap_exception(e.error)
def stop(self): """ Stop bitcoin server. """ try: self.proxy.stop() except JSONRPCException, e: raise _wrap_exception(e.error)
def getdifficulty(self): """ Returns the proof-of-work difficulty as a multiple of the minimum difficulty. """ try: return self.proxy.getdifficulty() except JSONRPCException, e: raise _wrap_exception(e.error)
def getgenerate(self): """ Returns :const:`True` or :const:`False`, depending on whether generation is enabled. """ try: return self.proxy.getgenerate() except JSONRPCException, e: raise _wrap_exception(e.error)
def getdifficulty(self): """ Returns the proof-of-work difficulty as a multiple of the minimum difficulty. """ try: return self.proxy.getdifficulty() except JSONRPCException,e: raise _wrap_exception(e.error)
def getinfo(self): """ Returns an :class:`~bitcoin.data.ServerInfo` object containing various state info. """ try: return ServerInfo(**self.proxy.getinfo()) except JSONRPCException, e: raise _wrap_exception(e.error)
def __init__(self, user, password, host='localhost', port=8332): """ Create a new bitcoin server connection. """ url = 'http://%s:%s@%s:%s/' % (user, password, host, port) try: self.proxy = ServiceProxy(url) except JSONRPCException, e: raise _wrap_exception(e.error)
def __init__(self, user, password, host='localhost', port=8332): """ Create a new bitcoin server connection. """ url = 'http://%s:%s@%s:%s/' % ( user, password, host, port ) try: self.proxy = ServiceProxy(url) except JSONRPCException,e: raise _wrap_exception(e.error)
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)
def getaccount(self, bitcoinaddress): """ Returns the account associated with the given address. Arguments: - *bitcoinaddress* -- Bitcoin address to get account for. """ try: return self.proxy.getaccount(bitcoinaddress) except JSONRPCException, e: raise _wrap_exception(e.error)
def backupwallet(self, destination): """ Safely copies ``wallet.dat`` to *destination*, which can be a directory or a path with filename. Arguments: - *destination* -- directory or path with filename to backup wallet to. """ try: return self.proxy.backupwallet(destination) except JSONRPCException,e: raise _wrap_exception(e.error)
def getaccount(self, bitcoinaddress): """ Returns the account associated with the given address. Arguments: - *bitcoinaddress* -- Bitcoin address to get account for. """ try: return self.proxy.getaccount(bitcoinaddress) except JSONRPCException,e: raise _wrap_exception(e.error)
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)
def getaddressesbyaccount(self, account): """ Returns the list of addresses for the given account. Arguments: - *account* -- Account to get list of addresses for. """ try: return self.proxy.getaddressesbyaccount(account) except JSONRPCException, e: raise _wrap_exception(e.error)
def getaddressesbyaccount(self, account): """ Returns the list of addresses for the given account. Arguments: - *account* -- Account to get list of addresses for. """ try: return self.proxy.getaddressesbyaccount(account) except JSONRPCException,e: raise _wrap_exception(e.error)
def backupwallet(self, destination): """ Safely copies ``wallet.dat`` to *destination*, which can be a directory or a path with filename. Arguments: - *destination* -- directory or path with filename to backup wallet to. """ try: return self.proxy.backupwallet(destination) except JSONRPCException, e: raise _wrap_exception(e.error)
def getaccount(request, bitcoinaddress): """ Returns the account associated with the given address. Arguments: - *bitcoinaddress* -- Bitcoin address to get account for. """ # Make sure the user owns the requested account. try: return Address.objects.get(address=bitcoinaddress, user=request.user).label except Exception, e: raise _wrap_exception(e)
def getaccountaddress(self, account): """ Returns the current bitcoin address for receiving payments to an account. Arguments: - *account* -- Account for which the address should be returned. """ try: return self.proxy.getaccountaddress(account) except JSONRPCException, e: raise _wrap_exception(e.error)
def getaccountaddress(self, account): """ Returns the current bitcoin address for receiving payments to an account. Arguments: - *account* -- Account for which the address should be returned. """ try: return self.proxy.getaccountaddress(account) except JSONRPCException,e: raise _wrap_exception(e.error)
def setaccount(self, bitcoinaddress, account): """ Sets the account associated with the given address. Arguments: - *bitcoinaddress* -- Bitcoin address to associate. - *account* -- Account to associate the address to. """ try: return self.proxy.setaccount(bitcoinaddress, account) except JSONRPCException,e: raise _wrap_exception(e.error)
def setaccount(self, bitcoinaddress, account): """ Sets the account associated with the given address. Arguments: - *bitcoinaddress* -- Bitcoin address to associate. - *account* -- Account to associate the address to. """ try: return self.proxy.setaccount(bitcoinaddress, account) except JSONRPCException, e: raise _wrap_exception(e.error)
def validateaddress(request, validateaddress): """ Validate a bitcoin address and return information for it. The information is represented by a dictionary. Arguments: - *validateaddress* -- Address to validate. """ try: return conn.validateaddress(validateaddress).__dict__ except JSONRPCException, e: raise _wrap_exception(e.error)
def getreceivedbyaddress(request, bitcoinaddress, minconf=1): """ Returns the total amount received by a bitcoin address in transactions with at least a certain number of confirmations. Arguments: - *bitcoinaddress* -- Address to query for total amount. - *minconf* -- Number of confirmations to require, defaults to 1. """ try: return str(conn.getreceivedbyaddress(bitcoinaddress, minconf)) except JSONRPCException, e: raise _wrap_exception(e.error)
def getreceivedbyaccount(self, account, 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: return self.proxy.getreceivedbyaccount(account, minconf) except JSONRPCException, e: raise _wrap_exception(e.error)
def getreceivedbyaccount(self, account, 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: return self.proxy.getreceivedbyaccount(account, minconf) except JSONRPCException,e: raise _wrap_exception(e.error)
def validateaddress(self, validateaddress): """ Validate a bitcoin address and return information for it. The information is represented by a :class:`~bitcoin.data.AddressValidation` object. Arguments: - *validateaddress* -- Address to validate. """ try: return AddressValidation(**self.proxy.validateaddress(validateaddress)) except JSONRPCException,e: raise _wrap_exception(e.error)
def getbalance(self, account=None): """ 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: if account is None: return self.proxy.getbalance() else: return self.proxy.getbalance(account) except JSONRPCException,e: raise _wrap_exception(e.error)
def getbalance(self, account=None): """ 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: if account is None: return self.proxy.getbalance() else: return self.proxy.getbalance(account) except JSONRPCException, e: raise _wrap_exception(e.error)
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)
def listreceivedbyaccount(self, minconf=1, includeempty=False): """ Returns a list of accounts. Each account is represented with a :class:`~bitcoin.data.AccountInfo` object. Arguments: - *minconf* -- Minimum number of confirmations before payments are included. - *includeempty* -- Whether to include addresses that haven't received any payments. """ try: return [AccountInfo(**x) for x in self.proxy.listreceivedbyaccount(minconf, includeempty)] except JSONRPCException,e: raise _wrap_exception(e.error)
def validateaddress(self, validateaddress): """ Validate a bitcoin address and return information for it. The information is represented by a :class:`~bitcoin.data.AddressValidation` object. Arguments: - *validateaddress* -- Address to validate. """ try: return AddressValidation( **self.proxy.validateaddress(validateaddress)) except JSONRPCException, e: raise _wrap_exception(e.error)
def listtransactions(self, account, count=10): """ Returns a list of the last transactions for an account. Each transaction is represented with a :class:`~bitcoin.data.TransactionInfo` object. Arguments: - *minconf* -- Minimum number of confirmations before payments are included. - *count* -- Number of transactions to return. """ try: return [TransactionInfo(**x) for x in self.proxy.listtransactions(account, count)] except JSONRPCException,e: raise _wrap_exception(e.error)
def getnewaddress(self, account=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. """ try: if account is None: return self.proxy.getnewaddress() else: return self.proxy.getnewaddress(account) except JSONRPCException, e: raise _wrap_exception(e.error)
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)