Verified contractExactMIT
ContentReader
Address0xe6d62620a284565b7f0b3581a6972ba3be5c8b13
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- Yes, 10,000 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,387,147
- Creation transaction
- 0xd8d875d59a…a17aa5f7a6
src/ContentReader.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {IDirectoryStore} from "@abxfs/core/interfaces/IDirectoryStore.sol";
import {IFileStore} from "@abxfs/core/interfaces/IFileStore.sol";
interface IContentReaderOrg {
function paramHooks() external view returns (address configure, address augment, address transfer);
}
interface IContentReaderHook {
struct Version {
bytes32 cid;
uint40 publishedAt;
address publisher;
}
function versionAt(address org, uint256 tokenId, uint256 version) external view returns (Version memory);
function getTag(address org, uint256 tokenId, string calldata tag) external view returns (uint256, bytes32);
function latestVersion(address org, uint256 tokenId) external view returns (uint256, bytes32);
}
/// @title ContentReader
/// @notice Native, EVM-readable ABX FS content. FastLZ storage is decoded by FileStore.readChunk;
/// file-level off-chain codecs are deliberately unsupported. No state, roles, or ETH.
/// @dev Limits bound decoded bytes and chunk reads, not gas: arbitrary storage backends may spend gas
/// while retrieving a chunk. Callers that must contain failures should also cap call gas.
contract ContentReader {
IDirectoryStore public immutable directoryStore;
IFileStore public immutable fileStore;
struct Range {
uint256 first;
uint256 last;
uint256 skip;
uint256 readBytes;
}
error StoreMismatch();
error RootNotFound(bytes32 cid);
error NotDirectory(bytes32 cid);
error IsDirectory(bytes32 cid);
error CodecNotSupported(bytes32 codec);
error ByteLimitExceeded(uint256 bytesRequired, uint256 maxBytes);
error ChunkLimitExceeded(uint256 chunksRequired, uint256 maxChunks);
error InvalidFileLength();
error RangeOutOfBounds(uint256 start, uint256 length, uint256 size);
error InvalidProjectSelector();
error ConfigureHookNotFound();
constructor(address directoryStore_, address fileStore_) {
directoryStore = IDirectoryStore(directoryStore_);
fileStore = IFileStore(fileStore_);
if (address(directoryStore.fileStore()) != fileStore_) revert StoreMismatch();
}
/// @notice Resolve an exact path. An empty path preserves a file or directory root.
/// @dev Uses DirectoryStore's path rules (no leading/trailing slash or empty segments; 32 segments maximum).
function resolve(bytes32 root, string memory path) public view returns (bytes32 cid, bool isDirectory) {
isDirectory = directoryStore.directoryExists(root);
if (!isDirectory && !fileStore.fileExists(root)) revert RootNotFound(root);
if (bytes(path).length == 0) return (root, isDirectory);
if (!isDirectory) revert NotDirectory(root);
return directoryStore.resolvePath(root, path);
}
/// @notice Read a whole file, refusing its recorded byte/chunk totals before allocating content.
function readFile(bytes32 cid, uint256 maxBytes, uint256 maxChunks)
public
view
returns (string memory mimeType, bytes memory data)
{
IFileStore.FileInfo memory info = fileStore.getFileInfo(cid);
_codec(info.codec);
_limits(info.size, info.chunkCount, maxBytes, maxChunks);
data = new bytes(info.size);
uint256 offset;
for (uint256 i; i < info.chunkCount; ++i) {
bytes memory chunk = fileStore.readChunk(cid, i);
if (chunk.length == 0 || chunk.length > info.size - offset) revert InvalidFileLength();
_copy(data, offset, chunk, 0, chunk.length);
offset += chunk.length;
}
if (offset != info.size) revert InvalidFileLength();
return (info.mimeType, data);
}
/// @notice Read a path, using index.html when the exact path resolves to a directory.
function readPath(bytes32 root, string memory path, uint256 maxBytes, uint256 maxChunks)
public
view
returns (string memory mimeType, bytes memory data)
{
(bytes32 cid, bool isDirectory) = resolve(root, path);
if (isDirectory) (cid, isDirectory) = directoryStore.lookup(cid, "index.html");
if (isDirectory) revert IsDirectory(cid);
return readFile(cid, maxBytes, maxChunks);
}
/// @notice Read [start, start + length). Bounds are strict; a zero length at EOF is valid.
/// @dev maxBytes covers the FULL decoded sizes of all touched chunks, including bytes outside the
/// returned range. locateByte finds both endpoints without reading or scanning preceding chunks.
function readFileRange(bytes32 cid, uint256 start, uint256 length, uint256 maxBytes, uint256 maxChunks)
external
view
returns (string memory mimeType, bytes memory data)
{
IFileStore.FileInfo memory info = fileStore.getFileInfo(cid);
_codec(info.codec);
if (start > info.size || length > info.size - start) revert RangeOutOfBounds(start, length, info.size);
if (length == 0) return (info.mimeType, new bytes(0));
// The output itself is bounded even if a noncanonical store reports inconsistent endpoints.
_limits(length, 0, maxBytes, maxChunks);
Range memory r = _rangeBounds(cid, start, length);
if (r.last >= info.chunkCount || r.readBytes < length) revert InvalidFileLength();
_limits(r.readBytes, r.last - r.first + 1, maxBytes, maxChunks);
data = _range(cid, length, r);
return (info.mimeType, data);
}
/// @notice Resolve an org project through that org's own configure hook, including older orgs.
/// @dev version=0 and an empty tag selects latest; otherwise use either a 1-based version or tag.
/// This is a content convenience, not an attestation that an arbitrary org is factory-registered.
function projectRoot(address org, uint256 tokenId, uint256 version, string memory tag)
public
view
returns (bytes32 cid)
{
if (version != 0 && bytes(tag).length != 0) revert InvalidProjectSelector();
(address configure,,) = IContentReaderOrg(org).paramHooks();
if (configure == address(0)) revert ConfigureHookNotFound();
IContentReaderHook hook = IContentReaderHook(configure);
if (version != 0) return hook.versionAt(org, tokenId, version).cid;
if (bytes(tag).length != 0) (, cid) = hook.getTag(org, tokenId, tag);
else (, cid) = hook.latestVersion(org, tokenId);
}
function readProject(
address org,
uint256 tokenId,
uint256 version,
string memory tag,
string memory path,
uint256 maxBytes,
uint256 maxChunks
) external view returns (string memory mimeType, bytes memory data) {
return readPath(projectRoot(org, tokenId, version, tag), path, maxBytes, maxChunks);
}
function _rangeBounds(bytes32 cid, uint256 start, uint256 length) private view returns (Range memory r) {
uint256 firstStart;
uint256 lastStart;
uint256 lastSize;
(r.first, firstStart,,) = fileStore.locateByte(cid, start);
(r.last, lastStart,, lastSize) = fileStore.locateByte(cid, start + length - 1);
if (r.last < r.first || firstStart > start || lastStart > start + length - 1) revert InvalidFileLength();
r.skip = start - firstStart;
r.readBytes = lastStart + lastSize - firstStart;
}
function _range(bytes32 cid, uint256 length, Range memory r) private view returns (bytes memory data) {
data = new bytes(length);
uint256 copied;
uint256 readBytes;
uint256 skip = r.skip;
for (uint256 i = r.first; i <= r.last; ++i) {
bytes memory chunk = fileStore.readChunk(cid, i);
if (chunk.length <= skip || chunk.length > r.readBytes - readBytes) revert InvalidFileLength();
readBytes += chunk.length;
uint256 take = chunk.length - skip;
if (take > length - copied) take = length - copied;
_copy(data, copied, chunk, skip, take);
copied += take;
skip = 0;
}
if (copied != length || readBytes != r.readBytes) revert InvalidFileLength();
}
function _codec(bytes32 codec) private pure {
if (codec != bytes32(0)) revert CodecNotSupported(codec);
}
function _limits(uint256 size, uint256 chunks, uint256 maxBytes, uint256 maxChunks) private pure {
if (size > maxBytes) revert ByteLimitExceeded(size, maxBytes);
if (chunks > maxChunks) revert ChunkLimitExceeded(chunks, maxChunks);
}
function _copy(bytes memory dest, uint256 destOffset, bytes memory src, uint256 srcOffset, uint256 length)
private
pure
{
assembly ("memory-safe") {
mcopy(add(add(dest, 32), destOffset), add(add(src, 32), srcOffset), length)
}
}
}
../contracts-core/src/interfaces/IChunkStore.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/// @title IChunkStore
/// @notice Content-addressed chunk records for ABX FS. A chunk's id binds the backend that holds it, the exact input
/// given to that backend, and the hash and size of the chunk's data (what the backend's `retrieve` returns,
/// or what a reader obtains by following the backend's retrieval scheme):
///
/// id = keccak256(0x30 ‖ backend:20 ‖ keccak256(input):32 ‖ dataHash:32 ‖ size:6)
///
/// For a readable backend the chain checks `dataHash` and `size` when the chunk is written and on every read.
/// For any other backend they are claims bound into the id: a false claim yields an id no honest writer
/// would compute, so it can never occupy anyone else's chunk. See `specs/abx-fs.md` §4.
interface IChunkStore {
error InvalidBackend(address backend);
error EmptyChunk();
error DataMismatch(bytes32 id);
error ChunkNotFound(bytes32 id);
/// @notice The backend's `retrieve` reverted, or its return data is not shaped like one ABI-encoded `bytes` of the
/// recorded size (an offset other than 0x20, or too short or too long for that size). Ask that backend's
/// `retrieval()` how to fetch the bytes instead. A well-shaped result whose length or hash disagrees with the
/// record is `DataMismatch`.
error RetrieveFailed(bytes32 id, address backend);
event ChunkWritten(
bytes32 indexed id, address indexed backend, bytes32 pointer, bytes32 inputHash, bytes32 dataHash, uint48 size
);
/// @notice Store `input` through `backend` and record the chunk. Returns the existing id without calling the
/// backend when the chunk is already recorded.
function write(address backend, bytes calldata input, bytes32 dataHash, uint48 size) external returns (bytes32 id);
/// @notice The chunk id for these parts (see the notice above).
function chunkId(address backend, bytes32 inputHash, bytes32 dataHash, uint48 size)
external
pure
returns (bytes32 id);
/// @notice The chunk's data, checked against its record. Reverts `RetrieveFailed` when the backend cannot serve it.
function read(bytes32 id) external view returns (bytes memory data);
function exists(bytes32 id) external view returns (bool);
/// @notice The recorded size of the chunk's data. Reverts `ChunkNotFound` if the id is not recorded.
function sizeOf(bytes32 id) external view returns (uint48 size);
/// @notice The whole record. Reverts `ChunkNotFound` if the id is not recorded.
function recordOf(bytes32 id) external view returns (address backend, uint48 size, bytes32 pointer, bytes32 dataHash);
}
../contracts-core/src/interfaces/IDirectoryStore.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {IFileStore} from "./IFileStore.sol";
/// @title IDirectoryStore
/// @notice Immutable, content-addressed ABX FS directories: a tree of entry nodes, at most three branch levels above
/// its leaves, sorted by name in unsigned byte order across the whole tree. Children are files of `fileStore()`
/// or directories of this store. A zero
/// root is the empty directory, which always exists. See `specs/abx-fs.md` §5.
interface IDirectoryStore {
struct Entry {
string name;
bytes32 cid;
bool isDirectory;
}
/// @notice Create (or return the existing) entry node: a leaf of name records or a branch.
function putEntryNode(bytes calldata blob) external returns (bytes32 nodeId);
/// @notice Bind a directory. `rootNode == 0` is the empty directory.
function createDirectory(bytes32 rootNode) external returns (bytes32 cid);
/// @notice `keccak256(0x32 ‖ rootNode)`.
function directoryCid(bytes32 rootNode) external pure returns (bytes32 cid);
function fileStore() external view returns (IFileStore);
function directoryExists(bytes32 cid) external view returns (bool);
function rootOf(bytes32 cid) external view returns (bytes32 root);
function entryCount(bytes32 cid) external view returns (uint256);
function getEntry(bytes32 cid, uint256 index) external view returns (Entry memory entry);
/// @notice Entries in `[start, end)`. Callers page.
function getEntries(bytes32 cid, uint256 start, uint256 end) external view returns (Entry[] memory entries);
function lookup(bytes32 dirCid, string calldata name) external view returns (bytes32 cid, bool isDirectory);
/// @notice Resolve a path of 1 to 32 `/`-separated segments from `rootCid`. More segments revert `PathTooDeep`: split a
/// deeper path and resolve each part from the directory the previous part ended at. An empty path or segment, a
/// leading or trailing `/`, a missing name or a segment under a file reverts `PathNotFound`.
function resolvePath(bytes32 rootCid, string calldata path) external view returns (bytes32 cid, bool isDirectory);
}
../contracts-core/src/interfaces/IFileStore.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {IChunkStore} from "./IChunkStore.sol";
/// @title IFileStore
/// @notice Immutable, content-addressed ABX FS files: a tree of chunk nodes (at most three branch levels above its
/// leaves) over `ChunkStore` chunks, a MIME type, and an optional off-chain codec. A file's data is its
/// chunks' data joined in order; with a codec, its content is `decode(codec, data)` and readers check it
/// against `outputHash`. A zero root is the empty file. See `specs/abx-fs.md` §5 and §6.
interface IFileStore {
struct FileInfo {
bytes32 root;
uint8 rootLevel;
uint256 chunkCount;
/// @dev Total bytes of the joined chunk data (before any file-level codec is decoded).
uint256 size;
string mimeType;
bytes32 codec;
bytes32 outputHash;
}
/// @notice Create (or return the existing) chunk node: a leaf of `chunkId:32 ‖ size:6` records or a branch.
function putChunkNode(bytes calldata blob) external returns (bytes32 nodeId);
/// @notice Bind a file. `mimeType` is 1..127 bytes. `codec` and `outputHash` are both zero or both set.
/// `rootNode == 0` is the empty file, which takes no codec.
function createFile(bytes32 rootNode, string calldata mimeType, bytes32 codec, bytes32 outputHash)
external
returns (bytes32 cid);
/// @notice `keccak256(0x31 ‖ rootNode ‖ keccak256(mimeType) ‖ codec ‖ outputHash)`.
function fileCid(bytes32 rootNode, string calldata mimeType, bytes32 codec, bytes32 outputHash)
external
pure
returns (bytes32 cid);
function chunkStore() external view returns (IChunkStore);
function fileExists(bytes32 cid) external view returns (bool);
function getFileInfo(bytes32 cid) external view returns (FileInfo memory info);
function chunkCount(bytes32 cid) external view returns (uint256);
function getChunkId(bytes32 cid, uint256 index) external view returns (bytes32 id);
/// @notice Chunk ids in `[start, end)`. Callers page.
function getChunkIds(bytes32 cid, uint256 start, uint256 end) external view returns (bytes32[] memory ids);
/// @notice The chunk holding byte `offset` of the file's data: its index, the offset of its first byte, its id
/// and its size.
function locateByte(bytes32 cid, uint256 offset)
external
view
returns (uint256 index, uint256 chunkStart, bytes32 chunkId, uint256 chunkSize);
/// @notice One chunk's data, through `ChunkStore.read`.
function readChunk(bytes32 cid, uint256 index) external view returns (bytes memory data);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 10000,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/",
"solady/=../contracts-core/lib/solady/src/",
"@abxfs/core/=../contracts-core/src/",
"@abxfs/protobuf/=../protobuf-sol/src/",
"@abxfs/core-script/=../contracts-core/script/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.