convert satoshis to btc in listunspent
[electrum-nvc.git] / lib / commands.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 from util import *
21 from bitcoin import *
22 from decimal import Decimal
23 import bitcoin
24
25 protected_commands = ['payto', 'password', 'mktx', 'get_seed', 'importprivkey','signmessage', 'signrawtransaction','dumpprivkey' ]
26
27 class Commands:
28
29     def __init__(self, wallet, interface):
30         self.wallet = wallet
31         self.interface = interface
32
33     def _run(self, method, args, password_getter):
34         if method in protected_commands:
35             self.password = apply(password_getter,())
36         f = eval('self.'+method)
37         apply(f,args)
38         self.password = None
39
40     def get_history(self, addr):
41         h = self.wallet.get_history(addr)
42         if h is None: h = self.wallet.interface.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0]
43         print_json(h)
44
45     def listunspent(self):
46         l = self.wallet.get_unspent_coins()
47         for i in l: i["value"] = i["value"]*1e-8
48         print_json(l)
49
50     def createrawtransaction(self, inputs, outputs):
51         # convert to own format
52         for i in inputs:
53             i['tx_hash'] = i['txid']
54             i['index'] = i['vout']
55         outputs = map(lambda x: (x[0],int(1e8*x[1])), outputs.items())
56         tx = Transaction.from_io(inputs, outputs)
57         print_msg( tx )
58
59     def signrawtransaction(self, raw_tx, input_info, private_keys):
60         tx = Transaction(raw_tx)
61         unspent_coins = self.wallet.get_unspent_coins()
62
63         # convert private_keys to dict 
64         pk = {}
65         for sec in private_keys:
66             address = bitcoin.address_from_private_key(sec)
67             pk[address] = sec
68         private_keys = pk
69
70         for txin in tx.inputs:
71             # convert to own format
72             txin['tx_hash'] = txin['prevout_hash']
73             txin['index'] = txin['prevout_n']
74
75             for item in input_info:
76                 if item.get('txid') == txin['tx_hash'] and item.get('vout') == txin['index']:
77                     txin['raw_output_script'] = item['scriptPubKey']
78                     txin['redeemScript'] = item.get('redeemScript')
79                     txin['electrumKeyID'] = item.get('electrumKeyID')
80                     break
81             else:
82                 for item in unspent_coins:
83                     if txin['tx_hash'] == item['tx_hash'] and txin['index'] == item['index']:
84                         txin['raw_output_script'] = item['raw_output_script']
85                         break
86                 else:
87                     # if neither, we might want to get it from the server..
88                     raise
89
90             # find the address:
91             import deserialize
92             if txin.get('electrumKeyID'):
93                 n, for_change = txin.get('electrumKeyID')
94                 sec = wallet.sequence.get_private_key(n, for_change, seed)
95                 address = bitcoin.address_from_private_key(sec)
96                 txin['address'] = address
97                 private_keys[address] = sec
98
99             elif txin.get("redeemScript"):
100                 txin['address'] = bitcoin.hash_160_to_bc_address(bitcoin.hash_160(txin.get("redeemScript").decode('hex')), 5)
101
102             elif txin.get("raw_output_script"):
103                 addr = deserialize.get_address_from_output_script(txin.get("raw_output_script").decode('hex'))
104                 sec = wallet.get_private_key(addr, self.password)
105                 if sec: 
106                     private_keys[addr] = sec
107                     txin['address'] = addr
108
109         tx.sign( private_keys )
110         print_json({ "hex":str(tx),"complete":tx.is_complete})
111
112     def decoderawtransaction(self, raw):
113         tx = Transaction(raw)
114         print_json( tx.deserialize() )
115
116     def sendrawtransaction(self, raw):
117         tx = Transaction(raw)
118         r, h = wallet.sendtx( tx )
119         print_msg(h)
120
121     def createmultisig(self, num, pubkeys):
122         assert isinstance(pubkeys, list)
123         print_json( Transaction.multisig_script(pubkeys, num) )
124     
125     def freeze(self,addr):
126         print_msg(self.wallet.freeze(addr))
127         
128     def unfreeze(self,addr):
129         print_msg(self.wallet.unfreeze(addr))
130
131     def prioritize(self, addr):
132         print_msg(self.wallet.prioritize(addr))
133
134     def unprioritize(self, addr):
135         print_msg(self.wallet.unprioritize(addr))
136
137     def dumpprivkey(self, addr):
138         sec = self.wallet.get_private_key(addr, self.password)
139         print_msg( sec )
140
141     def validateaddress(self,addr):
142         is_valid = self.wallet.is_valid(addr)
143         out = { 'isvalid':is_valid }
144         if is_valid:
145             is_mine = self.wallet.is_mine(addr)
146             out['address'] = addr
147             out['ismine'] = is_mine
148             if is_mine:
149                 out['pubkey'] = self.wallet.get_public_key(addr)
150             
151         print_json(out)
152
153         
154     def balance(self, addresses = []):
155         if addresses == []:
156             c, u = self.wallet.get_balance()
157             if u:
158                 print_msg(Decimal( c ) / 100000000 , Decimal( u ) / 100000000)
159             else:
160                 print_msg(Decimal( c ) / 100000000)
161         else:
162             for addr in addresses:
163                 c, u = wallet.get_addr_balance(addr)
164                 if u:
165                     print_msg("%s %s, %s" % (addr, str(Decimal(c)/100000000), str(Decimal(u)/100000000)))
166                 else:
167                     print_msg("%s %s" % (addr, str(Decimal(c)/100000000)))
168
169
170     def get_seed(self):
171         import mnemonic
172         seed = self.wallet.decode_seed(self.password)
173         print_msg(seed + ' "' + ' '.join(mnemonic.mn_encode(seed)) + '"')
174
175     def importprivkey(self, sec):
176         try:
177             addr = wallet.import_key(sec,self.password)
178             wallet.save()
179             print_msg("Keypair imported: ", addr)
180         except BaseException as e:
181             print_msg("Error: Keypair import failed: " + str(e))
182
183
184     def sign_message(self, address, message):
185         print_msg(self.wallet.sign_message(address, message, self.password))
186
187
188     def verify_message(self, address, signature, message):
189         try:
190             EC_KEY.verify_message(address, signature, message)
191             print_msg(True)
192         except BaseException as e:
193             print_error("Verification error: {0}".format(e))
194             print_msg(False)
195
196
197     def _mktx(self, to_address, amount, fee = None, change_addr = None, from_addr = None):
198         for k, v in self.wallet.labels.items():
199             if v == to_address:
200                 to_address = k
201                 print_msg("alias", to_address)
202                 break
203             if change_addr and v == change_addr:
204                 change_addr = k
205
206         amount = int(10000000*amount)
207         if fee: fee = int(10000000*fee)
208         return self.wallet.mktx( [(to_address, amount)], self.password, fee , change_addr, from_addr)
209
210
211     def mktx(self, to_address, amount, fee = None, change_addr = None, from_addr = None):
212         tx = self._mktx(to_address, amount, fee, change_addr, from_addr)
213         out = {"hex":str(tx), "complete":tx.is_complete}
214         if not tx.is_complete: 
215             out['input_info'] = repr(tx.input_info).replace(' ','')
216         print_json(out)
217
218
219     def payto(self, to_address, amount, fee = None, change_addr = None, from_addr = None):
220         tx = self._mktx(to_address, amount, fee, change_addr, from_addr)
221         r, h = wallet.sendtx( tx )
222         print_msg(h)
223
224
225     def history(self):
226         import datetime
227         balance = 0
228         for item in self.wallet.get_tx_history():
229             tx_hash, conf, is_mine, value, fee, balance, timestamp = item
230             try:
231                 time_str = datetime.datetime.fromtimestamp( timestamp).isoformat(' ')[:-3]
232             except:
233                 time_str = "----"
234
235             label, is_default_label = self.wallet.get_label(tx_hash)
236             if not label: label = tx_hash
237             else: label = label + ' '*(64 - len(label) )
238
239             print_msg("%17s"%time_str, "  " + label + "  " + format_satoshis(value)+ "  "+ format_satoshis(balance))
240         print_msg("# balance: ", format_satoshis(balance))
241
242
243     def setlabel(self, tx, label):
244         self.wallet.labels[tx] = label
245         self.wallet.save()
246             
247
248     def contacts(self):
249         c = {}
250         for addr in self.wallet.addressbook:
251             c[addr] = self.wallet.labels.get(addr)
252         print_json(c)
253