<jupyter_start><jupyter_text>(1) getrawtransaction<jupyter_code>raw_tx = cli.getrawtransaction("54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713", False, "00000000000000ecbbff6bafb7efa2f7df05b227d5c73dca8f2635af32a2e949")
# Splitting by the amount which is 1 sat for every output (except for the last output which was used as change)
# Reminder: Amount is in little-endian
# This already cuts off the very last output (change address), since this has an amount other than 1 sat
outputs = raw_tx.split("0100000000000000")
len(outputs)
# 948 = 1 input and 947 outputs, the last output won't be used, though
# based on https://bitcoin.stackexchange.com/questions/35959/how-is-the-whitepaper-decoded-from-the-blockchain-tx-with-1000x-m-of-n-multisi
pdf = ""
for output in outputs[1:-2]:
    # There are 3 65-byte data parts in every output element, preceded with 41 (65 in decimal)
    # At the beginning of each output there are two more bytes (c9 which means 201 bytes in pubkey script follow and 51 which is OP_1) to cut, thus 3 bytes in total
    cur = 6
    pdf += output[cur:cur+130]
    cur += 132
    pdf += output[cur:cur+130]
    cur += 132
    pdf += output[cur:cur+130]

# Same cut with the second last output as above, 3 bytes at the beginning and 2 bytes and the end 
pdf += outputs[-2][6:-4]


with open("bitcoin.pdf", "wb") as f:
    f.write(unhexlify(pdf[16:-16]))

# sha256 hash of the pdf should be b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553<jupyter_output><empty_output><jupyter_text>(2) gettxout  a) Calling gettxout one by one for every output<jupyter_code>outputs_prun = []
for i in range(0,946):
    output = cli.gettxout("54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713", i)
    outputs_prun.append(output)
pdf_prun = ""
for output in outputs_prun[:-1]:
    cur = 4
    pdf_prun += output['scriptPubKey']['hex'][cur:cur+130]
    cur += 132
    pdf_prun += output['scriptPubKey']['hex'][cur:cur+130]
    cur += 132
    pdf_prun += output['scriptPubKey']['hex'][cur:cur+130]

# Same cut with the last output as above
pdf_prun += outputs_prun[-1]['scriptPubKey']['hex'][4:-4]

with open("bitcoin_prun.pdf", "wb") as f:
    f.write(unhexlify(pdf_prun[16:-16]))

# sha256 hash of the pdf should be b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553<jupyter_output><empty_output><jupyter_text>b) Making only one big RPC call for all outputs<jupyter_code>outputs_prun_2 = cli.multi([("gettxout","54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713", i) for i in range(0,946)])
pdf_prun_2 = ""
for output in outputs_prun_2[:-1]:
    cur = 4
    pdf_prun_2 += output['result']['scriptPubKey']['hex'][cur:cur+130]
    cur += 132
    pdf_prun_2 += output['result']['scriptPubKey']['hex'][cur:cur+130]
    cur += 132
    pdf_prun_2 += output['result']['scriptPubKey']['hex'][cur:cur+130]

# Same cut with the last output as above
pdf_prun_2 += outputs_prun_2[-1]['result']['scriptPubKey']['hex'][4:-4]

with open("bitcoin_prun_2.pdf", "wb") as f:
    f.write(unhexlify(pdf_prun_2[16:-16]))

# sha256 hash of the pdf should be b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553<jupyter_output><empty_output><jupyter_text>(3) Unused last output<jupyter_code>unused = cli.gettxout("54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713", 946)
unused
# No sensible message<jupyter_output><empty_output><jupyter_text>File sigs<jupyter_code># Hex signature at the beginning: 25 50 44 46 --> %PDF
# Trailer: 0A 25 25 45 4F 46 0A --> .%%EOF.<jupyter_output><empty_output><jupyter_text>(4) Comparison with Musk tweet<jupyter_code>musk_coinbase = cli.getrawtransaction("88349380e1f6be0eaefcad8f5abe3303769596caeed63d5de6150bf420e8b0f0")
# Different tx then below, this is from a coinbase tx (from the input script)
# Basically the same approach as with the Genesis block
musk_coinbase
musk_opreturn = cli.gettxout("c3a369ccb3667503280b41494b88215f1953268140ac430288be0fc692c9cc58", 1)
musk_opreturn
# 496e20726574726f73706563742c2069742077617320696e6576697461626c65 in hex  
# In retrospect, it was inevitable in ASCII<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11169956668271545, 'fraction_numerical': 0.1533461723639865, 'mean_word_length': 6.114726027397261, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '650281', 'license': 'MIT', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1642, 'n_words': 357, 'path': 'whitepaper.ipynb', 'repo_name': 'k9ert/s2f-python', 'size': '4154'}