Coverage Report

Created: 2026-09-14 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
65.6k
{
33
65.6k
  bytes encoded;
34
126k
  while (_n > 0x7f)
35
60.6k
  {
36
60.6k
    encoded.emplace_back(uint8_t(0x80 | (_n & 0x7f)));
37
60.6k
    _n >>= 7;
38
60.6k
  }
39
65.6k
  encoded.emplace_back(_n);
40
65.6k
  return encoded;
41
65.6k
}
42
43
bytes encodeByteArray(bytes const& _data)
44
32.7k
{
45
32.7k
  return bytes{0x0a} + varintEncoding(_data.size()) + _data;
46
32.7k
}
47
48
bytes encodeHash(bytes const& _data)
49
32.7k
{
50
32.7k
  return bytes{0x12, 0x20} + picosha2::hash256(_data);
51
32.7k
}
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
14.9k
{
60
14.9k
  static std::string const alphabet{"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"};
61
14.9k
  bigint data(util::toHex(_data, HexPrefix::Add));
62
14.9k
  std::string output;
63
702k
  while (data)
64
687k
  {
65
687k
    output += alphabet[static_cast<size_t>(data % alphabet.size())];
66
687k
    data /= alphabet.size();
67
687k
  }
68
14.9k
  reverse(output.begin(), output.end());
69
14.9k
  return output;
70
14.9k
}
71
72
struct Chunk
73
{
74
1
  Chunk() = default;
75
  Chunk(bytes _hash, size_t _size, size_t _blockSize):
76
32.7k
    hash(std::move(_hash)),
77
32.7k
    size(_size),
78
32.7k
    blockSize(_blockSize)
79
32.7k
  {}
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
32.7k
{
146
  // when we reach root it will be the only node in that level
147
32.7k
  while (_currentLevel.size() != 1)
148
1
    _currentLevel = buildNextLevel(_currentLevel);
149
150
  // top level's only node stores the hash for file
151
32.7k
  return _currentLevel.front().hash;
152
32.7k
}
153
}
154
155
bytes solidity::util::ipfsHash(std::string _data)
156
32.7k
{
157
32.7k
  size_t const maxChunkSize = 1024 * 256;
158
32.7k
  size_t chunkCount = _data.length() / maxChunkSize + (_data.length() % maxChunkSize > 0 ? 1 : 0);
159
32.7k
  chunkCount = chunkCount == 0 ? 1 : chunkCount;
160
161
32.7k
  Chunks allChunks;
162
163
65.5k
  for (size_t chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++)
164
32.7k
  {
165
32.7k
    bytes chunkBytes = asBytes(
166
32.7k
      _data.substr(chunkIndex * maxChunkSize, std::min(maxChunkSize, _data.length() - chunkIndex * maxChunkSize))
167
32.7k
    );
168
169
32.7k
    bytes lengthAsVarint = varintEncoding(chunkBytes.size());
170
171
32.7k
    bytes protobufEncodedData;
172
    // Type: File
173
32.7k
    protobufEncodedData += bytes{0x08, 0x02};
174
32.7k
    if (!chunkBytes.empty())
175
32.7k
    {
176
      // Data (length delimited bytes)
177
32.7k
      protobufEncodedData += bytes{0x12};
178
32.7k
      protobufEncodedData += lengthAsVarint;
179
32.7k
      protobufEncodedData += chunkBytes;
180
32.7k
    }
181
    // filesize: length as varint
182
32.7k
    protobufEncodedData += bytes{0x18} + lengthAsVarint;
183
184
    // PBDag:
185
    // Data: (length delimited bytes)
186
32.7k
    bytes blockData = encodeByteArray(protobufEncodedData);
187
188
    // Multihash: sha2-256, 256 bits
189
32.7k
    allChunks.emplace_back(
190
32.7k
      encodeHash(blockData),
191
32.7k
      chunkBytes.size(),
192
32.7k
      blockData.size()
193
32.7k
    );
194
32.7k
  }
195
196
32.7k
  return groupChunksBottomUp(std::move(allChunks));
197
32.7k
}
198
199
std::string solidity::util::ipfsHashBase58(std::string _data)
200
14.9k
{
201
14.9k
  return base58Encode(ipfsHash(std::move(_data)));
202
14.9k
}