From fc7c14b26225627c74b23467bb442247ca35d010 Mon Sep 17 00:00:00 2001 From: Czarek Nakamoto Date: Mon, 18 May 2026 08:48:12 -0400 Subject: [PATCH] trezor import export sign broadcast functions --- cmake/CheckTrezor.cmake | 28 +- src/device_trezor/trezor/protocol.cpp | 453 ++++++++++++++++++++++++ src/device_trezor/trezor/protocol.hpp | 27 ++ src/wallet/api/pending_transaction.cpp | 143 ++++++++ src/wallet/api/pending_transaction.h | 18 + src/wallet/api/unsigned_transaction.cpp | 2 + src/wallet/api/wallet.cpp | 31 +- src/wallet/api/wallet.h | 2 + src/wallet/api/wallet2_api.h | 15 + src/wallet/wallet2.cpp | 158 +++++++++ src/wallet/wallet2.h | 5 +- 11 files changed, 853 insertions(+), 29 deletions(-) diff --git a/cmake/CheckTrezor.cmake b/cmake/CheckTrezor.cmake index 4fae15fad..57e3d4866 100644 --- a/cmake/CheckTrezor.cmake +++ b/cmake/CheckTrezor.cmake @@ -40,9 +40,6 @@ if (USE_DEVICE_TREZOR) # Protobuf handling the cache variables set in docker. if(NOT Protobuf_FOUND AND NOT Protobuf_LIBRARY AND NOT Protobuf_PROTOC_EXECUTABLE AND NOT Protobuf_INCLUDE_DIR) message(STATUS "Could not find Protobuf") - elseif(NOT Protobuf_LIBRARY OR NOT EXISTS "${Protobuf_LIBRARY}") - message(STATUS "Protobuf library not found: ${Protobuf_LIBRARY}") - unset(Protobuf_FOUND) elseif(NOT Protobuf_PROTOC_EXECUTABLE OR NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") message(STATUS "Protobuf executable not found: ${Protobuf_PROTOC_EXECUTABLE}") unset(Protobuf_FOUND) @@ -93,31 +90,8 @@ if(Protobuf_FOUND AND USE_DEVICE_TREZOR) endif() endif() -# Protobuf compilation test -if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON) - execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} -I "${CMAKE_CURRENT_LIST_DIR}" -I "${Protobuf_INCLUDE_DIR}" "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.proto" --cpp_out ${CMAKE_BINARY_DIR} RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE ERR) - if(RET) - message(STATUS "Protobuf test generation failed: ${OUT} ${ERR}") - endif() - - try_compile(Protobuf_COMPILE_TEST_PASSED - "${CMAKE_BINARY_DIR}" - SOURCES - "${CMAKE_BINARY_DIR}/test-protobuf.pb.cc" - "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.cpp" - CMAKE_FLAGS - "-DINCLUDE_DIRECTORIES=${Protobuf_INCLUDE_DIR};${CMAKE_BINARY_DIR}" - "-DCMAKE_CXX_STANDARD=11" - LINK_LIBRARIES ${Protobuf_LIBRARY} - OUTPUT_VARIABLE OUTPUT - ) - if(NOT Protobuf_COMPILE_TEST_PASSED) - message(STATUS "Protobuf Compilation test failed: ${OUTPUT}.") - endif() -endif() - # Try to build protobuf messages -if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON AND Protobuf_COMPILE_TEST_PASSED) +if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON) set(ENV{PROTOBUF_INCLUDE_DIRS} "${Protobuf_INCLUDE_DIR}") set(ENV{PROTOBUF_PROTOC_EXECUTABLE} "${Protobuf_PROTOC_EXECUTABLE}") set(TREZOR_PROTOBUF_PARAMS "") diff --git a/src/device_trezor/trezor/protocol.cpp b/src/device_trezor/trezor/protocol.cpp index 0e59a16ba..acbc5ac76 100644 --- a/src/device_trezor/trezor/protocol.cpp +++ b/src/device_trezor/trezor/protocol.cpp @@ -29,12 +29,19 @@ #include "version.h" #include "protocol.hpp" +#include "string_tools.h" +#include "cryptonote_basic/cryptonote_format_utils.h" +#include #include #include +#include #include #include #include #include +#include +#include +#include #include #include #include @@ -450,6 +457,10 @@ namespace tx { } } + void Signer::export_source_entry(MoneroTransactionSourceEntry *dst, size_t idx, bool need_ring_keys, bool need_ring_indices){ + set_tx_input(dst, idx, need_ring_keys, need_ring_indices); + } + void Signer::set_tx_input(MoneroTransactionSourceEntry * dst, size_t idx, bool need_ring_keys, bool need_ring_indices){ const cryptonote::tx_source_entry & src = cur_tx().sources[idx]; const tools::wallet2::transfer_details & transfer = get_source_transfer(idx); @@ -1096,6 +1107,448 @@ namespace tx { memwipe(plaintext.get(), keys_len); } + namespace { + + std::string bin_to_hex_lower(const std::string &bin) + { + return epee::string_tools::buff_to_hex_nodelimer(bin); + } + + rapidjson::Value dest_entry_to_json(const messages::monero::MoneroTransactionDestinationEntry &e, rapidjson::Document::AllocatorType &a) + { + rapidjson::Value o(rapidjson::kObjectType); + if (e.has_amount()) + o.AddMember("amount", e.amount(), a); + if (e.has_addr()) + { + rapidjson::Value addr(rapidjson::kObjectType); + const auto &ad = e.addr(); + if (ad.has_spend_public_key()) + { + const std::string h = bin_to_hex_lower(ad.spend_public_key()); + addr.AddMember("spend_public_key", rapidjson::Value(h.c_str(), static_cast(h.size()), a), a); + } + if (ad.has_view_public_key()) + { + const std::string h = bin_to_hex_lower(ad.view_public_key()); + addr.AddMember("view_public_key", rapidjson::Value(h.c_str(), static_cast(h.size()), a), a); + } + o.AddMember("addr", addr, a); + } + if (e.has_is_subaddress()) + o.AddMember("is_subaddress", e.is_subaddress(), a); + if (e.has_original()) + o.AddMember("original", rapidjson::Value(e.original().c_str(), static_cast(e.original().size()), a), a); + if (e.has_is_integrated()) + o.AddMember("is_integrated", e.is_integrated(), a); + return o; + } + + rapidjson::Value source_entry_to_json(const messages::monero::MoneroTransactionSourceEntry &e, rapidjson::Document::AllocatorType &a) + { + rapidjson::Value o(rapidjson::kObjectType); + rapidjson::Value ring(rapidjson::kArrayType); + for (int i = 0; i < e.outputs_size(); ++i) + { + const auto &out = e.outputs(i); + rapidjson::Value ring_m(rapidjson::kObjectType); + if (out.has_idx()) + ring_m.AddMember("idx", out.idx(), a); + if (out.has_key()) + { + rapidjson::Value key(rapidjson::kObjectType); + const auto &k = out.key(); + if (k.has_dest()) + { + const std::string h = bin_to_hex_lower(k.dest()); + key.AddMember("dest", rapidjson::Value(h.c_str(), static_cast(h.size()), a), a); + } + if (k.has_commitment()) + { + const std::string h = bin_to_hex_lower(k.commitment()); + key.AddMember("commitment", rapidjson::Value(h.c_str(), static_cast(h.size()), a), a); + } + ring_m.AddMember("key", key, a); + } + ring.PushBack(ring_m, a); + } + o.AddMember("outputs", ring, a); + if (e.has_real_output()) + o.AddMember("real_output", e.real_output(), a); + if (e.has_real_out_tx_key()) + { + const std::string h = bin_to_hex_lower(e.real_out_tx_key()); + o.AddMember("real_out_tx_key", rapidjson::Value(h.c_str(), static_cast(h.size()), a), a); + } + rapidjson::Value add_keys(rapidjson::kArrayType); + for (int i = 0; i < e.real_out_additional_tx_keys_size(); ++i) + { + const std::string h = bin_to_hex_lower(e.real_out_additional_tx_keys(i)); + add_keys.PushBack(rapidjson::Value(h.c_str(), static_cast(h.size()), a), a); + } + o.AddMember("real_out_additional_tx_keys", add_keys, a); + if (e.has_real_output_in_tx_index()) + o.AddMember("real_output_in_tx_index", e.real_output_in_tx_index(), a); + if (e.has_amount()) + o.AddMember("amount", e.amount(), a); + if (e.has_rct()) + o.AddMember("rct", e.rct(), a); + if (e.has_mask()) + { + const std::string h = bin_to_hex_lower(e.mask()); + o.AddMember("mask", rapidjson::Value(h.c_str(), static_cast(h.size()), a), a); + } + if (e.has_subaddr_minor()) + o.AddMember("subaddr_minor", e.subaddr_minor(), a); + return o; + } + + std::string monero_default_bip44_path(uint32_t subaddr_account) + { + std::ostringstream oss; + oss << "m/44'/128'/" << subaddr_account << "'"; + return oss.str(); + } + + } // namespace + + + std::string trezor_connect_monero_sign_transaction_to_json( + wallet_shim *wallet, + const unsigned_tx_set *utx, + size_t tx_idx, + hw::tx_aux_data *aux_data, + cryptonote::network_type network_type) + { + CHECK_AND_ASSERT_THROW_MES(utx && aux_data, "null argument"); + CHECK_AND_ASSERT_THROW_MES(std::get<0>(utx->transfers) == 0, "Unsupported non zero offset"); + CHECK_AND_ASSERT_THROW_MES(tx_idx < utx->txes.size(), "Invalid transaction index"); + + Signer signer(wallet, utx, tx_idx, aux_data); + auto init_req = signer.step_init(); + const auto &tsx = init_req->tsx_data(); + + rapidjson::Document doc; + doc.SetObject(); + auto &alloc = doc.GetAllocator(); + + const std::string path_str = monero_default_bip44_path(utx->txes[tx_idx].subaddr_account); + doc.AddMember("path", rapidjson::Value(path_str.c_str(), static_cast(path_str.size()), alloc), alloc); + doc.AddMember("networkType", static_cast(network_type), alloc); + + rapidjson::Value tsx_data(rapidjson::kObjectType); + tsx_data.AddMember("version", tsx.version(), alloc); + if (tsx.has_payment_id() && !tsx.payment_id().empty()) + { + const std::string h = bin_to_hex_lower(tsx.payment_id()); + tsx_data.AddMember("payment_id", rapidjson::Value(h.c_str(), static_cast(h.size()), alloc), alloc); + } + tsx_data.AddMember("unlock_time", static_cast(tsx.unlock_time()), alloc); + + rapidjson::Value outputs(rapidjson::kArrayType); + for (int i = 0; i < tsx.outputs_size(); ++i) + outputs.PushBack(dest_entry_to_json(tsx.outputs(i), alloc), alloc); + tsx_data.AddMember("outputs", outputs, alloc); + + if (tsx.has_change_dts()) + tsx_data.AddMember("change_dts", dest_entry_to_json(tsx.change_dts(), alloc), alloc); + + if (tsx.has_num_inputs()) + tsx_data.AddMember("num_inputs", tsx.num_inputs(), alloc); + if (tsx.has_mixin()) + tsx_data.AddMember("mixin", tsx.mixin(), alloc); + if (tsx.has_fee()) + tsx_data.AddMember("fee", tsx.fee(), alloc); + if (tsx.has_account()) + tsx_data.AddMember("account", tsx.account(), alloc); + + if (tsx.minor_indices_size() > 0) + { + rapidjson::Value minors(rapidjson::kArrayType); + for (int i = 0; i < tsx.minor_indices_size(); ++i) + minors.PushBack(tsx.minor_indices(i), alloc); + tsx_data.AddMember("minor_indices", minors, alloc); + } + + if (tsx.has_rsig_data()) + { + const auto &rd = tsx.rsig_data(); + rapidjson::Value rj(rapidjson::kObjectType); + if (rd.has_rsig_type()) + rj.AddMember("rsig_type", rd.rsig_type(), alloc); + if (rd.has_bp_version()) + rj.AddMember("bp_version", rd.bp_version(), alloc); + rapidjson::Value grp(rapidjson::kArrayType); + for (int i = 0; i < rd.grouping_size(); ++i) + grp.PushBack(static_cast(rd.grouping(i)), alloc); + rj.AddMember("grouping", grp, alloc); + tsx_data.AddMember("rsig_data", rj, alloc); + } + + if (tsx.integrated_indices_size() > 0) + { + rapidjson::Value ii(rapidjson::kArrayType); + for (int i = 0; i < tsx.integrated_indices_size(); ++i) + ii.PushBack(tsx.integrated_indices(i), alloc); + tsx_data.AddMember("integrated_indices", ii, alloc); + } + + if (tsx.has_client_version()) + tsx_data.AddMember("client_version", tsx.client_version(), alloc); + if (tsx.has_hard_fork()) + tsx_data.AddMember("hard_fork", tsx.hard_fork(), alloc); + if (tsx.has_monero_version()) + tsx_data.AddMember("monero_version", rapidjson::Value(tsx.monero_version().c_str(), static_cast(tsx.monero_version().size()), alloc), alloc); + + doc.AddMember("tsx_data", tsx_data, alloc); + + rapidjson::Value inputs(rapidjson::kArrayType); + const size_t n_in = utx->txes[tx_idx].sources.size(); + for (size_t i = 0; i < n_in; ++i) + { + messages::monero::MoneroTransactionSourceEntry src_pb; + signer.export_source_entry(&src_pb, i, true, true); + inputs.PushBack(source_entry_to_json(src_pb, alloc), alloc); + } + doc.AddMember("inputs", inputs, alloc); + + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + doc.Accept(writer); + return std::string(buffer.GetString(), buffer.GetSize()); + } + + namespace { + + std::string hex_to_bin(const std::string &hex) + { + std::string stripped = hex; + if (stripped.size() >= 2 && stripped[0] == '0' && (stripped[1] == 'x' || stripped[1] == 'X')) + stripped = stripped.substr(2); + std::string bin; + if (!epee::string_tools::parse_hexstr_to_binbuff(stripped, bin)) + throw std::invalid_argument("Invalid hex string"); + return bin; + } + + const rapidjson::Value & parse_connect_response(const std::string &response_json, rapidjson::Document &doc) + { + if (doc.Parse(response_json.c_str()).HasParseError()) + throw std::invalid_argument("Trezor Connect response: JSON parse error"); + if (!doc.IsObject()) + throw std::invalid_argument("Trezor Connect response: expected JSON object"); + + if (doc.HasMember("payload") && doc["payload"].IsObject()) { + if (doc.HasMember("success") && doc["success"].IsBool() && !doc["success"].GetBool()) + throw std::invalid_argument("Trezor Connect response: success is false"); + return doc["payload"]; + } + + return doc; + } + + std::string get_hex_field(const rapidjson::Value &obj, const char *name, bool required = true) + { + if (!obj.HasMember(name) || !obj[name].IsString()) { + if (required) + throw std::invalid_argument(std::string("Trezor Connect response: missing field ") + name); + return {}; + } + return hex_to_bin(std::string(obj[name].GetString(), obj[name].GetStringLength())); + } + + const rapidjson::Value * get_array_field(const rapidjson::Value &obj, const char *name) + { + if (!obj.HasMember(name) || !obj[name].IsArray()) + return nullptr; + return &obj[name]; + } + + void prepare_connect_signing_state( + Signer &signer, + const rapidjson::Value &payload, + const cryptonote::transaction &prefix_tx) + { + TData &ct = signer.tdata(); + signer.step_init(); + ct.tx = prefix_tx; + + CHECK_AND_ASSERT_THROW_MES( + payload.HasMember("tx_prefix_hash") && payload["tx_prefix_hash"].IsString(), + "Trezor Connect response: missing tx_prefix_hash"); + + ::crypto::hash computed{}; + cryptonote::get_transaction_prefix_hash(ct.tx, computed); + const std::string expected = hex_to_bin( + std::string(payload["tx_prefix_hash"].GetString(), payload["tx_prefix_hash"].GetStringLength())); + if (expected.size() != sizeof(computed) || + crypto_verify_32( + reinterpret_cast(computed.data), + reinterpret_cast(expected.data()))) { + throw exc::proto::SecurityException("Transaction prefix hash does not match"); + } + + ct.tx_prefix_hash = expected; + + ct.rv = std::make_shared(); + if (prefix_tx.version > 1) { + const auto &src_rct = prefix_tx.rct_signatures; + ct.rv->type = src_rct.type; + ct.rv->txnFee = src_rct.txnFee; + ct.rv->message = src_rct.message; + ct.rv->outPk = src_rct.outPk; + ct.rv->ecdhInfo = src_rct.ecdhInfo; + if (rct::is_rct_bulletproof_plus(src_rct.type)) + ct.rv->p.bulletproofs_plus = src_rct.p.bulletproofs_plus; + else if (rct::is_rct_bulletproof(src_rct.type)) + ct.rv->p.bulletproofs = src_rct.p.bulletproofs; + } + + if (payload.HasMember("rv") && payload["rv"].IsObject()) { + const auto &rvj = payload["rv"]; + if (rvj.HasMember("txn_fee") && rvj["txn_fee"].IsUint64()) + ct.rv->txnFee = rvj["txn_fee"].GetUint64(); + if (rvj.HasMember("rv_type") && rvj["rv_type"].IsUint()) + ct.rv->type = static_cast(rvj["rv_type"].GetUint()); + if (rvj.HasMember("message") && rvj["message"].IsString()) + string_to_key(ct.rv->message, hex_to_bin(std::string(rvj["message"].GetString(), rvj["message"].GetStringLength()))); + } + + if (payload.HasMember("extra") && payload["extra"].IsString()) { + const std::string extra_bin = hex_to_bin( + std::string(payload["extra"].GetString(), payload["extra"].GetStringLength())); + ct.tx.extra.assign(extra_bin.begin(), extra_bin.end()); + } + + const rapidjson::Value *pseudo_outs = get_array_field(payload, "pseudo_outs"); + if (pseudo_outs) { + ct.pseudo_outs.clear(); + ct.rv->p.pseudoOuts.clear(); + for (rapidjson::SizeType i = 0; i < pseudo_outs->Size(); ++i) { + if (!(*pseudo_outs)[i].IsString()) + throw std::invalid_argument("Trezor Connect response: invalid pseudo_out entry"); + const std::string po = hex_to_bin( + std::string((*pseudo_outs)[i].GetString(), (*pseudo_outs)[i].GetStringLength())); + ct.pseudo_outs.push_back(po); + rct::key k{}; + string_to_key(k, po); + ct.rv->p.pseudoOuts.push_back(k); + } + } + + ct.rv->mixRing.resize(signer.num_inputs()); + } + + tools::wallet2::pending_tx build_pending_tx_from_tdata(const TData &cdata) + { + tools::wallet2::pending_tx cpend; + cpend.tx = cdata.tx; + cpend.dust = 0; + cpend.fee = cpend.tx.rct_signatures.txnFee; + cpend.dust_added_to_fee = false; + cpend.change_dts = cdata.tx_data.change_dts; + cpend.selected_transfers = cdata.tx_data.selected_transfers; + cpend.key_images = ""; + cpend.dests = cdata.tx_data.dests; + cpend.construction_data = cdata.tx_data; + + std::string key_images; + const bool all_are_txin_to_key = std::all_of(cdata.tx.vin.begin(), cdata.tx.vin.end(), [&](const cryptonote::txin_v &s_e) -> bool { + CHECKED_GET_SPECIFIC_VARIANT(s_e, const cryptonote::txin_to_key, in, false); + key_images += boost::lexical_cast(in.k_image) + " "; + return true; + }); + if (!all_are_txin_to_key) + throw std::invalid_argument("Not all are txin_to_key"); + cpend.key_images = key_images; + + return cpend; + } + + void fill_key_images_from_signed_tx( + std::vector<::crypto::key_image> &key_images, + const TData &cdata, + const unsigned_tx_set &utx) + { + key_images.clear(); + key_images.resize(std::get<2>(utx.transfers).size()); + for (size_t cidx = 0; cidx < key_images.size(); ++cidx) + key_images[cidx] = std::get<2>(utx.transfers)[cidx].m_key_image; + + const size_t num_sources = cdata.tx_data.sources.size(); + CHECK_AND_ASSERT_THROW_MES(num_sources == cdata.tx.vin.size(), "Invalid tx.vin size"); + + for (size_t src_idx = 0; src_idx < num_sources; ++src_idx) { + CHECK_AND_ASSERT_THROW_MES(src_idx < cdata.tx_data.selected_transfers.size(), "Invalid source index"); + size_t idx_map_src = cdata.tx_data.selected_transfers[src_idx]; + CHECK_AND_ASSERT_THROW_MES(idx_map_src >= std::get<0>(utx.transfers), "Invalid offset"); + idx_map_src -= std::get<0>(utx.transfers); + CHECK_AND_ASSERT_THROW_MES(idx_map_src < key_images.size(), "Invalid key image index"); + + const auto vini = boost::get(cdata.tx.vin[src_idx]); + key_images[idx_map_src] = vini.k_image; + } + } + + } // namespace + + trezor_connect_signed_tx trezor_connect_monero_apply_sign_response( + wallet_shim *wallet, + const unsigned_tx_set *utx, + size_t tx_idx, + hw::tx_aux_data *aux_data, + const std::string &response_json, + const cryptonote::transaction *prefix_tx) + { + CHECK_AND_ASSERT_THROW_MES(wallet && utx && aux_data, "null argument"); + CHECK_AND_ASSERT_THROW_MES(prefix_tx, "Trezor Connect: call commitTrezor first (unsigned transaction missing)"); + CHECK_AND_ASSERT_THROW_MES(std::get<0>(utx->transfers) == 0, "Unsupported non zero offset"); + CHECK_AND_ASSERT_THROW_MES(tx_idx < utx->txes.size(), "Invalid transaction index"); + + rapidjson::Document doc; + const rapidjson::Value &payload = parse_connect_response(response_json, doc); + + Signer signer(wallet, utx, tx_idx, aux_data); + prepare_connect_signing_state(signer, payload, *prefix_tx); + + const size_t num_sources = signer.num_inputs(); + + const rapidjson::Value *signatures = get_array_field(payload, "signatures"); + if (!signatures) + throw std::invalid_argument("Trezor Connect response: missing signatures array"); + if (signatures->Size() != num_sources) + throw std::invalid_argument("Trezor Connect response: signatures count mismatch"); + + const rapidjson::Value *pseudo_outs = get_array_field(payload, "pseudo_outs"); + + for (rapidjson::SizeType i = 0; i < signatures->Size(); ++i) { + if (!(*signatures)[i].IsString()) + throw std::invalid_argument("Trezor Connect response: invalid signature entry"); + auto ack = std::make_shared(); + ack->set_signature(hex_to_bin( + std::string((*signatures)[i].GetString(), (*signatures)[i].GetStringLength()))); + if (pseudo_outs && i < pseudo_outs->Size() && (*pseudo_outs)[i].IsString()) + ack->set_pseudo_out(hex_to_bin( + std::string((*pseudo_outs)[i].GetString(), (*pseudo_outs)[i].GetStringLength()))); + signer.step_sign_input_ack(ack); + } + + auto final_ack = std::make_shared(); + final_ack->set_salt(get_hex_field(payload, "salt", false)); + final_ack->set_rand_mult(get_hex_field(payload, "rand_mult", false)); + final_ack->set_tx_enc_keys(get_hex_field(payload, "tx_enc_keys", false)); + final_ack->set_cout_key(get_hex_field(payload, "cout_key", false)); + final_ack->set_opening_key(get_hex_field(payload, "opening_key", true)); + + signer.step_final_ack(final_ack); + + trezor_connect_signed_tx result; + result.ptx = build_pending_tx_from_tdata(signer.tdata()); + result.tx_device_aux = signer.store_tx_aux_info(); + fill_key_images_from_signed_tx(result.key_images, signer.tdata(), *utx); + return result; + } + } } } diff --git a/src/device_trezor/trezor/protocol.hpp b/src/device_trezor/trezor/protocol.hpp index 7ffadd9aa..987c95eb5 100644 --- a/src/device_trezor/trezor/protocol.hpp +++ b/src/device_trezor/trezor/protocol.hpp @@ -340,8 +340,35 @@ namespace tx { const TData & tdata() const { return m_ct; } + + TData & tdata() { + return m_ct; + } + + void export_source_entry(MoneroTransactionSourceEntry *dst, size_t idx, bool need_ring_keys, bool need_ring_indices); }; + struct trezor_connect_signed_tx { + tools::wallet2::pending_tx ptx; + std::string tx_device_aux; + std::vector<::crypto::key_image> key_images; + }; + + std::string trezor_connect_monero_sign_transaction_to_json( + wallet_shim *wallet, + const unsigned_tx_set *utx, + size_t tx_idx, + hw::tx_aux_data *aux_data, + cryptonote::network_type network_type); + + trezor_connect_signed_tx trezor_connect_monero_apply_sign_response( + wallet_shim *wallet, + const unsigned_tx_set *utx, + size_t tx_idx, + hw::tx_aux_data *aux_data, + const std::string &response_json, + const cryptonote::transaction *prefix_tx); + // TX Key decryption void load_tx_key_data(hw::device_cold::tx_key_data_t & res, const std::string & data); diff --git a/src/wallet/api/pending_transaction.cpp b/src/wallet/api/pending_transaction.cpp index 1f714d229..548e24352 100644 --- a/src/wallet/api/pending_transaction.cpp +++ b/src/wallet/api/pending_transaction.cpp @@ -44,6 +44,11 @@ #include "bc-ur/src/bc-ur.hpp" +#if defined(DEVICE_TREZOR_READY) && DEVICE_TREZOR_READY +#include "device/device_cold.hpp" +#include "device_trezor/trezor/protocol.hpp" +#endif + using namespace std; namespace Monero { @@ -210,6 +215,144 @@ std::string PendingTransactionImpl::commitUR(int max_fragment_length) { } } +std::string PendingTransactionImpl::commitTrezor(uint64_t tx_index) +{ +#if !defined(DEVICE_TREZOR_READY) || !DEVICE_TREZOR_READY + (void)tx_index; + m_errorString = tr("This build was compiled without Trezor support"); + m_status = Status_Error; + return ""; +#else + if (tx_index >= m_pending_tx.size()) + { + m_errorString = tr("Invalid transaction index"); + m_status = Status_Error; + return ""; + } + try + { + tools::wallet2 *w = m_wallet.m_wallet.get(); + tools::wallet2::unsigned_tx_set utx; + w->construct_unsigned_tx_set_for_signing(m_pending_tx, utx); + if (std::get<0>(utx.transfers) != 0) + { + m_errorString = tr("Unsupported unsigned transaction transfer offset"); + m_status = Status_Error; + return ""; + } + hw::tx_aux_data aux_data; + const int bpv = w->use_fork_rules(HF_VERSION_BULLETPROOF_PLUS, -10) ? 4 + : (w->use_fork_rules(HF_VERSION_CLSAG, -10) ? 3 + : (w->use_fork_rules(HF_VERSION_SMALLER_BP, -10) ? 2 : 1)); + aux_data.bp_version = bpv; + aux_data.hard_fork = w->get_current_hard_fork(); + aux_data.client_version = static_cast(bpv >= 4 ? 4u : 3u); + + hw::wallet_shim shim; + shim.get_tx_pub_key_from_received_outs = std::bind(&tools::wallet2::get_tx_pub_key_from_received_outs, w, std::placeholders::_1); + + const std::string json = hw::trezor::protocol::tx::trezor_connect_monero_sign_transaction_to_json( + &shim, + &utx, + static_cast(tx_index), + &aux_data, + w->nettype()); + + m_trezor_connect_session = std::make_unique(); + m_trezor_connect_session->utx = std::move(utx); + m_trezor_connect_session->aux = aux_data; + m_trezor_connect_session->shim = shim; + m_trezor_connect_session->prefix_tx = m_pending_tx[tx_index].tx; + m_trezor_connect_session->tx_idx = tx_index; + + m_errorString.clear(); + m_status = Status_Ok; + return json; + } + catch (const std::exception &e) + { + m_errorString = e.what(); + m_status = Status_Error; + return ""; + } +#endif +} + +bool PendingTransactionImpl::commitTrezorNext(const std::string &response_json, uint64_t tx_index) +{ +#if !defined(DEVICE_TREZOR_READY) || !DEVICE_TREZOR_READY + (void)response_json; + (void)tx_index; + m_errorString = tr("This build was compiled without Trezor support"); + m_status = Status_Error; + return false; +#else + if (tx_index >= m_pending_tx.size()) + { + m_errorString = tr("Invalid transaction index"); + m_status = Status_Error; + return false; + } + try + { + tools::wallet2 *w = m_wallet.m_wallet.get(); + std::unique_ptr session = std::move(m_trezor_connect_session); + std::unique_ptr fallback_session; + + if (!session || session->tx_idx != tx_index) + { + fallback_session = std::make_unique(); + session = std::move(fallback_session); + w->construct_unsigned_tx_set_for_signing(m_pending_tx, session->utx); + if (std::get<0>(session->utx.transfers) != 0) + { + m_errorString = tr("Unsupported unsigned transaction transfer offset"); + m_status = Status_Error; + return false; + } + const int bpv = w->use_fork_rules(HF_VERSION_BULLETPROOF_PLUS, -10) ? 4 + : (w->use_fork_rules(HF_VERSION_CLSAG, -10) ? 3 + : (w->use_fork_rules(HF_VERSION_SMALLER_BP, -10) ? 2 : 1)); + session->aux.bp_version = bpv; + session->aux.hard_fork = w->get_current_hard_fork(); + session->aux.client_version = static_cast(bpv >= 4 ? 4u : 3u); + session->shim.get_tx_pub_key_from_received_outs = std::bind( + &tools::wallet2::get_tx_pub_key_from_received_outs, w, std::placeholders::_1); + session->prefix_tx = m_pending_tx[tx_index].tx; + session->tx_idx = tx_index; + } + + const auto signed_tx = hw::trezor::protocol::tx::trezor_connect_monero_apply_sign_response( + &session->shim, + &session->utx, + static_cast(tx_index), + &session->aux, + response_json, + &session->prefix_tx); + + m_pending_tx[tx_index] = signed_tx.ptx; + m_key_images = signed_tx.key_images; + + if (!signed_tx.tx_device_aux.empty()) + { + if (m_tx_device_aux.size() <= tx_index) + m_tx_device_aux.resize(tx_index + 1); + m_tx_device_aux[tx_index] = signed_tx.tx_device_aux; + } + + m_errorString.clear(); + m_status = Status_Ok; + return commit(); + } + catch (const std::exception &e) + { + m_errorString = e.what(); + m_status = Status_Error; + return false; + } +#endif +} + uint64_t PendingTransactionImpl::amount() const { diff --git a/src/wallet/api/pending_transaction.h b/src/wallet/api/pending_transaction.h index 0cc6c58e9..32a7a296f 100644 --- a/src/wallet/api/pending_transaction.h +++ b/src/wallet/api/pending_transaction.h @@ -31,9 +31,14 @@ #include "wallet/api/wallet2_api.h" #include "wallet/wallet2.h" +#include #include #include +#if defined(DEVICE_TREZOR_READY) && DEVICE_TREZOR_READY +#include "device/device_cold.hpp" +#endif + namespace Monero { @@ -47,6 +52,8 @@ public: std::string errorString() const override; bool commit(const std::string &filename = "", bool overwrite = false) override; std::string commitUR(int max_fragment_length = 130) override; + std::string commitTrezor(uint64_t tx_index = 0) override; + bool commitTrezorNext(const std::string &response_json, uint64_t tx_index = 0) override; uint64_t amount() const override; uint64_t dust() const override; uint64_t fee() const override; @@ -72,6 +79,17 @@ private: std::unordered_set m_signers; std::vector m_tx_device_aux; std::vector m_key_images; + +#if defined(DEVICE_TREZOR_READY) && DEVICE_TREZOR_READY + struct TrezorConnectSession { + tools::wallet2::unsigned_tx_set utx; + hw::tx_aux_data aux; + hw::wallet_shim shim; + cryptonote::transaction prefix_tx; + size_t tx_idx{0}; + }; + std::unique_ptr m_trezor_connect_session; +#endif }; diff --git a/src/wallet/api/unsigned_transaction.cpp b/src/wallet/api/unsigned_transaction.cpp index fd03e959d..7232e518f 100644 --- a/src/wallet/api/unsigned_transaction.cpp +++ b/src/wallet/api/unsigned_transaction.cpp @@ -34,11 +34,13 @@ #include "cryptonote_basic/cryptonote_format_utils.h" #include "cryptonote_basic/cryptonote_basic_impl.h" +#include "cryptonote_config.h" #include #include #include #include +#include #include "bc-ur/src/bc-ur.hpp" diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index c24b4a97d..cc85398ca 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -1545,7 +1545,7 @@ bool WalletImpl::importKeyImages(const string &filename) return false; } - return true; + return true; } @@ -3479,4 +3479,33 @@ std::string WalletImpl::serializeCacheToJson() const return std::string(m_wallet->serialize_cache_to_json()); } +std::string WalletImpl::exportTrezorTdis() const +{ + return m_wallet->export_trezor_tdis(); +} + +bool WalletImpl::importTrezorEncryptedKeyImagesJson(const string &json) +{ + if (checkBackgroundSync("cannot import key images")) + return false; + if (!trustedDaemon()) { + setStatusError(tr("Key images can only be imported with a trusted daemon")); + return false; + } + try + { + uint64_t spent = 0, unspent = 0; + uint64_t height = m_wallet->import_trezor_encrypted_key_images_json(json, spent, unspent); + LOG_PRINT_L2("Trezor encrypted key images imported to height " << height << ", " + << print_money(spent) << " spent, " << print_money(unspent) << " unspent"); + } + catch (const std::exception &e) + { + LOG_ERROR("Error importing Trezor encrypted key images: " << e.what()); + setStatusError(string(tr("Failed to import key images: ")) + e.what()); + return false; + } + return true; +} + } // namespace diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 98c03b9c1..5248badb8 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -337,6 +337,8 @@ private: bool getWaitsForDeviceReceive(); virtual std::string serializeCacheToJson() const override; + virtual std::string exportTrezorTdis() const override; + bool importTrezorEncryptedKeyImagesJson(const std::string &json) override; }; diff --git a/src/wallet/api/wallet2_api.h b/src/wallet/api/wallet2_api.h index 3d11929f9..760fc8859 100644 --- a/src/wallet/api/wallet2_api.h +++ b/src/wallet/api/wallet2_api.h @@ -92,6 +92,8 @@ struct PendingTransaction // commit transaction or save to file if filename is provided. virtual bool commit(const std::string &filename = "", bool overwrite = false) = 0; virtual std::string commitUR(int max_fragment_length = 130) = 0; + virtual std::string commitTrezor(uint64_t tx_index = 0) = 0; + virtual bool commitTrezorNext(const std::string &response_json, uint64_t tx_index = 0) = 0; virtual uint64_t amount() const = 0; virtual uint64_t dust() const = 0; virtual uint64_t fee() const = 0; @@ -1220,6 +1222,19 @@ struct Wallet //! serialize wallet cache to JSON virtual std::string serializeCacheToJson() const = 0; + + + /*! + * \brief exportTrezorTdis — export transfer details for Trezor cold key-image sync as JSON + * + * Returns an object `{ "tdis": [ ... ] }` with hex pubkeys and indices per output. + */ + virtual std::string exportTrezorTdis() const = 0; + + /*! + * \brief importTrezorEncryptedKeyImagesJson — import key images from Trezor-style encrypted JSON + */ + virtual bool importTrezorEncryptedKeyImagesJson(const std::string &json) = 0; }; /** diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a7532d7ec..bb0ada04d 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -98,6 +98,7 @@ extern "C" { #include "crypto/keccak.h" #include "crypto/crypto-ops.h" +#include } using namespace std; using namespace crypto; @@ -7838,6 +7839,20 @@ std::string wallet2::dump_tx_to_str(const std::vector &ptx_vector) c return std::string(UNSIGNED_TX_PREFIX) + ciphertext; } //---------------------------------------------------------------------------------------------------- +void wallet2::construct_unsigned_tx_set_for_signing(const std::vector& ptx_vector, unsigned_tx_set &utx) const +{ + utx.txes.clear(); + utx.txes.reserve(ptx_vector.size()); + for (const auto &tx : ptx_vector) + utx.txes.push_back(get_construction_data_with_decrypted_short_payment_id(tx, m_account.get_device())); + + utx.new_transfers = std::make_tuple(static_cast(0), static_cast(0), std::vector()); + + transfer_container transfers_copy; + get_transfers(transfers_copy); + utx.transfers = std::make_tuple(static_cast(0), static_cast(transfers_copy.size()), std::move(transfers_copy)); +} +//---------------------------------------------------------------------------------------------------- bool wallet2::load_unsigned_tx(const std::string &unsigned_filename, unsigned_tx_set &exported_txs) const { std::string s; @@ -16384,4 +16399,147 @@ std::pair wallet2::estimate_tx_size_and_weight(bool use_rct, i return std::make_pair(size, weight); } //---------------------------------------------------------------------------------------------------- +// Returns JSON: { "tdis": [ { "out_key", "tx_pub_key", optional "additional_tx_pub_keys", indices }, ... ] } +// Hex fields are 64-char lower-case pubkeys; additional_tx_pub_keys matches protocol::ki::key_image_data +// (subset of txn additional pubkeys for this transfer — currently 0 or 1 entry). +std::string wallet2::export_trezor_tdis() const +{ + rapidjson::Document doc; + doc.SetObject(); + auto &alloc = doc.GetAllocator(); + + rapidjson::Value tdis(rapidjson::kArrayType); + tdis.Reserve(static_cast(m_transfers.size()), alloc); + + for (const auto &td : m_transfers) + { + const crypto::public_key out_key = td.get_public_key(); + const crypto::public_key tx_pub_key = get_tx_pub_key_from_received_outs(td); + const std::vector additional_tx_pub_keys = cryptonote::get_additional_tx_pub_keys_from_extra(td.m_tx); + + std::vector additional_for_tdi; + if (!additional_tx_pub_keys.empty() && additional_tx_pub_keys.size() > td.m_internal_output_index) + additional_for_tdi.push_back(additional_tx_pub_keys[td.m_internal_output_index]); + + rapidjson::Value obj(rapidjson::kObjectType); + const std::string out_hex = epee::string_tools::pod_to_hex(out_key); + const std::string tx_pub_hex = epee::string_tools::pod_to_hex(tx_pub_key); + obj.AddMember("out_key", rapidjson::Value(out_hex.c_str(), static_cast(out_hex.size()), alloc), alloc); + obj.AddMember("tx_pub_key", rapidjson::Value(tx_pub_hex.c_str(), static_cast(tx_pub_hex.size()), alloc), alloc); + if (!additional_for_tdi.empty()) + { + rapidjson::Value aux(rapidjson::kArrayType); + aux.Reserve(static_cast(additional_for_tdi.size()), alloc); + for (const auto &apk : additional_for_tdi) + { + const std::string ah = epee::string_tools::pod_to_hex(apk); + aux.PushBack(rapidjson::Value(ah.c_str(), static_cast(ah.size()), alloc), alloc); + } + obj.AddMember("additional_tx_pub_keys", aux, alloc); + } + obj.AddMember("internal_output_index", rapidjson::Value(static_cast(td.m_internal_output_index)), alloc); + obj.AddMember("sub_addr_major", rapidjson::Value(td.m_subaddr_index.major), alloc); + obj.AddMember("sub_addr_minor", rapidjson::Value(td.m_subaddr_index.minor), alloc); + tdis.PushBack(obj, alloc); + } + + doc.AddMember("tdis", tdis, alloc); + + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + doc.Accept(writer); + return std::string(buffer.GetString(), buffer.GetSize()); +} +//---------------------------------------------------------------------------------------------------- +namespace +{ + static void decrypt_trezor_exported_ki_blob(const std::string &cipher, const std::string &iv, const std::string &key32, crypto::key_image &ki, crypto::signature &sig) + { + THROW_WALLET_EXCEPTION_IF(iv.size() != crypto_aead_chacha20poly1305_ietf_NPUBBYTES, + error::wallet_internal_error, "Trezor KI JSON: IV must be 12 bytes"); + THROW_WALLET_EXCEPTION_IF(key32.size() != 32, + error::wallet_internal_error, "Trezor KI JSON: encryption key must be 32 bytes"); + THROW_WALLET_EXCEPTION_IF(cipher.size() < crypto_aead_chacha20poly1305_ietf_ABYTES, + error::wallet_internal_error, "Trezor KI JSON: key_image ciphertext too short"); + char buf[96]; + unsigned long long out_len = 0; + const int r = crypto_aead_chacha20poly1305_ietf_decrypt( + reinterpret_cast(buf), &out_len, nullptr, + reinterpret_cast(cipher.data()), cipher.size(), + nullptr, 0, + reinterpret_cast(iv.data()), + reinterpret_cast(key32.data())); + THROW_WALLET_EXCEPTION_IF(r != 0, + error::wallet_internal_error, "Trezor KI JSON: decryption failed (wrong key or corrupt ciphertext)"); + THROW_WALLET_EXCEPTION_IF(out_len != 96, + error::wallet_internal_error, "Trezor KI JSON: unexpected plaintext length"); + memcpy(ki.data, buf, 32); + memcpy(sig.c.data, buf + 32, 32); + memcpy(sig.r.data, buf + 64, 32); + memwipe(buf, sizeof(buf)); + } +} + +//---------------------------------------------------------------------------------------------------- +uint64_t wallet2::import_trezor_encrypted_key_images_json(const std::string &json, uint64_t &spent, uint64_t &unspent, bool check_spent) +{ + rapidjson::Document doc; + doc.Parse(json.c_str()); + THROW_WALLET_EXCEPTION_IF(doc.HasParseError(), error::wallet_internal_error, + std::string("Trezor KI JSON: parse error at offset ") + std::to_string(doc.GetErrorOffset())); + + const rapidjson::Value *proot = &doc; + if (doc.IsObject() && doc.HasMember("payload") && doc["payload"].IsObject()) + proot = &doc["payload"]; + + THROW_WALLET_EXCEPTION_IF(!proot->IsObject(), error::wallet_internal_error, "Trezor KI JSON: expected JSON object"); + const rapidjson::Value &root = *proot; + + THROW_WALLET_EXCEPTION_IF(!root.HasMember("key_images") || !root["key_images"].IsArray(), + error::wallet_internal_error, "Trezor KI JSON: missing or invalid key_images array"); + + std::string enc_key_bin; + if (root.HasMember("signature") && root["signature"].IsString()) + { + THROW_WALLET_EXCEPTION_IF(!epee::string_tools::parse_hexstr_to_binbuff(root["signature"].GetString(), enc_key_bin), + error::wallet_internal_error, "Trezor KI JSON: invalid hex in signature"); + } + else if (root.HasMember("enc_key") && root["enc_key"].IsString()) + { + THROW_WALLET_EXCEPTION_IF(!epee::string_tools::parse_hexstr_to_binbuff(root["enc_key"].GetString(), enc_key_bin), + error::wallet_internal_error, "Trezor KI JSON: invalid hex in enc_key"); + } + else + { + THROW_WALLET_EXCEPTION(error::wallet_internal_error, "Trezor KI JSON: need signature or enc_key (32-byte hex)"); + } + THROW_WALLET_EXCEPTION_IF(enc_key_bin.size() != 32, + error::wallet_internal_error, "Trezor KI JSON: encryption key must decode to 32 bytes"); + + const rapidjson::Value &arr = root["key_images"]; + std::vector> ski; + ski.reserve(arr.Size()); + + for (rapidjson::SizeType i = 0; i < arr.Size(); ++i) + { + const rapidjson::Value &el = arr[i]; + THROW_WALLET_EXCEPTION_IF(!el.IsObject(), error::wallet_internal_error, "Trezor KI JSON: key_images entry must be object"); + THROW_WALLET_EXCEPTION_IF(!el.HasMember("iv") || !el["iv"].IsString(), error::wallet_internal_error, "Trezor KI JSON: missing iv"); + THROW_WALLET_EXCEPTION_IF(!el.HasMember("key_image") || !el["key_image"].IsString(), error::wallet_internal_error, "Trezor KI JSON: missing key_image"); + + std::string iv_bin, blob_bin; + THROW_WALLET_EXCEPTION_IF(!epee::string_tools::parse_hexstr_to_binbuff(el["iv"].GetString(), iv_bin), + error::wallet_internal_error, "Trezor KI JSON: invalid hex in iv"); + THROW_WALLET_EXCEPTION_IF(!epee::string_tools::parse_hexstr_to_binbuff(el["key_image"].GetString(), blob_bin), + error::wallet_internal_error, "Trezor KI JSON: invalid hex in key_image"); + + crypto::key_image ki{}; + crypto::signature sig{}; + decrypt_trezor_exported_ki_blob(blob_bin, iv_bin, enc_key_bin, ki, sig); + ski.emplace_back(std::move(ki), std::move(sig)); + } + + return import_key_images(ski, 0, spent, unspent, check_spent); +} +//---------------------------------------------------------------------------------------------------- } diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 37a2447d2..a1ca49e90 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -1207,6 +1207,8 @@ private: void commit_tx(std::vector& ptx_vector); bool save_tx(const std::vector& ptx_vector, const std::string &filename) const; std::string dump_tx_to_str(const std::vector &ptx_vector) const; + //! Populate \p utx from pending txs for cold/device signing (uses live transfer indices). + void construct_unsigned_tx_set_for_signing(const std::vector& ptx_vector, unsigned_tx_set &utx) const; std::string save_multisig_tx(multisig_tx_set txs); bool save_multisig_tx(const multisig_tx_set &txs, const std::string &filename); std::string save_multisig_tx(const std::vector& ptx_vector); @@ -1717,7 +1719,8 @@ private: bool is_unattended() const { return m_unattended; } std::pair estimate_tx_size_and_weight(bool use_rct, int n_inputs, int ring_size, int n_outputs, size_t extra_size); - + std::string export_trezor_tdis() const; + uint64_t import_trezor_encrypted_key_images_json(const std::string &json, uint64_t &spent, uint64_t &unspent, bool check_spent = true); bool get_rpc_payment_info(bool mining, bool &payment_required, uint64_t &credits, uint64_t &diff, uint64_t &credits_per_hash_found, cryptonote::blobdata &hashing_blob, uint64_t &height, uint64_t &seed_height, crypto::hash &seed_hash, crypto::hash &next_seed_hash, uint32_t &cookie); bool daemon_requires_payment(); bool make_rpc_payment(uint32_t nonce, uint32_t cookie, uint64_t &credits, uint64_t &balance); -- 2.50.1 (Apple Git-155)