Converting a Base64 PSBT Transaction to Raw/Serialized Hex
In this article, we will explore the process of converting a base64 encoded Programming Script Binaries (PSBT) transaction to its raw and serialized (hex) format. We will use the Bitcoin Core CLI as our toolset.
Prerequisites
- Familiarity with Bitcoin basics and its blockchain architecture.
- Understanding of PSBT and Base64 encoding standards.
Step 1: Convert a 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 is an example of its usage:
bitcoincore-cli converttopsbt
Replace
with the base64 encoded PSBT transaction as it appears 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 its serialized (hex) format. We can use a tool like psbt
again or use the cointool
library for this purpose.
Here is an example of converting a raw transaction from the Bitcoin Core CLI:
bitcoincore-cli cointool -t raw2hex
Replace
with the actual raw transaction as it appears in the Bitcoin Core output.
Step 3: Compare and Verify
To ensure that your conversion process is correct, we can compare the base64-encoded PSBT transaction with its serialized (hex) equivalent:
bitcoincore-cli cointool -t hex2raw
This command will produce a raw transaction from the original base64-encoded transaction.
Sample Output
Here is a sample output for the above steps using a dummy PSBT transaction as input:
Base64 encoded PSBT transactionPSBT("1.3.0", {
"encoding": "base64",
"scriptSig": ["010101000000000000000000000000000000000"],
"blockNumber": "100000",
"transactionIndex": "500000"
})
Conversion Code
Here is a simple Python script to convert from base64 to raw hex and then serialize:
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 cointool library to convert from raw to hexif encoding == "raw":
return cointool(tsb_json, "hex2raw")
elif encoding == "hex":
return cointool(tsb_json, "hex2raw")
Example usagebase64_transaction = "PSBT(\"1.3.0\", { \"encoding\": \"base64\", \"scriptSig\":[\"01010100000000000000000000000000000000\", \"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 the base64 encoded PSBT transaction to a Python dictionary using json.loads()
. Then, we use the cointool
library to convert it from raw to hex format. The result is printed as raw and serialized (hex) transactions.
Conclusion
Converting between base64 encoded and raw/serialized (hex) formats in Bitcoin Core involves two steps: first, converting the PSBT transaction from base64 to raw; second, converting the raw transaction to its serialized (hex) equivalent. This process can be done using various tools and libraries provided by Bitcoin Core CLI.