Skip to content

Commit 2c4eb4a

Browse files
[Aeternity]: Catch all exceptions on TWAnyAddressIsValid for Aeternity coin (#3832)
1 parent b002af3 commit 2c4eb4a

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/Coin.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ bool TW::validateAddress(TWCoinType coin, const string& address, const PrefixVar
210210
// dispatch
211211
auto* dispatcher = coinDispatcher(coin);
212212
assert(dispatcher != nullptr);
213-
return dispatcher->validateAddress(coin, address, prefix);
213+
try {
214+
return dispatcher->validateAddress(coin, address, prefix);
215+
} catch (...) {
216+
return false;
217+
}
214218
}
215219

216220
bool TW::validateAddress(TWCoinType coin, const std::string& string) {
@@ -221,20 +225,25 @@ bool TW::validateAddress(TWCoinType coin, const std::string& string) {
221225
// dispatch
222226
auto* dispatcher = coinDispatcher(coin);
223227
assert(dispatcher != nullptr);
224-
bool isValid = false;
225-
// First check HRP.
226-
if (hrp != nullptr && !std::string(hrp).empty()) {
227-
isValid = dispatcher->validateAddress(coin, string, Bech32Prefix(hrp));
228-
}
229-
// Then check UTXO
230-
if ((p2pkh != 0 || p2sh != 0) && !isValid) {
231-
return isValid || dispatcher->validateAddress(coin, string, Base58Prefix{.p2pkh = p2pkh, .p2sh = p2sh});
232-
}
233-
// Then check normal
234-
if (!isValid) {
235-
isValid = dispatcher->validateAddress(coin, string, std::monostate());
228+
229+
try {
230+
bool isValid = false;
231+
// First check HRP.
232+
if (hrp != nullptr && !std::string(hrp).empty()) {
233+
isValid = dispatcher->validateAddress(coin, string, Bech32Prefix(hrp));
234+
}
235+
// Then check UTXO
236+
if ((p2pkh != 0 || p2sh != 0) && !isValid) {
237+
return isValid || dispatcher->validateAddress(coin, string, Base58Prefix{.p2pkh = p2pkh, .p2sh = p2sh});
238+
}
239+
// Then check normal
240+
if (!isValid) {
241+
isValid = dispatcher->validateAddress(coin, string, std::monostate());
242+
}
243+
return isValid;
244+
} catch (...) {
245+
return false;
236246
}
237-
return isValid;
238247
}
239248

240249
namespace TW::internal {

tests/chains/Aeternity/AddressTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//
33
// Copyright © 2017 Trust Wallet.
44

5+
#include <TrustWalletCore/TWAnyAddress.h>
56
#include <Aeternity/Address.h>
67
#include <HexCoding.h>
7-
#include <PrivateKey.h>
88
#include <gtest/gtest.h>
99

1010
namespace TW::Aeternity::tests {
@@ -20,6 +20,7 @@ TEST(AeternityAddress, FromString) {
2020
auto address = Address("ak_2p5878zbFhxnrm7meL7TmqwtvBaqcBddyp5eGzZbovZ5FeVfcw");
2121
ASSERT_EQ(address.string(), "ak_2p5878zbFhxnrm7meL7TmqwtvBaqcBddyp5eGzZbovZ5FeVfcw");
2222
ASSERT_ANY_THROW(Address("invalid"));
23+
ASSERT_ANY_THROW(Address("behave@wallet"));
2324
}
2425

2526
} // namespace TW::Aeternity::tests
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright © 2017 Trust Wallet.
4+
5+
#include <TrustWalletCore/TWAnyAddress.h>
6+
#include <gtest/gtest.h>
7+
#include <TestUtilities.h>
8+
9+
namespace TW::Aeternity::tests {
10+
11+
// `TWAnyAddressIsValid` must catch exceptions and return false.
12+
TEST(AeternityAddress, IsValid) {
13+
ASSERT_FALSE(TWAnyAddressIsValid(STRING("invalid").get(), TWCoinTypeAeternity));
14+
ASSERT_FALSE(TWAnyAddressIsValid(STRING("behave@wallet").get(), TWCoinTypeAeternity));
15+
ASSERT_FALSE(TWAnyAddressIsValid(STRING("a").get(), TWCoinTypeAeternity));
16+
}
17+
18+
} // namespace TW::Aeternity::tests

0 commit comments

Comments
 (0)