1

When i used web3.toAscii I received the following error :

enter image description here

I think the reason is my web3 version i.e. '1.0.0-beta.34'

If so, is there any alternative command for web3 version 1 for the following command ?

transaction = web3.eth.getTransaction('0x0c485e0f155f7f216d06e70ef85b684392c9b190c2f7c0af67ec1b56d6945498')
input = web3.toAscii(transaction.input)
console.log(input)

I also used following command :

web3.utils.hexToAscii(0x0c485e0f155f7f216d06e70ef85b684392c9b190c2f7c0af67ec1b56d6945498)

However, I receive the following error :

enter image description here

That mean its input cannot be a transaction hash.

1

The equivalent should be web3.utils.hexToAscii

web3.utils.hexToAscii('0x4920686176652031303021');
> "I have 100!"
  • Thanks, I already tried this, however its input cannot be a transaction hash. I explained this in my question. – Questioner May 7 '18 at 20:15
  • @sas You are missing " around your parameter web3.utils.hexToAscii("0x0c485e0f155f7f216d06e70ef85b684392c9b190c2f7c0af67ec1b56d6945498"). In any case it doesn't make much sense to use hexToAscii with a transaction hash or a transaction.input. It was intended to use on some fields that were generated by asciiToHex like block.extraData. If you want to parse a transaction input then you need the contract abi and using a library like ethereumjs-abi to read the parameters values of the call. – Ismael May 7 '18 at 20:29
  • You're right, however this time, the output is not as expected :'\fH^\u000f\u0015_!m\u0006ç\u000eø[hCɱÂ÷À¯gì\u001bVÖT'" . Because accordong to this answer : ethereum.stackexchange.com/questions/11144/… I want to decode transaction data such that it would be readable. – Questioner May 7 '18 at 20:39
  • It does work in the link because it was generated by toHex (equivalent to today's asciiToHex). It doesn't work in your case because it is a raw function call. As I said you will need the contract ABI and a ABI parser to extract info from the parameters of the call. – Ismael May 7 '18 at 21:50
1

Like Ismael mention, you have to use hexToAscii with web3@^1.0

But looks like you are using it on the transaction.hash and not the transaction.input

EDIT: example:

Submit transaction:

const Tx = require('ethereumjs-tx');
const web3 = require('web3');
const data = web3.utils.toHex('Hello world!');
const rawTx = {
    nonce: web3.utils.toHex(nonce),
    gasPrice: web3.utils.toHex(gasPrice),
    gasLimit: web3.utils.toHex(gasCost),
    to: '0x0000000000000000000000000000000000000000',
    value: '0x00',
    data,
};
const privateKeyWithoutZero = privateKey.startsWith('0x') ? privateKey.slice(2, privateKey.length) : privateKey;
const privateKeyBuffer = new Buffer(privateKeyWithoutZero, 'hex');
const transaction = new Tx(rawTx);
transaction.sign(privateKeyBuffer);

const serializedTx = transaction.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
    .on('transactionHash', (txHash) => {
        console.log('Transaction hash', txHash);
})
    .on('receipt', (receipt) => {
        console.log('Transaction sent with sucess. debug:', receipt);
});

Then, getTransaction:

web3.eth.getTransaction('<TRANSACTION-HASH>')
        .then((transaction) => {
          const inputData = Web3.utils.hexToAscii(transaction.input);
          console.log(inputData); // Hello world!
        });
  • could u mention an example for transaction.input ? I mean using web3.utils.hexToAscii. Thanks. – Questioner May 7 '18 at 21:38
  • Example added above – oktapodia May 7 '18 at 22:39
  • Thank you. Now it's readable. I'm going to install an abi parser like this : github.com/ethereumjs/ethereumjs-abi to decode the tx data. – Questioner May 8 '18 at 0:26
  • Depends of what is your goal, but if you just want to read the input data, you don't need to use ethereum-abi, I updated my example with a simple working script – oktapodia May 8 '18 at 9:28
  • Thanks. In fact, I intend to get Tx data using HTML and JavaScript. I created a github repository here : github.com/s1241/GUI-for-Smart-Contract its smart contract tested successfully, however html and javascript is not complete. Are you interested in having a look its HTML file ? – Questioner May 8 '18 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.