Ethereum web3.js and hexadecimal data
As a developer that we use the web3, it is possible that you have encountered problems with hexadecimal data of the transactions. In this article, we will go to the web3. in the concept of objects of objects and hexadecimal and we will explore how to overcome common problems.
What is web3?
Web3 is a JavaScript library for interaction with Ethereum blockchain. It provides a decentralized application (DAPP) for the API and a set of utilities in the Ethereum Network. When working with transactions, web3 uses an “TX” object, which represents the transaction sent to the block chain.
Web33 hexadecimal data. You in transactions
HEX data is used to display without processing transaction data stored in the block chain. It is a series of hexadecimal characters that contain information on transactions such as hash, load and other information. Web3. You are embedded in the “Tx” object.
Object tx
and hexadecimal data
When creating a new agreement using the web3.it, the tx
object is initialized with several characteristics:
No: address of the sender
- to
: DIRECTORATE OF THE recipient
- Chainid
: Ethereum Blockchain Id of Network (where appropriate, 44445)
- Value ‘: the amount to be transmitted
However, by printing or registering data related to Hex, the use of methods such as Console.log (tx.rawtransaction.hex)
It appears as a hexadecimal characters chain. But if we deepen, we see that they are actually without hexadecimal data without processing those that represent the agreement itself.
Problem: Hidden hexagonal data
Suppose you are trying to obtain special information of hexadecimal data using methods such as Json.parsse ()
or hex2abiddr ()
. In doing so, you can face problems, since these characteristics cannot directly analyze the hexadecimal data in the object.
Why is this happening?
The reason why their hexadecimal data seems hidden is that web3 uses a specific coding scheme to save transaction data in the block chain. This coding scheme includes integrated hexadecimal data as part of the transaction load, not as separate bytes.
In other words, when printing or registering tx.rawtransaction.hex
, it actually prints unprocessed hexadecimal data from the Ethereum network, which is stored in the transaction load. However, its program does not have direct access to this load, so it appears as a hidden hexadecimal character.
Problem solution
You can use some solutions to overcome this problem:
1
Use tx.rawtransaction.hex
directly : instead of printing or registering hexadecimal data separately, you can transfer ‘tx.rawtransaction.hexdirectly in its characteristics you need.
- Extract hexadecimal data using web3. You Built -in Methods : You can use web3. Intelves methods built astx.rawtransaction.hex () To obtain hexadecimal data from the transaction load.
3
Use another coding scheme : If you create a personalized application, you may have to enter your coding scheme to store and transfer transaction data.
Example of code
Here is an example of a code fragment that shows how to use web3. You Built -in methods to obtain hexadecimal data:
“ JavaScript
Const web3 = ask (‘web3’);
// Create a new web instance
Const web3 = new web3 ();
// Obtain the current instance of the Ethereum supplier
Const a provider = wait web3.eth.getprovider ();
Const tx = {{{
Of: ‘0x1234567890abcdef’,
A: ‘0x9876543210FEDCBA’,
Chainid: 44445,
Value: ‘1.2 ether’
};
// Delete hexadecimal data using the web3.yes built methods -in
Consta rawtransaction = expect a service provider.eth.sendrawtransaction (TX);
Const txhex = wait for web3.eth.abitohex (rawtransaction.rawtransaction);
Console.log (Txhex); // Print the data obtained
// Alternatively, you can use:
Const txdata = rawtransaction.rawtransaction;
Const hex = web3.theutil.