Skip to content

Commit

Permalink
[Aptos]: fix short address padding for aptos (#3139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed May 3, 2023
1 parent 8763d75 commit e360319
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion samples/kmp/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.trustwallet:wallet-core-kotlin:3.1.25")
implementation("com.trustwallet:wallet-core-kotlin:3.1.30")
}
}
val commonTest by getting {
Expand Down
4 changes: 2 additions & 2 deletions src/Aptos/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace TW::Aptos {

class Address : public Move::Address<Address, 32> {
class Address : public Move::Address<Address, 32, true> {
public:
using AptosAddress = Move::Address<Address, 32>;
using AptosAddress = Move::Address<Address, 32, true>;
using AptosAddress::size;
using AptosAddress::bytes;

Expand Down
24 changes: 14 additions & 10 deletions src/Move/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
#include <array>

namespace TW::Move {
template <typename Derived, std::size_t N>
template <typename Derived, std::size_t N = 32, bool StrictPadding = false>
class Address {
private:
static constexpr std::size_t shortSizeAddress = 3;
static constexpr std::size_t hexShortSizeAddress = shortSizeAddress - 2;
static constexpr std::size_t hexSizeAddress = N*2;
static constexpr std::size_t hexNonPaddedSizeAddress = hexSizeAddress - 1;

static std::string normalize(const std::string& string, std::size_t hexLen) {
std::string hexStr((size * 2) - hexLen, '0');
Expand All @@ -34,14 +33,15 @@ class Address {

/// Determines whether a string makes a valid address.
static bool isValid(const std::string& string) {
if (!is_hex_encoded(string)) {
return false;
}
auto address = string;
if (address.starts_with("0x")) {
address = address.substr(2);
std::size_t hexLen = address.size();

if ((hexLen == hexShortSizeAddress || hexLen == hexNonPaddedSizeAddress) && hexLen < hexSizeAddress) {
address = normalize(address, hexLen);
}
}
if (address.size() == hexShortSizeAddress || (StrictPadding && (address.size() < hexSizeAddress))) {
address = normalize(address, address.size());
}
if (address.size() != 2 * Address::size) {
return false;
Expand All @@ -58,12 +58,16 @@ class Address {
}
auto hexFunctor = [&string]() {
std::size_t hexLen = string.size() - 2;
bool isExpectedLen = hexLen == hexShortSizeAddress || hexLen == hexNonPaddedSizeAddress;
if (string.starts_with("0x") && isExpectedLen) {
bool isExpectedLen = hexLen == hexShortSizeAddress;
if (string.starts_with("0x") && (isExpectedLen || (StrictPadding && (hexLen < hexSizeAddress)))) {
//! We have specific address like 0x1, padding it.
return parse_hex(normalize(string.substr(2), hexLen));
} else {
return parse_hex(string);
auto address = string;
if (StrictPadding && (address.size() < hexSizeAddress)) {
address = normalize(address, address.size());
}
return parse_hex(address);
}
};

Expand Down
11 changes: 10 additions & 1 deletion tests/chains/Aptos/AddressTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ TEST(AptosAddress, Valid) {
ASSERT_TRUE(Address::isValid("0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b"));
ASSERT_TRUE(Address::isValid("eeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b"));
ASSERT_TRUE(Address::isValid("19aadeca9388e009d136245b9a67423f3eee242b03142849eb4f81a4a409e59c"));
ASSERT_TRUE(Address::isValid("777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb"));
ASSERT_TRUE(Address::isValid("0x777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb"));
ASSERT_TRUE(Address::isValid("eeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175")); // too short -> automatically padded
}

TEST(AptosAddress, Invalid) {
ASSERT_FALSE(Address::isValid("Seff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b")); // Invalid hex character
ASSERT_FALSE(Address::isValid("eeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175bb")); // Invalid length: too long
ASSERT_FALSE(Address::isValid("eeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175")); // Invalid length: too short

}

TEST(AptosAddress, FromPrivateKey) {
Expand All @@ -44,6 +47,12 @@ TEST(AptosAddress, FromPublicKey) {
TEST(AptosAddress, FromString) {
auto address = Address("eeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b");
ASSERT_EQ(address.string(), "0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b");


address = Address("0x777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb");
ASSERT_EQ(address.string(), "0x0000777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb");
address = Address("777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb");
ASSERT_EQ(address.string(), "0x0000777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb");
}

TEST(AptosAddress, ShortString) {
Expand Down

0 comments on commit e360319

Please sign in to comment.