| 153 | } |
| 154 | |
| 155 | bytes solidity::util::ipfsHash(std::string _data) |
| 156 | { |
| 157 | size_t const maxChunkSize = 1024 * 256; |
| 158 | size_t chunkCount = _data.length() / maxChunkSize + (_data.length() % maxChunkSize > 0 ? 1 : 0); |
| 159 | chunkCount = chunkCount == 0 ? 1 : chunkCount; |
| 160 | |
| 161 | Chunks allChunks; |
| 162 | |
| 163 | for (size_t chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++) |
| 164 | { |
| 165 | bytes chunkBytes = asBytes( |
| 166 | _data.substr(chunkIndex * maxChunkSize, std::min(maxChunkSize, _data.length() - chunkIndex * maxChunkSize)) |
| 167 | ); |
| 168 | |
| 169 | bytes lengthAsVarint = varintEncoding(chunkBytes.size()); |
| 170 | |
| 171 | bytes protobufEncodedData; |
| 172 | // Type: File |
| 173 | protobufEncodedData += bytes{0x08, 0x02}; |
| 174 | if (!chunkBytes.empty()) |
| 175 | { |
| 176 | // Data (length delimited bytes) |
| 177 | protobufEncodedData += bytes{0x12}; |
| 178 | protobufEncodedData += lengthAsVarint; |
| 179 | protobufEncodedData += chunkBytes; |
| 180 | } |
| 181 | // filesize: length as varint |
| 182 | protobufEncodedData += bytes{0x18} + lengthAsVarint; |
| 183 | |
| 184 | // PBDag: |
| 185 | // Data: (length delimited bytes) |
| 186 | bytes blockData = encodeByteArray(protobufEncodedData); |
| 187 | |
| 188 | // Multihash: sha2-256, 256 bits |
| 189 | allChunks.emplace_back( |
| 190 | encodeHash(blockData), |
| 191 | chunkBytes.size(), |
| 192 | blockData.size() |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | return groupChunksBottomUp(std::move(allChunks)); |
| 197 | } |
| 198 | |
| 199 | std::string solidity::util::ipfsHashBase58(std::string _data) |
| 200 | { |
nothing calls this directly
no test coverage detected