From 2cc5bb2cb51350a2a5f3851deca9f44d285a7e09 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Mon, 22 Jan 2024 17:28:25 -0600 Subject: [PATCH] Fixed issue so that the retrived data reconstructed string is restricted to 59 bytes: REMOVES GARBAGE --- script/DappIndexer.s.sol | 10 ++++++++-- test/DappIndexer.t.sol | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/script/DappIndexer.s.sol b/script/DappIndexer.s.sol index 48a662a..9a68f8a 100644 --- a/script/DappIndexer.s.sol +++ b/script/DappIndexer.s.sol @@ -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; } diff --git a/test/DappIndexer.t.sol b/test/DappIndexer.t.sol index 002aa79..8b604cf 100644 --- a/test/DappIndexer.t.sol +++ b/test/DappIndexer.t.sol @@ -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; }