Skip to content

Commit

Permalink
Replace parse_hex with boost::unhex (#944)
Browse files Browse the repository at this point in the history
* replace parse_hex with boost::unhex

* Add android test
  • Loading branch information
hewigovens authored May 5, 2020
1 parent cb6e57f commit b76214d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}
21 changes: 0 additions & 21 deletions src/HexCoding.cpp

This file was deleted.

33 changes: 8 additions & 25 deletions src/HexCoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "Data.h"

#include <boost/algorithm/hex.hpp>

#include <array>
#include <string>
#include <tuple>
Expand Down Expand Up @@ -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<uint8_t>((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.
Expand Down
5 changes: 5 additions & 0 deletions swift/Tests/Blockchains/EthereumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 22 additions & 0 deletions tests/HexCodingTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

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);
}
}
2 changes: 1 addition & 1 deletion walletconsole/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit b76214d

Please sign in to comment.