From b76214dea46c3fbc68619dfe129329e77e187bf3 Mon Sep 17 00:00:00 2001 From: hewig <360470+hewigovens@users.noreply.github.com> Date: Wed, 6 May 2020 01:20:27 +0800 Subject: [PATCH] Replace parse_hex with boost::unhex (#944) * replace parse_hex with boost::unhex * Add android test --- .github/workflows/linux-ci.yml | 2 +- .../ethereum/TestEthereumAddress.kt | 23 +++++++++++++ src/HexCoding.cpp | 21 ------------ src/HexCoding.h | 33 +++++-------------- swift/Tests/Blockchains/EthereumTests.swift | 5 +++ tests/HexCodingTests.cpp | 22 +++++++++++++ walletconsole/lib/CMakeLists.txt | 2 +- 7 files changed, 60 insertions(+), 48 deletions(-) create mode 100644 android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumAddress.kt delete mode 100644 src/HexCoding.cpp create mode 100644 tests/HexCodingTests.cpp diff --git a/.github/workflows/linux-ci.yml b/.github/workflows/linux-ci.yml index 666c0343f1c..e911669998c 100644 --- a/.github/workflows/linux-ci.yml +++ b/.github/workflows/linux-ci.yml @@ -32,7 +32,7 @@ jobs: run: | tools/generate-files cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCODE_COVERAGE=ON -DBOOST_ROOT=${BOOST_ROOT_1_72_0} - make -Cbuild + make -Cbuild -j12 build/tests/tests tests --gtest_output=xml env: CC: /usr/bin/clang diff --git a/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumAddress.kt b/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumAddress.kt new file mode 100644 index 00000000000..f4b29c5db5d --- /dev/null +++ b/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumAddress.kt @@ -0,0 +1,23 @@ +package com.trustwallet.core.app.blockchains.ethereum + +import org.junit.Assert.assertEquals +import org.junit.Test +import wallet.core.jni.AnyAddress +import wallet.core.jni.CoinType +import org.junit.Assert.assertFalse + +class TestEthereumAddress { + + init { + System.loadLibrary("TrustWalletCore") + } + + @Test + fun testEthereumAddresses() { + val any = AnyAddress("0x7d8bf18c7ce84b3e175b339c4ca93aed1dd166f1", CoinType.ETHEREUM) + assertEquals(any.coin(), CoinType.ETHEREUM) + assertEquals(any.description(), "0x7d8bf18C7cE84b3E175b339c4Ca93aEd1dD166F1") + + assertFalse(AnyAddress.isValid("0xMQqpqMQgCBuiPkoXfgZZsJvuzCeI1zc00z6vHJj4", CoinType.ETHEREUM)) + } +} diff --git a/src/HexCoding.cpp b/src/HexCoding.cpp deleted file mode 100644 index 03f43d298b3..00000000000 --- a/src/HexCoding.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -#include "HexCoding.h" - -#include - -std::tuple TW::value(uint8_t c) { - if (c >= '0' && c <= '9') - return std::make_tuple(c - '0', true); - if (c >= 'a' && c <= 'z') - return std::make_tuple(c - 'a' + 10, true); - if (c >= 'A' && c <= 'Z') - return std::make_tuple(c - 'A' + 10, true); - - // Invalid digit - return std::make_tuple(0, false); -} diff --git a/src/HexCoding.h b/src/HexCoding.h index 33f9aecd93d..9f6b0561a4c 100644 --- a/src/HexCoding.h +++ b/src/HexCoding.h @@ -8,6 +8,8 @@ #include "Data.h" +#include + #include #include #include @@ -61,32 +63,13 @@ inline Data parse_hex(const Iter begin, const Iter end) { if (end - begin >= 2 && *begin == '0' && *(begin + 1) == 'x') { it += 2; } - - Data result; - result.reserve(((end - begin) + 1) / 2); - - while (it != end) { - auto high = value(*it); - if (!std::get<1>(high)) { - return {}; - } - it += 1; - - if (it == end) { - result.push_back(std::get<0>(high)); - break; - } - - auto low = value(*it); - if (!std::get<1>(low)) { - return {}; - } - it += 1; - - result.push_back(static_cast((std::get<0>(high) << 4) | std::get<0>(low))); + try { + std::string temp; + boost::algorithm::unhex(it, end, std::back_inserter(temp)); + return Data(temp.begin(), temp.end()); + } catch (...) { + return {}; } - - return result; } /// Parses a string of hexadecimal values. diff --git a/swift/Tests/Blockchains/EthereumTests.swift b/swift/Tests/Blockchains/EthereumTests.swift index 391315b8b6c..9db6e3b3f23 100644 --- a/swift/Tests/Blockchains/EthereumTests.swift +++ b/swift/Tests/Blockchains/EthereumTests.swift @@ -14,6 +14,11 @@ class EthereumTests: XCTestCase { XCTAssertEqual(anyAddress?.description, "0x7d8bf18C7cE84b3E175b339c4Ca93aEd1dD166F1") XCTAssertEqual(anyAddress?.coin, .ethereum) + + let invalid = "0xMQqpqMQgCBuiPkoXfgZZsJvuzCeI1zc00z6vHJj4" + XCTAssertNil(Data(hexString: invalid)) + XCTAssertNil(AnyAddress(string: invalid, coin: .ethereum)) + XCTAssertFalse(AnyAddress.isValid(string: invalid, coin: .ethereum)) } func testSigner() { diff --git a/tests/HexCodingTests.cpp b/tests/HexCodingTests.cpp new file mode 100644 index 00000000000..c902afc9c7c --- /dev/null +++ b/tests/HexCodingTests.cpp @@ -0,0 +1,22 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#include "HexCoding.h" +#include "Data.h" +#include + +namespace TW { + +TEST(HexCoding, validation) { + const std::string valid = "0x7d8bf18c7ce84b3e175b339c4ca93aed1dd166f1"; + const std::string invalid = "0xMQqpqMQgCBuiPkoXfgZZsJvuzCeI1zc00z6vHJj4"; + const auto bytes = parse_hex(invalid); + const auto bytes2 = parse_hex(valid); + + ASSERT_TRUE(bytes.empty()); + ASSERT_EQ("0x" + hex(bytes2), valid); +} +} diff --git a/walletconsole/lib/CMakeLists.txt b/walletconsole/lib/CMakeLists.txt index 8e740a6bb4f..b1ac8a1afac 100644 --- a/walletconsole/lib/CMakeLists.txt +++ b/walletconsole/lib/CMakeLists.txt @@ -2,7 +2,7 @@ file(GLOB_RECURSE walletconsolelib_sources *.cpp) add_library(walletconsolelib ${walletconsolelib_sources}) #target_link_libraries(tests gtest_main TrezorCrypto TrustWalletCore protobuf Boost::boost) -#target_link_libraries(walletconsolelib TrezorCrypto TrustWalletCore protobuf Boost::boost) +target_link_libraries(walletconsolelib TrezorCrypto TrustWalletCore protobuf Boost::boost) target_include_directories(walletconsolelib PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) target_compile_options(walletconsolelib PRIVATE "-Wall")