Coverage Report

Created: 2022-08-24 06:52

/src/solidity/libsolutil/IpfsHash.cpp
Line
Count
Source (jump to first uncovered line)
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 std;
27
using namespace solidity;
28
using namespace solidity::util;
29
30
namespace
31
{
32
bytes varintEncoding(size_t _n)
33
14.5k
{
34
14.5k
  bytes encoded;
35
31.8k
  while (_n > 0x7f)
36
17.2k
  {
37
17.2k
    encoded.emplace_back(uint8_t(0x80 | (_n & 0x7f)));
38
17.2k
    _n >>= 7;
39
17.2k
  }
40
14.5k
  encoded.emplace_back(_n);
41
14.5k
  return encoded;
42
14.5k
}
43
44
bytes encodeByteArray(bytes const& _data)
45
7.23k
{
46
7.23k
  return bytes{0x0a} + varintEncoding(_data.size()) + _data;
47
7.23k
}
48
49
bytes encodeHash(bytes const& _data)
50
7.23k
{
51
7.23k
  return bytes{0x12, 0x20} + picosha2::hash256(_data);
52
7.23k
}
53
54
bytes encodeLinkData(bytes const& _data)
55
18
{
56
18
  return bytes{0x12} + varintEncoding(_data.size()) + _data;
57
18
}
58
59
string base58Encode(bytes const& _data)
60
3.60k
{
61
3.60k
  static string const alphabet{"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"};
62
3.60k
  bigint data(util::toHex(_data, HexPrefix::Add));
63
3.60k
  string output;
64
169k
  while (data)
65
166k
  {
66
166k
    output += alphabet[static_cast<size_t>(data % alphabet.size())];
67
166k
    data /= alphabet.size();
68
166k
  }
69
3.60k
  reverse(output.begin(), output.end());
70
3.60k
  return output;
71
3.60k
}
72
73
struct Chunk
74
{
75
6
  Chunk() = default;
76
  Chunk(bytes _hash, size_t _size, size_t _blockSize):
77
    hash(std::move(_hash)),
78
    size(_size),
79
    blockSize(_blockSize)
80
7.24k
  {}
81
82
  bytes hash = {};
83
  size_t size = 0;
84
  size_t blockSize = 0;
85
};
86
87
using Chunks = vector<Chunk>;
88
89
Chunk combineLinks(Chunks& _links)
90
6
{
91
6
  bytes data = {};
92
6
  bytes lengths = {};
93
6
  Chunk chunk = {};
94
6
  for (Chunk& link: _links)
95
18
  {
96
18
    chunk.size += link.size;
97
18
    chunk.blockSize += link.blockSize;
98
99
18
    data += encodeLinkData(
100
18
      bytes {0x0a} +
101
18
      varintEncoding(link.hash.size()) +
102
18
      std::move(link.hash) +
103
18
      bytes{0x12, 0x00, 0x18} +
104
18
      varintEncoding(link.blockSize)
105
18
    );
106
107
18
    lengths += bytes{0x20} + varintEncoding(link.size);
108
18
  }
109
110
6
  bytes blockData = data + encodeByteArray(bytes{0x08, 0x02, 0x18} + varintEncoding(chunk.size) + lengths);
111
112
6
  chunk.blockSize += blockData.size();
113
6
  chunk.hash = encodeHash(blockData);
114
115
6
  return chunk;
116
6
}
117
118
Chunks buildNextLevel(Chunks& _currentLevel)
119
6
{
120
6
  size_t const maxChildNum = 174;
121
122
6
  Chunks nextLevel;
123
6
  Chunks links;
124
125
6
  for (Chunk& chunk: _currentLevel)
126
18
  {
127
18
    links.emplace_back(std::move(chunk.hash), chunk.size, chunk.blockSize);
128
18
    if (links.size() == maxChildNum)
129
0
    {
130
0
      nextLevel.emplace_back(combineLinks(links));
131
0
      links = {};
132
0
    }
133
18
  }
134
6
  if (!links.empty())
135
6
    nextLevel.emplace_back(combineLinks(links));
136
137
6
  return nextLevel;
138
6
}
139
140
/// Builds a tree starting from the bottom level where nodes are data nodes.
141
/// Data nodes should be calculated and passed as the only level in chunk levels
142
/// Each next level is calculated as following:
143
///   - Pick up to maxChildNum (174) nodes until a whole level is added, group them and pass to the node in the next level
144
///   - Do this until the current level has only one node, return the hash in that node
145
bytes groupChunksBottomUp(Chunks _currentLevel)
146
7.21k
{
147
  // when we reach root it will be the only node in that level
148
7.22k
  while (_currentLevel.size() != 1)
149
6
    _currentLevel = buildNextLevel(_currentLevel);
150
151
  // top level's only node stores the hash for file
152
7.21k
  return _currentLevel.front().hash;
153
7.21k
}
154
}
155
156
bytes solidity::util::ipfsHash(string _data)
157
7.21k
{
158
7.21k
  size_t const maxChunkSize = 1024 * 256;
159
7.21k
  size_t chunkCount = _data.length() / maxChunkSize + (_data.length() % maxChunkSize > 0 ? 1 : 0);
160
7.21k
  chunkCount = chunkCount == 0 ? 1 : chunkCount;
161
162
7.21k
  Chunks allChunks;
163
164
14.4k
  for (size_t chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++)
165
7.23k
  {
166
7.23k
    bytes chunkBytes = asBytes(
167
7.23k
      _data.substr(chunkIndex * maxChunkSize, min(maxChunkSize, _data.length() - chunkIndex * maxChunkSize))
168
7.23k
    );
169
170
7.23k
    bytes lengthAsVarint = varintEncoding(chunkBytes.size());
171
172
7.23k
    bytes protobufEncodedData;
173
    // Type: File
174
7.23k
    protobufEncodedData += bytes{0x08, 0x02};
175
7.23k
    if (!chunkBytes.empty())
176
7.23k
    {
177
      // Data (length delimited bytes)
178
7.23k
      protobufEncodedData += bytes{0x12};
179
7.23k
      protobufEncodedData += lengthAsVarint;
180
7.23k
      protobufEncodedData += chunkBytes;
181
7.23k
    }
182
    // filesize: length as varint
183
7.23k
    protobufEncodedData += bytes{0x18} + lengthAsVarint;
184
185
    // PBDag:
186
    // Data: (length delimited bytes)
187
7.23k
    bytes blockData = encodeByteArray(protobufEncodedData);
188
189
    // Multihash: sha2-256, 256 bits
190
7.23k
    allChunks.emplace_back(
191
7.23k
      encodeHash(blockData),
192
7.23k
      chunkBytes.size(),
193
7.23k
      blockData.size()
194
7.23k
    );
195
7.23k
  }
196
197
7.21k
  return groupChunksBottomUp(std::move(allChunks));
198
7.21k
}
199
200
string solidity::util::ipfsHashBase58(string _data)
201
3.60k
{
202
3.60k
  return base58Encode(ipfsHash(std::move(_data)));
203
3.60k
}