Skip to content

Commit 77ca408

Browse files
Convert CID to string multibase representation (#51)
Signed-off-by: Sergey Kaprovich <[email protected]>
1 parent 76fa805 commit 77ca408

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

include/libp2p/multi/content_identifier.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <vector>
1010

1111
#include <boost/operators.hpp>
12+
#include <libp2p/multi/multibase_codec.hpp>
1213
#include <libp2p/multi/multicodec_type.hpp>
1314
#include <libp2p/multi/multihash.hpp>
1415

@@ -36,7 +37,7 @@ namespace libp2p::multi {
3637
* @param base is a human-readable multibase prefix
3738
* @returns human readable representation of the CID
3839
*/
39-
std::string toPrettyString(const std::string &base);
40+
std::string toPrettyString(const std::string &base) const;
4041

4142
bool operator==(const ContentIdentifier &c) const;
4243
bool operator<(const ContentIdentifier &c) const;

include/libp2p/multi/content_identifier_codec.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define LIBP2P_CONTENT_IDENTIFIER_CODEC_HPP
88

99
#include <libp2p/multi/content_identifier.hpp>
10+
#include <libp2p/multi/multibase_codec/codecs/base58.hpp>
1011

1112
namespace libp2p::multi {
1213

@@ -20,7 +21,8 @@ namespace libp2p::multi {
2021
enum class EncodeError {
2122
INVALID_CONTENT_TYPE = 1,
2223
INVALID_HASH_TYPE,
23-
INVALID_HASH_LENGTH
24+
INVALID_HASH_LENGTH,
25+
VERSION_UNSUPPORTED
2426
};
2527

2628
enum class DecodeError {
@@ -39,6 +41,18 @@ namespace libp2p::multi {
3941

4042
static outcome::result<ContentIdentifier> decode(
4143
gsl::span<const uint8_t> bytes);
44+
45+
/**
46+
* @brief Encode CID v0 to string representation
47+
* @param cid - input CID for encode
48+
* @param encoding - type of the encoding, ignored for CID v0 (always
49+
* Base58)
50+
* @todo Sergey Kaprovich: FIL-133 add support for CID v1
51+
* @return CID string
52+
*/
53+
static outcome::result<std::string> toString(
54+
const ContentIdentifier &cid,
55+
MultibaseCodec::Encoding encoding = MultibaseCodec::Encoding::BASE58);
4256
};
4357

4458
} // namespace libp2p::multi

src/multi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ target_link_libraries(p2p_cid
4242
p2p_multihash
4343
p2p_sha
4444
p2p_uvarint
45+
p2p_multibase_codec
4546
)

src/multi/content_identifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace libp2p::multi {
1717
content_type{content_type},
1818
content_address{std::move(content_address)} {}
1919

20-
std::string ContentIdentifier::toPrettyString(const std::string &base) {
20+
std::string ContentIdentifier::toPrettyString(const std::string &base) const {
2121
/// TODO(Harrm) FIL-14: hash type is a subset of multicodec type, better move them
2222
/// to one place
2323
std::string hash_type = MulticodecType::getName(

src/multi/content_identifier_codec.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ OUTCOME_CPP_DEFINE_CATEGORY(libp2p::multi, ContentIdentifierCodec::EncodeError,
2020
return "Hash length is invalid; Must be 32 bytes for sha256 in version 0";
2121
case E::INVALID_HASH_TYPE:
2222
return "Hash type is invalid; Must be sha256 in version 0";
23+
case E::VERSION_UNSUPPORTED:
24+
return "Content identifier version unsupported";
2325
}
2426
return "Unknown error";
2527
}
@@ -115,4 +117,19 @@ namespace libp2p::multi {
115117
}
116118
return DecodeError::RESERVED_VERSION;
117119
}
120+
121+
outcome::result<std::string> ContentIdentifierCodec::toString(
122+
const ContentIdentifier &cid, MultibaseCodec::Encoding encoding) {
123+
std::ignore = encoding;
124+
std::string result;
125+
OUTCOME_TRY(cid_bytes, encode(cid));
126+
switch (cid.version) {
127+
case ContentIdentifier::Version::V0:
128+
result = detail::encodeBase58(cid_bytes);
129+
break;
130+
default:
131+
return EncodeError::VERSION_UNSUPPORTED;
132+
}
133+
return result;
134+
}
118135
} // namespace libp2p::multi

test/libp2p/multi/cid_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ TEST(CidTest, PrettyString) {
4343
+ libp2p::common::hex_lower(EXAMPLE_MULTIHASH.getHash()));
4444
}
4545

46+
/**
47+
* @given CID with sample multihash and it's string representation from
48+
* reference implementation tests
49+
* @when Convert given CID to string
50+
* @then Generated and reference string representations must be equal
51+
*/
52+
TEST(CidTest, MultibaseStringSuccess) {
53+
const Multihash reference_multihash =
54+
"12209658BF8A26B986DEE4ACEB8227B6A74D638CE4CDB2D72CD19516A6F83F1BFDD3"_multihash;
55+
ContentIdentifier cid(ContentIdentifier::Version::V0, MulticodecType::DAG_PB,
56+
reference_multihash);
57+
EXPECT_OUTCOME_TRUE(cid_string, ContentIdentifierCodec::toString(cid))
58+
ASSERT_EQ(cid_string, "QmYTYMTdkVyB8we45bdXfZuDu5vCjRVX8QNTFLhC7K8C7t");
59+
}
60+
4661
/**
4762
* @given CID of different versions
4863
* @when compare CIDs

0 commit comments

Comments
 (0)