/src/solidity/libsolutil/IpfsHash.cpp
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #include <libsolutil/IpfsHash.h> |
20 | | |
21 | | #include <libsolutil/Exceptions.h> |
22 | | #include <libsolutil/picosha2.h> |
23 | | #include <libsolutil/CommonData.h> |
24 | | #include <libsolutil/Numeric.h> |
25 | | |
26 | | using namespace solidity; |
27 | | using namespace solidity::util; |
28 | | |
29 | | namespace |
30 | | { |
31 | | bytes varintEncoding(size_t _n) |
32 | 52.6k | { |
33 | 52.6k | bytes encoded; |
34 | 101k | while (_n > 0x7f) |
35 | 48.5k | { |
36 | 48.5k | encoded.emplace_back(uint8_t(0x80 | (_n & 0x7f))); |
37 | 48.5k | _n >>= 7; |
38 | 48.5k | } |
39 | 52.6k | encoded.emplace_back(_n); |
40 | 52.6k | return encoded; |
41 | 52.6k | } |
42 | | |
43 | | bytes encodeByteArray(bytes const& _data) |
44 | 26.3k | { |
45 | 26.3k | return bytes{0x0a} + varintEncoding(_data.size()) + _data; |
46 | 26.3k | } |
47 | | |
48 | | bytes encodeHash(bytes const& _data) |
49 | 26.3k | { |
50 | 26.3k | return bytes{0x12, 0x20} + picosha2::hash256(_data); |
51 | 26.3k | } |
52 | | |
53 | | bytes encodeLinkData(bytes const& _data) |
54 | 8 | { |
55 | 8 | return bytes{0x12} + varintEncoding(_data.size()) + _data; |
56 | 8 | } |
57 | | |
58 | | std::string base58Encode(bytes const& _data) |
59 | 12.0k | { |
60 | 12.0k | static std::string const alphabet{"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}; |
61 | 12.0k | bigint data(util::toHex(_data, HexPrefix::Add)); |
62 | 12.0k | std::string output; |
63 | 568k | while (data) |
64 | 556k | { |
65 | 556k | output += alphabet[static_cast<size_t>(data % alphabet.size())]; |
66 | 556k | data /= alphabet.size(); |
67 | 556k | } |
68 | 12.0k | reverse(output.begin(), output.end()); |
69 | 12.0k | return output; |
70 | 12.0k | } |
71 | | |
72 | | struct Chunk |
73 | | { |
74 | 1 | Chunk() = default; |
75 | | Chunk(bytes _hash, size_t _size, size_t _blockSize): |
76 | 26.3k | hash(std::move(_hash)), |
77 | 26.3k | size(_size), |
78 | 26.3k | blockSize(_blockSize) |
79 | 26.3k | {} |
80 | | |
81 | | bytes hash = {}; |
82 | | size_t size = 0; |
83 | | size_t blockSize = 0; |
84 | | }; |
85 | | |
86 | | using Chunks = std::vector<Chunk>; |
87 | | |
88 | | Chunk combineLinks(Chunks& _links) |
89 | 1 | { |
90 | 1 | bytes data = {}; |
91 | 1 | bytes lengths = {}; |
92 | 1 | Chunk chunk = {}; |
93 | 1 | for (Chunk& link: _links) |
94 | 8 | { |
95 | 8 | chunk.size += link.size; |
96 | 8 | chunk.blockSize += link.blockSize; |
97 | | |
98 | 8 | data += encodeLinkData( |
99 | 8 | bytes {0x0a} + |
100 | 8 | varintEncoding(link.hash.size()) + |
101 | 8 | std::move(link.hash) + |
102 | 8 | bytes{0x12, 0x00, 0x18} + |
103 | 8 | varintEncoding(link.blockSize) |
104 | 8 | ); |
105 | | |
106 | 8 | lengths += bytes{0x20} + varintEncoding(link.size); |
107 | 8 | } |
108 | | |
109 | 1 | bytes blockData = data + encodeByteArray(bytes{0x08, 0x02, 0x18} + varintEncoding(chunk.size) + lengths); |
110 | | |
111 | 1 | chunk.blockSize += blockData.size(); |
112 | 1 | chunk.hash = encodeHash(blockData); |
113 | | |
114 | 1 | return chunk; |
115 | 1 | } |
116 | | |
117 | | Chunks buildNextLevel(Chunks& _currentLevel) |
118 | 1 | { |
119 | 1 | size_t const maxChildNum = 174; |
120 | | |
121 | 1 | Chunks nextLevel; |
122 | 1 | Chunks links; |
123 | | |
124 | 1 | for (Chunk& chunk: _currentLevel) |
125 | 8 | { |
126 | 8 | links.emplace_back(std::move(chunk.hash), chunk.size, chunk.blockSize); |
127 | 8 | if (links.size() == maxChildNum) |
128 | 0 | { |
129 | 0 | nextLevel.emplace_back(combineLinks(links)); |
130 | 0 | links = {}; |
131 | 0 | } |
132 | 8 | } |
133 | 1 | if (!links.empty()) |
134 | 1 | nextLevel.emplace_back(combineLinks(links)); |
135 | | |
136 | 1 | return nextLevel; |
137 | 1 | } |
138 | | |
139 | | /// Builds a tree starting from the bottom level where nodes are data nodes. |
140 | | /// Data nodes should be calculated and passed as the only level in chunk levels |
141 | | /// Each next level is calculated as following: |
142 | | /// - Pick up to maxChildNum (174) nodes until a whole level is added, group them and pass to the node in the next level |
143 | | /// - Do this until the current level has only one node, return the hash in that node |
144 | | bytes groupChunksBottomUp(Chunks _currentLevel) |
145 | 26.2k | { |
146 | | // when we reach root it will be the only node in that level |
147 | 26.2k | while (_currentLevel.size() != 1) |
148 | 1 | _currentLevel = buildNextLevel(_currentLevel); |
149 | | |
150 | | // top level's only node stores the hash for file |
151 | 26.2k | return _currentLevel.front().hash; |
152 | 26.2k | } |
153 | | } |
154 | | |
155 | | bytes solidity::util::ipfsHash(std::string _data) |
156 | 26.2k | { |
157 | 26.2k | size_t const maxChunkSize = 1024 * 256; |
158 | 26.2k | size_t chunkCount = _data.length() / maxChunkSize + (_data.length() % maxChunkSize > 0 ? 1 : 0); |
159 | 26.2k | chunkCount = chunkCount == 0 ? 1 : chunkCount; |
160 | | |
161 | 26.2k | Chunks allChunks; |
162 | | |
163 | 52.5k | for (size_t chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++) |
164 | 26.3k | { |
165 | 26.3k | bytes chunkBytes = asBytes( |
166 | 26.3k | _data.substr(chunkIndex * maxChunkSize, std::min(maxChunkSize, _data.length() - chunkIndex * maxChunkSize)) |
167 | 26.3k | ); |
168 | | |
169 | 26.3k | bytes lengthAsVarint = varintEncoding(chunkBytes.size()); |
170 | | |
171 | 26.3k | bytes protobufEncodedData; |
172 | | // Type: File |
173 | 26.3k | protobufEncodedData += bytes{0x08, 0x02}; |
174 | 26.3k | if (!chunkBytes.empty()) |
175 | 26.3k | { |
176 | | // Data (length delimited bytes) |
177 | 26.3k | protobufEncodedData += bytes{0x12}; |
178 | 26.3k | protobufEncodedData += lengthAsVarint; |
179 | 26.3k | protobufEncodedData += chunkBytes; |
180 | 26.3k | } |
181 | | // filesize: length as varint |
182 | 26.3k | protobufEncodedData += bytes{0x18} + lengthAsVarint; |
183 | | |
184 | | // PBDag: |
185 | | // Data: (length delimited bytes) |
186 | 26.3k | bytes blockData = encodeByteArray(protobufEncodedData); |
187 | | |
188 | | // Multihash: sha2-256, 256 bits |
189 | 26.3k | allChunks.emplace_back( |
190 | 26.3k | encodeHash(blockData), |
191 | 26.3k | chunkBytes.size(), |
192 | 26.3k | blockData.size() |
193 | 26.3k | ); |
194 | 26.3k | } |
195 | | |
196 | 26.2k | return groupChunksBottomUp(std::move(allChunks)); |
197 | 26.2k | } |
198 | | |
199 | | std::string solidity::util::ipfsHashBase58(std::string _data) |
200 | 12.0k | { |
201 | 12.0k | return base58Encode(ipfsHash(std::move(_data))); |
202 | 12.0k | } |