Skip to content

Commit de5fdd2

Browse files
committed
Address catenocrypt's review
1 parent ef9e59e commit de5fdd2

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

src/Filecoin/Address.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ bool Address::isValid(const Data& data) {
1919
if (data.size() < 2) {
2020
return false;
2121
}
22-
if (!getType(data[0])) {
22+
Type type = getType(data[0]);
23+
if (type == Type::Invalid) {
2324
return false;
24-
}
25-
Type type = *getType(data[0]);
26-
if (type == Type::ID) {
25+
} else if (type == Type::ID) {
2726
// Verify varuint encoding
28-
if (data.size() > 11)
27+
if (data.size() > 11) {
2928
return false;
30-
if (data.size() == 11 && data[10] > 0x01)
29+
}
30+
if (data.size() == 11 && data[10] > 0x01) {
3131
return false;
32+
}
3233
int i;
33-
for (i = 1; i < data.size(); i++)
34-
if ((data[i] & 0x80) == 0)
34+
for (i = 1; i < data.size(); i++) {
35+
if ((data[i] & 0x80) == 0) {
3536
break;
37+
}
38+
}
3639
return i == data.size() - 1;
3740
} else {
38-
return data.size() == 1 + Address::payloadSize(type);
41+
return data.size() == (1 + Address::payloadSize(type));
3942
}
4043
}
4144

@@ -49,7 +52,7 @@ static bool isValidID(const std::string& string) {
4952
}
5053
try {
5154
size_t chars;
52-
std::stoull(string.substr(2), &chars);
55+
[[maybe_unused]] uint64_t id = std::stoull(string.substr(2), &chars);
5356
return chars > 0;
5457
} catch (...) {
5558
return false;
@@ -65,7 +68,7 @@ static bool isValidBase32(const std::string& string, Address::Type type) {
6568
}
6669

6770
// Check size
68-
if (decoded.size() != size + 4) {
71+
if (decoded.size() != size + checksumSize) {
6972
return false;
7073
}
7174

@@ -89,19 +92,19 @@ bool Address::isValid(const std::string& string) {
8992
}
9093
// Get address type.
9194
auto type = parseType(string[1]);
92-
if (!type) {
95+
if (type == Type::Invalid) {
9396
return false;
9497
}
9598

9699
// ID addresses are special, they are just numbers.
97-
return type == Type::ID ? isValidID(string) : isValidBase32(string, *type);
100+
return type == Type::ID ? isValidID(string) : isValidBase32(string, type);
98101
}
99102

100103
Address::Address(const std::string& string) {
101104
if (!isValid(string))
102105
throw std::invalid_argument("Invalid address data");
103106

104-
Type type = *(parseType(string[1]));
107+
Type type = parseType(string[1]);
105108
// First byte is type
106109
bytes.push_back(static_cast<uint8_t>(type));
107110
if (type == Type::ID) {

src/Filecoin/Address.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include <array>
1212
#include <cstdint>
13-
#include <optional>
1413
#include <string>
1514
#include <vector>
1615

@@ -23,6 +22,7 @@ class Address {
2322
SECP256K1 = 1,
2423
ACTOR = 2,
2524
BLS = 3,
25+
Invalid,
2626
};
2727

2828
/// Address data with address type prefix.
@@ -47,14 +47,14 @@ class Address {
4747
[[nodiscard]] std::string string() const;
4848

4949
/// Returns the type of an address.
50-
Type type() const { return *getType(bytes[0]); }
50+
Type type() const { return getType(bytes[0]); }
5151

5252
/// Address prefix
5353
static constexpr char PREFIX = 'f';
5454

5555
public:
5656
/// Attempts to get the type by number.
57-
static std::optional<Type> getType(uint8_t raw) {
57+
static Type getType(uint8_t raw) {
5858
switch (raw) {
5959
case 0:
6060
return Type::ID;
@@ -65,16 +65,16 @@ class Address {
6565
case 3:
6666
return Type::BLS;
6767
default:
68-
return {};
68+
return Type::Invalid;
6969
}
7070
}
7171

7272
/// Attempts to get the type by ASCII.
73-
static std::optional<Type> parseType(char c) {
73+
static Type parseType(char c) {
7474
if (c >= '0' && c <= '3') {
7575
return static_cast<Type>(c - '0');
7676
} else {
77-
return {};
77+
return Type::Invalid;
7878
}
7979
}
8080

src/Filecoin/Transaction.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ static Data encodeVaruint(const uint256_t& value) {
1414
Data data;
1515
encode256BE(data, value, 256);
1616
size_t i = 0;
17-
for (i = 0; i < data.size(); ++i)
18-
if (data[i] != 0)
17+
for (i = 0; i < data.size(); ++i) {
18+
if (data[i] != 0) {
1919
break;
20+
}
21+
}
2022
Data small;
2123
small.assign(data.begin() + i - 1, data.end());
2224
return small;
@@ -54,8 +56,8 @@ Data Transaction::cid() const {
5456
Data Transaction::serialize(Data& signature) const {
5557
// Wrap signature in object.
5658
Data sigObject;
57-
sigObject.push_back(0x01); // Prepend IKTSecp256k1 type
58-
sigObject.insert(sigObject.end(), signature.begin(), signature.end());
59+
TW::append(sigObject, 0x01); // Prepend IKTSecp256k1 type
60+
TW::append(sigObject, signature);
5961
// Create Filecoin SignedMessage
6062
auto signedMessage = Cbor::Encode::array({message(), Cbor::Encode::bytes(sigObject)});
6163
return signedMessage.encoded();

tests/Filecoin/TransactionTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2017-2019 Trust.
1+
// Copyright © 2017-2020 Trust.
22
//
33
// This file is part of Trust. The full Trust copyright notice, including
44
// terms governing use, modification, and redistribution, is contained in the

0 commit comments

Comments
 (0)