Decrypting the Raw ScriptOutput: Extracting the ScriptPubKey

If you are using zeroMQ to receive transactions from Bitcoin Core, you are probably dealing with the raw script output in JSON format. The “script” field contains a serialized version of a Bitcoin script, which we need to extract the “scriptPubKey”. This type represents the public key that can be used for digital signatures and other cryptographic operations.

In this article, we will look at how to extract the scriptpubkey (the actual bytes that represent the public key) from the raw script output.

Understanding Bitcoin Script Format

Before we dive into the code, let’s quickly look at the format of a Bitcoin script. The “script” field in Bitcoin Core transactions typically consists of a serialized version of the following:

  • OP_DUP: duplicates the current operand (in this case, the value)
  • OP_P2PKH: indicates that these are private keys
  • OP_D2SH: converts the script to a human-readable format
  • … and so on

The output of the raw script typically looks like this:

{value: 200000,

script:

}

Decode script output

To get “scriptPubKey” , we need to decode the raw output of the script. As an example, we can use the following Python code snippet:

“` Python

JSON import

on def extract_scriptpubkey(script output):

Split the script into individual elements

elements = script output[‘script’].split(‘\x00’)

Initialize the scriptpubkey variable

script_pubkey = None

Iterate over each element and try to decode it as a script signature

for element in elements:

if element.startswith(‘OP_DUP’):

Extract the value of the duplicate operand (input)

value = int.from_bytes(element[7:9], byteorder=’large’)

Check if this is an OP_P2PKH instruction with a private key

if ‘P2SH’ in element and ‘OP_01’ in element:

Extract the public Key from input (assuming it is based on 1 indexing)

public key = int.from_bytes(element[9:16], byte order=’large’)

Set the scriptpubkey variable with the extracted public key

if script_pubkey is None:

script_pubkey = bytes([public_key])

else:

script_pubkey += bytes([public_key])

elif ‘P2SH’ in element and ‘OP_01’ in element:

Extract the public key from input (assuming it is based on 1 indexing)

public key = int.from_bytes(element[9:16], byte order=’large’)

Set the scriptpubkey variable to a dummy value for the OP_DUP statement

if script_pubkey is None:

script_pubkey = bytes([public_key])

else:

script_pubkey += bytes([public_key])

elif ‘OP_01’ in element:

Extract the public key from the input (assuming it is based on 1 indexing)

public_key = int.from_bytes(element[8:16], byte_order=’large’)

Set the scriptpubkey variable to a dummy value for the OP_DUP statement

if script_pubkey is None:

script_pubkey = bytes([public_key])

else:

script_pubkey += bytes([public_key])

return script_pubkey

Example usage

script_output = json.loads(‘{“value”: 200000, “script”: “

script_pubkey = script_pubkey extraction (script_output)

print (script_pubkey.