Transaction Base64 PSBT to Raw/Serialized Hex
In this article, we will consider the process of converting a base64-encoded PSBT (Programming Script Binaries) transaction into its raw and serialized (hex) formats. We will be using the Bitcoin Core CLI as our toolkit.
Prerequisites
- Familiarity with the basics of Bitcoin and its blockchain architecture.
- Understanding of PSBT and Base64 encoding standards.
Step 1: Convert Base64 PSBT transaction to Raw
To convert a base64-encoded PSBT transaction, we need to use the psbt
command-line tool provided by Bitcoin Core. The converttopsbt
command is used for this purpose. Here’s an example of how to use it:
bitcoincore-cli converttopsbt
Replace
with the actual base64 encoded PSBT transaction as shown in the Bitcoin Core output.
Step 2: Convert the raw transaction to serialized hex
After converting the PSBT transaction from raw format, we need to convert it to serialized (hex) format. We can again use a tool like psbt
or use the cointool
library for this purpose.
Here’s an example of how to convert a raw transaction from the Bitcoin Core CLI:
bitcoincore-cli cointool -t raw2hex
Replace
with the actual raw transaction as shown in the Bitcoin Core output.
Step 3: Compare and Check
To verify that the conversion process is correct, we can compare the base64-encoded PSBT transaction to its serialized (hexadecimal) equivalent:
bitcoincore-cli cointool -t hex2raw
This command will create a raw transaction from the original transaction in base64 encoding.
Example Output
Here is an example output for the above steps using a fictitious PSBT transaction as input:
PSBT transaction in Base64 encodingPSBT("1.3.0", {
"encoding": "base64",
"scriptSig": ["0101010000000000000000000000000000000000"],
"blockNumber": "100000",
"transactionIndex": "500000"
})
Conversion Code
Here’s a simple Python script to convert from base64 to raw and then serialize hex:
import base64
import json
def base64_to_tsb(base64_transaction):
Use the psbt command line tool, provided by Bitcoin Coreoutput = bitcoincore-cli converttopsbt(base64_transaction)
return json.loads(output)
def tsb_to_raw(tsb_json, encoding="base64"):
Use the cointool library to convert from raw to hexif encoding == "raw":
return cointool(tsb_json, "hex2raw")
elif encoding == "hex":
return cointool(tsb_json, "hex2raw")
Usage examplebase64_transaction = "PSBT(\"1.3.0\", { \"encoding\": \"base64\", \"scriptSig\":[\"010101000000000000000000000000000000\", \"blockNumber\": \"100000\", \"transactionIndex\": \"500000\" })"
tsb_json = base64_to_tsb(base64_transaction)
raw_hex = tsb_to_raw(tsb_json, encoding="hex")
print(raw_hex)
Output:
In this code example, we first convert a base64-encoded PSBT transaction to a Python dictionary using json.loads()
. Then we use the cointool
library to convert it from raw to hex. The result is output as raw and serialized (hex) transactions.
Conclusion
Converting between base64 and raw/serialized (hex) formats in Bitcoin Core involves two steps: first, converting a PSBT transaction from base64 to raw; secondly, converting the raw transaction into its serialized (hexadecimal) equivalent. This process can be implemented using various tools and libraries provided by the Bitcoin Core CLI.