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

Base64 PSBT Transaction Conversion 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, serialized (hex) format. We will use the Bitcoin Core CLI as our toolchain.

Prerequisites

  • Familiarity with the fundamentals 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 is 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 hexadecimal

After converting the PSBT transaction from raw format, we need to convert it to serialized (hex) format. We can use a tool like psbt again or use the cointool library for this purpose.

Here is 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.

Sample Output

Here is a sample output for the above steps using a dummy PSBT transaction as input:




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

Base64 encoded PSBT transaction

PSBT("1.3.0", {

"encoding": "base64",

"scriptSig": ["0101010000000000000000000000000000000"],

"blockNumber": "100000",

"transactionIndex": "500000"

})

Conversion Code

Here is a simple Python script to convert from base64 to raw hexadecimal and then serialize:

import base64

import JSON

def base64_to_tsb(base64_transaction):


Use the 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 the cointool library to convert from raw to hex

if encoding == "raw":

return cointool(tsb_json, "hex2raw")

elif encoding == "hex":

return cointool(tsb_json, "hex2raw")


Usage example

base64_transaction = "PSBT(\"1.3.0\", { \"encoding\": \"base64\", \"scriptSig\":[\"010101000000000000000000000000000\", \"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(). We then use the cointool library to convert it from raw to hexadecimal format. The output 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 achieved using various tools and libraries provided by the Bitcoin Core CLI.

Ethereum Linux Bitcoind

Leave a Comment

Your email address will not be published. Required fields are marked *