Skip to content

Commit

Permalink
Merge pull request #10 from victor-tucci/ledger-speculos
Browse files Browse the repository at this point in the history
Ledger emulator support
  • Loading branch information
victor-tucci authored Jun 14, 2023
2 parents 95877a1 + 169e797 commit 401c753
Show file tree
Hide file tree
Showing 19 changed files with 675 additions and 468 deletions.
1 change: 1 addition & 0 deletions cmake/StaticBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ build_external(zmq
${zmq_patch}
CONFIGURE_COMMAND ./configure ${zmq_cross_host} --prefix=${DEPS_DESTDIR} --enable-static --disable-shared
--disable-curve-keygen --enable-curve --disable-drafts --disable-libunwind --with-libsodium
--disable-libbsd --disable-perf
--without-pgm --without-norm --without-vmci --without-docs --with-pic --disable-Werror
"CC=${deps_cc}" "CXX=${deps_cxx}" "CFLAGS=-fstack-protector ${deps_CFLAGS}" "CXXFLAGS=-fstack-protector ${deps_CXXFLAGS}"
${cross_extra}
Expand Down
27 changes: 27 additions & 0 deletions src/common/string_util.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "string_util.h"
#include <cassert>
#include <iomanip>
#include <sstream>

namespace tools {
Expand Down Expand Up @@ -114,4 +115,30 @@ std::string friendly_duration(std::chrono::nanoseconds dur) {
return os.str();
}

std::string short_duration(std::chrono::duration<double> dur) {
std::ostringstream os;
os << std::fixed << std::setprecision(1);
if (dur >= 36h)
os << dur / 24h;
else if (dur >= 90min)
os << dur / 1h;
else if (dur >= 90s)
os << dur / 1min;
else if (dur >= 1s)
os << dur / 1s;
else if (dur >= 100ms)
os << std::setprecision(0) << dur / 1ms;
else if (dur >= 1ms)
os << dur / 1ms;
else if (dur >= 100us)
os << std::setprecision(0) << dur / 1us;
else if (dur >= 1us)
os << dur / 1us;
else if (dur >= 1ns)
os << std::setprecision(0) << dur / 1ns;
else
os << "0s";
return os.str();
}

}
5 changes: 4 additions & 1 deletion src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ T make_from_guts(std::string_view s) {

std::string lowercase_ascii_string(std::string_view src);

/// Converts a duration into a human friendlier string.
/// Converts a duration into a human friendlier string. such as "3d7d47m12s" or "347µs"
std::string friendly_duration(std::chrono::nanoseconds dur);

/// Converts a duration into a shorter, single-unit fractional display such as `42.3min`
std::string short_duration(std::chrono::duration<double> dur);

/// Given an array of string arguments, look for strings of the format <prefix><value> and return <value>
/// Returns empty string view if not found.
template <typename It>
Expand Down
3 changes: 2 additions & 1 deletion src/device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ endif()
if (HIDAPI_FOUND)
target_sources(device PRIVATE
device_ledger.cpp
device_io_hid.cpp
io_hid.cpp
io_ledger_tcp.cpp
)
target_link_libraries(device PRIVATE hidapi_libusb)
endif()
68 changes: 34 additions & 34 deletions src/device/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ namespace cryptonote
}

namespace hw {
namespace {
//device funcion not supported
#define dfns() \
throw std::runtime_error(std::string("device function not supported: ")+ std::string(__FUNCTION__) + \
std::string(" (device.hpp line ")+std::to_string(__LINE__)+std::string(").")); \
return false;
}

class device_progress {
public:
virtual double progress() const { return 0; }
Expand All @@ -92,29 +84,33 @@ namespace hw {

public:

device(): mode(NONE) {}
device(const device &hwdev) {}
virtual ~device() {}
device() = default;
device(const device&) = delete;
device& operator=(const device&) = delete;
device(device&&) = default;
device& operator=(device&&) = default;

virtual ~device() = default;

explicit virtual operator bool() const = 0;
enum device_mode {
virtual bool is_hardware_device() const = 0;
enum class mode {
NONE,
TRANSACTION_CREATE_REAL,
TRANSACTION_CREATE_FAKE,
TRANSACTION_PARSE
};
enum device_type
enum class type
{
SOFTWARE = 0,
LEDGER = 1,
TREZOR = 2
};


enum device_protocol_t {
PROTOCOL_DEFAULT,
PROTOCOL_PROXY, // Originally defined by Ledger
PROTOCOL_COLD, // Originally defined by Trezor
enum class protocol {
DEFAULT,
PROXY, // Originally defined by Ledger
COLD, // Originally defined by Trezor
};

/* ======================================================================= */
Expand All @@ -123,18 +119,22 @@ namespace hw {
virtual bool set_name(std::string_view name) = 0;
virtual std::string get_name() const = 0;

virtual bool init(void) = 0;
// Optional; can be used to take an address parameter if required (e.g. Ledger TCP uses this
// to specify the TCP address).
virtual void set_address(std::string_view address) {}

virtual bool init() = 0;
virtual bool release() = 0;

virtual bool connect(void) = 0;
virtual bool disconnect(void) = 0;
virtual bool connect() = 0;
virtual bool disconnect() = 0;

virtual bool set_mode(device_mode mode) { this->mode = mode; return true; }
virtual device_mode get_mode() const { return mode; }
virtual bool set_mode(mode m) { mode_ = m; return true; }
virtual mode get_mode() const { return mode_; }

virtual device_type get_type() const = 0;
virtual type get_type() const = 0;

virtual device_protocol_t device_protocol() const { return PROTOCOL_DEFAULT; };
virtual protocol device_protocol() const { return protocol::DEFAULT; };
virtual void set_callback(i_device_callback * callback) {};
virtual void set_derivation_path(const std::string &derivation_path) {};

Expand All @@ -144,9 +144,9 @@ namespace hw {
/* ======================================================================= */
/* LOCKER */
/* ======================================================================= */
virtual void lock(void) = 0;
virtual void unlock(void) = 0;
virtual bool try_lock(void) = 0;
virtual void lock() = 0;
virtual void unlock() = 0;
virtual bool try_lock() = 0;


/* ======================================================================= */
Expand Down Expand Up @@ -255,24 +255,24 @@ namespace hw {
// secret key value, if necessary.
virtual bool update_staking_tx_secret_key(crypto::secret_key& key) = 0;

virtual bool close_tx(void) = 0;
virtual bool close_tx() = 0;

virtual bool has_ki_cold_sync(void) const { return false; }
virtual bool has_tx_cold_sign(void) const { return false; }
virtual bool has_ki_live_refresh(void) const { return true; }
virtual bool has_ki_cold_sync() const { return false; }
virtual bool has_tx_cold_sign() const { return false; }
virtual bool has_ki_live_refresh() const { return true; }
virtual bool compute_key_image(const cryptonote::account_keys& ack, const crypto::public_key& out_key, const crypto::key_derivation& recv_derivation, size_t real_output_index, const cryptonote::subaddress_index& received_index, cryptonote::keypair& in_ephemeral, crypto::key_image& ki) { return false; }
virtual void computing_key_images(bool started) {};
virtual void set_network_type(cryptonote::network_type network_type) { }
virtual void display_address(const cryptonote::subaddress_index& index, const std::optional<crypto::hash8> &payment_id) {}

protected:
device_mode mode;
mode mode_ =mode::NONE;
} ;

struct mode_resetter {
device& hwref;
mode_resetter(hw::device& dev) : hwref(dev) { }
~mode_resetter() { hwref.set_mode(hw::device::NONE);}
~mode_resetter() { hwref.set_mode(hw::device::mode::NONE);}
};

class device_registry {
Expand Down
42 changes: 14 additions & 28 deletions src/device/device_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@
#include "cryptonote_config.h"
#include <sodium/crypto_generichash.h>

namespace hw {

namespace core {

device_default::device_default() { }

device_default::~device_default() { }
namespace hw::core {

/* ===================================================================== */
/* === Misc ==== */
/* ===================================================================== */

// EW, this crap is NASTY. See the comment/TODO in crypto/crypto.cpp (which is where this
// nasty crap was copied from).
static inline unsigned char *operator &(crypto::ec_scalar &scalar) {
return &reinterpret_cast<unsigned char &>(scalar);
}
Expand All @@ -61,32 +58,28 @@ namespace hw {
/* ======================================================================= */
/* SETUP/TEARDOWN */
/* ======================================================================= */
bool device_default::set_name(std::string_view name) {
this->name = name;
bool device_default::set_name(std::string_view n) {
name = n;
return true;
}
std::string device_default::get_name() const {
return name;
}

bool device_default::init(void) {
bool device_default::init() {
return true;
}
bool device_default::release() {
return true;
}

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

bool device_default::set_mode(device_mode mode) {
return device::set_mode(mode);
}

/* ======================================================================= */
/* LOCKER */
/* ======================================================================= */
Expand All @@ -112,10 +105,11 @@ namespace hw {
return true;
}
bool device_default::get_public_address(cryptonote::account_public_address &pubkey) {
dfns();
throw std::runtime_error{"device function not supported: get_public_address"};
}
bool device_default::get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) {
dfns();
throw std::runtime_error{"device function not supported: get_secret_keys"};

}
/* ======================================================================= */
/* SUB ADDRESS */
Expand Down Expand Up @@ -476,17 +470,9 @@ namespace hw {


/* ---------------------------------------------------------- */
static device_default *default_core_device = NULL;
void register_all(std::map<std::string, std::unique_ptr<device>> &registry) {
if (!default_core_device) {
default_core_device = new device_default();
default_core_device->set_name("default_core_device");

}
registry.insert(std::make_pair("default", std::unique_ptr<device>(default_core_device)));
auto dev = std::make_unique<device_default>();
dev->set_name("default_core_device");
registry.emplace("default", std::move(dev));
}


}

}
26 changes: 10 additions & 16 deletions src/device/device_default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,30 @@ namespace hw {

class device_default : public hw::device {
public:
device_default();
~device_default();
device_default() = default;

device_default(const device_default &device) = delete;
device_default& operator=(const device_default &device) = delete;
bool is_hardware_device() const override { return false; };

explicit operator bool() const override { return false; };

/* ======================================================================= */
/* ======================================================================= */
/* SETUP/TEARDOWN */
/* ======================================================================= */
bool set_name(std::string_view name) override;
std::string get_name() const override;

bool init(void) override;
bool init() override;
bool release() override;

bool connect(void) override;
bool connect() override;
bool disconnect() override;

bool set_mode(device_mode mode) override;

device_type get_type() const override {return device_type::SOFTWARE;};
type get_type() const override { return type::SOFTWARE; };

/* ======================================================================= */
/* LOCKER */
/* ======================================================================= */
void lock(void) override;
void unlock(void) override;
bool try_lock(void) override;
void lock() override;
void unlock() override;
bool try_lock() override;

/* ======================================================================= */
/* WALLET & ADDRESS */
Expand Down Expand Up @@ -152,7 +146,7 @@ namespace hw {

bool update_staking_tx_secret_key(crypto::secret_key& key) override;

bool close_tx(void) override;
bool close_tx() override;
};

}
Expand Down
Loading

0 comments on commit 401c753

Please sign in to comment.