1 # -*- coding: utf-8 -*-
2 # Spesmilo -- Python Bitcoin user interface
3 # Copyright © 2011 Luke Dashjr
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 only.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from jsonrpc.proxy import JSONRPCException
21 def __init__(self, uri):
25 if hasattr(self, 'access'):
27 basea = jsonrpc.ServiceProxy(self.uri)
29 for access in (basea.bitcoin.__getattr__('1'), basea.bitcoin.v1, basea):
31 info = access.getinfo()
32 except JSONRPCException:
34 if not 'rpcversion' in info:
35 info['rpcversion'] = 0
36 if not info['rpcversion'] in (0, 1):
38 self.rpcversion = info['rpcversion']
43 def _fromAmount(self, n):
44 if self.rpcversion == 0:
45 return int(round(n * 100000000))
51 def transactions(self, *args):
52 txl = self._access().listtransactions(*args)
54 for k in ('amount', 'fee'):
55 if k in tx: tx[k] = self._fromAmount(tx[k])
58 def get_transaction(self, txid):
59 # NOTE: returns a list like transactions, in case we both sent and received
60 tx = self._access().gettransaction(txid)
61 for detail in tx['details']:
62 for k, v in tx.iteritems():
63 if k == 'details': continue
68 b = self._access().getbalance()
69 b = self._fromAmount(b)
73 return self._access().stop()
75 def validate_address(self, address):
76 return self._access().validateaddress(address)['isvalid']
78 def send(self, address, amount):
80 raise ValueError(self.tr('Bitcoin does not support precision requested'))
81 if self.rpcversion == 0:
82 if amount % 1000000 and self._access().getinfo()['version'] < 32100:
83 raise ValueError(self.tr('This server does not support precision requested'))
85 return self.access.sendtoaddress(address, amount)
87 def default_address(self):
88 return self._access().getaccountaddress('')
90 def new_address(self):
91 return self._access().getnewaddress('')
93 def is_initialised(self):
95 info = self._access().getinfo()
98 if 'isinitialized' in info:
99 return info['isinitialized']
100 # This only happens on older bitcoind which only respond when initialized...