From 33dc31e9af528ceee4672cfc1699819e2e17c171 Mon Sep 17 00:00:00 2001 From: steven nevins Date: Wed, 29 Nov 2023 20:15:38 -0500 Subject: [PATCH] fix: hashed names --- src/FileSystem.sol | 13 +++++-------- src/interfaces/IFileSystem.sol | 6 +++--- test/FileSystem/CreateDirectory.sol | 4 ++-- test/FileSystem/ReadDirectory.t.sol | 4 ++-- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/FileSystem.sol b/src/FileSystem.sol index bc8620a..ac2d4bd 100644 --- a/src/FileSystem.sol +++ b/src/FileSystem.sol @@ -51,14 +51,12 @@ contract FileSystem is IFileSystem { bytes32[] calldata _inodeChecksums ) external returns (bytes32 directoryChecksum) { if (_fileNames.length != _inodeChecksums.length) revert LengthMismatch(); - bytes32[] memory hashedFiles = hashFiles(_fileNames, _inodeChecksums); + bytes memory concatenatedFiles = concatenateFiles(_fileNames, _inodeChecksums); for (uint256 i; i < _inodeChecksums.length; i++) { if (!inodeExists(_inodeChecksums[i])) revert InodeNotFound(); } - directoryChecksum = keccak256( - bytes.concat(bytes1(uint8(InodeType.Directory)), keccak256(abi.encodePacked(hashedFiles))) - ); + directoryChecksum = keccak256(bytes.concat(bytes1(uint8(InodeType.Directory)), keccak256(concatenatedFiles))); if (inodeExists(directoryChecksum)) return directoryChecksum; inodes[directoryChecksum].directory = Directory(_fileNames, _inodeChecksums); emit DirectoryCreated(directoryChecksum, _fileNames, _inodeChecksums); @@ -127,18 +125,17 @@ contract FileSystem is IFileSystem { /** * @inheritdoc IFileSystem */ - function hashFiles( + function concatenateFiles( string[] calldata _fileNames, bytes32[] calldata _filePointers - ) public pure returns (bytes32[] memory hashedFiles) { + ) public pure returns (bytes memory concatenatedFiles) { uint256 length = _fileNames.length; - hashedFiles = new bytes32[](length); bytes memory filename; for (uint256 i; i < length; i++) { filename = bytes(_fileNames[i]); if (filename.length == 0) revert InvalidFileName(); if (_containsForbiddenChars(filename)) revert InvalidCharacter(); - hashedFiles[i] = keccak256(bytes.concat(keccak256(filename), keccak256(bytes.concat(_filePointers[i])))); + concatenatedFiles = abi.encodePacked(concatenatedFiles, keccak256(filename), _filePointers[i]); } } diff --git a/src/interfaces/IFileSystem.sol b/src/interfaces/IFileSystem.sol index 95ef709..5dac931 100644 --- a/src/interfaces/IFileSystem.sol +++ b/src/interfaces/IFileSystem.sol @@ -101,12 +101,12 @@ interface IFileSystem { * @notice Hashes a list of file names in the directory * @param _fileNames List of file names * @param _inodeChecksums List of checksums for the inodes - * @return Hashed names + * @return The concatenated files */ - function hashFiles( + function concatenateFiles( string[] calldata _fileNames, bytes32[] calldata _inodeChecksums - ) external view returns (bytes32[] memory); + ) external view returns (bytes memory); /** * @notice Mapping of checksum pointer to Inode struct diff --git a/test/FileSystem/CreateDirectory.sol b/test/FileSystem/CreateDirectory.sol index e3fb23d..6110dd5 100644 --- a/test/FileSystem/CreateDirectory.sol +++ b/test/FileSystem/CreateDirectory.sol @@ -7,7 +7,7 @@ contract CreateDirectory is FileSystemTest { bytes internal metadata; bytes internal fileContent; bytes32[] internal chunkChecksums; - bytes32[] internal hashedFiles; + bytes internal hashedFiles; bytes32 internal fileChecksum; function setUp() public override { @@ -23,7 +23,7 @@ contract CreateDirectory is FileSystemTest { function test_CreateDirectory() public { bytes32 checksum = fileSystem.createDirectory(fileNames, filePointers); - hashedFiles = fileSystem.hashFiles(fileNames, filePointers); + hashedFiles = fileSystem.concatenateFiles(fileNames, filePointers); checksum = keccak256( abi.encodePacked(bytes1(uint8(InodeType.Directory)), keccak256(abi.encodePacked(hashedFiles))) ); diff --git a/test/FileSystem/ReadDirectory.t.sol b/test/FileSystem/ReadDirectory.t.sol index a912866..d208a91 100644 --- a/test/FileSystem/ReadDirectory.t.sol +++ b/test/FileSystem/ReadDirectory.t.sol @@ -4,7 +4,7 @@ pragma solidity 0.8.23; import "test/FileSystem/FileSystemTest.t.sol"; contract ReadDirectory is FileSystemTest { - bytes32[] internal hashedFiles; + bytes internal hashedFiles; bytes internal metadata; bytes internal fileContent; bytes32[] internal chunkChecksums; @@ -24,7 +24,7 @@ contract ReadDirectory is FileSystemTest { function test_ReadDirectory() public { fileSystem.createDirectory(fileNames, filePointers); - hashedFiles = fileSystem.hashFiles(fileNames, filePointers); + hashedFiles = fileSystem.concatenateFiles(fileNames, filePointers); checksum = keccak256( abi.encodePacked(bytes1(uint8(InodeType.Directory)), keccak256(abi.encodePacked(hashedFiles))) );