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;
});
};
ClaimStatus
? What isCategory
?string, uint
and notuint, string
?result.output
, but only 6 in your contract function. Is this really your most recent contract code?