|
| 1 | +import hashlib |
| 2 | + |
| 3 | +from electroncash import bitcoinfiles |
| 4 | +from electroncash.transaction import Transaction |
| 5 | + |
| 6 | + |
| 7 | +def upload_file(wallet, file_bytes, config={}, password=None): |
| 8 | + file_tx_id = None |
| 9 | + tx_batch = [] |
| 10 | + |
| 11 | + file_size = len(file_bytes) |
| 12 | + assert file_size <= 10522 |
| 13 | + |
| 14 | + metadata = dict() |
| 15 | + metadata['filename'] = 'data' |
| 16 | + metadata['fileext'] = 'json' |
| 17 | + metadata['filesize'] = file_size |
| 18 | + metadata['file_sha256'] = hashlib.sha256(file_bytes).hexdigest() |
| 19 | + metadata['prev_file_sha256'] = None |
| 20 | + metadata['uri'] = None |
| 21 | + |
| 22 | + cost = bitcoinfiles.calculateUploadCost(file_size, metadata) |
| 23 | + file_receiver_address = wallet.get_addresses()[0] # todo change this |
| 24 | + |
| 25 | + # TODO, guard tokens during this transaction? |
| 26 | + ##################################################################################### |
| 27 | + |
| 28 | + file_220_chunks = [] |
| 29 | + for i in range(1, (len(file_bytes) // 220) + 2): |
| 30 | + file_220_chunks.append(file_bytes[(i-1)*220:i*220]) |
| 31 | + |
| 32 | + funding_tx = bitcoinfiles.getFundingTxn(wallet, file_receiver_address, cost, config) |
| 33 | + wallet.sign_transaction(funding_tx, password) |
| 34 | + tx_batch.append(funding_tx) |
| 35 | + |
| 36 | + prev_tx = funding_tx |
| 37 | + for i in range(len(file_220_chunks)): |
| 38 | + tx, is_metadata_tx = bitcoinfiles.getUploadTxn( |
| 39 | + wallet, prev_tx=prev_tx, chunk_index=i, chunk_count=len(file_220_chunks), |
| 40 | + chunk_data=file_220_chunks[i], config=config, metadata=metadata, file_receiver=file_receiver_address |
| 41 | + ) |
| 42 | + wallet.sign_transaction(tx, password) |
| 43 | + tx_batch.append(tx) |
| 44 | + prev_tx = tx |
| 45 | + |
| 46 | + if not is_metadata_tx: # last chunk didn't fit into the metadata tx |
| 47 | + tx, is_metadata_tx = bitcoinfiles.getUploadTxn( |
| 48 | + wallet, prev_tx=prev_tx, chunk_index=i+1, chunk_count=len(file_220_chunks), |
| 49 | + chunk_data=b'', config=config, metadata=metadata, file_receiver=file_receiver_address |
| 50 | + ) |
| 51 | + wallet.sign_transaction(tx, password) |
| 52 | + tx_batch.append(tx) |
| 53 | + if is_metadata_tx: |
| 54 | + file_tx_id = tx.txid() |
| 55 | + |
| 56 | + for tx in tx_batch: |
| 57 | + status, tx_id = wallet.network.broadcast_transaction(tx) |
| 58 | + assert status |
| 59 | + return file_tx_id |
| 60 | + |
| 61 | + |
| 62 | +def download_file(wallet, tx_id): |
| 63 | + network = wallet.network |
| 64 | + |
| 65 | + status, raw_metadata_tx = network.get_raw_tx_for_txid(tx_id) |
| 66 | + assert status |
| 67 | + metadata_tx = Transaction(raw_metadata_tx) |
| 68 | + |
| 69 | + bitcoin_files_metadata_msg = bitcoinfiles.BfpMessage.parseBfpScriptOutput(metadata_tx.outputs()[0][1]) |
| 70 | + |
| 71 | + chunk_count = bitcoin_files_metadata_msg.op_return_fields['chunk_count'] |
| 72 | + chunk_data = bitcoin_files_metadata_msg.op_return_fields['chunk_data'] |
| 73 | + chunk_data_is_empty = chunk_data == b'' |
| 74 | + |
| 75 | + downloaded_transactions = [] |
| 76 | + assert chunk_count != 0 |
| 77 | + |
| 78 | + def get_tx_chunks(tx_id, index): |
| 79 | + status, raw_tx = network.get_raw_tx_for_txid(tx_id) |
| 80 | + assert status is True |
| 81 | + tx = Transaction(raw_tx) |
| 82 | + try: |
| 83 | + data = bitcoinfiles.parseOpreturnToChunks( |
| 84 | + tx.outputs()[0][1].to_script(), allow_op_0=False, allow_op_number=False |
| 85 | + ) |
| 86 | + except bitcoinfiles.BfpOpreturnError: # It's the funding tx probably |
| 87 | + return |
| 88 | + downloaded_transactions.append( |
| 89 | + {'tx_id': metadata_tx.txid(), |
| 90 | + 'data': data[0] |
| 91 | + } |
| 92 | + ) |
| 93 | + index += 1 |
| 94 | + if index <= chunk_count - 1: # TODO removed <= to < |
| 95 | + get_tx_chunks(tx.inputs()[0]['prevout_hash'], index) |
| 96 | + |
| 97 | + if chunk_count == 1: |
| 98 | + if not chunk_data_is_empty: |
| 99 | + downloaded_transactions.append({'tx_id': metadata_tx.txid(), 'data': chunk_data}) |
| 100 | + # DONE! FINISHED! |
| 101 | + if chunk_count > 1 or (chunk_count == 1 and chunk_data_is_empty): |
| 102 | + if not chunk_data_is_empty: |
| 103 | + downloaded_transactions.append({'tx_id': metadata_tx.txid(), 'data': chunk_data}) |
| 104 | + index = 0 |
| 105 | + get_tx_chunks(metadata_tx.inputs()[0]['prevout_hash'], index) |
| 106 | + |
| 107 | + f = b'' |
| 108 | + downloaded_transactions.reverse() |
| 109 | + for element in downloaded_transactions: |
| 110 | + f += element['data'] |
| 111 | + assert hashlib.sha256(f).hexdigest() == bitcoin_files_metadata_msg.op_return_fields['file_sha256'].hex() |
| 112 | + return f |
| 113 | + |
| 114 | + |
0 commit comments