0

I have set up a Hyperledger Besu network for private transactions using Orion. I am using a javascript client to write to and read data from a smart contract written in Solidity. I am following the approach as described here: https://github.com/ConsenSys/web3js-eea/tree/master/example/multiNodeExamplePrivacyGroup

My smart contract function returns back multiple values and the output is encoded in Hex format.

Problem: I am not able to decode the Hex data and convert it into ascii or UTF-8 string so that I can use the returned data in my Javascript client app. How I can parse or decode the hex string in the output so that I can identify and read the multiple values in my javascript client?

Below you see the log which shows the output as logged by the Javascript client. Only one piece of the data is decoded well and it is the description string "Package is super damaged!" but not the other values.

console.log:

GOT Value from http://10.0.0.46:8545: 
{
  from: '0x62a...1f9787',
  to: '0x408b...d3d',
  output: '0x00000000000000000000000062ac1677fdeea5c187e3d8ab6ad200bb951f978700000000000000000000000000000000000000000000000000000000000007770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005fd7d50f00000000000000000000000000000000000000000000000000000000000000195061636b6167652069732073757065722064616d616765642100000000000000',
  commitmentHash: '0xf6b5356117958ef625bfeae49bab832c06de798bc67e1de3a9d166d4640e265d',
  transactionHash: '0x5f09b2d984e977e3fdd86299c12dc1bc5ef57b9bc264f63632d97e0c94434154',
  privateFrom: 'JpYbuv...SE=',
  privacyGroupId: 'omCbE+ixKq...MTw6M=',
  status: '0x1',
  logs: [],
  logsBloom: '0x00...000',
  blockHash: '0x3a5d43856e1f80b12497a0177e55a2c14c55d3088fa17409496eccb132c39f19',
  blockNumber: '0xad2',
  transactionIndex: '0x0'
}
GOT result.output hex to ascii from http://10.0.0.46:8545: b¬wýî¥ÁãØ«jÒ»wÀ_×ÕPackage is super damaged!

Code snippet (Solidity smart contract function):

function value(uint _deliveryNumber) view public returns (address, uint, ClaimStatus, Category, string memory, uint)  {
        return (claims[_deliveryNumber].issuer, claims[_deliveryNumber].deliveryNumber, claims[_deliveryNumber].status, claims[_deliveryNumber].category, claims[_deliveryNumber].description, claims[_deliveryNumber].timestamp);
    }

Code snippet (Javascript client):

 const getValue = (value, url, address, privateFrom, privacyGroupId, privateKey) => {
  const web3 = new EEAClient(new Web3(url), 2018);
  const contract = new web3.eth.Contract(ClaimsAbi);

  // eslint-disable-next-line no-underscore-dangle
  const functionAbi = contract._jsonInterface.find(e => {
    return e.name === "value";
  });

  const functionArgs = web3.eth.abi
  .encodeParameters(functionAbi.inputs, [value])
  .slice(2);

  const functionCall = {
    to: address,
    data: functionAbi.signature + functionArgs,
    privateFrom,
    privacyGroupId,
    privateKey
  };

  return web3.eea
    .sendRawTransaction(functionCall)
    .then(transactionHash => {
      console.log("transactionHash:" + transactionHash);
      return web3.priv.getTransactionReceipt(
        transactionHash,
        orion.node3.publicKey
      );
    })
    .then(result => {
      console.log(`GOT Value from ${url}:`, result);
      console.log(`GOT result.output hex to ascii from ${url}:`, web3.utils.hexToAscii(result.output));
      return result.output;
    });
};
8
  • What is ClaimStatus? What is Category? – goodvibration Dec 15 '20 at 8:14
  • These are enums:/* * Enums */ enum ClaimStatus { CREATED, DISPATCHED, IN_PROGRESS, INVESTIGATION_DONE, ABORTED, FINAL } enum Category { DAMAGED, LOST } – Christian Schepp Dec 15 '20 at 8:18
  • And are you sure that the last two return values are string, uint and not uint, string? – goodvibration Dec 15 '20 at 8:27
  • Plus, I see 7 return values in your result.output, but only 6 in your contract function. Is this really your most recent contract code? – goodvibration Dec 15 '20 at 8:30
  • I return 6 values and the order of return values should be good with description (string) and block.timestamp (uint). – Christian Schepp Dec 15 '20 at 8:39
0

The resolution of my problem to decode the output of a Solidity smart contract function is to use the web3.eth.abi.decodeParameters function (https://web3js.readthedocs.io/en/v1.2.11/web3-eth-abi.html#decodeparameters).

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.