Fixed issue so that the retrived data reconstructed string is restricted to 59 bytes: REMOVES GARBAGE

This commit is contained in:
David E. Perez Negron R. 2024-01-22 17:28:25 -06:00
parent bd03f224ef
commit 2cc5bb2cb5
2 changed files with 20 additions and 8 deletions

View File

@ -51,8 +51,14 @@ contract DappIndexerScript is Script {
// Concatenate the two bytes32 variables
bytes memory concatenatedBytes = abi.encodePacked(part1, part2);
// Convert the concatenated bytes to a string
string memory str = string(concatenatedBytes);
// Truncate the concatenated bytes to 59 bytes
bytes memory truncatedBytes = new bytes(59);
for (uint i = 0; i < 59; i++) {
truncatedBytes[i] = concatenatedBytes[i];
}
// Convert the truncated bytes to a string
string memory str = string(truncatedBytes);
return str;
}

View File

@ -30,16 +30,22 @@ contract DappIndexerTest is Test {
DappIndexer.PackedCID memory dapp1 = dappIdxr.getCID(bytes32("Dapp01"));
assertEq(CID1, dapp1.CID1);
assertEq(CID2, dapp1.CID2);
//string memory retrivedDapp = Bytes32PairToString(dapp1.CID1, dapp1.CID2);
//assertEq(dappsURI[0], retrivedDapp[0:59]);
}
string memory retrivedDappURI = Bytes32PairToString(dapp1.CID1, dapp1.CID2);
assertEq(dappsURI[0], retrivedDappURI);
}
function Bytes32PairToString(bytes32 part1, bytes32 part2) public pure returns (string memory) {
// Concatenate the two bytes32 variables
bytes memory concatenatedBytes = abi.encodePacked(part1, part2);
// Convert the concatenated bytes to a string
string memory str = string(concatenatedBytes);
// Truncate the concatenated bytes to 59 bytes
bytes memory truncatedBytes = new bytes(59);
for (uint i = 0; i < 59; i++) {
truncatedBytes[i] = concatenatedBytes[i];
}
// Convert the truncated bytes to a string
string memory str = string(truncatedBytes);
return str;
}