-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
313 lines (254 loc) · 11.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import sys
from kivy.app import App
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.treeview import TreeView, TreeViewLabel
from kivy.uix.popup import Popup
from kivy.uix.listview import ListView, ListItemLabel, ListItemButton
from kivy.adapters.listadapter import ListAdapter
from kivy.core.clipboard import Clipboard
from kivy.config import Config
# golden ratio
Config.set('graphics', 'width', 309)
Config.set('graphics', 'height', 500)
#Config.set('graphics', 'resizable', 0)
from kivy.support import install_twisted_reactor
install_twisted_reactor()
from twisted.internet import task, reactor
from decimal import Decimal as D
import backend_core
import clipboard
import obelisk
class RootWidget(BoxLayout):
pass
class BalanceSection(BoxLayout):
def __init__(self, backend, **kwargs):
super(BalanceSection, self).__init__(**kwargs)
self.backend = backend
main_layout = BoxLayout(orientation='horizontal')
# TODO wire up the real amount to a formatter which gets fed here
self.balance_label = Label(text='00.00 mBTC', font_size=28, bold=True)
main_layout.add_widget(self.balance_label)
self.add_widget(main_layout)
def recalc_balance(self):
total_balance = 0
for addr, history in self.backend.addrs.iteritems():
total_balance += sum(row[3] for row in history
if row[-1] == obelisk.MAX_UINT32)
millis = D(total_balance) / 100000
self.balance_label.text = "%.2f mBTC" % millis
class ReceiveSection(BoxLayout):
def __init__(self, backend, **kwargs):
super(ReceiveSection, self).__init__(**kwargs)
self.backend = backend
main_layout = BoxLayout(orientation='vertical')
nextaddress_label = Label(text='click below to copy\nnext receive address',
size_hint_y=0.5, font_size=20, halign='center')
main_layout.add_widget(nextaddress_label)
# TODO wire this text input up to a real address which may change when a transaction to this address occurs
self.current_address = self.backend.current_address
shortened_address = self.current_address[:6] + "..."
nextaddress_input = Button(
text=shortened_address,
size_hint_y=0.3,
font_size=20,
foreground_color=(1,1,1,1))
nextaddress_input.bind(on_press=self.copy_address_to_clipboard)
nextaddress_input.next_address = self.current_address
main_layout.add_widget(nextaddress_input)
self.add_widget(main_layout)
def copy_address_to_clipboard(self, instance):
address = instance.next_address
clipboard.copy(address)
self.show_copied_popup(address)
print Clipboard.get('UTF8_STRING')
def show_copied_popup(self, address):
btnclose = Button(text='Close this popup', size_hint_y=None, height='50sp')
content = BoxLayout(orientation='vertical')
row_length = 15
for i in range(0, len(address), row_length):
content.add_widget(Label(text=address[i:i + row_length]))
content.add_widget(btnclose)
popup = Popup(content=content, title='Copied Address',
size_hint=(None, None), size=('300dp', '300dp'))
btnclose.bind(on_release=popup.dismiss)
button = Button(text='Open popup', size_hint=(None, None),
size=('150sp', '70dp'),
on_release=popup.open)
popup.open()
col = AnchorLayout()
col.add_widget(button)
return col
class SendSection(BoxLayout):
def __init__(self, backend, **kwargs):
super(SendSection, self).__init__(**kwargs)
self.backend = backend
self.main_layout = BoxLayout(orientation='vertical')
self.sendaddress = TextInput(hint_text="Enter a payment address", size_hint_y=0.2, font_size=20)
self.main_layout.add_widget(self.sendaddress)
self.sendsection = BoxLayout(orientation='horizontal', size_hint_y=0.4)
self.amount_mbtc = TextInput(text='0', halign='right', font_size=20, padding=(20, 20))
self.sendsection.add_widget(self.amount_mbtc)
self.sendsection.add_widget(Label(text='mBTC', halign='left'))
self.sendbutton = Button(text='Send')
self.sendbutton.bind(on_press=self.call_send)
self.sendsection.add_widget(self.sendbutton)
self.main_layout.add_widget(self.sendsection)
self.add_widget(self.main_layout)
def call_send(self, instance):
address = self.sendaddress.text
if not address or address[0] != "1":
# should show a popup here
print "Invalid address"
return
amount_satoshis = int(D(self.amount_mbtc.text) * 10**5)
# debug
print 'should send', str(amount_satoshis) ,'to address', address
# TODO validate address
# TODO call backend send function
fee = 10000
optimal_outputs = self.backend.select_outputs(amount_satoshis + fee)
print optimal_outputs
if optimal_outputs is None:
self.show_invalid_balance_popup()
return
# Add inputs
tx = obelisk.Transaction()
for output in optimal_outputs.points:
add_input(tx, output.point)
add_output(tx, address, amount_satoshis)
# Change output.
change = optimal_outputs.change - fee
add_output(tx, self.backend.current_change_address, change)
for i, output in enumerate(optimal_outputs.points):
obelisk.sign_transaction_input(tx, i, output.key)
#print tx.serialize().encode("hex")
self.backend.broadcast(tx)
def show_invalid_balance_popup(self):
btnclose = Button(text='Close this popup', size_hint_y=None, height='50sp')
content = BoxLayout(orientation='vertical')
row_length = 15
content.add_widget(Label(text='Not enough money for send.'))
content.add_widget(btnclose)
popup = Popup(content=content, title='Not enough...',
size_hint=(None, None), size=('300dp', '300dp'))
btnclose.bind(on_release=popup.dismiss)
button = Button(text='Open popup', size_hint=(None, None),
size=('150sp', '70dp'),
on_release=popup.open)
popup.open()
col = AnchorLayout()
col.add_widget(button)
return col
def add_input(tx, prevout):
input = obelisk.TxIn()
input.previous_output.hash = prevout.hash
input.previous_output.index = prevout.index
tx.inputs.append(input)
def add_output(tx, address, value):
output = obelisk.TxOut()
output.value = value
output.script = obelisk.output_script(address)
tx.outputs.append(output)
class TransactionSection(BoxLayout):
def __init__(self, backend, **kwargs):
super(TransactionSection, self).__init__(**kwargs)
self.backend = backend
self.transactions = [{'value': 'No transactions'}]
main_layout = BoxLayout(orientation='vertical')
self.list_view = None
self.transaction_history = self.make_transaction_widget()
main_layout.add_widget(self.transaction_history)
self.add_widget(main_layout)
def make_transaction_widget(self):
print 'make_transaction_widget called'
args_converter = lambda row_index, rec: {'text': str(rec['value']),
'size_hint_y': None,
'height': 25}
#self.transaction_history = ListView(item_strings=self.get_transactions(0,50))
print self.transactions
transaction_history = ListAdapter(data=self.transactions,
args_converter=args_converter,
cls=ListItemLabel,
selection_mode='single',
allow_empty_selection=False)
if self.list_view is None:
self.list_view = ListView(adapter=transaction_history)
else:
self.list_view.adapter = transaction_history
return self.list_view
def get_transactions(self, start=0, amount=100):
print self.transactions
#item_strings=[('row ' + str(index) + ': +500 mBTC sent to 1MNmTP...') for index in range(amount)]
transaction_strings=[]
for txhash, tx in self.transactions:
if s_index != obelisk.MAX_UINT32:
transaction_strings.append('+' + tx['value'] + ' satoshis received on ' + tx['address'])
else:
transaction_strings.append(tx['value'] + ' satoshis sent from ' + tx['addrdress'])
print transaction_strings
return transaction_strings
class MainApp(App):
def __init__(self, backend):
super(MainApp, self). __init__()
self.backend = backend
self.transactions = {}
def cb(self, addr, history):
if addr == self.backend.current_address and history:
print "Next key in wallet."
self.backend.next_key()
#print addr, history
display_history = []
for row in history:
o_hash, o_index, o_height, value, s_hash, s_index, s_height = row
display_addr = addr[:6] + "..."
display_history.append(
("%s +%s mBTC" % (display_addr, value / 10**5), o_height))
if s_index == obelisk.MAX_UINT32:
continue
display_history.append(
("%s -%s mBTC" % (display_addr, value / 10**5), s_height))
display_history.sort(key=lambda display_row: display_row[1])
self.transactions[addr] = display_history
self.rebuild_history()
def rebuild_history(self):
mixed = []
for key, display_history in self.transactions.iteritems():
for row in display_history:
mixed.append({'value': row[0]})
#self.trans.transaction_history.add_node(TreeViewLabel(text=str(value) + ' satoshis sent to/from ' + addr))
# For display
self.trans.transactions = mixed
#self.trans.remove_widget(self.trans.transaction_history)
self.trans.transaction_history = self.trans.make_transaction_widget()
#self.trans.add_widget(self.trans.transaction_history)
#print self.transactions
self.balance.recalc_balance()
def build(self):
root = RootWidget()
main_layout = BoxLayout(orientation='vertical')
self.balance = BalanceSection(backend=self.backend, size_hint_y=0.4)
main_layout.add_widget(self.balance)
main_layout.add_widget(ReceiveSection(backend=self.backend, size_hint_y=1))
main_layout.add_widget(SendSection(backend=self.backend, size_hint_y=0.7))
self.trans = TransactionSection(backend=self.backend, size_hint_y=1)
main_layout.add_widget(self.trans)
root.add_widget(main_layout)
later = task.LoopingCall(self.backend.update, self.cb)
later.start(5)
return root
if __name__ == '__main__':
if len(sys.argv) == 1: sys.argv[1:] = ["correct horse battery staple"]
print "\n".join(sys.argv)
#import mnemonic
#seedphrase = mnemonic.mn_decode(sys.argv[1])
seed = "945d3c0e7b2343f327f1c6ec900e5406"
print seed.encode("hex")
backend = backend_core.Backend(seed)
MainApp(backend).run()