Degravating raw scriptutput: Scriptpubkey isolation
When receiving transactions from Bitcoin Core using Zeromq, you probably have to deal with the RAW script in JSON format. The “Script” field includes the series of the Bitcoin script, which we need to extract “Scriptpubkey”. This type represents a public key that can be used for digital signatures and other cryptographic operations.
In this article, we will go through the way of recovery
Understanding Bitcoin Scriptformat
Before immersing in the code, quickly review the Bitcoin script format. “Script” field in Bitcoin Core Transations Bitists from the series of the following elements:
Op_dup
: Duplicate Operand duplicate (in this case value)
OP_P2PKH
: specifies that we are dealing with private keys
OP_D2SH
: transforms the script into a human reading format
- … and so on
The exit of a raw script usually looks like this:
`Json
{Value: 200000,
Script:
}
Scriptoutput decoding
To separate “Scriptpubkey”, we must decode the exit of the raw script. We can use the following fragment of Python code as an example:
“ Python
Import json
Def fragment_scriptpubkey (script_output):
#Plicz script in individual elements
Elements = script_output [‘script’].
Initiate the variable scriptpubkey
script_pubkey = none
#Eter on each element and try to recreate it as a script signature
For the element in the elements:
If Element.Startswith (“OP_DUP”):
Separate the value of the duplicate operand (input)
Value = int.from_bytes (element [7: 9], Byteregder = “BIG”)
Check if this is OP_P2PKH instructions with a private key signature
If “P2SH” in element I “OP_01” in the element:
Separate the public key from the entrance (assuming it is an indexing based on 1)
Public_key = int.from_bytes (element [9:16], Bytorer = “Big”)
Set the Scriptpubkey variable with a separate public key
If script_pubkey is not:
script_pubkey = bytes ([public_key])
Otherwise:
script_pubkey += bytes ([public_key])
Elif “P2SH” in element I “OP_01” in the element:
Separate the public key from the entrance (assuming it is an indexing based on 1)
Public_key = int.from_bytes (element [9:16], Bytorer = “Big”)
Set the Scriptpubkey variable with a dummy value for OP_DUP instructions
If script_pubkey is not:
script_pubkey = bytes ([public_key])
Otherwise:
script_pubkey += bytes ([public_key])
Elif “OP_01” in the element:
Separate the public key from the entrance (assuming it is an indexing based on 1)
Public_key = int.from_bytes (element [8:16], Bytorer = “Big”)
Set the Scriptpubkey variable with a dummy value for OP_DUP instructions
If script_pubkey is not:
script_pubkey = bytes ([public_key])
Otherwise:
script_pubkey += bytes ([public_key])
Return Script_pubkey
Sample use
script_output = json.lylads (‘{“value”: 200000, “script”: “
script_pubkey = wording_scriptpubkey (script_output)
Print (script_pubkey.
Leave a Reply