-
Notifications
You must be signed in to change notification settings - Fork 0
/
xpop.py
1063 lines (858 loc) · 32.7 KB
/
xpop.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
import cv2
import sys
import time
from signal import signal, SIGINT
from threading import Thread, Lock
from pyzbar.pyzbar import decode
import json
import brotli
import base64
import tkinter
import PIL.Image, PIL.ImageTk
import math
import json
import xrpl
import hashlib
from binascii import hexlify, unhexlify
import math
import base64
import pyqrcode
from datetime import datetime
# Set according to what your connected camera is
CAMERAPORT = 0
def err(e):
sys.stderr.write("Error: " + e + "\n")
return False
# This function shouldn't technically be needed but xrpl-py needs updating
# for validation fields. There's only one signing field: sfSignature, we need to
# clip around it.
# @return {
# without_signature: input sans signing field,
# key: key, signature: signature, ledger_hash: ledgerhash }
def process_validation_message(val):
if type(val) == str:
val = unhexlify(val)
upto = 0
rem = len(val)
ret = {}
sig_start = 0
sig_end = 0
# Flags
if val[upto] != 0x22 or rem < 5:
return err("validation: sfFlags missing")
upto += 5; rem -= 5;
# LedgerSequence
if val[upto] != 0x26 or rem < 5:
return err("validation: sfLedgerSequence missing")
upto += 5; rem -= 5;
# CloseTime (optional)
if val[upto] == 0x27:
if rem < 5:
return err("validation: sfCloseTime missing payload")
upto += 5; rem -= 5
# SigningTime
if val[upto] != 0x29 or rem < 5:
return err("validation: sfSigningTime missing")
upto += 5; rem -= 5;
# LoadFee (optional)
if val[upto] == 0x20 and rem >= 2 and val[upto+1] == 0x18:
if rem < 6:
return err("validation: sfLoadFee missing payload")
upto += 6; rem -= 6
# ReserveBase (optional)
if val[upto] == 0x20 and rem >= 2 and val[upto+1] == 0x1F:
if rem < 6:
return err("validation: sfReserveBase missing payload")
upto += 6; rem -= 6
# ReserveIncrement (optional)
if val[upto] == 0x20 and rem >= 2 and val[upto+1] == 0x20:
if rem < 6:
return err("validation: sfReserveIncrement missing payload")
upto += 6; rem -= 6
# BaseFee (optional)
if val[upto] == 0x35:
if rem < 9:
return err("validation: sfBaseFee missing payload")
upto += 9; rem -= 9
# Cookie (optional)
if val[upto] == 0x3A:
if rem < 9:
return err("validation: sfCookie missing payload")
upto += 9; rem -= 9
# ServerVersion (optional)
if val[upto] == 0x3B:
if rem < 9:
return err("validation: sfServerVersion missing payload")
upto += 9; rem -= 9
# LedgerHash
if val[upto] != 0x51 or rem < 33:
return err("validation: sfLedgerHash missing")
ret["ledger_hash"] = str(hexlify(val[upto+1:upto+33]), 'utf-8').upper()
upto += 33; rem -= 32
# ConsensusHash (optional)
if val[upto] == 0x50 and rem >= 2 and val[upto+1] == 0x17:
if rem < 34:
return err("validation: sfConsensusHash payload missing")
upto += 34; rem -= 34
# ValidatedHash (optioonal)
if val[upto] == 0x50 and rem >= 2 and val[upto+1] == 0x19:
if rem < 34:
return err("validation: sfValidatedHash payload missing")
upto += 34; rem -= 34
# SigningPubKey
if val[upto] != 0x73 or rem < 3:
return err("validation: sfSigningPubKey missing")
keysize = val[upto+1]
upto += 2; rem -= 2
if keysize > rem:
return err("validation: sfSigningPubKey incomplete")
ret["key"] = str(hexlify(val[upto:upto+keysize]), 'utf-8').upper()
upto += keysize; rem -= keysize
# Signature
sigstart = upto
if val[upto] != 0x76 or rem < 3:
return err("validation: sfSignature missing")
sigsize = val[upto+1]
upto += 2; rem -= 2
if sigsize > rem:
return err("validation: sfSignature incomplete")
ret["signature"] = val[upto:upto+sigsize]
upto += sigsize; rem -= sigsize
ret["without_signature"] = val[:sigstart] + val[upto:]
return ret
# Create a variable length prefix for a xrpl serialized vl field
def make_vl_bytes(l):
if type(l) == float:
l = ceil(l)
if type(l) != int:
return False
if l <= 192:
return bytes([l])
elif l <= 12480:
b1 = math.floor((l - 193) / 256 + 193)
return bytes([b1, l - 193 - 256 * (b1 - 193)])
elif l <= 918744:
b1 = math.floor((l - 12481) / 65536 + 241)
b2 = math.floor((l - 12481 - 65536 * (b1 - 241)) / 256)
return bytes([b1, b2, l - 12481 - 65536 * (b1 - 241) - 256 * b2])
else:
return err("Cannot generate vl for length = " + str(l) + ", too large")
def sha512(x):
m = hashlib.sha512()
m.update(x)
return m.digest()
def sha512h(x):
m = hashlib.sha512()
m.update(x)
return m.digest()[:32]
def hash_txn(txn):
if type(txn) == str:
txn = unhexlify(txn)
return sha512h(b'TXN\x00' + txn)
# Hash the txn and meta data as a leaf node in the shamap
def hash_txn_and_meta(txn, meta):
if type(txn) == str:
txn = unhexlify(txn)
if type(meta) == str:
meta = unhexlify(meta)
vl1 = make_vl_bytes(len(txn))
vl2 = make_vl_bytes(len(meta))
if vl1 == False or vl2 == False:
return False
return sha512h(b'SND\x00' + vl1 + txn + vl2 + meta + hash_txn(txn))
def hash_ledger(idx, coins, phash, txroot, acroot, pclose, close, res, flags):
if type(idx) == str:
idx = int(idx)
if type(coins) == str:
coins = int(coins)
if type(phash) == str:
phash = unhexlify(phash)
if type(txroot) == str:
txroot = unhexlify(txroot)
if type(acroot) == str:
acroot = unhexlify(acroot)
if type(pclose) == str:
pclose = int(pclose)
if type(close) == str:
close = int(close)
if type(res) == str:
res = int(res)
if type(flags) == str:
flags = int(flags)
if type(idx) != int or type(coins) != int or type(pclose) != int \
or type(close) != int or type(res) != int or type(flags) != int:
return err("Invalid int arguments to hash_ledger")
idx = int.to_bytes(idx, byteorder='big', length=4)
coins = int.to_bytes(coins, byteorder='big', length=8)
pclose = int.to_bytes(pclose, byteorder='big', length=4)
close = int.to_bytes(close, byteorder='big', length=4)
res = int.to_bytes(res, byteorder='big', length=1)
flags = int.to_bytes(flags, byteorder='big', length=1)
if type(phash) != bytes or type(txroot) != bytes or type(acroot) != bytes:
return err("Invalid bytes arguments to hash_ledger")
return sha512h(b'LWR\x00' + idx + coins + phash + txroot + acroot + pclose + close + res + flags)
def hash_proof(proof):
if type(proof) != list:
return err('Proof must be a list')
if len(proof) < 16:
return False
hasher = hashlib.sha512()
hasher.update(b'MIN\x00')
for i in range(16):
if type(proof[i]) == str:
hasher.update(unhexlify(proof[i]))
elif type(proof[i]) == list:
hasher.update(hash_proof(proof[i]))
else:
return err("Unknown object in proof list")
return hasher.digest()[:32]
def proof_contains(proof, h):
if type(proof) != list or len(proof) < 16:
return False
if type(h) == str:
h = unhexlify(h)
for i in range(16):
if type(proof[i]) == str and unhexlify(proof[i]) == h or \
type(proof[i]) == list and proof_contains(proof[i], h):
return True
return False
def verify(xpop, vl_key):
if type(xpop) == str:
try:
#print('raw xpop', xpop)
xpop = json.loads(xpop)
except:
return err("Invalid json")
if type(xpop) != dict:
return err("Expecting either a string or a dict")
if not "ledger" in xpop:
return err("XPOP did not contain ledger")
if not "validation" in xpop:
return err("XPOP did not contain validation")
if not "transaction" in xpop:
return err("XPOP did not contain transaction")
ledger = xpop["ledger"]
validation = xpop["validation"]
transaction = xpop["transaction"]
if not "unl" in validation:
return err("XPOP did not contain valdation.unl")
if not "data" in validation:
return err("XPOP did not contain validation.data")
unl = validation["unl"]
data = validation["data"]
if not "public_key" in unl:
return err("XPOP did not contain validation.unl.public_key")
if type(vl_key) == bytes:
vl_key = hexlify(vl_key).upper()
##
## Part A: Validate and decode UNL
##
# 1. If the vl key is wrong then everything is wrong.
if vl_key.lower() != unl["public_key"].lower():
return err("XPOP vl key is not one we recognise")
# 2. Grab the manifest and signature as bytes objects
if not "manifest" in unl:
return err("XPOP did not contain validation.unl.manifest")
if not "signature" in unl:
return err("XPOP did not contain validation.unl.signature")
manifest = None
signature = None
try:
manifest = xrpl.core.binarycodec.decode(\
str(hexlify(base64.b64decode(unl["manifest"])), "utf-8"))
signature = unhexlify(unl["signature"])
except:
return err("XPOP invalid validation.unl.manifest (should be base64) or validation.unl.signature")
if not "MasterSignature" in manifest or not "Signature" in manifest:
return err("XPOP invalid validation.unl.manifest serialization")
# 3. Re-encode the manifest without signing fields so we can check the signature
manifestnosign = b'MAN\x00' + \
unhexlify(xrpl.core.binarycodec.encode_for_signing(manifest)[8:])
# 4. Check master signature (vl_key over vl manifest)
if not xrpl.core.keypairs.is_valid_message(\
manifestnosign,\
unhexlify(manifest["MasterSignature"]), manifest["PublicKey"]):
return err("XPOP vl signature validation failed")
# 5. Get UNL signing key
signing_key = manifest["SigningPubKey"]
# 6. Get raw UNL payload
payload = None
if not "blob" in unl:
return err("XPOP invalid validation.unl.blob")
payload = base64.b64decode(unl["blob"])
# 7. Check UNL blob signature
if not xrpl.core.keypairs.is_valid_message(\
payload,\
unhexlify(unl["signature"]),
signing_key):
return err("XPOP invalid validation.unl.blob signature")
# 8. Decode UNL blob
try:
payload = json.loads(payload)
except:
return err("XPOP invalid validation.unl.blob json")
if not "sequence" in payload:
return err("XPOP missing validation.unl.blob.sequence")
if not "expiration" in payload:
return err("XPOP missing validation.unl.blob.expiration")
if not "validators" in payload:
return err("XPOP missing validation.unl.blob.validators")
unlseq = payload["sequence"] # these are not validated but are returned
unlexp = payload["expiration"] # to the user(dev) for additional validation
validators = {}
# 9. Check UNL internal manifests and get validator signing keys
for v in payload["validators"]:
if not "validation_public_key" in v:
return err("XPOP missing validation_public_key from unl entry")
if not "manifest" in v:
return err("XPOP missing manifest from unl entry")
manifest = None
try:
manifest = base64.b64decode(v["manifest"])
manifest = str(hexlify(manifest), "utf-8")
manifest = xrpl.core.binarycodec.decode(manifest)
except:
return err("XPOP invalid manifest in unl entry")
if not "MasterSignature" in manifest:
return err("XPOP manifest missing master signature in unl entry")
if not "SigningPubKey" in manifest:
return err("XPOP manifest missing signing key in unl entry")
# 10. Check each validator's manifest is signed correctly
#manifestnosign = b'MAN\x00' + \
# unhexlify(xrpl.core.binarycodec.encode_for_signing(manifest)[8:])
# RH NOTE: this doesn't provide any real additional safety since the whole blob is already signed
# and verifying these uses a lot of cpu cycles... so it's left commented oout
#if not xrpl.core.keypairs.is_valid_message(\
# manifestnosign,
# unhexlify(manifest["MasterSignature"]),
# v["validation_public_key"]):
# return err("XPOP a unl entry was invalidly signed")
# 11. Compute the node public address from the signing key
nodepub = xrpl.core.addresscodec.encode_node_public_key(\
unhexlify(manifest["SigningPubKey"]))
# 12. Add the verified validator to the verified validator list
validators[nodepub] = manifest["SigningPubKey"]
##
## Part B: Validate TXN and META proof, and compute ledger hash
##
# 13. Check if the transaction and meta is actually in the proof
computed_tx_hash = hash_txn(transaction["blob"])
computed_tx_hash_and_meta = hash_txn_and_meta(transaction["blob"], transaction["meta"])
if not proof_contains(transaction["proof"], computed_tx_hash_and_meta):
return err("Txn and meta were not present in provided proof")
# 14. Compute the tx merkle root
computed_tx_root = hash_proof(transaction["proof"])
# 15. Compute the ledger hash from the tx merkle root
computed_ledger_hash = \
hash_ledger(ledger["index"], ledger["coins"], ledger["phash"], computed_tx_root, \
ledger["acroot"], ledger["pclose"], ledger["close"], ledger["cres"], ledger["flags"])
if computed_ledger_hash == False:
return False
computed_ledger_hash = str(hexlify(computed_ledger_hash), 'utf-8').upper()
##
## Part C: Check validations to see if a quorum was reached on the computed ledgerhash
##
# 16. Calculate the minimum number of UNL validation signatures we need
quorum = math.ceil(len(validators) * 0.8)
votes = 0
# 17. Step through the provided validation messages and check each one's signature and ledger hash
for nodepub in data:
# RH NOTE: Any validation messages not the UNL are skipped, although this should probably be an error
if not nodepub in validators:
continue
# 18. Parse the validation message
valmsg = process_validation_message(data[nodepub])
if valmsg == False:
err("Warning: XPOP contained invalid validation from " + nodepub)
continue
# 19. Check the signing key matches the key we have on file from the (now verified) UNL
if valmsg["key"] != validators[nodepub]:
err("Warning: XPOP contained invalid KEY for validation from " + nodepub)
continue
# 20. Check the ledger hash in the validation message matches the one we generated from tx merkel
if valmsg["ledger_hash"] != computed_ledger_hash:
#err("Warning: XPOP contained validation for another ledger hash")
continue
# 21. Check the signature on the validation message (expensive)
valpayload = b'VAL\x00' + valmsg["without_signature"]
if not xrpl.core.keypairs.is_valid_message(valpayload, valmsg["signature"], valmsg["key"]):
err("Warning: XPOP contained validation with invalid signature")
continue
# 22. If all is well the successfully verified validation message counts as a vote toward quorum
votes += 1
##
## Part D: Return useful information to the caller
##
# 23. If there were insufficiently many votes in favour of the txn we always return False
if votes < quorum:
return False
try:
tx = xrpl.core.binarycodec.decode(transaction["blob"])
meta = xrpl.core.binarycodec.decode(transaction["meta"])
except:
return err("Error decoding txblob and meta")
ret = {
"verified": True,
"tx_blob": tx,
"tx_meta": meta,
"ledger_hash": computed_ledger_hash,
"ledger_index": ledger["index"],
"ledger_unixtime": xrpl.utils.ripple_time_to_posix(ledger["close"]),
"validator_quorum": quorum,
"validator_count": len(validators),
"validator_votes": votes,
"vl_master_key": vl_key,
"vl_expiration_unixtime": xrpl.utils.ripple_time_to_posix(unlexp),
"vl_sequence": unlseq,
"tx_source": tx["Account"],
"tx_hash": str(hexlify(computed_tx_hash), 'utf-8').upper()
}
if "InvoiceID" in tx:
ret["tx_invoice_id"] = tx["InvoiceID"]
ret["tx_is_payment"] = tx["TransactionType"] == "Payment"
if "DestinationTag" in tx:
ret["tx_destination_tag"] = tx["DestinationTag"]
if "Destination" in tx:
ret["tx_destination"] = tx["Destination"]
# 24. Search the meta for the modified nodes and construct a delivered amount field for xrp payments
if "AffectedNodes" in meta:
for af in meta["AffectedNodes"]:
if "ModifiedNode" in af:
mn = af["ModifiedNode"]
if "FinalFields" in mn and "PreviousFields" in mn and \
mn["LedgerEntryType"] == "AccountRoot" and "Account" in mn["FinalFields"] and \
mn["FinalFields"]["Account"] == tx["Destination"] and \
"Balance" in mn["PreviousFields"] and "Balance" in mn["FinalFields"]:
ret["tx_delivered_drops"] = \
int(mn["FinalFields"]["Balance"]) - int(mn["PreviousFields"]["Balance"])
break
return ret
def ctrlc(signal_received, frame):
global os_return_code
print("bye!")
dying = True
sys.exit(os_return_code)
signal(SIGINT, ctrlc)
def startup():
global camera_res_x
global camera_res_y
global camera_brightness
cam.set(cv2.CAP_PROP_FRAME_WIDTH, camera_res_x)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_res_y)
cam.set(cv2.CAP_PROP_BRIGHTNESS, camera_brightness)
# cam.set(cv2.CAP_PROP_FOCUS, 0) # set focus as near as possible
# cam.set(cv2.CAP_PROP_AUTOFOCUS, 1) # disable auto focus
# _, img = cam.read()
# img = cv2.flip(img, -1)
# cv2.imwrite("frame.jpg", img)
def cleanup():
cam.release()
cv2.destroyAllWindows()
def video_display(thread_id):
global display_frame
global display_text
global dying
global complete
global final_text
global ui_font_size
global ui_refresh_interval
global wait_after_complete
global qr_text
global timeout
global first_frame_seen
global qr_display_time
window = tkinter.Tk()
window.title("xPoP")
# window.attributes("-fullscreen", True)
window.geometry("800x600")
window.update()
w = window.winfo_width()
h = window.winfo_height()
canvas = tkinter.Canvas(window, width = w, height = h)
label = tkinter.Label(window, text="xpop", fg="Blue", font=("Helvetica", ui_font_size), anchor=tkinter.N)
label.place(x=w/2, y=0, anchor=tkinter.N)
canvas.pack()
qr = pyqrcode.create(qr_text)
qr_img = tkinter.BitmapImage(data = qr.xbm(scale=8))
display_time_start = time.time()
colour = "Blue"
complete_counter = 0
try:
while not dying:
if time.time() > t_end:
dying = True
break
photo = None
dp = []
mutex_ui.acquire()
if len(display_frame) == 1:
dp.append(display_frame[0])
mutex_ui.release()
elapsed = int(time.time() - display_time_start) # this is a global timer so we only show QR once
if elapsed < qr_display_time and not first_frame_seen:
canvas.delete("all")
canvas.create_image((800 - qr_img.width())/2, (600-qr_img.height())/2, image=qr_img, anchor=tkinter.NW)
text = "Please pay to the following QR code ~ " + str(qr_display_time - elapsed) + "s left"
else:
colour = "Red"
if len(dp) > 0 and type(dp[0]) != type(None):
try:
# dp[0] = cv2.resize(dp[0], (w,h))
photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(dp[0][:,:,::-1]))
except:
if dying:
break
finally:
pass
if photo:
canvas.delete("all")
canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)
text = display_text + " ~ " + str(math.floor(t_end-time.time())) + str("s until timeout")
if complete:
colour = "Green"
text = final_text + "\n" + "Please wait... " + str(math.floor((wait_after_complete - complete_counter)/ui_refresh_interval))
complete_counter = complete_counter + 1
if complete_counter > wait_after_complete:
dying = True
break
label.configure(text=text, fg=colour)
window.update_idletasks()
window.update()
time.sleep(ui_refresh_interval)
except:
dying = True
return
# print("writing frame.jpg")
# cv2.imwrite("/tmp/ramdisk/frame.jpg", img)
def video_reader(thread_id):
global dying
global first_frame_seen
global display_frame
global frame_raw
global unprocessed_frame_cap
global min_frame_delay
unprocessed_frames = 0
while not dying and not complete:
_, img = cam.read()
mutex_frame_raw.acquire()
try:
if first_frame_seen or len(frame_raw) == 0:
frame_raw.append(img)
unprocessed_frames = len(frame_raw)
else:
frame_raw[0] = img
if unprocessed_frames > unprocessed_frame_cap:
first_frame_seen = False
frame_raw = [img]
finally:
mutex_frame_raw.release()
#cam.set(cv2.CAP_PROP_FOCUS, 0) # set focus as near as possible
if not mutex_ui.locked():
mutex_ui.acquire()
if len(display_frame) == 0:
display_frame.append(img)
else:
display_frame[0] = img
mutex_ui.release()
# time.sleep(min_frame_delay)
def attempt_reconstructions():
global parity_data
global parity_len
global frame_count
global frame_data
global mutex_ui
global dying
global first_frame_seen
global display_text
global final_text
global expected_frame_count
global frame_raw
global t_end
global timeout
global verify_key
global verify_result
global complete
if complete or dying:
return
mutex_frame_data.acquire()
try:
p = sorted(parity_data.keys())
if len(p) < 1:
return
pairs = [[0, p[0]]]
for i in range(1, len(p)):
pairs.append([p[i-1], p[i]])
for i in pairs:
lower = i[0]
higher = i[1]
# count missing
missing_count = 0
missing = -1
for j in range(lower+1, higher+1):
if j in frame_data:
continue
missing_count = missing_count + 1
if missing_count > 1:
break
missing = j
if missing_count != 1:
continue
# execution to here means there is only one frame missing in this span
# now we can perform a reconstruction
r = []
for x in range(0, len(parity_data[higher])):
sum = parity_data[higher][x]
if lower > 0: # subtract lower parity frame
sum = sum + (85 - parity_data[lower][x])
else:
sum = sum - 33
# subtract frames we have
for j in range(lower+1, higher+1):
if j in frame_data and x < len(frame_data[j]):
sum = sum + (85 - (frame_data[j][x] - 33))
sum = sum % 85
sum = sum + 33
r.append(sum)
# trim final frame when applicable
if missing == higher:
r = r[0:parity_len[higher]]
frame_data[missing] = bytes(r)
print("reconstructed frame : " + str(missing) + "[" + str(lower) + ", " + str(higher) + "]")
except Exception as e:
print("exception: ", e)
finally:
mutex_frame_data.release()
def try_complete():
global parity_data
global parity_len
global frame_count
global frame_data
global mutex_ui
global dying
global first_frame_seen
global display_text
global final_text
global expected_frame_count
global frame_raw
global t_end
global timeout
global verify_key
global verify_result
global complete
global os_return_code
if complete or dying:
return
if frame_count == -1:
return
attempt_reconstructions()
mutex_frame_data.acquire()
try:
if len(frame_data) == frame_count:
mutex_ui.acquire()
final_text = "Processing XRPL Proof of Payment..."
complete = True
mutex_ui.release()
full_data = b''
for i in range(1, frame_count+1):
full_data += frame_data[i]
try:
full_data = brotli.decompress(base64.a85decode(full_data)).decode('utf-8')
except:
full_data = full_data.decode('utf-8')
verify_result = verify(full_data, verify_key)
#print(verify_result)
mutex_ui.acquire()
final_text = "Invalid xPoP / Verification Failed."
if verify_result:
if not verify_result["tx_is_payment"]:
print("INVALID - Not a payment transaction.")
elif not ("tx_destination" in verify_result):
print("INVALID - Not a payment transaction [2].")
elif not ("tx_destination_tag" in verify_result):
print("INVALID - Missing destination tag.")
elif not ("tx_delivered_drops" in verify_result):
print("INVALID - Payment did not deliver XRP.")
elif verify_result["tx_destination"] != xrp_addr:
print("INVALID - Payment was sent to the wrong address.")
elif verify_result["tx_destination_tag"] != xrp_dt:
print("INVALID - Payment was sent to the wrong dest tag.")
elif verify_result["tx_delivered_drops"] < xrp_amount * 1000000:
print("INVALID - Payment did not deliver enough drops.")
else:
final_text = "Valid xPoP, payment matches. Thank you."
os_return_code = 50
print("VALID - Valid xPoP matches requested payment")
else:
print("INVALID - xPoP failed verification.")
#final_text = "Valid xPoP! " + verify_result["tx_blob"]["TransactionType"] + " " + verify_result["tx_meta"]["TransactionResult"] + "\n" + verify_result["tx_hash"]
#if "tx_destination" in verify_result:
# final_text = final_text + "\n" + "Destination: " + verify_result["tx_destination"]
#if "tx_destination_tag" in verify_result:
# final_text = final_text + "\n" + "Tag: " + str(verify_result["tx_destination_tag"])
#if "tx_delivered_drops" in verify_result:
# final_text = final_text + "\n" + "Drops delivered: " + str(verify_result["tx_delivered_drops"])
mutex_ui.release()
dying = True
finally:
mutex_frame_data.release()
def video_decoder(thread_id):
global dying
global first_frame_seen
global display_text
global final_text
global expected_frame_count
global frame_raw
global frame_data
global frame_count
global t_end
global timeout
global verify_key
global verify_result
global complete
global parity_data
global parity_len
while not dying and not complete:
mutex_frame_raw.acquire()
img = None
if len(frame_raw) > 0:
if first_frame_seen:
img = frame_raw.pop(0)
else:
img = frame_raw[0]
mutex_frame_raw.release()
else:
mutex_frame_raw.release()
time.sleep(0.1)
print("thread waiting", thread_id)
continue
#img = cv2.flip(img, -1)
if type(img) == type(None):
print("cant read video device")
dying = True
continue
dimg = decode(img)
if len(dimg) == 0:
img = cv2.flip(img, -1)
dimg = decode(img)
if len(dimg) > 0:
if not first_frame_seen:
t_end = time.time() + timeout
first_frame_seen = True
b = dimg[0].data
if len(b) < 8:
continue
packet_type = b[0:4]
is_par = packet_type == b'XPAR'
if not is_par and packet_type != b'XPOP':
continue
frame_count = -1
frame_number = -1
frame_size = -1
txt = ""
try:
frame_count = int(b[6:8], 16) # for parity data this is actually the end frame
frame_number = int(b[4:6],16) # for parity data this is the begin frame
frame_size = int(b[8:12], 16)
if not is_par:
txt = "xPoP Acquired: " + str(math.floor(100*len(frame_data)/frame_count)) + "%"
txt = txt + " [" + str(len(frame_data)) + "/" + str(frame_count) + "]"
except:
pass
if frame_count == -1:
continue
if not is_par:
mutex_ui.acquire()
display_text = txt
mutex_ui.release()
if is_par:
if frame_number != 1 or frame_count in parity_data:
continue
mutex_frame_data.acquire()
parity_data[frame_count] = b[12:]
parity_len[frame_count] = frame_size
mutex_frame_data.release()
try_complete()
continue
if expected_frame_count == -1:
expected_frame_count = frame_count
elif expected_frame_count != frame_count:
# reset the whole thing if a different xpop is given
expected_frame_count = frame_count
mutex_frame_data.acquire()
frame_data = {}
mutex_frame_raw.acquire()
frame_raw = []
first_frame_seen = False
parity_data = {}
parity_len = {}
mutex_frame_raw.release()
mutex_frame_data.release()
if frame_number in frame_data:
try_complete()
continue
mutex_frame_data.acquire()
frame_data[frame_number] = b[12:]
mutex_frame_data.release()
try_complete()
mutex_frame_raw = Lock()
mutex_frame_data = Lock()
mutex_ui = Lock()
frame_count = -1
frame_raw = []
frame_data = {}
parity_data = {}
parity_len = {}
cam = cv2.VideoCapture(CAMERAPORT)