-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPublication.py
More file actions
1399 lines (1091 loc) · 61 KB
/
Publication.py
File metadata and controls
1399 lines (1091 loc) · 61 KB
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
# Bitclamp: a cryptocurrency-based publication tool
# Copyright (C) 2016 Joe Testa <jtesta@positronsecurity.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms version 3 of the GNU General Public License as
# published by the Free Software Foundation.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import binascii, fcntl, hashlib, json, os, math, struct, sys, time
from Utils import *
from TxRecord import *
from RPCClient import *
# Maintains information about a publication campaign. Maintains state regarding what has been published, and what is left.
class Publication:
# Flags for the "blockchain" parameter of the constructor. Specifies what
# block chain to publish on.
BLOCKCHAIN_BTC = 1
BLOCKCHAIN_DOGE = 2
# Flags for the "network" parameter of the constructor. Specifies whether
# to use the real network (mainnet) or the test network.
NETWORK_MAINNET = 1
NETWORK_TESTNET = 2
# The number of confirmations a transaction needs in order to be considered
# "finalized". From 1 up to this value, it is only considered "accepted".
CONFIRMATION_THRESHOLD_BTC = 9
CONFIRMATION_THRESHOLD_DOGE = 90
SINGLE_OUTPUT_SIZE = 448
# The minimum transaction amout. See https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L161
DUST_THRESHOLD = 0.00010000 #0.00000546
# Header bytes to denote the beginning and termination of a publication.
# These appear first in each transaction payload.
HEADER_BEGIN = b'\x00\x11\x22\x33\x44\x55\x66\x77'
HEADER_NOOP = b'\x12\x23\x34\x45\x56\x67\x78\x89'
HEADER_TERMINATE = b'\x88\x99\xaa\xbb\xcc\xdd\xee\xff'
NONCE_LEN = 16
NONCE_SALT = b'\x09\xf9\x11\x02\x9d\x74\xe3\x5b\xd8\x41\x56\xc5\x63\x56\x88\xc0'
# Constants for the general flag field in the publication header.
GENERAL_FLAG_DEADMAN_SWITCH_FILE = 1
GENERAL_FLAG_DEADMAN_SWITCH_KEY = 1<<1
# Constants for the content type field of the start of publication header.
CONTENT_TYPE_RESERVED = 0
CONTENT_TYPE_UNDEFINED = 1
CONTENT_TYPE_DOCUMENT = 2
CONTENT_TYPE_PICTURE = 3
CONTENT_TYPE_SOUND = 4
CONTENT_TYPE_VIDEO = 5
CONTENT_TYPE_SOURCECODE = 6
CONTENT_TYPE_DIGITALSIG = 7
CONTENT_TYPE_ARCHIVE = 8
CONTENT_TYPE_MAP = {CONTENT_TYPE_RESERVED:'auto', CONTENT_TYPE_UNDEFINED:'undefined', CONTENT_TYPE_DOCUMENT:'document', CONTENT_TYPE_PICTURE:'picture', CONTENT_TYPE_SOUND:'sound', CONTENT_TYPE_VIDEO:'video', CONTENT_TYPE_SOURCECODE:'sourcecode', CONTENT_TYPE_DIGITALSIG:'digitalsignature', CONTENT_TYPE_ARCHIVE:'archive'}
# Constants for the compression type field of the start of publication
# header.
COMPRESSION_TYPE_RESERVED = 0
COMPRESSION_TYPE_NONE = 1
COMPRESSION_TYPE_ZIP = 2
COMPRESSION_TYPE_GZIP = 3
COMPRESSION_TYPE_BZIP2 = 4
COMPRESSION_TYPE_XZ = 5
COMPRESSION_TYPE_LZMA = 6
#COMPRESSION_TYPE_7ZIP = 7 # The 7z tool sadly won't write to stdout...
# A map that translates the constants above into their string
# representations.
COMPRESSION_TYPE_MAP_STR = {COMPRESSION_TYPE_RESERVED:'auto', COMPRESSION_TYPE_NONE:'none', COMPRESSION_TYPE_ZIP:'zip', COMPRESSION_TYPE_GZIP:'gzip', COMPRESSION_TYPE_BZIP2:'bzip2', COMPRESSION_TYPE_XZ:'xz', COMPRESSION_TYPE_LZMA:'lzma'}
# A map that returns the corresponding file extensions, given the
# compression type.
COMPRESSION_TYPE_MAP_EXT = {COMPRESSION_TYPE_RESERVED:'', COMPRESSION_TYPE_NONE:'', COMPRESSION_TYPE_ZIP:'.zip', COMPRESSION_TYPE_GZIP:'.gz', COMPRESSION_TYPE_BZIP2:'.bz2', COMPRESSION_TYPE_XZ:'.xz', COMPRESSION_TYPE_LZMA:'.lzma'}
# Types of encryption used.
ENCRYPTION_TYPE_RESERVED = 0
ENCRYPTION_TYPE_NONE = 1
ENCRYPTION_TYPE_GPG2_AES256_SHA512 = 2
ENCRYPTION_TYPE_MAP = {ENCRYPTION_TYPE_RESERVED:'', ENCRYPTION_TYPE_NONE:'none',ENCRYPTION_TYPE_GPG2_AES256_SHA512:'GPG2 AES256 SHA512'}
# To publish with no file name, set filename to "". Setting it to None
# causes it to be set to the filepath.
def __init__(self, *args):
# If only 9 arguments are present, the constructor for publishing
# deadman switch keys should be used instead.
if len(args) == 10:
self.deadman_switch_key_init(args)
return
self.rpc_client = args[0]
self.block_listener = args[1]
self.filepath = args[2]
self.content_type = args[3]
self.compression_type = args[4]
self.filename = args[5]
self.file_description = args[6]
self.nocrypto = args[7]
self.deadman_switch_path = args[8]
self.blockchain = args[9]
self.test_or_reg_network = args[10]
self.num_outputs = args[11]
self.num_transactions = args[12]
self.txfee = args[13]
self.change_address = args[14]
self.debug = args[15]
self.verbose = args[16]
# If not None, this is the filesystem path were the initial publication
# address and amount should be placed. This is only used during unit
# testing.
self.unittest_publication_address = args[17]
if len(self.filepath) == 0:
raise Exception('filepath arg is required!')
if not os.path.isfile(self.filepath):
raise Exception("%s is not a regular file!" % self.filepath)
if self.txfee < 0.0:
raise Exception("txfee may not be less than 0!: %.8f" % txfee)
# The general flags field of the publication header. See GENERAL_FLAG_*
# constants.
self.general_flags = 0
# The encryption key that protects the data as its being published.
# This is published in the very last block.
self.temporal_key = b'\x00' * 32
# Initialization vector for encryption. Currently reserved for future
# use.
self.temporal_iv = b'\x00' * 32
# Extra data for encryption. Currently reserved for future use.
self.temporal_extra = b'\x00' * 32
# Number of bytes in unconfirmed transactions.
self.bytes_unconfirmed = 0
# Number of bytes in confirmed transactions.
self.bytes_confirmed = 0
# The next percentage threshold that needs to be passed in order to
# print progress to stdout. In other words, if set to 15, then once
# 15% of the file's bytes are confirmed, a progress message is printed
# and this is incremented to 20.
self.next_percentage_alert_marker = 0
# Initialize the txrecords data structure.
self.init_txrecords_and_ending_noop_array()
# Set to True when the publication termination message has been sent.
self.termination_record_sent = False
# We generate a new address for each publication. This is the one
# legit key used to spend coins sent during each transaction.
self.address = self.rpc_client.getnewaddress()
# To sign raw transactions, we need the address's private key.
self.privkey = self.rpc_client.dumpprivkey(self.address)
# To create a P2SH address, we need to the raw ECDSA public key from
# the address.
self.pubkey = self.rpc_client.validateaddress(self.address)['pubkey']
self.d("Public key for publishing: %s" % self.pubkey)
# The number of bytes published per transaction. This is not valid for
# the header or termination transactions; only the bulk intermediate
# ones. Four bytes are subtracted to account for the file offset.
self.num_bytes_per_tx = (self.num_outputs * Publication.SINGLE_OUTPUT_SIZE) - 4
# The amount we have to publish with. This gets whittled down each
# transaction by fees. Any leftover is sent back to the user as change
# (--change argument).
self.amount = -1.0
# Signifies that all bytes in the file were read.
self.end_of_file_reached = False
# True when the user sent funds to start the publication process.
self.received_funds = False
# Signifies that the terminating message was sent to the change address.
self.change_sent = False
# Signifies whether or not this publication is fully complete (meaning
# that all data was sent and all transactions surpassed the threshold).
self.complete = False
# When deadman switch mode is enabled, this tracks whether or not the
# key has been written to disk yet.
self.deadman_switch_wrote_key = False
# If the user did not manually set a content type, we need to figure it
# out.
if self.content_type == Publication.CONTENT_TYPE_RESERVED:
self.content_type = Utils.find_content_type(self.filepath)
print("Automatic detection of content type is: %s" % Publication.get_content_type_str(self.content_type))
if self.content_type == Publication.CONTENT_TYPE_UNDEFINED:
print("\nError: could not determine content type of file. You must re-run the program with --content-type and manually set the type.\n")
sys.exit(-1)
if self.content_type == Publication.CONTENT_TYPE_ARCHIVE:
print("\nNote: the 'archive' type is for miscellaneous files. It is strongly recommended that you manually set the type to document, video, source code, etc. with the --content-type argument, if possible. This makes your publication more easily searchable & identifiable.\n")
# The bytes to publish. This will be the bytes in filepath if no
# compression is selected. Otherwise, it will be the compressed
# bytes returned by Utils.compress_file()
self.file_bytes = None
# The user wants no compression used, so just read in the file and
# leave it as-is.
if self.compression_type == Publication.COMPRESSION_TYPE_NONE:
with open(self.filepath, 'rb') as f:
self.file_bytes = f.read()
# The user selected automatic detection of optimal compression.
elif self.compression_type == Publication.COMPRESSION_TYPE_RESERVED:
self.file_bytes, self.compression_type = Utils.find_optimal_compression(self.filepath, self.v)
else: # The user selected a specific compression type.
self.file_bytes = Utils.compress_file(self.filepath, self.compression_type)
self.encryption_type = Publication.ENCRYPTION_TYPE_NONE
# If temporal encryption is enabled...
if not self.nocrypto:
self.file_bytes, self.temporal_key = Utils.encrypt(self.file_bytes)
self.encryption_type = Publication.ENCRYPTION_TYPE_GPG2_AES256_SHA512
self.v('Using encryption type: %s' % Publication.get_encryption_str(self.encryption_type))
else:
print("WARNING: temporal encryption is disabled! Be sure you understand what the implications of this are!")
self.filesize = len(self.file_bytes)
if self.filesize == 0:
raise Exception('Size of file to publish must not be zero!')
new_extension = Publication.COMPRESSION_TYPE_MAP_EXT[self.compression_type]
if self.filename != '' and new_extension != '':
self.filename += new_extension
print("Automatically adding file extension '%s' to file name to reflect usage of %s compression: %s" % (new_extension, Publication.COMPRESSION_TYPE_MAP_STR[self.compression_type], self.filename))
# Calculate the hash of the file.
self.file_hash = hashlib.sha256(self.file_bytes).digest()
self.d("SHA256 of file bytes: %s" % binascii.hexlify(self.file_hash).decode('ascii'))
# If we are doing a plaintext publication, store the file hash in the
# key field since it is unused. The hash will not be divulged in the
# header, but in the termination message instead.
if self.nocrypto:
self.temporal_key = self.file_hash
self.file_hash = b'\x00' * 32
self.v('Omitting the SHA256 hash from the publication header; placing in termination message instead.')
# If we are in deadman switch publish mode, set the flag in the general
# headers.
if self.deadman_switch_path is not None:
self.general_flags |= Publication.GENERAL_FLAG_DEADMAN_SWITCH_FILE
self.d("Setting GENERAL_FLAG_DEADMAN_SWITCH_FILE.")
# For resuming publication after interruptions.
self.state_file = os.path.join(os.getcwd(), "bitclamp_state_" + os.path.basename(self.filepath) + '_' + time.strftime("%Y-%m-%d_%H-%M") + '.state')
# Set a flag that denotes we are NOT trying to publish a deadman switch
# key (see constructor below for that code).
self.deadman_switch_key_publish_mode = False
# Special constructor for when publishing a deadman switch key.
def deadman_switch_key_init(self, args):
self.rpc_client = args[0]
self.block_listener = args[1]
self.filepath = args[2]
self.blockchain = args[3]
self.test_or_reg_network = args[4]
self.txfee = args[5]
self.change_address = args[6]
self.debug = args[7]
self.verbose = args[8]
self.unittest_publication_address = args[9]
if not os.path.isfile(self.filepath):
raise Exception("%s is not a regular file!" % self.filepath)
if self.txfee < 0.0:
raise Exception("txfee may not be less than 0!: %.8f" % txfee)
key_lines = None
with open(self.filepath, 'r') as f:
key_lines = f.readlines()
# Ensure that we read exactly four lines.
if len(key_lines) != 4:
raise Exception("Key file does not have 4 lines (" + len(key_lines) + "). It appears to be corrupted.")
# Decode each of the four lines and add them to the bytes to publish.
self.file_bytes = binascii.unhexlify(key_lines[0].strip())
self.file_bytes += binascii.unhexlify(key_lines[1].strip())
self.file_bytes += binascii.unhexlify(key_lines[2].strip())
self.file_bytes += binascii.unhexlify(key_lines[3].strip())
self.filesize = len(self.file_bytes)
# Ensure that we read exactly 128 bytes.
if self.filesize != 128:
raise Exception("Decoded key file does not yield 128 bytes!. It appears to be corrupted.")
self.num_outputs = 1
self.num_transactions = 1
self.num_bytes_per_tx = Publication.SINGLE_OUTPUT_SIZE
self.general_flags = Publication.GENERAL_FLAG_DEADMAN_SWITCH_KEY
self.content_type = Publication.CONTENT_TYPE_UNDEFINED
self.compression_type = Publication.COMPRESSION_TYPE_NONE
self.encryption_type = Publication.ENCRYPTION_TYPE_NONE
self.file_hash = b'\x00' * 32
self.filename = ''
self.file_description = ''
self.txrecords = []
self.bytes_unconfirmed = 0
self.bytes_confirmed = 0
self.end_of_file_reached = False
self.temporal_key = None
self.temporal_iv = None
self.temporal_extra = None
self.deadman_switch_path = None
self.termination_record_sent = False
self.change_sent = False
self.init_txrecords_and_ending_noop_array()
# We generate a new address for each publication. This is the one
# legit key used to spend coins sent during each transaction.
self.address = self.rpc_client.getnewaddress()
# To sign raw transactions, we need the address's private key.
self.privkey = self.rpc_client.dumpprivkey(self.address)
# To create a P2SH address, we need to the raw ECDSA public key from
# the address.
self.pubkey = self.rpc_client.validateaddress(self.address)['pubkey']
self.d("Public key for publishing: %s" % self.pubkey)
# Set a flag that denotes we are trying to publish a deadman switch key.
self.deadman_switch_key_publish_mode = True
# When serializing this object, exclude the BlockListener, as it contains
# a socket (among other things not worth keeping). Also exclude the
# RPCClient, as it should be re-created upon restoration in case
# credentials are changed.
def __getstate__(self):
state = self.__dict__.copy()
del state['block_listener']
del state['rpc_client']
del state['unittest_publication_address']
return state
# When restoring a serialized object, set block_listener to None; the
# set_block_listener() method should be used once the listener is
# available.
def __setstate__(self, state):
self.__dict__.update(state)
self.block_listener = None
self.rpc_client = None
self.unittest_publication_address = None
# Prints a message when debugging is enabled.
def d(self, s):
if self.debug:
print(s)
# Prints a message when verbosity is enabled.
def v(self, s):
if self.verbose:
print(s)
# Return the number of confirmations a transaction needs in order to be
# considered finalized.
def get_confirmation_threshold(self):
return Publication.CONFIRMATION_THRESHOLD_BTC if self.blockchain == Publication.BLOCKCHAIN_BTC else Publication.CONFIRMATION_THRESHOLD_DOGE
# Returns "BTC" or "DOGE", depending on which chain we are publishing on.
def get_currency_str(self):
return "BTC" if self.blockchain == Publication.BLOCKCHAIN_BTC else "DOGE"
# Returns the amount that each TX output should have, excluding the fee
# (since vin - vout = fee).
@staticmethod
def get_tx_output_amount(d, chain, total_amount, txfee, nbytes, noutputs, recurse_level=0):
kb = nbytes / 1024
if chain == Publication.BLOCKCHAIN_DOGE:
kb = math.ceil(kb)
# The fee for this upcoming transaction is the number of bytes we're
# about to send, in KB, times the per-KB fee.
fee = kb * txfee
# In a test environment, the Dogecoin daemon was observed to randomly
# reject a transaction because of 'insufficient priority'. In that
# case, we will recurse and re-calculate the fee amount. We will
# bump up an entire transaction's fee by 1 or 2 DOGE depending on how
# many consecutive failures we've had.
if (chain == Publication.BLOCKCHAIN_DOGE):
if recurse_level > 1:
fee += 2
elif recurse_level > 0:
fee += 1
per_output_amount = (total_amount - fee) / noutputs
# To account for any rounding, multiply the return value by the number
# of outputs. We may lose an extra satoshi to the tx fee.
next_total_amount = per_output_amount * noutputs
d("get_tx_output_amount(total_amount: %.8f, txfee: %.8f, nbytes: %d, noutputs: %d); fee = KB (%f) * txfee (%.8f) = %.8f; Per-output amount = (total_amount (%.8f) - fee (%.8f)) / num_outputs (%d) = %.8f; next total_amount: %.8f" % (total_amount, txfee, nbytes, noutputs, kb, txfee, fee, total_amount, fee, noutputs, per_output_amount, next_total_amount))
return next_total_amount, per_output_amount
# Returns the position in the file up to which it was already read.
def get_file_position(self):
return self.bytes_unconfirmed
# Adds a new TxRecord as a child to the specified parent.
def add_txrecord(self, parent_txrecord, new_txrecord):
for i in range(0, self.num_transactions):
if self.txrecords[i][-1] == parent_txrecord:
self.txrecords[i].append(new_txrecord)
# Updates the file position.
def update_unconfirmed_bytes(self, num_bytes):
self.bytes_unconfirmed += num_bytes
self.d("update_unconfirmed_bytes(%d); count: %d" % (num_bytes, self.bytes_unconfirmed))
# Updates the number of bytes in confirmed transactions.
def update_confirmed_bytes(self, num_bytes):
self.bytes_confirmed += num_bytes
self.d("update_confirmed_bytes(%d); count: %d" % (num_bytes, self.bytes_confirmed))
percent_complete = (self.bytes_confirmed / self.filesize) * 100
if (num_bytes > 0) and (percent_complete > self.next_percentage_alert_marker):
print("\t%d%% completed." % self.next_percentage_alert_marker)
while percent_complete > self.next_percentage_alert_marker:
self.next_percentage_alert_marker += 5
# Return an estimate as to how long the specified number of transactions
# will take.
@staticmethod
def get_time_estimate(num_blocks, chain):
mins = num_blocks
if chain == Publication.BLOCKCHAIN_BTC:
mins = mins * 10
hours = 0
days = 0
ret = '%d minutes' % mins
if mins >= 60:
hours = int(mins / 60)
mins = mins % 60
ret = '%d hours, %d minutes' % (hours, mins)
if hours >= 24:
days = int(hours / 24)
hours = hours % 24
ret = '%d days, %d hours, %d minutes' % (days, hours, mins)
if days > 100:
ret = 'a long freaking time (%s)' % ret
return ret
# Initializes the txrecords list of lists. See comment below.
def init_txrecords_and_ending_noop_array(self):
# A list that holds lists of TxRecords. Entries in the top list
# correspond to generations of records (there are more than one when
# num_transactions > 1). Inner lists track the progression of
# TxRecords. Once a transaction surpasses the
# CONFIRMATION_THRESHOLD_* value, it is removed, as we are certain
# it will not be reverted.
self.txrecords = []
# Tracks whether the termination record was sent in this generation.
self.ending_noop_sent = []
self.num_ending_noops_sent = 0
for i in range(0, self.num_transactions):
self.txrecords.append([])
self.ending_noop_sent.append(False)
# Makes a P2SH address given script bytes.
def make_p2sh_address(self, script_bytes):
h = hashlib.new('ripemd160')
h.update(hashlib.sha256(script_bytes).digest())
hash160 = h.digest()
version = b'\x05'
if self.blockchain == Publication.BLOCKCHAIN_DOGE:
version = b'\x16' # 22, from src/chainparams.cpp
if self.test_or_reg_network:
version = b'\xc4' # 196, for testnet
tag = hashlib.sha256(hashlib.sha256(version + hash160).digest()).digest()[:4]
return Utils.base58_encode(version + hash160 + tag)
# Returns a string representation of this Publication object.
def __str__(self):
s = "\n\n"
for generation in range(0, len(self.txrecords)):
s += "\tGen %d:\n" % generation
for txrecord in self.txrecords[generation]:
s += "\t\t" + str(txrecord) + "\n"
return "Publication:\n\tFile path: %s\n\tFilename: %s\n\tFile size: %d\n\tTemporal key: %s\n\tBytes unconfirmed: %d\n\tBytes confirmed: %d%s" % (self.filepath, self.filename, self.filesize, binascii.hexlify(self.temporal_key).decode('ascii'), self.bytes_unconfirmed, self.bytes_confirmed, s)
# Argument must be of size 'single_output_size'
def make_redeem_script(self, byte_block):
byte_block_len = len(byte_block)
# If the block is not aligned to 32 bytes, then we need to pad it with
# zeros.
mod = byte_block_len % 32
if mod != 0:
byte_block += b'\x00' * (32 - mod)
# Update the length since we just modified the block.
byte_block_len = len(byte_block)
num_keys = int(byte_block_len / 32)
if num_keys > 14:
print("make_redeem_script(): no more than 14 keys may be present: %d" % num_keys)
sys.exit(-1)
# Convert the number of keys to the appropriate OP_* value to specify
# how many total keys we are presenting (+1 is for the legit key).
# For example, if one data key is present, then 80 + 1 + 1 = 82 = 0x52
# = OP_2 (which is correct, since there is also one legit key).
op_num_keys = bytes([80 + num_keys + 1])
# 0x51 = OP_1 (meaning that at least one key/signature must be
# presented.)
redeem_script = b'\x51'
# Set the first key to be the real public key we will use to later
# spend the funds.
redeem_script += b'\x21' + binascii.unhexlify(self.pubkey)
start_pos = 0
end_pos = 32
for i in range(0, num_keys):
# 0x21 = Push next 0x21 bytes to stack
# 0x02 = Prefix for public key
redeem_script += b'\x21\x02' + byte_block[start_pos:end_pos]
start_pos += 32
end_pos += 32
# 0xae = OP_CHECKMULTISIG (check that one key & signature out of the 15
# is valid)
redeem_script += op_num_keys + b'\xae'
return redeem_script, self.make_p2sh_address(redeem_script)
# If we are in deadman switch mode, write out the encryption key, if we
# didn't already.
def store_deadman_switch_key(self, next_txid):
# If we are not in deadman switch mode, or if we already wrote out the
# key, just return without doing anything.
if self.deadman_switch_path is None or \
self.deadman_switch_wrote_key is True:
return
with open(self.deadman_switch_path, 'w') as f:
f.write(next_txid)
f.write("\n")
f.write(binascii.hexlify(self.temporal_key).decode('ascii'))
f.write("\n")
f.write(binascii.hexlify(self.temporal_iv).decode('ascii'))
f.write("\n")
f.write(binascii.hexlify(self.temporal_extra).decode('ascii'))
self.deadman_switch_wrote_key = True
# Begins the publication process.
def begin(self):
self.v('Beginning to publish %s using %d outputs per %d transaction%s / %d bytes per transaction.' % (self.filepath, self.num_outputs, self.num_transactions, 's' if (self.num_transactions > 1) else '', self.num_bytes_per_tx))
# The number of blocks is calculated based on the file size, number of
# bytes per transaction, and number of transactions we send per block.
# Two is added to account for the termination message and the sending
# of change upon completion.
num_blocks = math.ceil(self.filesize / (self.num_transactions * self.num_bytes_per_tx)) + 2
# If multiple transactions are selected, the beginning and ending NOOPs
# must be accounted for as well.
if self.num_transactions > 1:
num_blocks += 2
self.v('%s is %d bytes long. Publication will take %d blocks, which, under optimal conditions, will take %s.' % (self.filepath, self.filesize, num_blocks, Publication.get_time_estimate(num_blocks, self.blockchain)))
# 8 bytes to denote the start of a file
# 8 bytes for nonce
# 32 bytes for nonce hash
# 2 bytes reserved (0x0000)
# 1 byte for general flags
# 1 byte for encryption type
# 1 byte for the content type
# 1 byte for the compression type
# 4 bytes for the file size
# 32 bytes for the SHA256 hash of the file
# 1 byte for the filename length (N)
# 1 byte for the description length (M)
# N bytes for the filename (up to 128)
# M bytes for the description (up to 128)
nonce = os.urandom(Publication.NONCE_LEN)
nonce_hash = hashlib.sha256(Publication.NONCE_SALT + nonce).digest()
header_bytes = \
Publication.HEADER_BEGIN + \
nonce + \
nonce_hash + \
b'\x00\x00' + \
struct.pack('!B', self.general_flags) + \
struct.pack('!B', self.encryption_type) + \
struct.pack('!B', self.content_type) + \
struct.pack('!B', self.compression_type) + \
struct.pack('!I', self.filesize) + \
self.file_hash + \
struct.pack('!B', len(self.filename)) + \
struct.pack('!B', len(self.file_description)) + \
self.filename.encode('utf-8') + \
self.file_description.encode('utf-8')
self.d("Header bytes: %s" % binascii.hexlify(header_bytes).decode('ascii'))
# Read the first bytes out of the file. Since we have to include a
# header, subtract those bytes from the single output size.
bytes_to_read = min(Publication.SINGLE_OUTPUT_SIZE - len(header_bytes), self.filesize)
first_block = self.file_bytes[0:bytes_to_read]
# Create the redeem script and P2SH address for the first transaction.
redeem_script, p2sh_address = self.make_redeem_script(header_bytes + first_block)
txrecord = TxRecord([redeem_script], [p2sh_address], bytes_to_read)
self.update_unconfirmed_bytes(bytes_to_read)
# Since this is the first record, add it to the beginning of all
# generations.
for i in range(0, self.num_transactions):
self.txrecords[i].append(txrecord)
# Estimate the cost to publish this file, then tell the user where &
# how much to send.
cost, unused0, unused1, unused2, unused3, num_block_generations, unused5, unused6 = Utils.get_estimate(self.rpc_client, self.filepath, self.blockchain, self.num_outputs, self.num_transactions, self.txfee)
print("To begin publication, send %.8f %s to %s" % (cost + 0.00000001, self.get_currency_str(), p2sh_address))
self.write_unit_test_info(p2sh_address, cost+0.00000001, self.block_listener.port)
cli = 'bitcoin-cli'
if self.blockchain == Publication.BLOCKCHAIN_DOGE:
cli = 'dogecoin-cli'
self.d("%s sendtoaddress %s %.8f; i=0; while [ $i -lt %d ]; do %s generate 1 $PUBKEY; sleep 1.2; let \"i++\"; done" % (cli, p2sh_address, cost, num_block_generations, cli))
# Wait for a transaction to get at least 1 confirmation which paid us
# to begin.
self.wait_for_funds(txrecord, p2sh_address)
print("Received funds. Beginning publication...")
self.received_funds = True
# Check that the amount sent by the user meets the minimum.
sent_amount = 0.0
for value in txrecord.get_values():
sent_amount += float(value)
if sent_amount < cost:
print("Warning: only %.8f was sent instead of the minimum (%.8f)!" % (sent_amount, cost))
txrecord.set_total_amount(sent_amount)
# If we are trying to publish a deadman switch key, set
# end_of_file_reached to True in order to skip the termination message,
# which isn't necessary.
if self.deadman_switch_key_publish_mode:
self.end_of_file_reached = True
self.termination_record_sent = True
self.resume()
# If self.txrecords is be properly configured, this resumes publication.
def resume(self):
continue_flag = True
first_block = True
sending_termination_record = False
sending_change = False
while continue_flag:
#
# This is a complex structure used to track what records need to be
# transmitted, and what their predecessors are. There are 4 cases
# for it:
#
# 1. One input record goes to multiple output records. This
# happens at the start of a publication when multiple transactions
# used. NOOPs are used to split the one input into multiple
# outputs. The structure would look like this:
#
# [([previous_txrecord], [next_txrecord1, next_txrecord2, ...])]
#
#
# 2. One input record goes to one output record. This happens
# when one transaction per block is used. The structure would
# look like this:
#
# [([previous_txrecord], [next_txrecord])]
#
#
# 3. One input record goes to one output record, and there are
# multiple pairs. This happens when multiple transactions per
# block is used. The structure would look like this:
#
# [([previous_txrecord1], [next_txrecord1]),
# ([previous_txrecord2], [next_txrecord2]), ...]
#
#
# 4. Multiple input records go to a single output. This happens
# at the end of a multiple-transaction publication to join the
# generations back into one (using NOOPs). The structure would
# look like this:
#
# [([previous_txrecord1, previous_txrecord2, ...], next_txrecord)]
#
#
txrecords_to_transmit = []
if first_block and (self.num_transactions > 1):
self.d("Creating initial NOOP messages...")
previous_txrecord = self.txrecords[0][-1]
next_txrecords = []
for generation in range(0, self.num_transactions):
next_txrecord = self.create_noop_record()
self.txrecords[generation].append(next_txrecord)
next_txrecords.append(next_txrecord)
txrecords_to_transmit = [([previous_txrecord], next_txrecords)]
elif (self.num_transactions == 1) and (self.end_of_file_reached is False):
self.d("Creating next block for single-transaction publication...")
previous_txrecord = self.txrecords[0][-1]
next_txrecord = self.create_next_txrecord()
self.add_txrecord(previous_txrecord, next_txrecord)
txrecords_to_transmit = [([previous_txrecord], [next_txrecord])]
elif (self.num_transactions > 1) and (self.num_ending_noops_sent != self.num_transactions):
self.d("Creating next blocks for multi-transaction publication...")
for generation in range(0, self.num_transactions):
previous_txrecord = self.txrecords[generation][-1]
if self.end_of_file_reached:
if self.ending_noop_sent[generation]:
continue
self.d("Creating ending NOOP for generation %d" % generation)
next_txrecord = self.create_noop_record()
self.ending_noop_sent[generation] = True
self.num_ending_noops_sent += 1
else:
next_txrecord = self.create_next_txrecord()
self.add_txrecord(previous_txrecord, next_txrecord)
txrecords_to_transmit.append(([previous_txrecord], [next_txrecord]))
elif self.termination_record_sent is False:
self.d("Sending termination record...")
termination_txrecord = self.create_termination_record()
termination_txrecord.set_last_record()
last_txrecords = []
for generation in range(0, self.num_transactions):
# Get all the previous records.
last_txrecords.append(self.txrecords[generation][-1])
# Append the termination record to the end of all generations.
self.txrecords[generation].append(termination_txrecord)
txrecords_to_transmit = [(last_txrecords, [termination_txrecord])]
sending_termination_record = True
else:
termination_txrecord = self.txrecords[0][-1]
print("Sending about %.8f in change to %s..." % (termination_txrecord.get_total_amount(), self.change_address))
sending_change = True
txrecords_to_transmit = [([termination_txrecord], [TxRecord([], [self.change_address], 0)])]
# Now transmit/re-transmit all the txrecords.
self.transmit_txrecords(txrecords_to_transmit)
first_block = False
if sending_termination_record:
self.termination_record_sent = True
# Add termination record to the end of all generations.
for generation in range(0, self.num_transactions):
self.txrecords[generation].append(termination_txrecord)
if sending_change:
self.change_sent = True
continue_flag = False
print("\nPublication complete! Waiting for the transactions to surpass the confirmation threshold. This phase is optional.")
# Keep waiting until all TxRecords have surpassed the confirmation
# threshold.
while not self.complete:
self.wait_for_new_block([], True)
i = 0
self.d("\nTxRecords:")
for generation in self.txrecords:
for txrecord in generation:
i += 1
self.d("\t%s" % txrecord)
print("%d transactions awaiting full confirmation..." % i)
print("All transactions fully confirmed.")
# Transmits/re-transmits TxRecords.
def transmit_txrecords(self, txrecords_to_transmit):
# Now transmit/re-transmit all the txrecords.
while len(txrecords_to_transmit) > 0:
txrecords_to_watch = []
initial_nop_split = False
for transaction in txrecords_to_transmit:
previous_txrecords = transaction[0]
next_txrecords = transaction[1]
next_txrecord = None
if len(next_txrecords) == 1:
next_txrecord = next_txrecords[0]
# Handle case #1 (see huge comment on
# txrecords_to_transmit, above).
else:
initial_nop_split = True
redeem_scripts = []
p2sh_addresses = []
for txr in next_txrecords:
redeem_scripts.extend(txr.redeem_scripts)
p2sh_addresses.extend(txr.p2sh_addresses)
next_txrecord = TxRecord(redeem_scripts, p2sh_addresses, 0)
# Send this transaction on its way!
self.send_transaction(previous_txrecords, next_txrecord)
txrecords_to_watch.append(next_txrecord)
self.d("Waiting for next block...")
self.wait_for_new_block(txrecords_to_watch)
# Handle case #1 (see huge comment on txrecords_to_transmit,
# above).
if initial_nop_split and (txrecords_to_watch[0].get_confirmations() > 0):
self.d("Handling NOOP split...")
bogus_txrecord = txrecords_to_watch[0]
for generation in range(0, self.num_transactions):
temp_txrecord = self.txrecords[generation][-1]
temp_txrecord.set_txid(bogus_txrecord.get_txid())
temp_txrecord.set_total_amount(bogus_txrecord.get_total_amount() / self.num_transactions)
temp_txrecord.set_confirmations(bogus_txrecord.get_confirmations())
temp_txrecord.add_output_script(bogus_txrecord.output_scripts.pop(0))
temp_txrecord.add_vout_num(bogus_txrecord.vout_nums.pop(0))
temp_txrecord.add_value(bogus_txrecord.values.pop(0))
# The number of confirmations is 1 or more, so no need to
# retransmit.
txrecords_to_transmit = []
else:
# Go through all the ones we were watching.
for watched_txrecord in txrecords_to_watch:
# If this one has 1 or more confirmations...
if watched_txrecord.get_confirmations() > 0:
# ... update the total number of confirmed bytes...
self.update_confirmed_bytes(watched_txrecord.num_bytes)
# ... and remove it from the to-transmit list.
for transaction in txrecords_to_transmit:
if transaction[1][0] == watched_txrecord:
txrecords_to_transmit.remove(transaction)
else:
self.d("TXID not confirmed in last block: %s" % watched_txrecord.get_txid())
num_retransmit = len(txrecords_to_transmit)
if num_retransmit > 0:
self.d("Re-transmitting %d unconfirmed records..." % num_retransmit)
# Creates and returns a TxRecord with a termination message.
def create_termination_record(self):
key = self.temporal_key
iv = self.temporal_iv
extra = self.temporal_extra
# If the deadman switch is enabled, overwrite the key information.
if self.deadman_switch_path is not None:
key = b'\xff' * 32
iv = b'\xff' * 32
extra = b'\xff' * 32
# 8 bytes to denote the end of the file
# 8 bytes for the nonce
# 32 bytes for the nonce hash
# 4 reserved bytes
# 32 bytes for the temporal encryption key
# 32 bytes for the temporal IV (currently not used)
# 32 bytes for extra encryption data (currently not used)
nonce = os.urandom(Publication.NONCE_LEN)
nonce_hash = hashlib.sha256(Publication.HEADER_TERMINATE + nonce + Publication.NONCE_SALT).digest()
header_termination_bytes = Publication.HEADER_TERMINATE + \
nonce + \
nonce_hash + \
b'\x00\x00\x00\x00' + \
key + \
iv + \
extra
self.d("\nTermination bytes: %s" % binascii.hexlify(header_termination_bytes).decode('ascii'))
redeem_script, p2sh_address = self.make_redeem_script(header_termination_bytes)
return TxRecord([redeem_script], [p2sh_address], 0)
# Creates and returns a TxRecord with a NOOP message.
def create_noop_record(self):
nonce = os.urandom(Publication.NONCE_LEN)
nonce_hash = hashlib.sha256(Publication.HEADER_NOOP + nonce + Publication.NONCE_SALT).digest()
header_noop_bytes = Publication.HEADER_NOOP + nonce + nonce_hash
redeem_script, p2sh_address = self.make_redeem_script(header_noop_bytes)
return TxRecord([redeem_script], [p2sh_address], 0)
# Creates the next TxRecord, based on the number of bytes previously read
# from the file.
def create_next_txrecord(self):
redeem_scripts = []
p2sh_addresses = []
file_pos = self.get_file_position()