Skip to content

Instantly share code, notes, and snippets.

@kdmukai
Last active June 13, 2023 00:24
Show Gist options
  • Save kdmukai/0d07e6c4470863f458acc7b4d2d14788 to your computer and use it in GitHub Desktop.
Save kdmukai/0d07e6c4470863f458acc7b4d2d14788 to your computer and use it in GitHub Desktop.
Adding an OP_RETURN output to a psbt using `embit`
"""
dependency: pip install embit
"""
from embit import compact
from embit.psbt import PSBT, OutputScope
from embit.script import Script
class OPCODES:
OP_RETURN = 106
OP_PUSHDATA1 = 76
psbt = PSBT.from_base64("cHNidP8BAHQC...")
raw_payload_data = "This is your message".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(psbt.to_string())
@xavierfiechter
Copy link

Thank you for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment