You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Sign inDemoInstall


Package Overview
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies



This library provides a Python interface for building BitcoinSV scripts and transactions.



TX Engine

This library provides a Python interface for building BitcoinSV scripts and transactions.

The classes Script, Context, Tx, TxIn and TxOut are imported from the top level of tx_engine.

For documentation of the Python Classes see here

Python Installation

As this library is hosted on PyPi ( it can be installed using the following command:

pip install tx-engine

Example Script execution

>>> from tx_engine import Script, Context

>>> s = Script.parse_string("OP_10 OP_5 OP_DIV")
>>> c = Context(script=s)
>>> c.evaluate()
>>> c.get_stack()


The context is the environment in which bitcoin scripts are executed.

  • evaluate_core - executes the script, does not decode stack to numbers
  • evaluate - executes the script and decode stack elements to numbers

Context Stacks

Context now has:

  • raw_stack - which contains the stack prior to converting to numbers
  • raw_alt_stack - as above for the alt_stack

Example from unit tests of usingraw_stack:

script = Script([OP_PUSHDATA1, 0x02, b"\x01\x02"])
context = Context(script=script)
self.assertEqual(context.raw_stack, [[1,2]])

Quiet Evalutation

Both evaluate and evaluate_core have a parameter quiet. If the quiet parameter is set to True the evaluate function does not print out exceptions when executing code. This quiet parameter is currently only used in unit tests.

Inserting Numbers into Script

  • encode_num() is now insert_num()


Bitcoin transactions are represented by the Tx class. Where possible the existing tx_engine attributes and methods have be maintained for the classes Tx, TxIn and TxOut.

The following attributes and methods have been removed:

  • demopFunc/demopper, isTestNet
  • tx_fetcher, fetch_tx(), value(), script_pubkey(), add_extrac_script_sig_info()
  • serialised_demopped(), fee(), coinbase_height()

BytesIO has been replaced by bytes

Example Tx class usage

from tx_engine import Tx

raw_tx = bytes.fromhex(
tx = Tx.parse(raw_tx)
assert tx.version == 1

Script Debugger

The script debugger enables the user to examine the stack status as the script is executing as well as writing interactive script.

Example debugger usage:

% cd python/src
% python3 -f ../examples/
Script debugger
For help, type "help".
Loading filename: ../examples/
altstack = [], stack = []
(gdb) list
0: OP_1
1: OP_2
altstack = [], stack = []
(gdb) s
0: OP_1
altstack = [], stack = [1]
(gdb) s
1: OP_2
altstack = [], stack = [1, 2]
(gdb) s
altstack = [], stack = [3]

The debugger supports the following commands:

  • h, help - Prints a list of commands
  • q, quit, exit -- Quits the program
  • file [filename] - Loads the specified script file for debugging
  • list - List the current script file contents
  • run - Runs the current loaded script until breakpoint or error
  • i [script] -- Execute script interactively
  • hex - Display the main stack in hexidecimal values
  • dec - Display the main stack in decimal values
  • reset - Reset the script to the staring position
  • s, step - Step over the next instruction
  • c - Continue the current loaded script until breakpoint or error
  • b - Adds a breakpoint on the current operation
  • b [n] - Adds a breakpoint on the nth operation
  • info break - List all the current breakpoints
  • d [n] - Deletes breakpoint number n



Did you know?


Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog


Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc