Skip to content

Instantly share code, notes, and snippets.

@luisschwab
Last active September 25, 2024 02:07
Show Gist options
  • Save luisschwab/d17642306dced3f4a2baef321b994ec7 to your computer and use it in GitHub Desktop.
Save luisschwab/d17642306dced3f4a2baef321b994ec7 to your computer and use it in GitHub Desktop.
create a transaction with a OP_RETURN output
#!/usr/bin/python3
'''
this script takes in an PSBT and adds an
OP_RETURN output (OP_RETURN OP_PUSHDATA1 <len(message)> <message>)
with the chosen message payload
usage:
interactive:
./op-return-builder.py
static:
./op-return-builder.py <base64 encoded PSBT> <message (80 char max)>
'''
import sys
from embit import compact
from embit.psbt import PSBT, OutputScope
from embit.script import Script
class OPCODES:
OP_RETURN = 106
OP_PUSHDATA1 = 76
if len(sys.argv) == 3:
psbt_string = sys.argv[1]
opreturn_string = sys.argv[2]
else:
psbt_string = input("Paste your base64 encoded PSBT below:\n")
opreturn_string = input("\nWrite your message: ")
psbt = PSBT.from_base64(psbt_string)
raw_payload_data = opreturn_string.encode()
data = (compact.to_bytes(OPCODES.OP_RETURN) +
compact.to_bytes(OPCODES.OP_PUSHDATA1) +
compact.to_bytes(len(raw_payload_data)) +
raw_payload_data)
script = Script(data)
output = OutputScope()
output.script_pubkey = script
output.value = 0
psbt.outputs.append(output)
print("\nModified PSBT:")
print(psbt.to_string())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment