Squrve-Benchmarks / spider2 /lite /external /blockchain_data_transformations.md
satissss's picture
Upload 174 files
47d665b verified

In this guide, you will learn how to work with blockchain data, specifically focusing on address transformations and how to represent TRON blockchain addresses. We will also introduce some key concepts related to JavaScript functions that you may need to apply when working with such data in your queries.

TRON Addresses and Base58 Encoding TRON addresses use a specialized format that differs from Ethereum addresses, though they are closely related. The conversion of an Ethereum-like address (hexadecimal) to a TRON address (Base58 format) involves several key steps:

Hexadecimal to Byte Array Conversion: Blockchain addresses are commonly represented in hexadecimal form (prefixed with 0x). These hex strings must be converted into byte arrays for further processing. In JavaScript, this can be done using libraries such as ethers.js.

Checksum Calculation Using SHA-256: A checksum ensures the integrity of the address. For TRON, the address bytes undergo two rounds of SHA-256 hashing. Only the first 4 bytes of the second hash are retained as the checksum.

Base58 Encoding: Once the checksum is generated, the byte array (composed of the address bytes followed by the checksum) is encoded using Base58. This encoding method is commonly used in blockchain systems to make the addresses shorter and more readable.

Final Address Format: After the conversion process, you will get the final TRON address, which can be used in TRON blockchain queries.

JavaScript Functions for Address Conversion When working with blockchain data in SQL, it's important to understand how JavaScript functions can be used within SQL queries to handle custom transformations like address conversions. In this case, the function hexToTron can be used to convert a standard Ethereum-like hex address into a TRON-compatible format.

Here’s an outline of how this function works:

encode58(buffer): This function converts a byte array into a Base58 string. It uses an alphabet of characters commonly used in TRON and Bitcoin addresses. sha256(msgBytes): This function computes the SHA-256 hash of the input byte array. It uses the ethers.js library to perform the hashing operation and returns the resulting byte array. Checksum and Final Address: The function then appends the checksum to the address bytes, and finally encodes everything using Base58 to produce the TRON address. These steps are essential when querying TRON blockchain data, as addresses must be transformed into the correct format for the blockchain to recognize them.