-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
1018 lines (912 loc) · 48.7 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
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
#
# Satochip 2-Factor-Authentication app for the Satochip Bitcoin Hardware Wallet
# (c) 2019 by Toporin - 16DMCk4WUaHofchAhpMaQS4UPm4urcy2dN
# Sources available on https://github.com/Toporin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty, ListProperty
from kivy.clock import Clock
from kivy.utils import platform
from kivy.storage.jsonstore import JsonStore
from kivy.logger import Logger
from kivy.logger import LoggerHistory
from datetime import datetime
from time import sleep
import logging
from os import urandom
from hashlib import sha1, sha256
import pyaes
import hmac
import certifi
import urllib3
import requests
import ssl
import json
import base64
import threading
import queue
from cryptos import transaction, main #deserialize
from cryptos.coins import Bitcoin, BitcoinCash, Litecoin
from cryptos.main import num_to_var_int
from cashaddress import convert # cashAddr conversion for bcash
from xmlrpc.client import ServerProxy
#ethereum utils
try:
from eth_utils import keccak
from py_eth_sig_utils import eip712 # ethereum sign_typed_message
import rlp
import eth_messages
import eth_transactions
except Exception as ex:
Logger.warning("Exception during ethereum package import: "+str(ex))
from TxParser import TxParser
DEBUG= True
DEBUG_SECRET_2FA= "00"*20 #b'\0'*20
APPROVE_TX="Approve tx!"
REJECT_TX="Reject tx!"
LOG_SEP="-"*60 + "\n"
BLOCKSIZE= 16
POLLING_INTERVAL= 5.0
if DEBUG:
Logger.setLevel(logging.DEBUG)
else:
Logger.setLevel(logging.INFO)
ca_path = certifi.where()
#print("CA Path: "+ca_path)
context = ssl.SSLContext()
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context.load_verify_locations(ca_path)
SERVER_LIST= [
'https://cosigner.electrum.org',
'https://cosigner.satochip.io',
'http://sync.imaginary.cash:8081',
]
if DEBUG: # debug server
SERVER_LIST.append('https://cosigner.satochip.io:81') #wrong port generate timeout errors
SERVER_LIST.append('https://wrongcosigner.satochip.io') # non-existant server
SERVER_LIST.append('https://www.google.com') # wrong server
#server_default= SERVER_LIST[0] #default
myqueue = queue.Queue()
myevent = threading.Event()
myevent.clear()
def get_message_from_server(obj, myqueue, myevent):
#print("In get_message_from_server")
while True:
if not myevent.isSet():
for keyhash in obj.myfactors.datastore.keys():
try:
#print("In get_message_from_server server: ", myserver.server)
message = myserver.server.get(keyhash)
if message is not None:
print("In get_message_from_server keyhash: ", keyhash)
print("In get_message_from_server message: ", message)
myqueue.put( (keyhash,message) )
myevent.set()
except TimeoutError as e:
Logger.warning("Satochip: server timeout: "+str(e))
myqueue.put( (keyhash,"ERR:TIMEOUT") )
myevent.set()
except Exception as e:
Logger.warning("Satochip: cannot contact server: "+str(e))
myqueue.put( (keyhash,"ERR:CONNECT") )
myevent.set()
sleep(POLLING_INTERVAL)
class Listener():
def __init__(self, parent):
self.parent = parent
self.received = set()
self.postbox = []
def clear(self, keyhash):
print('In clear() myserver.server: ', myserver.server)
myserver.server.delete(keyhash)
self.received.remove(keyhash)
myevent.clear() #
class Factors():
def __init__(self):
self.datastore = JsonStore('../data.json')
def add_new_factor(self,secret_2FA, label_2FA):
mac = hmac.new(bytes.fromhex(secret_2FA), "id_2FA".encode('utf-8'), sha1)
id_2FA_20b= mac.hexdigest()
id_2FA= sha256(mac.digest()).hexdigest()
mac = hmac.new(bytes.fromhex(secret_2FA), "key_2FA".encode('utf-8'), sha1)
key_2FA= mac.hexdigest()[0:32] # keep first 16 bytes out of 20
idreply_2FA=sha256(id_2FA.encode('utf-8')).hexdigest()
self.datastore.put(id_2FA, secret_2FA=secret_2FA, key_2FA= key_2FA, label_2FA=label_2FA, idreply_2FA=idreply_2FA, id_2FA_20b=id_2FA_20b)
Logger.info("Satochip: \nAdded new factor on "+ str(datetime.now())+"\n"
+"label: "+ label_2FA+"\n"
+"id_2FA: "+ id_2FA+"\n"
+"idreply_2FA: "+ idreply_2FA)
#Logger.debug("Satochip: secret_2FA"+ secret_2FA)
#Logger.debug("Satochip: key_2FA: "+ key_2FA)
def remove_factor(self, id):
if self.datastore.exists(id):
self.datastore.delete(id)
class Servers():
def __init__(self):
self.server_default= None
# on Android, stored in: /data/data/org.satochip.satochip2fa.satochip2fa/files/server.json
self.datastore = JsonStore('../servers.json')
# add SERVER_LIST if updated
for item in SERVER_LIST:
if item not in self.datastore:
self.add_new_server(item, default=False)
# get default server
for url in self.datastore.keys():
print('self.datastore[url]= ', self.datastore[url])
if self.datastore[url]['default']:
self.server_default= url
break
# if no default_server, take first of list
if self.server_default is None:
self.server_default= SERVER_LIST[0]
self.datastore[self.server_default]['default']= True
def add_new_server(self, url, default=False):
self.datastore.put(url, default=default)
Logger.info("Satochip: \nAdded new server on "+ str(datetime.now()) + " with url: "+ url)
def load_list_server(self):
myserver_list= []
for url in self.datastore.keys():
myserver_list.append(url)
return myserver_list
myservers= Servers()
class Server():
def __init__(self, url):
self.server= ServerProxy(url, allow_none=True, context=context)
self.url= url
def set_url(self,url):
self.server= ServerProxy(url, allow_none=True, context=context)
self.url= url
myserver= Server(myservers.server_default)
class OkButton(Button):
btn_approve_tx= StringProperty(APPROVE_TX)
class CancelButton(Button):
btn_reject_tx= StringProperty(REJECT_TX)
#class Satochip(GridLayout):
class Satochip(TabbedPanel):
btn_disabled= BooleanProperty(True)
btn_approve_qr_disabled= BooleanProperty(True)
display = StringProperty('Waiting for request...')
label_qr_data= StringProperty("Click on 'scan' button to scan a new QR code...")
label_logs= StringProperty('Contains the tx history...\n'+LOG_SEP)
label_2FA_label= StringProperty('Enter 2FA description here')
label_2FA_stored= StringProperty('')
myserver_list= ListProperty([])
myserver_default= StringProperty('')
def __init__(self, **kwargs):
super(Satochip, self).__init__(**kwargs)
self.listener = Listener(self)
self.myfactors= Factors()
if DEBUG:
self.myfactors.add_new_factor(DEBUG_SECRET_2FA, "Debug-2FA")
self.load_list_2FA()
# lists of servers to choose from
self.myserver_list= myservers.load_list_server()
self.myserver_default= myservers.server_default
#load log history
for record in reversed(LoggerHistory.history):
print(str(record))
if record.levelno>=20: #INFO
msg= record.getMessage()
if msg.startswith("[Satochip"):
self.label_logs+=msg.replace("[Satochip ] ","",1) +"\n"+LOG_SEP
def on_server_spinner(self, new_server):
Logger.info("Satochip: select new server: "+new_server)
self.myserver_default= new_server
old_server= myserver.url
myservers.datastore.put(new_server, default=True)
myservers.datastore.put(old_server, default=False)
myserver.set_url(new_server)
self.display = 'Waiting for request...'
def load_list_2FA(self):
self.label_2FA_stored="List of stored 2FA:\n\n"
for keyhash in self.myfactors.datastore.keys():
self.label_2FA_stored+="label: "+self.myfactors.datastore.get(keyhash)['label_2FA']+"\n"+"id: "+keyhash[0:32]+"...\n"+LOG_SEP
def approve_tx(self, btn):
letter= self.listener.postbox.pop()
keyhash= letter[0]
challenge= letter[1]
#compute response to challenge and send back...
reply= challenge+":"
if (btn.text == APPROVE_TX):
secret_2FA=bytes.fromhex(self.myfactors.datastore.get(keyhash)['secret_2FA'])
mac = hmac.new(secret_2FA, bytes.fromhex(challenge), sha1)
reply+= mac.hexdigest()
self.display = "Action approved!"
self.label_logs+= "Action approved" +"\n"+LOG_SEP
Logger.info("Satochip: APPROVED action with hash: "+challenge+" on "+str(datetime.now()))
else:
reply+= "00"*20
self.display = "Tx rejected!"
self.label_logs+= "Tx rejected" +"\n"+LOG_SEP
Logger.info("Satochip: REJECTED tx with hash: "+challenge+" on "+str(datetime.now()))
Logger.debug("Satochip: Challenge-response: "+ reply)
replyhash= self.myfactors.datastore.get(keyhash)['idreply_2FA']
# pad & encrypt reply
key_2FA= bytes.fromhex(self.myfactors.datastore.get(keyhash)['key_2FA'])
iv= urandom(16)
Logger.debug("Satochip: IV hex: "+ iv.hex())
plaintext= reply.encode("utf-8")
encrypter = pyaes.Encrypter(pyaes.AESModeOfOperationCBC(key_2FA, iv))
ciphertext = encrypter.feed(plaintext)
ciphertext += encrypter.feed()
ciphertext = iv+ciphertext
reply_encrypt= base64.b64encode(ciphertext).decode('ascii')
Logger.debug("Satochip: Reply_encrypt: "+reply_encrypt)
# send reply to server
Logger.debug("Satochip: Sent response to: "+replyhash)
myserver.server.put(replyhash, reply_encrypt)
self.listener.clear(keyhash)
self.btn_disabled= True
def update(self, dt):
# only one request at a time
if len(self.listener.received)!=0:
return
# check for message from the polling thread
print("Satochip update...")
if myevent.isSet(): # we have a message waiting!
try:
(keyhash, message)= myqueue.get(block=False) # non-blocking
except queue.Empty as ex: # should not happen!
#message= None
return
if message=="ERR:TIMEOUT":
self.display = "Error: server timeout! \nIf this persists, select another server in settings."
message= None
myevent.clear()
elif message == "ERR:CONNECT":
self.display = "Error: cannot connect to server! \nIf this persists, select another server in settings."
message= None
myevent.clear()
else:
try:
self.listener.received.add(keyhash)
label= self.myfactors.datastore.get(keyhash)['label_2FA']
txt="2FA: "+label+"\n"
Logger.debug("Satochip: Received challenge for: "+ keyhash)
Logger.debug("Satochip: Corresponding label: "+ label)
Logger.debug("Satochip: Challenge received: "+ message)
# decrypt message & remove padding
key_2FA= bytes.fromhex(self.myfactors.datastore.get(keyhash)['key_2FA'])
message= base64.b64decode(message)
iv= message[0:16]
ciphertext= message[16:]
decrypter = pyaes.Decrypter(pyaes.AESModeOfOperationCBC(key_2FA, iv))
decrypted = decrypter.feed(ciphertext)
decrypted += decrypter.feed()
Logger.debug("Satochip: Challenge decrypted: "+decrypted.decode('ascii'))
message= json.loads(decrypted)
if 'action' in message:
action= message['action']
else:
action= "sign_tx"
if action=="reset_seed":
authentikeyx= message['authentikeyx']
txt+= "Request: reset the seed!\nAuthentikey:"+authentikeyx
challenge= authentikeyx + 32*'FF'
elif action == "reset_2FA":
txt+= "Request: reset 2FA!\nID_2FA:"+keyhash
try:
id_2FA_20b= self.myfactors.datastore.get(keyhash)['id_2FA_20b']
except Exception as ex: # not supported for 2FA created in app version <=0.8/0.9
id_2FA_20b= keyhash
challenge= id_2FA_20b + 44*'AA'
elif action == "sign_msg":
msg= message['msg']
if 'alt' in message:
altcoin= message['alt']
headersize= bytes([ len(altcoin)+17 ])
paddedmsgbytes = headersize + altcoin.encode('utf8') + b" Signed Message:\n" + num_to_var_int(len(msg)) + bytes(msg, 'utf-8')
else:
altcoin= "Bitcoin"
paddedmsgbytes = b"\x18Bitcoin Signed Message:\n" + num_to_var_int(len(msg)) + bytes(msg, 'utf-8')
txt+= "Request: sign "+ altcoin +" message \n\n"+msg+"\n"
paddedmsghash= sha256(paddedmsgbytes).hexdigest()
challenge= paddedmsghash + 32*"BB"
elif action== "sign_tx":
txt+= "Request: sign transaction \n\n"
(tx_txt, challenge)= self.action_sign_tx(keyhash, message)
txt+= tx_txt
# sign message hash, currently only supports Ethereum
elif action== "sign_msg_hash":
altcoin= "Ethereum"
hash= message['hash']
msg= message['msg']
msg_type= message.get('msg_type','PERSONAL_MESSAGE')
if msg_type== "PERSONAL_MESSAGE" or msg_type== "MESSAGE":
# if message is in hex-format, convert it to utf8 string
if msg.startswith('0x'):
msg= msg[len('0x'):]
try:
int(msg, 16) # check if hex
import codecs
msg= codecs.decode(msg, "hex").decode('utf-8')
except Exception as ex:
msg= message['msg']
paddedmsghash= bytes(eth_messages.defunct_hash_message(text=msg)).hex() #paddedmsghash= bytes(messages.defunct_hash_message(text=msg)).hex() #sha256(paddedmsgbytes).hexdigest()
Logger.info("Msg hash1: "+hash)
Logger.info("Msg hash2: "+paddedmsghash)
txt+= "Request: sign "+ altcoin +" message \n\n"+msg+"\n\n"
if paddedmsghash!=hash:
txt+= "Warning: inconsistent msg hashes! \nYou should reject the request unless you know what you are doing!"
challenge= hash + 32*"CC"
elif msg_type== "TYPED_MESSAGE":
try:
txt+= "Request: sign "+ altcoin +" typed message \n\n"
json_data= json.loads(msg)
# check if domainSeparatorHex, hashStructMessageHex are present...
if "typedData" in json_data:
typed_data= json_data["typedData"]
else:
typed_data= json_data
try:
Logger.info(f"typed_data= {typed_data}")
msg_hash= eip712.encoding.encode_typed_data(typed_data).hex()
#raise Exception("Debug EIP712 build")
txt+= f"{typed_data}\n\n"
if msg_hash!=hash:
Logger.info(f"inconsistent msg hashes! computed hash: {msg_hash}")
txt+= "Warning: inconsistent msg hashes! \nYou should reject the request unless you know what you are doing!\n"
except Exception as ex:
# fallback: use domainSeparatorHex & hashStructMessageHex hashes for blind Signing
# This is NOT compliant with walletconnect specifications...
Logger.info(f"Exception while parsing typedData: {ex}")
domainSeparatorHex= json_data["domainSeparatorHex"]
hashStructMessageHex= json_data["hashStructMessageHex"]
msg_hash= keccak(bytes.fromhex('19') +
bytes.fromhex('01') +
bytes.fromhex(domainSeparatorHex) +
bytes.fromhex(hashStructMessageHex)).hex()
txt+= f"WARNING: could not parse typed_data (error: {ex}).\nBlind signing using hash: {hash}\n"
if msg_hash!=hash:
txt+= "Warning: inconsistent msg hashes! \nYou should reject the request unless you know what you are doing!\n"
Logger.info(f"Blind signing using msg_hash: {msg_hash}")
except Exception as ex:
txt= f"Failed to parse typed message with error: {ex}. \nYou should reject the request unless you know what you are doing!"
Logger.info(f"Exception while parsing typed_message: {ex}")
challenge= hash + 32*"CC"
# sign tx hash, currently supports Ethereum
elif action== "sign_tx_hash":
try:
# TODO: put in function
tx= message['tx']
hash= message['hash']
txbytes= bytes.fromhex(tx)
# parse eth tx type
txfirstbyte= txbytes[0]
if 0xc0 <= txfirstbyte <= 0xfe:
tx_type= 0
#tx_hash = keccak(txbytes).hex() # serialization depends on client, better deserialize and recomputes hash
txrlp= rlp.decode(txbytes, eth_transactions.Transaction)
tx_nonce= txrlp.nonce
tx_gas= txrlp.gas
tx_gas_price=txrlp.gas_price
tx_to='0x'+txrlp.to.hex()
tx_value= txrlp.value
tx_data='0x'+ txrlp.data.hex()
tx_chainid= message.get('chainId', txrlp.v) #
try:
tx_from= '0x'+message['from']
except Exception as ex:
tx_from= '(not supported)'
# reencode with chainId (EIP155)
tx2= eth_transactions.Transaction(txrlp.nonce, txrlp.gas_price, txrlp.gas, txrlp.to, txrlp.value, txrlp.data, tx_chainid, 0, 0)
txraw= rlp.encode(tx2)
print("txraw: " + txraw.hex())
tx_hash = keccak(txraw).hex()
print("tx_hash: " + tx_hash)
elif txfirstbyte== 0x2:
tx_type= 0x2
txrlp= rlp.decode(txbytes[1:], eth_transactions.TransactionEIP1559)
tx_nonce= txrlp.nonce
tx_gas= txrlp.gas
tx_to='0x'+txrlp.to.hex()
tx_value= txrlp.value
tx_data='0x'+ txrlp.data.hex()
tx_chainid= message.get('chainId', txrlp.chain_id)
tx_max_priority_fee_per_gas= txrlp.max_priority_fee_per_gas
tx_max_fee_per_gas= txrlp.max_fee_per_gas
tx_access_list= txrlp.access_list
try:
tx_from= '0x'+message['from']
except Exception as ex:
tx_from= '(not supported)'
# compute hash
tx_hash = keccak(txbytes).hex()
print("tx_hash: " + tx_hash)
else:
raise Exception(f'Unsupported Ethereum transaction type: {hex(txfirstbyte)}')
try:
chain_file= "./chains/_data/chains/eip155-"+str(tx_chainid)+".json"
Logger.info("ChainId file:"+chain_file)
chain_store = JsonStore(chain_file)
tx_coinname= chain_store.get("nativeCurrency")["name"]
tx_coinsymbol= chain_store.get("nativeCurrency")["symbol"]
tx_coindecimals=chain_store.get("nativeCurrency")["decimals"]
except Exception as ex:
tx_coinname= "(unknown)"
tx_coinsymbol= "(unknown)"
tx_coindecimals= 0
txt+= "Request: sign transaction \n\n"
#txt+="2FA: "+label+"\n"
txt+="Coin: "+tx_coinname+"\n"
txt+="From: "+tx_from+"\n"
txt+="To: "+tx_to+"\n"
txt+="Value: "+ str(tx_value/(10**tx_coindecimals)) +" " +tx_coinsymbol+"\n"
txt+="Gas: "+str(tx_gas)+"\n"
if tx_type==0x0:
txt+="Gas price: "+str(tx_gas_price)+"\n"
txt+="Max fee: "+str(tx_gas*tx_gas_price/(10**tx_coindecimals)) +" " +tx_coinsymbol+"\n"
elif tx_type==0x2:
txt+="Max fee per gas: "+str(tx_max_fee_per_gas)+"\n"
txt+="Max priority fee per gas: "+str(tx_max_priority_fee_per_gas)+"\n"
txt+="Data: "+tx_data+"\n" #todo: parse data
#txt+="Hash: "+tx_hash+"\n" #debug
if tx_hash!=hash:
txt+= "\nWarning! inconsistent tx hashes! \nYou should reject the request unless you know what you are doing!"
challenge= hash + 32*"CC"
except Exception as ex:
txt= f"Exception while parsing transaction: {ex}. \n\nYou should reject the request unless you know what you are doing!"
challenge= hash + 32*"CC"
else:
txt= "Unsupported operation: "+decrypted.decode('ascii')
challenge= 64*"00"
# 2FA challenges:
# - Tx approval: [ 32b Tx hash | 32-bit 0x00-padding ]
# - ECkey import:[ 32b coordx | 32-bit (0x10^key_nb)-padding ]
# - ECkey reset: [ 32b coordx | 32-bit (0x20^key_nb)-padding ]
# - 2FA reset: [ 20b 2FA_ID | 44-bit 0xAA-padding ]
# - Seed reset: [ 32b authentikey-coordx | 32-bit 0xFF-padding ]
# - Msg signing: [ 32b SHA26(btcHeader+msg) | 32-bit 0xBB-padding ]
# - Hash signing: [ 32b hash | 32-bit 0xBB-padding]
self.listener.postbox.append([keyhash, challenge])
self.display = txt
self.label_logs+= txt +"\n"
Logger.info("Satochip: \nNew challenge: "+challenge+"\nReceived on "+str(datetime.now())+":\n"+txt)
self.btn_disabled= False
return
except Exception as ex:
txt= f"Error during parsing of the request: {ex}" #TODO: pretty print message
self.display = txt
self.label_logs+= txt
Logger.warning(txt)
self.listener.clear(keyhash) # remove faulty request from server
def action_sign_tx(self, keyhash, message):
is_segwit= message['sw']
# coin type:
coin_type= message['ct']
if coin_type==0:
coin= Bitcoin(False)
elif coin_type==1: #btc testnet
coin= Bitcoin(True)
elif coin_type==2: #litecoin
istest= message['tn']
coin= Litecoin(testnet=istest)
elif coin_type==145: #bcash
istest= message['tn']
coin= BitcoinCash(testnet=istest)
is_segwit= True # bcash uses BIP143 for signature hash creation
else:
Logger.warning("Satochip: Coin not (yet) supported: "+str(coin_type))
coin=BaseCoin()
txt="Coin: "+coin.display_name+"\n"
# parse tx into a clear message for approval
pre_tx_hex=message['tx']
pre_tx= bytes.fromhex(pre_tx_hex)
pre_hash_hex= sha256(sha256(pre_tx).digest()).hexdigest()
challenge= pre_hash_hex+ 32*'00'
if is_segwit:
#parse segwit tx
txin_type=message['ty']
txparser= TxParser(pre_tx)
while not txparser.is_parsed():
chunk= txparser.parse_segwit_transaction()
Logger.debug("Satochip: hashPrevouts: "+txparser.hashPrevouts.hex())
Logger.debug("Satochip: hashSequence: "+txparser.hashSequence.hex())
Logger.debug("Satochip: txOutHash: "+txparser.txOutHash[::-1].hex())
Logger.debug("Satochip: txOutIndex: "+str(txparser.txOutIndexLong))
Logger.debug("Satochip: inputScript: "+txparser.inputScript.hex())
Logger.debug("Satochip: inputAmount: "+str(txparser.inputAmountLong))
Logger.debug("Satochip: nSequence: "+txparser.nSequence.hex())
Logger.debug("Satochip: hashOutputs: "+txparser.hashOutputs.hex())
Logger.debug("Satochip: nLocktime: "+txparser.nLocktime.hex())
Logger.debug("Satochip: nHashType: "+txparser.nHashType.hex())
script= txparser.inputScript.hex()
if txin_type== 'p2wpkh':
hash= transaction.output_script_to_h160(script)
hash= bytes.fromhex(hash)
addr= coin.hash_to_segwit_addr(hash)
Logger.debug("Satochip: p2wpkh address: "+addr)
elif txin_type== 'p2wsh': #multisig-segwit
addr= coin.script_to_p2wsh(script)
Logger.debug("Satochip: p2wsh address: "+addr)
elif txin_type== 'p2wsh-p2sh':
h= transaction.output_script_to_h160(script)
addr= coin.p2sh_scriptaddr("0020"+h)
Logger.debug("Satochip: p2wsh-p2sh address: "+addr)
elif txin_type== 'p2wpkh-p2sh':
# for p2wpkh-p2sh addres is derived from script hash, see https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH_nested_in_BIP16_P2SH
h= transaction.output_script_to_h160(script)
addr= coin.p2sh_scriptaddr("0014"+h)
Logger.debug("Satochip: p2wpkh-p2sh address: "+addr)
elif (coin_type==145) and (txin_type== 'p2pkh' or txin_type== 'p2sh'): # for bcash
addr= coin.scripttoaddr(script)
addr= convert.to_cash_address(addr) #cashAddr conversion
addr= addr.split(":",1)[-1] #remove prefix
Logger.debug("Satochip: txin type: "+ txin_type +" address: "+addr)
else:
addr= "unsupported script:"+script+"\n"
txt+="input:\n"
txt+= " "+"address: "+addr+" spent: "+str(txparser.inputAmountLong/100000)+"\n" #satoshi to mBtc
#parse outputs
outputs_hex= message['txo']
outputs= bytes.fromhex(outputs_hex)
hashOutputs=sha256(sha256(outputs[1:]).digest()).hexdigest()
outparser= TxParser(outputs)
while not outparser.is_parsed():
chunk= outparser.parse_outputs()
nb_outs= outparser.txCurrentOutput
Logger.debug("Satochip: nbrOutputs: "+str(nb_outs))
#txt+="nb_outputs: "+str(nb_outs) + "\n"
#txt+="outputs:\n"
txt+="outputs ("+ str(nb_outs) +"):\n"
amnt_out=0
for i in range(nb_outs):
amnt= outparser.outAmounts[i]
amnt_out+=amnt
script= outparser.outScripts[i].hex()
is_data_script=False
Logger.debug("Satochip: outScripts: "+script)
Logger.debug("Satochip: amount: "+str(amnt))
if script.startswith( '76a914' ):#p2pkh
addr= coin.scripttoaddr(script)
elif script.startswith( 'a914' ): #p2sh
addr= coin.scripttoaddr(script)
elif script.startswith( '0014' ):#p2wpkh
hash= bytes.fromhex(script[4:])
addr= coin.hash_to_segwit_addr(hash)
elif script.startswith( '0020' ): #p2wsh
hash= bytes.fromhex(script[4:])
addr= coin.hash_to_segwit_addr(hash)
elif script.startswith( '6a' ): # op_return data script
if script.startswith( '6a04534c5000' ): # SLP token
try:
addr= self.parse_slp_script(script)
except Exception as ex:
addr= 'Error during SLP script parsing! '
Logger.warning("Error during SLP script parsing: "+str(ex))
else:
addr= "DATA: "+ bytes.fromhex(script[6:]).decode('utf-8', errors='backslashreplace') # errors='ignore', 'backslashreplace', 'replace'
is_data_script= True
else:
addr= "unsupported script:"+script+"\n"
if coin_type==145 and not is_data_script:
addr= convert.to_cash_address(addr) #cashAddr conversion
addr= addr.split(":",1)[-1] #remove prefix
Logger.debug("Satochip: address: "+addr)
txt+= " "+"address: "+addr+" spent: "+str(amnt/100000)+"\n" #satoshi to mBtc
txt+= " "+"total: "+str(amnt_out/100000)+" m"+coin.coin_symbol+"\n" #satoshi to mBtc
if hashOutputs!=txparser.hashOutputs.hex():
txt+= "Warning! inconsistent output hashes!\n"
# non-segwit tx
else:
pre_tx_dic={}
try:
pre_tx_dic= transaction.deserialize(pre_tx)
except Exception as e:
Logger.warning("Exception during (non-segwit) tx parsing: "+str(e))
txt="Error parsing tx!"
self.listener.clear(keyhash) # TODO: remove & catch in update()
return
Logger.debug("Satochip: pre_tx_dic: "+str(pre_tx_dic))
# inputs
amount_in=0
ins= pre_tx_dic['ins']
nb_ins= len(ins)
#txt+="nb_inputs: "+str(nb_ins) + "\n"
#txt+="inputs:\n"
txt+="inputs ("+ str(nb_ins) +"):\n"
for i in ins:
script= i['script'].hex()
Logger.debug("Satochip: input script: "+script)
# recover script and corresponding addresse
if script=="":# all input scripts are removed for signing except 1
outpoint= i['outpoint']
hash= outpoint['hash'].hex()
index= outpoint['index']
#Logger.debug('Satochip: hash: hash:index: ' +hash+":"+str(index))
tx= coin.fetchtx(hash)
#Logger.debug('Satochip: tx: '+str(tx))
outs= tx['out']
out= outs[index]
val= out['value']
script= out['script']
addr= coin.scripttoaddr(script)
addr= "(empty for signing: "+ addr[0:16] +"...)"
if script.endswith("ae"):#m-of-n pay-to-multisig
m= int(script[0:2], 16)-80
n= int(script[-4:-2], 16)-80
txt+=" "+"multisig "+str(m)+"-of-"+str(n)+"\n"
addr= coin.p2sh_scriptaddr(script)
Logger.debug("Satochip: address multisig: "+addr)
else: #p2pkh, p2sh
addr= coin.scripttoaddr(script)
Logger.debug("Satochip: address: "+addr)
# get value from blockchain explorer
val=0
try:
unspent= coin.unspent_web(addr)
for d in unspent:
val+=d['value']
except Exception as e:
Logger.warning("Exception during coin.unspent_web request: "+str(e))
#try to get value from electrum server (seem slow...)
# try:
# hs= sha256(bytes.fromhex(script)).digest()
# hs= hs[::-1]
# balances= coin.balance(True, hs.hex())
# val= sum(balances)
# except Exception as e:
# Logger.warning("Exception during coin.balance request: "+str(e))
txt+=" "+"address: "+addr+" balance: "+str(val/100000) +"\n"
amount_in+=val
txt+=" "+"total: "+str(amount_in/100000)+" m"+coin.coin_symbol+"\n" #satoshi to mBtc
# outputs
fee=0
amount_out=0
outs= pre_tx_dic['outs']
nb_outs= len(outs)
#txt+="nb_outputs: "+str(nb_outs) + "\n"
#txt+="outputs:\n"
txt+="outputs ("+ str(nb_outs) +"):\n"
for o in outs:
val= (o['value'])
script= o['script'].hex()
Logger.debug("Satochip: output script: "+script)
if script.startswith( '76a914' ):# p2pkh
addr= coin.scripttoaddr(script)
elif script.startswith( 'a914' ): # p2sh
addr= coin.scripttoaddr(script)
elif script.startswith( '0014' ):#p2wpkh
hash= bytes.fromhex(script[4:])
addr= coin.hash_to_segwit_addr(hash)
elif script.startswith( '0020' ):#p2wsh
hash= bytes.fromhex(script[4:])
addr= coin.hash_to_segwit_addr(hash)
else:
addr= "unsupported script:"+script+"\n"
txt+=" "+"address: "+addr+" spent: "+str(val/100000)+"\n" #satoshi to mBtc
amount_out+=val
txt+=" "+"total: "+str(amount_out/100000)+" m"+coin.coin_symbol+"\n" #satoshi to mBtc
fee= amount_in-amount_out
if fee >=0:
txt+=" "+"fees: "+str(fee/100000)+" m"+coin.coin_symbol+"\n" #satoshi to mBtc
return (txt, challenge)
def scan_qr(self, on_complete):
if platform != 'android':
print("Qr code scanning is not supported!")
save_popup = SaveDialog(self)
save_popup.open()
return
from jnius import autoclass, cast
from android import activity
PythonActivity = autoclass('org.kivy.android.PythonActivity')
SimpleScannerActivity = autoclass("org.electrum.qr.SimpleScannerActivity")
Intent = autoclass('android.content.Intent')
intent = Intent(PythonActivity.mActivity, SimpleScannerActivity)
def on_qr_result(requestCode, resultCode, intent):
try:
if resultCode == -1: # RESULT_OK:
# this doesn't work due to some bug in jnius:
# contents = intent.getStringExtra("text")
String = autoclass("java.lang.String")
contents = intent.getStringExtra(String("text"))
on_complete(contents)
finally:
activity.unbind(on_activity_result=on_qr_result)
activity.bind(on_activity_result=on_qr_result)
PythonActivity.mActivity.startActivityForResult(intent, 0)
def on_qr(self, data):
self.label_qr_data= data.strip()
self.btn_approve_qr_disabled=False
def on_approve_qr(self):
try:
secret_2FA= bytearray.fromhex(self.label_qr_data) #check if hex
self.myfactors.add_new_factor(self.label_qr_data, self.label_2FA_label)
self.label_qr_data= "QR code added!"
self.label_logs+= "Second factor added\nlabel: "+self.label_2FA_label+"\n"+LOG_SEP
self.label_2FA_stored+= self.label_2FA_label+"\n"
self.load_list_2FA()
except ValueError:
Logger.warning("Satochip: Error: the qr code should provide a hexadecimal value")
self.label_qr_data= "Error: code should be a hexadecimal value"
self.label_logs+= "Error: the qr code should provide a hexadecimal value"+"\n"+LOG_SEP
self.btn_approve_qr_disabled=True
def parse_slp_script(self, script):
''' Basic script parsing for SLP tokens
See https://github.com/simpleledger/slp-specifications/blob/master/slp-token-type-1.md#formatting
keywords arg:
script: hex script to parse
returns:
a human-readable description of the SLP script
'''
dic_token_type={1:'token', 2:'security token', 3:'voting token', 4:'ticketing token'}
txt=''
offset=0
script_size= len(script)
def get_array_size(script, offset):
array_size= int( script[offset:(offset+2)], 16)
if (array_size>0 and array_size<76):
new_offset= offset+2
elif (array_size== 0x4c):
array_size= int( script[(offset+2):(offset+4)], 16)
new_offset= offset+4
elif (array_size== 0x4d):
array_size= int( script[(offset+2):(offset+4)], 16) +256*int( script[(offset+4):(offset+6)], 16)
new_offset= offset+6
elif (array_size== 0x4e):
array_size= ( int( script[(offset+2):(offset+4)], 16) +
int( script[(offset+4):(offset+6)], 16)*256 +
int( script[(offset+6):(offset+8)], 16)*65536 +
int( script[(offset+8):(offset+10)], 16)*16777216 )
new_offset= offset+10
else:
raise Exception(f'Error during SLP script parsing: bad opcode {hex(array_size)} !')
return (array_size, new_offset)
def get_array(script, offset, size):
array= script[offset:(offset+2*size)]
new_offset= offset+2*size
return (array, new_offset)
if script.startswith( '6a04534c5000' ):
lokad_id= "04534c5000"
offset+= 12
token_type_size, offset = get_array_size(script, offset)
token_type, offset= get_array(script, offset, token_type_size)
token_type= int(token_type, 16)
token_type_str= dic_token_type.get(token_type, 'unknown_type token')
transaction_type_size, offset = get_array_size(script, offset)
transaction_type, offset= get_array(script, offset, transaction_type_size)
transaction_type_str= bytes.fromhex(transaction_type).decode('utf-8', 'backslashreplace')
txt+= transaction_type_str + ' SLP ' + token_type_str
if (transaction_type=='47454e45534953'): # GENESIS
token_ticker_size, offset = get_array_size(script, offset)
token_ticker, offset= get_array(script, offset, token_ticker_size)
token_ticker_str= bytes.fromhex(token_ticker).decode('utf-8', 'backslashreplace')
txt+= '\n' + 16*' ' + ' with ticker: ' + token_ticker_str
token_name_size, offset = get_array_size(script, offset)
token_name, offset= get_array(script, offset, token_name_size)
token_name_str= bytes.fromhex(token_name).decode('utf-8', 'backslashreplace')
txt+= '\n' + 16*' ' + ' with name: ' + token_name_str
token_document_url_size, offset = get_array_size(script, offset)
token_document_url, offset= get_array(script, offset, token_document_url_size)
token_document_hash_size, offset = get_array_size(script, offset)
if (token_document_hash_size==32):
token_document_hash, offset= get_array(script, offset, token_document_hash_size)
txt+= '\n' + 16*' ' + ' with doc hash: ' + token_document_hash[0:16] + '...' + token_document_hash[-16:]
elif (token_document_hash_size==0):
pass
else:
return 'Error during SLP script parsing: bad token_document_hash_size!'
decimals_size, offset = get_array_size(script, offset)#= int( script[offset:(offset+2)], 16) ;
decimals, offset= get_array(script, offset, decimals_size)
decimals= int( decimals, 16)
txt+= '\n' + 16*' ' + ' with decimals: ' + str(decimals)
mint_baton_vout_size, offset = get_array_size(script, offset)
if (mint_baton_vout_size==1):
mint_baton_vout, offset= get_array(script, offset, mint_baton_vout_size)
mint_baton_vout= int( mint_baton_vout, 16)
txt+= '\n' + 16*' ' + ' with unique issuance: ' + 'NO'
elif (mint_baton_vout_size==0):
txt+= '\n' + 16*' ' + ' with unique issuance: ' + 'YES'
else:
return 'Error during SLP script parsing: bad mint_baton_vout_size!'
initial_token_mint_quantity_size, offset = get_array_size(script, offset)
initial_token_mint_quantity, offset= get_array(script, offset, initial_token_mint_quantity_size)
initial_token_mint_quantity= int( initial_token_mint_quantity, 16)
txt+= '\n' + 16*' ' + ' with initial issuance: ' + str(initial_token_mint_quantity/(10**decimals)) + 16*' '
txt+= '\n' + 16*' ' + ' with ' # for the spent: value
elif (transaction_type=='53454e44'): #SEND
token_id_size, offset = get_array_size(script, offset)
if (token_id_size!=32):
return 'Error during SLP script parsing: bad ID size!'
token_id, offset= get_array(script, offset, token_id_size)
txt+= '\n' + 16*' ' + 'with ID: ' + token_id[0:16] + '...' + token_id[-16:]
while (offset<script_size):
amount_size, offset = get_array_size(script, offset)
if (amount_size!=8):
return f'Error during SLP script parsing: bad amount size ({amount_size})!'
amount, offset = get_array(script, offset, amount_size)
amount= int(amount, 16)
txt+= '\n' + 16*' ' + 'with amount: ' + str(amount) + 16*' '
txt+= '\n' + 16*' ' + 'with ' # for the spent: value
elif (transaction_type== '4d494e54'): # MINT
token_id_size, offset = get_array_size(script, offset)
if (token_id_size!=32):
return 'Error during SLP script parsing: bad ID size!'
token_id, offset = get_array(script, offset, token_id_size)
txt+= '\n' + 16*' ' + ' with ID: ' + token_id[0:16] + '...' + token_id[-16:]
mint_baton_vout_size, offset = get_array_size(script, offset)
if (mint_baton_vout_size==1):
mint_baton_vout, offset = get_array(script, offset, mint_baton_vout_size)
mint_baton_vout= int( mint_baton_vout, 16)
txt+= '\n' + 16*' ' + ' with future issuance allowed: ' + 'YES'
elif (mint_baton_vout_size==0):
txt+= '\n' + 16*' ' + ' with future issuance allowed: ' + 'NO'
else:
return f'Error during SLP script parsing: bad mint_baton_vout_size {mint_baton_vout_size}!'
additional_token_quantity_size, offset = get_array_size(script, offset)
additional_token_quantity, offset = get_array(script, offset, additional_token_quantity_size)
additional_token_quantity= int( additional_token_quantity, 16)
txt+= '\n' + 16*' ' + ' with token issuance: ' + str(additional_token_quantity) + 16*' '
txt+= '\n' + 16*' ' + ' with ' # for the spent: value
elif (transaction_type== '434f4d4d4954'): # COMMIT
token_id_size, offset = get_array_size(script, offset)
token_id, offset = get_array(script, offset, token_id_size)
txt+= '\n' + 16*' ' + ' with ID: ' + token_id[0:16] + '...' + token_id[-16:]
# todo: parse rest of data
else:
txt= 'unknown tx type: ' + transaction_type_str + ' SLP ' + token_type_str
else:
txt+= 'Error during parsing: bad SLP script!'
return txt
class TestApp(App):
def build(self):
self.title = 'Satochip 2-Factor Authentication App'
root= Satochip()
# thread for polling server
print("Starting polling thread")
t = threading.Thread( target=get_message_from_server, args = (root, myqueue, myevent) )
t.daemon = True
t.start()
Clock.schedule_interval(root.update, POLLING_INTERVAL)
return root
# on platform where qr code scanning is not (yet) supported, it is possible to copy/paste the 2FA key via a popup windows...
class SaveDialog(Popup):
def __init__(self,my_widget,**kwargs):
print("Debug in")
super(SaveDialog,self).__init__(**kwargs)
self.my_widget = my_widget
#txt input
self.title='Set 2FA key'
self.content = BoxLayout(orientation="vertical")
self.name_input = TextInput(text='enter 2FA key here...')
#buttons
self.content2 = BoxLayout(orientation="horizontal")
self.save_button = Button(text='Save')
self.save_button.bind(on_press=self.save)
self.cancel_button = Button(text='Cancel')
self.cancel_button.bind(on_press=self.cancel)
self.content2.add_widget(self.save_button)