-
Notifications
You must be signed in to change notification settings - Fork 763
/
_gui.py
1058 lines (856 loc) · 39.2 KB
/
_gui.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
import os
import argparse
import uuid
import binascii
import random
import pkg_resources
from pprint import pprint
from time import time_ns
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository.Gdk import Color
from twisted.internet import gtk3reactor
gtk3reactor.install()
import txaio
txaio.use_twisted()
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks
from twisted.internet import reactor
import web3
import numpy as np
import click
from humanize import naturaldelta, naturaltime
from autobahn.util import parse_activation_code, hltype, hlid, hlval
from autobahn.wamp.serializer import CBORSerializer
from autobahn.twisted.util import sleep
from autobahn.twisted.wamp import ApplicationRunner
from autobahn.xbr import unpack_uint256
from autobahn.xbr import account_from_seedphrase, generate_seedphrase, account_from_ethkey
from autobahn.xbr._cli import Client
from autobahn.xbr._config import UserConfig, Profile
LOGO_RESOURCE = pkg_resources.resource_filename('autobahn', 'asset/xbr_gray.svg')
print(LOGO_RESOURCE, os.path.isfile(LOGO_RESOURCE))
class ApplicationWindow(Gtk.Assistant):
"""
Main application window which provides UI for the following functions:
* N) New account
* R) Recover account:
- R1) Backup cloud device in account enabled => download encrypted account data
from cloud backup device, requires email (and 2FA) verification and password
- R2) At least one device left in account and at hand => synchronize with existing device,
direct device-to-device encrypted account data transfer
- R3) Only cold storage recovery seed phrase left => account from seed-phrase full
recovery, including new email and 2FA verification.
See also:
* https://python-gtk-3-tutorial.readthedocs.io/en/latest/
* https://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html
"""
log = txaio.make_logger()
SELECTED_NONE = 0
SELECTED_NEW = 1
SELECTED_SYNCRONIZE = 2
SELECTED_RECOVER = 3
def __init__(self, reactor, session, config, config_path, profile, profile_name):
Gtk.Assistant.__init__(self)
self.reactor = reactor
self.session = session
self.config = config
self.config_path = config_path
self.profile = profile
self.profile_name = profile_name
self.input_seedphrase = None
self.input_email = None
self.input_password = None
self.output_account = None
self.output_ethadr = None
self.output_ethadr_raw = None
self.output_member_data = None
self.output_member_data_oid = uuid.UUID(bytes=b'\x00' * 16)
# configure assistant window/widget
self.set_title("XBR Network")
self.set_default_size(600, 600)
self.set_border_width(50)
self.set_resizable(False)
# setup assistant pages
self._setup_page1()
self._setup_page2()
self._setup_page3()
self._setup_page4()
self._setup_page5()
@inlineCallbacks
def start(self):
# start page depends on available user profile
if self.profile:
self.output_account = account_from_ethkey(self.profile.ethkey)
self.output_ethadr = web3.Web3.toChecksumAddress(self.output_account.address)
self.output_ethadr_raw = binascii.a2b_hex(self.output_ethadr[2:])
info = yield self.session.get_status()
if info:
now = str(np.datetime64(np.datetime64(info['status']['now'], 'ns'), 's'))
self._label5_now.set_label(now)
self._label5_chain.set_label(str(info['status']['chain']))
self._label5_status.set_label(str(info['status']['status']))
self._label5_xbrnetwork.set_label(str(info['config']['contracts']['xbrnetwork']))
self._label5_xbrtoken.set_label(str(info['config']['contracts']['xbrtoken']))
self._label5_blockhash.set_label('0x' + binascii.b2a_hex(info['status']['block']['hash']).decode())
self._label5_blocknumber.set_label(str(info['status']['block']['number']))
pprint(info)
member_data = yield self.session.get_member(self.output_ethadr_raw)
if not member_data:
self.log.info('ethadr {output_ethadr} is NOT yet a member',
output_ethadr=self.output_ethadr)
if self.profile.vaction_oid:
# switch to page "_setup_page4"
self.set_current_page(3)
else:
if self.profile.ethkey:
# switch to page "_setup_page3"
self.set_current_page(2)
else:
# switch to page "_setup_page2"
self.set_current_page(1)
else:
self.log.info('ok, ethadr {output_ethadr} already is a member: {member_data}',
output_ethadr=self.output_ethadr, member_data=member_data)
self.output_member_data = member_data
created_ago = naturaldelta((np.datetime64(time_ns(), 'ns') - self.output_member_data['created']) / 1000000000.)
created = naturaltime(np.datetime64(self.output_member_data['created'], 's'))
self._label2.set_label(str(self.output_member_data['oid']))
self._label4.set_label(str(self.output_member_data['address']))
self._label6.set_label('{} ({} ago)'.format(created, created_ago))
self._label8.set_label(str(self.output_member_data['level']))
self._label10.set_label(str(self.output_member_data['balance']['eth']))
self._label12.set_label(str(self.output_member_data['balance']['xbr']))
# switch to page "_setup_page5"
self.set_current_page(4)
else:
profile = Profile()
profile.path = self.config_path
profile.ethkey = None
profile.cskey = None
profile.username = None
profile.email = None
profile.network_url = 'ws://localhost:8090/ws'
profile.network_realm = 'xbrnetwork'
profile.member_oid = None
profile.vaction_oid = None
profile.vaction_requested = None
profile.vaction_verified = None
profile.data_url = None
profile.data_realm = None
profile.infura_url = None
profile.infura_network = None
profile.infura_key = None
profile.infura_secret = None
self.profile = profile
# switch to page "_setup_page1"
self.set_current_page(0)
def on_complete_toggled(self, checkbutton):
self.set_page_complete(self.complete, checkbutton.get_active())
# no config: select new/recovery
def _setup_page1(self):
"""
Setup page shown when no config/profile could be found. Allows to select from:
* new account
* synchronize device
* recover account
"""
grid1 = Gtk.Grid()
grid1.set_row_spacing(20)
grid1.set_column_spacing(20)
grid1.set_margin_top(20)
grid1.set_margin_bottom(20)
grid1.set_margin_start(20)
grid1.set_margin_end(20)
image1 = Gtk.Image()
image1.set_from_file(LOGO_RESOURCE)
grid1.attach(image1, 0, 0, 2, 1)
label0 = Gtk.Label(label='\n\nI am new and do not have an account yet:\n')
label0.set_alignment(0, 0.5)
grid1.attach(label0, 0, 1, 2, 1)
label1 = Gtk.Label(label='Create a new account or start from fresh. You only need an email address. [N]')
label1.set_alignment(0, 0.5)
label1.set_justify(Gtk.Justification.LEFT)
grid1.attach(label1, 1, 2, 1, 1)
button1 = Gtk.Button.new_with_label('New account')
def on_button1(res):
self.log.info('SELECTED_NEW: {res}', res=res)
self.set_current_page(1)
button1.connect('clicked', on_button1)
grid1.attach(button1, 0, 2, 1, 1)
label12 = Gtk.Label(label='\n\nI already have an existing account and want to use that:\n')
label12.set_alignment(0, 0.5)
grid1.attach(label12, 0, 3, 2, 1)
label22 = Gtk.Label(label='Restore account from cloud backup to this device. You will need access to\n'
'your account password and access to your account email address. [R1]')
label22.set_alignment(0, 0.5)
label22.set_justify(Gtk.Justification.LEFT)
label22.set_line_wrap(True)
label22.set_width_chars(12)
grid1.attach(label22, 1, 4, 1, 1)
button22 = Gtk.Button.new_with_label('Restore account')
def on_button22(res):
self.log.info('SELECTED_RESTORE: {res}', res=res)
self.set_current_page(2)
button22.connect('clicked', on_button22)
grid1.attach(button22, 0, 4, 1, 1)
label2 = Gtk.Label(label='Synchronize device with other device in account. You will need access to\n'
'another device currently connected to your account. [R2]')
label2.set_alignment(0, 0.5)
label2.set_justify(Gtk.Justification.LEFT)
label2.set_line_wrap(True)
label2.set_width_chars(12)
grid1.attach(label2, 1, 5, 1, 1)
button2 = Gtk.Button.new_with_label('Synchronize account')
def on_button2(res):
self.log.info('SELECTED_SYNCRONIZE: {res}', res=res)
self.set_current_page(2)
button2.connect('clicked', on_button2)
grid1.attach(button2, 0, 5, 1, 1)
label3 = Gtk.Label(label='Recover account from account seed phrase. You only need access to\n'
'your 12-24 word account recovery seed phrase. [R3]')
label3.set_alignment(0, 0.5)
label3.set_justify(Gtk.Justification.LEFT)
label3.set_line_wrap(True)
label3.set_width_chars(12)
grid1.attach(label3, 1, 6, 1, 1)
button3 = Gtk.Button.new_with_label('Recover account')
def on_button3(res):
self.log.info('SELECTED_RECOVER: {res}', res=res)
self.set_current_page(3)
button3.connect('clicked', on_button3)
grid1.attach(button3, 0, 6, 1, 1)
self.append_page(grid1)
# generate seed phrase
def _setup_page2(self):
"""
Setup page shown to generate a new seed phrase.
"""
box2_1 = Gtk.VBox()
box2_2 = Gtk.HBox()
image2_1 = Gtk.Image()
image2_1.set_from_file(LOGO_RESOURCE)
box2_2.add(image2_1)
box2_1.add(box2_2)
button2_1 = Gtk.Button.new_with_label('Generate seedphrase')
def on_button2_1(_):
self.input_seedphrase = generate_seedphrase(strength=256, language='english')
textbuffer2_1.set_text(self.input_seedphrase)
checkbutton2_1.set_sensitive(True)
button2_1.connect('clicked', on_button2_1)
box2_1.add(button2_1)
label2_1 = Gtk.Label(label='Backup your new seed phrase in a secure offline location (e.g. on printed paper):')
label2_1.set_alignment(0, 0.5)
label2_1.set_justify(Gtk.Justification.LEFT)
box2_1.add(label2_1)
textview2_1 = Gtk.TextView()
textbuffer2_1 = textview2_1.get_buffer()
textbuffer2_1.set_text('\n' * 5)
textview2_1.set_editable(False)
textview2_1.set_justification(Gtk.Justification.CENTER)
textview2_1.set_monospace(True)
textview2_1.set_wrap_mode(Gtk.WrapMode.WORD)
box2_1.add(textview2_1)
box2_3 = Gtk.HBox()
checkbutton2_1 = Gtk.CheckButton(label="I have backed up my seed phrase")
checkbutton2_1.set_active(False)
checkbutton2_1.set_sensitive(False)
def on_checkbutton2_1(_):
button2_2.set_sensitive(True)
checkbutton2_1.connect("toggled", on_checkbutton2_1)
box2_3.add(checkbutton2_1)
button2_2 = Gtk.Button.new_with_label('Continue')
button2_2.set_sensitive(False)
@inlineCallbacks
def on_button2_2(_):
self.output_account = account_from_seedphrase(self.input_seedphrase, index=0)
self.output_ethadr = web3.Web3.toChecksumAddress(self.output_account.address)
self.output_ethadr_raw = binascii.a2b_hex(self.output_ethadr[2:])
# https://eth-account.readthedocs.io/en/latest/eth_account.signers.html#eth_account.signers.local.LocalAccount.key
self.profile.ethkey = bytes(self.output_account.key)
self.profile.cskey = bytes(self.session._cskey_raw)
# set user eth key on client session
self.session.set_ethkey_from_profile(self.profile)
# save user config
self.config.profiles[self.profile_name] = self.profile
self.config.save(self.input_password)
self.log.info('XBR ETH key at address {ethadr} set from seed phrase (BIP39 account 0): "{seedphrase}"',
ethadr=self.output_ethadr,
seedphrase=self.input_seedphrase)
member_data = yield self.session.get_member(self.output_ethadr_raw)
pprint(member_data)
if not member_data:
self.log.info('ethadr {output_ethadr} is NOT yet a member',
output_ethadr=self.output_ethadr)
self.set_current_page(2)
else:
self.log.info('ok, ethadr {output_ethadr} already is a member: {member_data}',
output_ethadr=self.output_ethadr, member_data=member_data)
self.profile.member_oid = uuid.UUID(bytes=member_data['member_oid'])
self.profile.member_adr = self.output_ethadr
self.profile.email = member_data['email']
self.profile.username = member_data['username']
# save user config
self.config.profiles[self.profile_name] = self.profile
self.config.save(self.input_password)
self.output_member_data = member_data
self._label2.set_label(str(self._member_data['oid']))
self.set_current_page(4)
def run_on_button2_2(widget):
self.log.info('{func}({widget})', func=hltype(run_on_button2_2), widget=widget)
reactor.callLater(0, on_button2_2, widget)
button2_2.connect('clicked', run_on_button2_2)
box2_3.add(button2_2)
box2_1.add(box2_3)
self.append_page(box2_1)
# submit onboard request
def _setup_page3(self):
"""
:return:
"""
box1 = Gtk.VBox()
box2 = Gtk.HBox()
image1 = Gtk.Image()
image1.set_from_file(LOGO_RESOURCE)
box2.add(image1)
box1.add(box2)
grid1 = Gtk.Grid()
grid1.set_row_spacing(20)
grid1.set_column_spacing(20)
grid1.set_margin_top(20)
grid1.set_margin_bottom(20)
grid1.set_margin_start(20)
grid1.set_margin_end(20)
label1 = Gtk.Label(label='Your email address:')
grid1.attach(label1, 0, 0, 1, 1)
entry1 = Gtk.Entry()
entry1.set_text('')
entry1.set_max_length(255)
entry1.set_max_width_chars(40)
grid1.attach(entry1, 1, 0, 1, 1)
checks = {
'email': None,
'password': None,
'eula': None,
}
def check_all():
print('check_all')
for c in checks:
if checks[c] is None:
print('check failed', c)
button3.set_sensitive(False)
return
button3.set_sensitive(True)
def check_email(email):
if '@' in email:
return email
else:
return None
def check_password(password):
return True
def on_entry1(entry):
# joe.doe@example.com
checks['email'] = check_email(entry.get_text())
if checks['email']:
entry1.modify_fg(Gtk.StateFlags.NORMAL, None)
else:
entry1.modify_fg(Gtk.StateFlags.NORMAL, Color(50000, 0, 0))
check_all()
entry1.connect('changed', on_entry1)
label2 = Gtk.Label(label='New password:')
grid1.attach(label2, 0, 1, 1, 1)
entry2 = Gtk.Entry()
entry2.set_text('')
entry2.set_max_length(20)
entry2.set_max_width_chars(20)
entry2.set_visibility(False)
grid1.attach(entry2, 1, 1, 1, 1)
label2 = Gtk.Label(label='Repeat new password:')
grid1.attach(label2, 0, 2, 1, 1)
entry3 = Gtk.Entry()
entry3.set_text('')
entry3.set_max_length(20)
entry3.set_max_width_chars(20)
entry3.set_visibility(False)
grid1.attach(entry3, 1, 2, 1, 1)
def on_entry23(_):
pw1_ok = False
if check_password(entry2.get_text()):
pw1_ok = True
entry2.modify_fg(Gtk.StateFlags.NORMAL, None)
else:
entry2.modify_fg(Gtk.StateFlags.NORMAL, Color(50000, 0, 0))
pw2_ok = False
if check_password(entry3.get_text()):
pw2_ok = True
entry3.modify_fg(Gtk.StateFlags.NORMAL, None)
else:
entry3.modify_fg(Gtk.StateFlags.NORMAL, Color(50000, 0, 0))
if pw1_ok and pw2_ok and entry2.get_text() == entry3.get_text() and check_password(entry2.get_text()):
checks['password'] = entry2.get_text()
else:
checks['password'] = None
check_all()
entry2.connect('changed', on_entry23)
entry3.connect('changed', on_entry23)
label3 = Gtk.Label(label='EULA:')
grid1.attach(label3, 0, 3, 1, 1)
button1 = Gtk.CheckButton(label='I accept the EULA and terms of use')
button1.set_active(False)
button1.set_sensitive(True)
def on_button1(button):
if button.get_active():
checks['eula'] = True
else:
checks['eula'] = False
check_all()
button1.connect('toggled', on_button1)
grid1.attach(button1, 1, 3, 1, 1)
label3 = Gtk.Label(label='Cloud backup:')
grid1.attach(label3, 0, 4, 1, 1)
button2 = Gtk.CheckButton(label='Yes, enable encrypted cloud backup of my private keys')
button2.set_active(False)
button2.set_sensitive(True)
def on_button2(button):
check_all()
button2.connect('toggled', on_button2)
grid1.attach(button2, 1, 4, 1, 1)
button3 = Gtk.Button.new_with_label('Register account')
button3.set_sensitive(False)
@inlineCallbacks
def on_button3(_):
self.input_email = checks['email']
self.input_password = checks['password']
self.input_backup_enabled = button2.get_active()
self.input_username = 'anonymous'
self.input_username = '{}{}'.format(self.input_username, random.randint(0, 10000))
self.session.set_ethkey_from_profile(self.profile)
self.log.info('input_email: {input_email}', input_email=self.input_email)
self.log.info('input_username: {input_username}', input_username=self.input_username)
self.log.info('input_password: {input_password}', input_password=self.input_password)
result = yield self.session._do_onboard_member(self.input_username, self.input_email)
pprint(result)
self.profile.email = self.input_email
self.profile.username = self.input_username
self.profile.vaction_oid = str(uuid.UUID(bytes=result['vaction_oid']))
self.profile.vaction_requested = str(np.datetime64(result['timestamp'], 'ns'))
self.config.profiles[self.profile_name] = self.profile
self.config.save(self.input_password)
self.set_current_page(3)
def run_on_button3(widget):
self.log.info('{func}({widget})', func=hltype(run_on_button3), widget=widget)
reactor.callLater(0, on_button3, widget)
button3.connect('clicked', run_on_button3)
grid1.attach(button3, 2, 4, 1, 1)
box1.add(grid1)
self.append_page(box1)
# submit verification code
def _setup_page4(self):
"""
Page shown when member registration request was submitted, a verification email
sent, and the verification request ID returned.
The user now should check the email inbox for the received verification code,
and continue verifying the code.
:return:
"""
box1 = Gtk.VBox()
box2 = Gtk.HBox()
image1 = Gtk.Image()
image1.set_from_file(LOGO_RESOURCE)
box2.add(image1)
box1.add(box2)
box3 = Gtk.HBox()
label1 = Gtk.Label(label='Member registration submitted, verification request:')
label2 = Gtk.Label(label='8d5d7ffd-23d9-45a0-a686-00a49f29d3cd')
box3.add(label1)
box3.add(label2)
box1.add(box3)
label3 = Gtk.Label(label='Please check your email inbox, and enter the verification code received here:')
box1.add(label3)
entry1 = Gtk.Entry()
entry1.set_text('')
entry1.set_max_length(255)
entry1.set_max_width_chars(40)
box1.add(entry1)
def on_entry1(entry):
# "RWCN-94NV-CEHR" -> ("RWCN", "94NV", "CEHR") | None
vaction_code = parse_activation_code(entry.get_text())
if vaction_code:
entry1.modify_fg(Gtk.StateFlags.NORMAL, None)
button1.set_sensitive(True)
else:
entry1.modify_fg(Gtk.StateFlags.NORMAL, Color(50000, 0, 0))
button1.set_sensitive(False)
entry1.connect('changed', on_entry1)
button1 = Gtk.Button.new_with_label('Verify')
button1.set_sensitive(False)
@inlineCallbacks
def on_button1(_):
vaction_code = parse_activation_code(entry1.get_text())
if vaction_code:
vaction_code = '-'.join(vaction_code.groups())
if type(self.profile.vaction_oid) == str:
vaction_oid = uuid.UUID(self.profile.vaction_oid)
else:
vaction_oid = self.profile.vaction_oid
result = yield self.session._do_onboard_member_verify(vaction_oid, vaction_code)
pprint(result)
self.profile.vaction_verified = str(np.datetime64(result['created'], 'ns'))
self.profile.vaction_transaction = '0x' + str(binascii.b2a_hex(result['transaction']).decode())
self.profile.member_oid = str(uuid.UUID(bytes=result['member_oid']))
self.config.profiles[self.profile_name] = self.profile
self.config.save(self.input_password)
def run_on_button1(widget):
self.log.info('{func}({widget})', func=hltype(run_on_button1), widget=widget)
reactor.callLater(0, on_button1, widget)
button1.connect('clicked', run_on_button1)
box1.add(button1)
self.append_page(box1)
# show member data
def _setup_page5(self):
"""
Page shown for a user (private eth key) that already is member.
:return:
"""
box1 = Gtk.VBox()
box2 = Gtk.HBox()
image1 = Gtk.Image()
image1.set_from_file(LOGO_RESOURCE)
box2.add(image1)
box1.add(box2)
grid2 = Gtk.Grid()
grid2.set_row_spacing(20)
grid2.set_column_spacing(20)
grid2.set_margin_top(20)
grid2.set_margin_bottom(20)
grid2.set_margin_start(20)
grid2.set_margin_end(20)
grid2_y = 0
# Current server time
#
label5_now_title = Gtk.Label(label='Current server time:')
label5_now_title.set_alignment(1, 0.5)
grid2.attach(label5_now_title, 0, grid2_y, 1, 1)
self._label5_now = Gtk.Label()
self._label5_now.set_alignment(0, 0.5)
self._label5_now.set_selectable(False)
grid2.attach(self._label5_now, 1, grid2_y, 1, 1)
grid2_y += 1
# Blockchain ID (e.g. 1, 3 or 5777)
#
label5_chain_title = Gtk.Label(label='Blockchain ID:')
label5_chain_title.set_alignment(1, 0.5)
grid2.attach(label5_chain_title, 0, grid2_y, 1, 1)
self._label5_chain = Gtk.Label()
self._label5_chain.set_alignment(0, 0.5)
self._label5_chain.set_selectable(True)
grid2.attach(self._label5_chain, 1, grid2_y, 1, 1)
grid2_y += 1
# Current server time
#
label5_status_title = Gtk.Label(label='Service status:')
label5_status_title.set_alignment(1, 0.5)
grid2.attach(label5_status_title, 0, grid2_y, 1, 1)
self._label5_status = Gtk.Label()
self._label5_status.set_alignment(0, 0.5)
self._label5_status.set_selectable(False)
grid2.attach(self._label5_status, 1, grid2_y, 1, 1)
grid2_y += 1
# xbrnetwork address
#
label5_xbrnetwork_title = Gtk.Label(label='XBRNetwork contract:')
label5_xbrnetwork_title.set_alignment(1, 0.5)
grid2.attach(label5_xbrnetwork_title, 0, grid2_y, 1, 1)
self._label5_xbrnetwork = Gtk.Label()
self._label5_xbrnetwork.set_alignment(0, 0.5)
self._label5_xbrnetwork.set_selectable(True)
grid2.attach(self._label5_xbrnetwork, 1, grid2_y, 1, 1)
grid2_y += 1
# xbrtoken address
#
label5_xbrtoken_title = Gtk.Label(label='XBRToken contract:')
label5_xbrtoken_title.set_alignment(1, 0.5)
grid2.attach(label5_xbrtoken_title, 0, grid2_y, 1, 1)
self._label5_xbrtoken = Gtk.Label()
self._label5_xbrtoken.set_alignment(0, 0.5)
self._label5_xbrtoken.set_selectable(True)
grid2.attach(self._label5_xbrtoken, 1, grid2_y, 1, 1)
grid2_y += 1
# Current block hash
#
label5_blockhash_title = Gtk.Label(label='Current block hash:')
label5_blockhash_title.set_alignment(1, 0.5)
grid2.attach(label5_blockhash_title, 0, grid2_y, 1, 1)
self._label5_blockhash = Gtk.Label()
self._label5_blockhash.set_alignment(0, 0.5)
self._label5_blockhash.set_selectable(True)
grid2.attach(self._label5_blockhash, 1, grid2_y, 1, 1)
grid2_y += 1
# Current block number
#
label5_blocknumber_title = Gtk.Label(label='Current block number:')
label5_blocknumber_title.set_alignment(1, 0.5)
grid2.attach(label5_blocknumber_title, 0, grid2_y, 1, 1)
self._label5_blocknumber = Gtk.Label()
self._label5_blocknumber.set_alignment(0, 0.5)
self._label5_blocknumber.set_selectable(True)
grid2.attach(self._label5_blocknumber, 1, grid2_y, 1, 1)
grid2_y += 1
box1.add(grid2)
grid1 = Gtk.Grid()
grid1.set_row_spacing(20)
grid1.set_column_spacing(20)
grid1.set_margin_top(20)
grid1.set_margin_bottom(20)
grid1.set_margin_start(20)
grid1.set_margin_end(20)
label1 = Gtk.Label(label='User ID:')
label1.set_alignment(1, 0.5)
grid1.attach(label1, 0, 0, 1, 1)
self._label2 = Gtk.Label()
self._label2.set_alignment(0, 0.5)
self._label2.set_selectable(True)
grid1.attach(self._label2, 1, 0, 1, 1)
label3 = Gtk.Label(label='Eth Address:')
label3.set_alignment(1, 0.5)
grid1.attach(label3, 0, 1, 1, 1)
self._label4 = Gtk.Label()
self._label4.set_alignment(0, 0.5)
self._label4.set_selectable(True)
grid1.attach(self._label4, 1, 1, 1, 1)
label5 = Gtk.Label(label='Account Created:')
label5.set_alignment(1, 0.5)
grid1.attach(label5, 0, 2, 1, 1)
self._label6 = Gtk.Label()
self._label6.set_alignment(0, 0.5)
grid1.attach(self._label6, 1, 2, 1, 1)
label7 = Gtk.Label(label='Membership:')
label7.set_alignment(1, 0.5)
grid1.attach(label7, 0, 3, 1, 1)
self._label8 = Gtk.Label()
self._label8.set_alignment(0, 0.5)
grid1.attach(self._label8, 1, 3, 1, 1)
label9 = Gtk.Label(label='ETH Balance:')
label9.set_alignment(1, 0.5)
grid1.attach(label9, 0, 4, 1, 1)
self._label10 = Gtk.Label()
self._label10.set_alignment(0, 0.5)
grid1.attach(self._label10, 1, 4, 1, 1)
label11 = Gtk.Label(label='XBR Balance:')
label11.set_alignment(1, 0.5)
grid1.attach(label11, 0, 5, 1, 1)
self._label12 = Gtk.Label()
self._label12.set_alignment(0, 0.5)
grid1.attach(self._label12, 1, 5, 1, 1)
box1.add(grid1)
self.append_page(box1)
class ApplicationClient(Client):
async def onJoin(self, details):
self.log.info('Ok, client joined on realm "{realm}" [session={session}, authid="{authid}", authrole="{authrole}"]',
realm=hlid(details.realm),
session=hlid(details.session),
authid=hlid(details.authid),
authrole=hlid(details.authrole),
details=details)
if 'ready' in self.config.extra:
txaio.resolve(self.config.extra['ready'], (self, details))
@inlineCallbacks
def get_status(self):
if self.is_attached():
config = yield self.call('xbr.network.get_config')
status = yield self.call('xbr.network.get_status')
return {'config': config, 'status': status}
else:
self.log.warn('not connected: could not retrieve status')
@inlineCallbacks
def get_member(self, ethadr_raw):
if self.is_attached():
is_member = yield self.call('xbr.network.is_member', ethadr_raw)
if is_member:
member_data = yield self.call('xbr.network.get_member_by_wallet', ethadr_raw)
member_data['address'] = web3.Web3.toChecksumAddress(member_data['address'])
member_data['oid'] = uuid.UUID(bytes=member_data['oid'])
member_data['balance']['eth'] = web3.Web3.fromWei(unpack_uint256(member_data['balance']['eth']),
'ether')
member_data['balance']['xbr'] = web3.Web3.fromWei(unpack_uint256(member_data['balance']['xbr']),
'ether')
member_data['created'] = np.datetime64(member_data['created'], 'ns')
member_level = member_data['level']
member_data['level'] = {
# Member is active.
1: 'ACTIVE',
# Member is active and verified.
2: 'VERIFIED',
# Member is retired.
3: 'RETIRED',
# Member is subject to a temporary penalty.
4: 'PENALTY',
# Member is currently blocked and cannot current actively participate in the market.
5: 'BLOCKED',
}.get(member_level, None)
self.log.info(
'Member {member_oid} found for address 0x{member_adr} - current member level {member_level}',
member_level=hlval(member_data['level']),
member_oid=hlid(member_data['oid']),
member_adr=hlval(member_data['address']))
return member_data
else:
self.log.warn('Address {output_ethadr} is not a member in the XBR network',
output_ethadr=ethadr_raw)
else:
self.log.warn('not connected: could not retrieve member data for address {output_ethadr}',
output_ethadr=ethadr_raw)
class Application(object):
"""
Main XBR member application.
"""
log = txaio.make_logger()
DOTDIR = os.path.abspath(os.path.expanduser('~/.xbrnetwork'))
DOTFILE = 'config.ini'
async def start(self, reactor, url=None, realm=None, profile=None):
"""
Start main application. This will read the user configuration, potentially asking
for a user password.
:param reactor: Twisted reactor to use.
:param url: Optionally override network URL as defined in profile.
:param realm: Optionally override network URL as defined in profile.
:param profile: User profile name to load.
:return:
"""
txaio.start_logging(level='info')
self.log.info('ok, application starting for user profile "{profile}"', profile=profile)
if not os.path.isdir(self.DOTDIR):
os.mkdir(self.DOTDIR)
self.log.info('dotdir created: "{dotdir}"', dotdir=self.DOTDIR)
self._config_path = config_path = os.path.join(self.DOTDIR, self.DOTFILE)
self._profile_name = profile or 'default'
if not os.path.isfile(self._config_path):
self.log.info('no config exist under "{config_path}"', config_path=self._config_path)
self._config = UserConfig(self._config_path)
self._profile = None
else:
self._config = UserConfig(self._config_path)
# FIXME: start modal dialog to get password from user
def getpw():
return '123secret'
self._config.load(cb_get_password=getpw)
if self._profile_name not in self._config.profiles:
raise click.ClickException('no such profile "{}" in config "{}" with {} profiles'.format(self._profile_name, config_path, len(self._config.profiles)))
else:
self._profile = self._config.profiles[self._profile_name]
self.log.info('user profile "{profile_name}" loaded from "{config_path}":\n\n',
config_path=self._config_path, profile_name=self._profile_name)
pprint(self._profile.marshal())
print('\n\n')
extra = {
'ready': txaio.create_future(),
'done': txaio.create_future(),
'running': True,
'config': self._config,
'config_path': self._config_path,
'profile': self._profile,
'profile_name': self._profile_name,
}
# XBR network node used as a directory server and gateway to XBR smart contracts
network_url = url or (self._profile.network_url if self._profile and self._profile.network_url else 'ws://localhost:8090/ws')
# WAMP realm on network node, usually "xbrnetwork"
network_realm = realm or (self._profile.network_realm if self._profile and self._profile.network_realm else 'xbrnetwork')
runner = ApplicationRunner(url=network_url,
realm=network_realm,
extra=extra,
serializers=[CBORSerializer()])
self.log.info('ok, now connecting to "{network_url}", joining realm "{network_realm}" ..',
network_url=network_url,
network_realm=network_realm)
await runner.run(ApplicationClient,
reactor=reactor,
auto_reconnect=True,
start_reactor=False)
self.log.info('ok, application client connected!')
session, details = await extra['ready']
self.log.info('ok, application session joined: {details}', details=details)
def on_exit(_):
self.log.info('exiting application ..')
extra['running'] = False
txaio.resolve(extra['done'], None)
win = ApplicationWindow(reactor, session, self._config, self._config_path, self._profile, self._profile_name)
win.connect("cancel", on_exit)
win.connect("destroy", on_exit)
win.show_all()
await win.start()
ticks = 0
while extra['running']:
ticks += 1
self.log.info('ok, application main task still running at tick {ticks}', ticks=ticks)
await sleep(5)
self.log.info('ok, application main task ended!')
async def main(reactor, url, realm, profile):