Skip to content

Instantly share code, notes, and snippets.

@moonsettler
Last active December 17, 2024 00:35
Show Gist options
  • Save moonsettler/1b4af0677974811fd1e0b674624da9ae to your computer and use it in GitHub Desktop.
Save moonsettler/1b4af0677974811fd1e0b674624da9ae to your computer and use it in GitHub Desktop.
OP_PAIRCOMMIT python script
import hashlib
x1 = "Hello ".encode()
x2 = "World!".encode()
x1_size = len(x1)
x2_size = len(x2)
# CompactSize from https://github.com/btclib-org/btclib/blob/master/btclib/var_int.py
def compactsize(i: int) -> bytes:
"""Return the var_int bytes encoding of an integer."""
if i < 0x00:
raise f"negative integer: {i}"
if i < 0xFD: # 1 byte
return bytes([i])
if i <= 0xFFFF: # 2 bytes
return b"\xFD" + i.to_bytes(2, byteorder="little", signed=False)
if i <= 0xFFFFFFFF: # 4 bytes
return b"\xFE" + i.to_bytes(4, byteorder="little", signed=False)
if i <= 0xFFFFFFFFFFFFFFFF: # 8 bytes
return b"\xFF" + i.to_bytes(8, byteorder="little", signed=False)
raise f"integer too big for var_int encoding: '{i}'"
# PairCommit
h = hashlib.new('sha256')
h.update("PairCommit".encode())
tag_hash = h.digest()
# OP_PAIRCOMMIT
h = hashlib.new('sha256')
h.update(tag_hash)
h.update(tag_hash)
h.update(compactsize(x1_size))
h.update(x1)
h.update(compactsize(x2_size))
h.update(x2)
print(h.hexdigest())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment