Soy nuevo en el desarrollo de contratos inteligentes usando web3j. Me enfrento a un problema, al convertir Bytes32 [] devueltos desde el método wrapper of contract (.sol) a matriz.
Tengo contrato como:
pragma solidity ^0.4.4;
contract Person
{
Person[] public people;
struct Person
{
bytes32 firstName;
bytes32 lastName;
}
function addPerson(bytes32 _firstName, bytes32 _lastname) returns (bool success)
{
Person memory newPerson;
newPerson.firstName = _firstName;
newPerson.lastName = _lastname;
people.push(newPerson);
return true;
}
function getPeople() constant returns (bytes32[], bytes32[])
{
uint leng = people.length;
bytes32[] memory first_names = new bytes32[](leng);
bytes32[] memory last_names = new bytes32[](leng);
for (uint i = 0; i < people.length; i++ )
{
Person memory currentPerson;
currentPerson = people[i];
first_names[i] = (currentPerson.firstName);
last_names[i] = (currentPerson.lastName);
}
return (first_names,last_names);
}
}
Function from generated java wrapper as:
public RemoteCall<Tuple2<List<byte[]>, List<byte[]>>> getPeople() {
final Function function = new Function("getPeople",
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Bytes32>>() {}, new TypeReference<DynamicArray<Bytes32>>() {}));
return new RemoteCall<Tuple2<List<byte[]>, List<byte[]>>>(
new Callable<Tuple2<List<byte[]>, List<byte[]>>>() {
@Override
public Tuple2<List<byte[]>, List<byte[]>> call() throws Exception {
List<Type> results = executeCallMultipleValueReturn(function);;
return new Tuple2<List<byte[]>, List<byte[]>>(
(List<byte[]>) results.get(0).getValue(),
(List<byte[]>) results.get(1).getValue());
}
});
}
En mi función principal, intento acceder a Bytes of Array devueltos por el método getPeople. Pero no puedo convertir Bytes de matriz en cadena. Estoy almacenando valores de tuplas en el método principal como:
Tuple2<List<byte[]>, List<byte[]>> x = contract.getPeople().send();
Quiero convertir valor, x.getValue1()
a cadena. ¿Alguien puede decirme cómo hacerlo?
Pase su hexadecimal o byte en este método le devolverá la cadena.
var hexToUtf8 = function(hex) { if (!isHexStrict(hex)) throw new Error('El parámetro "'+ hex +'" debe ser una cadena HEX válida.');
var str = "";
var code = 0;
hex = hex.replace(/^0x/i,'');
// remove 00 padding from either side
hex = hex.replace(/^(?:00)*/,'');
hex = hex.split("").reverse().join("");
hex = hex.replace(/^(?:00)*/,'');
hex = hex.split("").reverse().join("");
var l = hex.length;
for (var i=0; i < l; i+=2) {
code = parseInt(hex.substr(i, 2), 16);
// if (code !== 0) {
str += String.fromCharCode(code);
// }
}
return utf8.decode(str);
};