Skip to content

Instantly share code, notes, and snippets.

@cirosantilli
Forked from shirriff/bitcoin-file-downloader.py
Created January 10, 2021 00:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cirosantilli/e90bd2e6c3fab25a20898e61e3ab3e90 to your computer and use it in GitHub Desktop.
Save cirosantilli/e90bd2e6c3fab25a20898e61e3ab3e90 to your computer and use it in GitHub Desktop.
This source code is embedded in Bitcoin transaction 6c53cd987119ef797d5adccd76241247988a0a5ef783572a9972e7371c5fb0cc
#
# File downloader
# Requires git://github.com/jgarzik/python-bitcoinrpc.git
#
# (c) 2013 Satoshi Nakamoto All Rights Reserved
#
# UNAUTHORIZED DUPLICATION AND/OR USAGE OF THIS PROGRAM IS PROHIBITED BY US AND INTERNATIONAL COPYRIGHT LAW
import sys
import os
import jsonrpc
import struct
from binascii import crc32,hexlify,unhexlify
if len(sys.argv) != 2:
print("""\
Usage: %s <txhash>
Set BTCRPCURL=http://user:pass@localhost:portnum""" % sys.argv[0], file=sys.stderr)
sys.exit()
proxy = jsonrpc.ServiceProxy(os.environ['BTCRPCURL'])
txid = sys.argv[1]
tx = proxy.getrawtransaction(txid,1)
data = b''
for txout in tx['vout'][0:-2]:
for op in txout['scriptPubKey']['asm'].split(' '):
if not op.startswith('OP_') and len(op) >= 40:
data += unhexlify(op.encode('utf8'))
length = struct.unpack('<L', data[0:4])[0]
checksum = struct.unpack('<L', data[4:8])[0]
data = data[8:8+length]
if checksum != crc32(data):
print('Checksum mismatch; expected %d but calculated %d' % (checksum, crc32(data)),
file=sys.stderr)
sys.exit()
sys.stdout.buffer.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment