Last active
October 14, 2024 17:07
-
-
Save luisschwab/d17642306dced3f4a2baef321b994ec7 to your computer and use it in GitHub Desktop.
add an OP_RETURN output to a PSBT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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