Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[for review] TREZOR: initial integration proposal #4241

Merged
merged 1 commit into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
[submodule "external/rapidjson"]
path = external/rapidjson
url = https://github.com/Tencent/rapidjson
[submodule "external/trezor-common"]
path = external/trezor-common
url = https://github.com/trezor/trezor-common.git
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ if(NOT MANUAL_SUBMODULES)
check_submodule(external/miniupnp)
check_submodule(external/unbound)
check_submodule(external/rapidjson)
check_submodule(external/trezor-common)
endif()
endif()

Expand Down Expand Up @@ -512,6 +513,16 @@ else (HIDAPI_FOUND)
message(STATUS "Could not find HIDAPI")
endif()

# Protobuf, optional. Required for TREZOR.
include(FindProtobuf)
find_package(Protobuf)
if(Protobuf_FOUND)
set(HAVE_PROTOBUF 1)
add_definitions(-DHAVE_PROTOBUF=1)
else(Protobuf_FOUND)
message(STATUS "Could not find Protobuf")
endif()

if(MSVC)
add_definitions("/bigobj /MP /W3 /GS- /D_CRT_SECURE_NO_WARNINGS /wd4996 /wd4345 /D_WIN32_WINNT=0x0600 /DWIN32_LEAN_AND_MEAN /DGTEST_HAS_TR1_TUPLE=0 /FIinline_c.h /D__SSE4_1__")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Dinline=__inline")
Expand Down
1 change: 1 addition & 0 deletions external/trezor-common
Submodule trezor-common added at 588f8e
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ if(PER_BLOCK_CHECKPOINT)
endif()

add_subdirectory(device)
add_subdirectory(device_trezor)
24 changes: 19 additions & 5 deletions src/cryptonote_basic/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ DISABLE_VS_WARNINGS(4244 4345)
m_creation_timestamp = 0;
}
//-----------------------------------------------------------------
void account_base::deinit()
{
try{
m_keys.get_device().disconnect();
} catch (const std::exception &e){
MERROR("Device disconnect exception: " << e.what());
}
}
//-----------------------------------------------------------------
void account_base::forget_spend_key()
{
m_keys.m_spend_secret_key = crypto::secret_key();
Expand Down Expand Up @@ -206,11 +215,16 @@ DISABLE_VS_WARNINGS(4244 4345)
void account_base::create_from_device(hw::device &hwdev)
{
m_keys.set_device(hwdev);
MCDEBUG("ledger", "device type: "<<typeid(hwdev).name());
hwdev.init();
hwdev.connect();
hwdev.get_public_address(m_keys.m_account_address);
hwdev.get_secret_keys(m_keys.m_view_secret_key, m_keys.m_spend_secret_key);
MCDEBUG("device", "device type: "<<typeid(hwdev).name());
CHECK_AND_ASSERT_THROW_MES(hwdev.init(), "Device init failed");
CHECK_AND_ASSERT_THROW_MES(hwdev.connect(), "Device connect failed");
try {
CHECK_AND_ASSERT_THROW_MES(hwdev.get_public_address(m_keys.m_account_address), "Cannot get a device address");
CHECK_AND_ASSERT_THROW_MES(hwdev.get_secret_keys(m_keys.m_view_secret_key, m_keys.m_spend_secret_key), "Cannot get device secret");
} catch (const std::exception &e){
hwdev.disconnect();
throw;
}
struct tm timestamp = {0};
timestamp.tm_year = 2014 - 1900; // year 2014
timestamp.tm_mon = 4 - 1; // month april
Expand Down
1 change: 1 addition & 0 deletions src/cryptonote_basic/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace cryptonote

hw::device& get_device() const {return m_keys.get_device();}
void set_device( hw::device &hwdev) {m_keys.set_device(hwdev);}
void deinit();

uint64_t get_createtime() const { return m_creation_timestamp; }
void set_createtime(uint64_t val) { m_creation_timestamp = val; }
Expand Down
2 changes: 2 additions & 0 deletions src/device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(device_headers
device.hpp
device_io.hpp
device_default.hpp
device_cold.hpp
log.hpp
)

Expand Down Expand Up @@ -72,5 +73,6 @@ target_link_libraries(device
cncrypto
ringct_basic
${OPENSSL_CRYPTO_LIBRARIES}
${Boost_SERIALIZATION_LIBRARY}
PRIVATE
${EXTRA_LIBRARIES})
16 changes: 15 additions & 1 deletion src/device/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "crypto/crypto.h"
#include "crypto/chacha.h"
#include "ringct/rctTypes.h"
#include "cryptonote_config.h"


#ifndef USE_DEVICE_LEDGER
Expand Down Expand Up @@ -99,10 +100,17 @@ namespace hw {
enum device_type
{
SOFTWARE = 0,
LEDGER = 1
LEDGER = 1,
TREZOR = 2
};


enum device_protocol_t {
PROTOCOL_DEFAULT,
PROTOCOL_PROXY, // Originally defined by Ledger
PROTOCOL_COLD, // Originally defined by Trezor
};

/* ======================================================================= */
/* SETUP/TEARDOWN */
/* ======================================================================= */
Expand All @@ -120,6 +128,7 @@ namespace hw {

virtual device_type get_type() const = 0;

virtual device_protocol_t device_protocol() const { return PROTOCOL_DEFAULT; };

/* ======================================================================= */
/* LOCKER */
Expand Down Expand Up @@ -204,6 +213,11 @@ namespace hw {

virtual bool close_tx(void) = 0;

virtual bool has_ki_cold_sync(void) const { return false; }
virtual bool has_tx_cold_sign(void) const { return false; }

virtual void set_network_type(cryptonote::network_type network_type) { }

protected:
device_mode mode;
} ;
Expand Down
71 changes: 71 additions & 0 deletions src/device/device_cold.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2017-2018, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#ifndef MONERO_DEVICE_COLD_H
#define MONERO_DEVICE_COLD_H

#include "wallet/wallet2.h"
#include <boost/function.hpp>


namespace hw {

typedef struct wallet_shim {
boost::function<crypto::public_key (const tools::wallet2::transfer_details &td)> get_tx_pub_key_from_received_outs;
} wallet_shim;

class tx_aux_data {
public:
std::vector<std::string> tx_device_aux; // device generated aux data
std::vector<cryptonote::address_parse_info> tx_recipients; // as entered by user
};

class device_cold {
public:

using exported_key_image = std::vector<std::pair<crypto::key_image, crypto::signature>>;

/**
* Key image sync with the cold protocol.
*/
virtual void ki_sync(wallet_shim * wallet,
const std::vector<::tools::wallet2::transfer_details> & transfers,
exported_key_image & ski) =0;

/**
* Signs unsigned transaction with the cold protocol.
*/
virtual void tx_sign(wallet_shim * wallet,
const ::tools::wallet2::unsigned_tx_set & unsigned_tx,
::tools::wallet2::signed_tx_set & signed_tx,
tx_aux_data & aux_data) =0;
};
}

#endif //MONERO_DEVICE_COLD_H
8 changes: 4 additions & 4 deletions src/device/device_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ namespace hw {
}

bool device_default::init(void) {
dfns();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't used anywhere, shouldn't the function definition be removed then?

return true;
}
bool device_default::release() {
dfns();
return true;
}

bool device_default::connect(void) {
dfns();
return true;
}
bool device_default::disconnect() {
dfns();
return true;
}

bool device_default::set_mode(device_mode mode) {
Expand Down
1 change: 1 addition & 0 deletions src/device/device_ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ namespace hw {
bool set_mode(device_mode mode) override;

device_type get_type() const override {return device_type::LEDGER;};
device_protocol_t device_protocol() const override { return PROTOCOL_PROXY; };

/* ======================================================================= */
/* LOCKER */
Expand Down
123 changes: 123 additions & 0 deletions src/device_trezor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright (c) 2014-2017, The Monero Project
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set(TREZOR_PROTOB_H
trezor/messages/messages.pb.h
trezor/messages/messages-common.pb.h
trezor/messages/messages-management.pb.h
trezor/messages/messages-monero.pb.h
)

set(TREZOR_PROTOB_CPP
trezor/messages/messages.pb.cc
trezor/messages/messages-common.pb.cc
trezor/messages/messages-management.pb.cc
trezor/messages/messages-monero.pb.cc
)

set(trezor_headers
trezor/exceptions.hpp
trezor/messages_map.hpp
trezor/protocol.hpp
trezor/transport.hpp
device_trezor_base.hpp
device_trezor.hpp
trezor.hpp
${TREZOR_PROTOB_H}
)

set(trezor_sources
trezor/messages_map.cpp
trezor/protocol.cpp
trezor/transport.cpp
device_trezor_base.cpp
device_trezor.cpp
${TREZOR_PROTOB_CPP}
)

set(trezor_private_headers)


include(FindProtobuf)
find_package(Protobuf) # REQUIRED

# Test for HAVE_PROTOBUF from the parent
if(Protobuf_FOUND AND HAVE_PROTOBUF)
if ("$ENV{PYTHON3}" STREQUAL "")
set(PYTHON3 "python3")
else()
set(PYTHON3 "$ENV{PYTHON3}" CACHE INTERNAL "Copied from environment variable")
endif()

execute_process(COMMAND ${PYTHON3} tools/build_protob.py WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/trezor RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE ERR)
if(RET)
message(WARNING "Trezor protobuf messages could not be regenerated (err=${RET}, python ${PYTHON})."
"OUT: ${OUT}, ERR: ${ERR}."
"Please read src/device_trezor/trezor/tools/README.md")
else()
message(STATUS "Trezor protobuf messages regenerated ${OUT}")
set(TREZOR_PROTOBUF_GENERATED 1)
endif()
endif()


if(TREZOR_PROTOBUF_GENERATED)
message(STATUS "Trezor support enabled")

add_definitions(-DPROTOBUF_INLINE_NOT_IN_HEADERS=0)

monero_private_headers(device_trezor
${device_private_headers}
${PROTOBUF_INCLUDE_DIR})

monero_add_library(device_trezor
${trezor_sources}
${trezor_headers}
${trezor_private_headers})

target_link_libraries(device_trezor
PUBLIC
device
cncrypto
ringct_basic
cryptonote_core
common
${SODIUM_LIBRARY}
${Boost_CHRONO_LIBRARY}
${PROTOBUF_LIBRARY}
PRIVATE
${EXTRA_LIBRARIES})

# set(WITH_DEVICE_TREZOR 1 PARENT_SCOPE)
# add_definitions(-DWITH_DEVICE_TREZOR=1)

else()
monero_private_headers(device_trezor)
monero_add_library(device_trezor device_trezor.cpp)
target_link_libraries(device_trezor PUBLIC cncrypto)
endif()
Loading