|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import "./CborDecode.sol"; |
| 5 | +import "./CidCbor.sol"; |
| 6 | +import "./TreeNodeCbor.sol"; |
| 7 | +import "./RecordCbor.sol"; |
| 8 | + |
| 9 | +library TreeCbor { |
| 10 | + using CBORDecoder for bytes; |
| 11 | + |
| 12 | + uint8 private constant CID_V1 = 0x01; |
| 13 | + uint8 private constant MULTICODEC_DAG_CBOR = 0x71; |
| 14 | + uint8 private constant MULTIHASH_SHA_256 = 0x12; |
| 15 | + uint8 private constant MULTIHASH_SIZE_32 = 0x20; |
| 16 | + |
| 17 | + struct Tree { |
| 18 | + CidCbor.Cid root; |
| 19 | + TreeNodeCbor.TreeNode[] nodes; |
| 20 | + RecordCbor.Record[] records; |
| 21 | + CidCbor.Cid[] cids; |
| 22 | + bool[] nodeOrRecord; |
| 23 | + } |
| 24 | + |
| 25 | + function itemIsNode(bytes memory cborData) internal pure returns (bool) { |
| 26 | + (uint len, uint byteIdx) = cborData.readFixedMap(0); |
| 27 | + |
| 28 | + string memory itemName; |
| 29 | + (itemName, byteIdx) = cborData.readString(byteIdx); |
| 30 | + return len == 2; |
| 31 | + } |
| 32 | + |
| 33 | + function readTree(bytes[] memory cborData, CidCbor.Cid memory root) internal pure returns (Tree memory) { |
| 34 | + TreeNodeCbor.TreeNode[] memory nodes = new TreeNodeCbor.TreeNode[](cborData.length); |
| 35 | + RecordCbor.Record[] memory records = new RecordCbor.Record[](cborData.length); |
| 36 | + CidCbor.Cid[] memory cids = new CidCbor.Cid[](cborData.length); |
| 37 | + bool[] memory nodeOrRecord = new bool[](cborData.length); |
| 38 | + |
| 39 | + for (uint i = 0; i < cborData.length; i++) { |
| 40 | + cids[i] = CidCbor.Cid( |
| 41 | + bytes4(abi.encodePacked(CID_V1, MULTICODEC_DAG_CBOR, MULTIHASH_SHA_256, MULTIHASH_SIZE_32)), |
| 42 | + sha256(cborData[i]), |
| 43 | + false |
| 44 | + ); |
| 45 | + uint byteIdx; |
| 46 | + if (itemIsNode(cborData[i])) { |
| 47 | + (nodes[i], byteIdx) = TreeNodeCbor.readTreeNode(cborData[i], 0); |
| 48 | + nodeOrRecord[i] = true; |
| 49 | + } else { |
| 50 | + (records[i], byteIdx) = RecordCbor.readRecord(cborData[i], 0); |
| 51 | + nodeOrRecord[i] = false; |
| 52 | + } |
| 53 | + require(byteIdx == cborData[i].length, "expected to read all bytes"); |
| 54 | + } |
| 55 | + |
| 56 | + return requireUniqueCidsAndRoot(Tree(root, nodes, records, cids, nodeOrRecord)); |
| 57 | + } |
| 58 | + |
| 59 | + function nodeByCid(Tree memory tree, CidCbor.Cid memory indexCid) |
| 60 | + internal |
| 61 | + pure |
| 62 | + returns (TreeNodeCbor.TreeNode memory, uint index) |
| 63 | + { |
| 64 | + return nodeByCid(tree, indexCid, 0); |
| 65 | + } |
| 66 | + |
| 67 | + function nodeByCid(Tree memory tree, CidCbor.Cid memory indexCid, uint startIdx) |
| 68 | + internal |
| 69 | + pure |
| 70 | + returns (TreeNodeCbor.TreeNode memory, uint index) |
| 71 | + { |
| 72 | + for (uint i = startIdx; i < tree.cids.length; i++) { |
| 73 | + if (CidCbor.cidMatches(tree.cids[i], indexCid)) { |
| 74 | + require(tree.nodeOrRecord[i], "expected node"); |
| 75 | + return (tree.nodes[i], i); |
| 76 | + } |
| 77 | + } |
| 78 | + revert("node not found"); |
| 79 | + } |
| 80 | + |
| 81 | + function recordByCid(Tree memory tree, CidCbor.Cid memory indexCid) |
| 82 | + internal |
| 83 | + pure |
| 84 | + returns (RecordCbor.Record memory, uint index) |
| 85 | + { |
| 86 | + return recordByCid(tree, indexCid, 0); |
| 87 | + } |
| 88 | + |
| 89 | + function recordByCid(Tree memory tree, CidCbor.Cid memory indexCid, uint startIdx) |
| 90 | + internal |
| 91 | + pure |
| 92 | + returns (RecordCbor.Record memory, uint index) |
| 93 | + { |
| 94 | + for (uint i = startIdx; i < tree.cids.length; i++) { |
| 95 | + if (CidCbor.cidMatches(tree.cids[i], indexCid)) { |
| 96 | + require(!tree.nodeOrRecord[i], "expected record"); |
| 97 | + return (tree.records[i], i); |
| 98 | + } |
| 99 | + } |
| 100 | + revert("record not found"); |
| 101 | + } |
| 102 | + |
| 103 | + function requireUniqueCidsAndRoot(Tree memory tree) internal pure returns (Tree memory) { |
| 104 | + bool rootIncluded; |
| 105 | + for (uint i = 0; i < tree.cids.length; i++) { |
| 106 | + if (!rootIncluded) { |
| 107 | + rootIncluded = CidCbor.cidMatches(tree.cids[i], tree.root); |
| 108 | + } |
| 109 | + for (uint j = i + 1; j < tree.nodes.length; j++) { |
| 110 | + require(!CidCbor.cidMatches(tree.cids[i], tree.cids[j]), "node cids must be unique"); |
| 111 | + } |
| 112 | + } |
| 113 | + require(rootIncluded, "root cid must be included"); |
| 114 | + return tree; |
| 115 | + } |
| 116 | +} |
0 commit comments