Skip to content

Instantly share code, notes, and snippets.

@shirriff
Last active January 4, 2016 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirriff/7e2b3cb0d7432175d719 to your computer and use it in GitHub Desktop.
Save shirriff/7e2b3cb0d7432175d719 to your computer and use it in GitHub Desktop.
Python functions to create an arbitrary Bitcoin peer-to-peer message, and a version message.
magic = 0xd9b4bef9
def makeMessage(magic, command, payload):
checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[0:4]
return struct.pack('L12sL4s', magic, command, len(payload), checksum) + payload
def getVersionMsg():
version = 60002
services = 1
timestamp = int(time.time())
addr_me = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333)
addr_you = utils.netaddr(socket.inet_aton("127.0.0.1"), 8333)
nonce = random.getrandbits(64)
sub_version_num = utils.varstr('')
start_height = 0
payload = struct.pack('<LQQ26s26sQsL', version, services, timestamp, addr_me,
addr_you, nonce, sub_version_num, start_height)
return makeMessage(magic, 'version', payload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment