Skip to content

Commit

Permalink
[Aeternity]: Catch all exceptions on TWAnyAddressIsValid for Aetern…
Browse files Browse the repository at this point in the history
…ity coin (#3832)
  • Loading branch information
satoshiotomakan authored May 10, 2024
1 parent b002af3 commit 2c4eb4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
37 changes: 23 additions & 14 deletions src/Coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ bool TW::validateAddress(TWCoinType coin, const string& address, const PrefixVar
// dispatch
auto* dispatcher = coinDispatcher(coin);
assert(dispatcher != nullptr);
return dispatcher->validateAddress(coin, address, prefix);
try {
return dispatcher->validateAddress(coin, address, prefix);
} catch (...) {
return false;
}
}

bool TW::validateAddress(TWCoinType coin, const std::string& string) {
Expand All @@ -221,20 +225,25 @@ bool TW::validateAddress(TWCoinType coin, const std::string& string) {
// dispatch
auto* dispatcher = coinDispatcher(coin);
assert(dispatcher != nullptr);
bool isValid = false;
// First check HRP.
if (hrp != nullptr && !std::string(hrp).empty()) {
isValid = dispatcher->validateAddress(coin, string, Bech32Prefix(hrp));
}
// Then check UTXO
if ((p2pkh != 0 || p2sh != 0) && !isValid) {
return isValid || dispatcher->validateAddress(coin, string, Base58Prefix{.p2pkh = p2pkh, .p2sh = p2sh});
}
// Then check normal
if (!isValid) {
isValid = dispatcher->validateAddress(coin, string, std::monostate());

try {
bool isValid = false;
// First check HRP.
if (hrp != nullptr && !std::string(hrp).empty()) {
isValid = dispatcher->validateAddress(coin, string, Bech32Prefix(hrp));
}
// Then check UTXO
if ((p2pkh != 0 || p2sh != 0) && !isValid) {
return isValid || dispatcher->validateAddress(coin, string, Base58Prefix{.p2pkh = p2pkh, .p2sh = p2sh});
}
// Then check normal
if (!isValid) {
isValid = dispatcher->validateAddress(coin, string, std::monostate());
}
return isValid;
} catch (...) {
return false;
}
return isValid;
}

namespace TW::internal {
Expand Down
3 changes: 2 additions & 1 deletion tests/chains/Aeternity/AddressTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//
// Copyright © 2017 Trust Wallet.

#include <TrustWalletCore/TWAnyAddress.h>
#include <Aeternity/Address.h>
#include <HexCoding.h>
#include <PrivateKey.h>
#include <gtest/gtest.h>

namespace TW::Aeternity::tests {
Expand All @@ -20,6 +20,7 @@ TEST(AeternityAddress, FromString) {
auto address = Address("ak_2p5878zbFhxnrm7meL7TmqwtvBaqcBddyp5eGzZbovZ5FeVfcw");
ASSERT_EQ(address.string(), "ak_2p5878zbFhxnrm7meL7TmqwtvBaqcBddyp5eGzZbovZ5FeVfcw");
ASSERT_ANY_THROW(Address("invalid"));
ASSERT_ANY_THROW(Address("behave@wallet"));
}

} // namespace TW::Aeternity::tests
18 changes: 18 additions & 0 deletions tests/chains/Aeternity/TWAnyAddressTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#include <TrustWalletCore/TWAnyAddress.h>
#include <gtest/gtest.h>
#include <TestUtilities.h>

namespace TW::Aeternity::tests {

// `TWAnyAddressIsValid` must catch exceptions and return false.
TEST(AeternityAddress, IsValid) {
ASSERT_FALSE(TWAnyAddressIsValid(STRING("invalid").get(), TWCoinTypeAeternity));
ASSERT_FALSE(TWAnyAddressIsValid(STRING("behave@wallet").get(), TWCoinTypeAeternity));
ASSERT_FALSE(TWAnyAddressIsValid(STRING("a").get(), TWCoinTypeAeternity));
}

} // namespace TW::Aeternity::tests

0 comments on commit 2c4eb4a

Please sign in to comment.