From 961b895622aa4cd3ea0b70495debab773fc0f05e Mon Sep 17 00:00:00 2001 From: SpyCheese Date: Tue, 27 Jun 2023 09:22:09 +0300 Subject: [PATCH] New tag for encrypted messages --- crypto/smc-envelope/WalletInterface.cpp | 2 +- tonlib/tonlib/TonlibClient.cpp | 42 +++++++++++++------------ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/crypto/smc-envelope/WalletInterface.cpp b/crypto/smc-envelope/WalletInterface.cpp index 4c2feca91..418bc4a48 100644 --- a/crypto/smc-envelope/WalletInterface.cpp +++ b/crypto/smc-envelope/WalletInterface.cpp @@ -68,7 +68,7 @@ void WalletInterface::store_gift_message(vm::CellBuilder &cb, const Gift &gift) } if (gift.is_encrypted) { - cb.store_long(1, 32); + cb.store_long(0x2167da4b, 32); } else { cb.store_long(0, 32); } diff --git a/tonlib/tonlib/TonlibClient.cpp b/tonlib/tonlib/TonlibClient.cpp index 069ba576a..1e7ef384b 100644 --- a/tonlib/tonlib/TonlibClient.cpp +++ b/tonlib/tonlib/TonlibClient.cpp @@ -2888,29 +2888,31 @@ struct ToRawTransactions { auto get_data = [body = std::move(body), body_cell = std::move(body_cell), init_state_cell = std::move(init_state_cell), this](td::Slice salt) mutable { tonlib_api::object_ptr data; - if (try_decode_messages_ && body->size() >= 32 && static_cast(body->prefetch_long(32)) <= 1) { - auto type = body.write().fetch_long(32); - td::Status status; + if (try_decode_messages_ && body->size() >= 32) { + auto type = static_cast(body.write().fetch_long(32)); + if (type == 0 || type == 0x2167da4b) { + td::Status status; - auto r_body_message = vm::CellString::load(body.write()); - LOG_IF(WARNING, r_body_message.is_error()) << "Failed to parse a message: " << r_body_message.error(); + auto r_body_message = vm::CellString::load(body.write()); + LOG_IF(WARNING, r_body_message.is_error()) << "Failed to parse a message: " << r_body_message.error(); - if (r_body_message.is_ok()) { - if (type == 0) { - data = tonlib_api::make_object(r_body_message.move_as_ok()); - } else { - auto encrypted_message = r_body_message.move_as_ok(); - auto r_decrypted_message = [&]() -> td::Result { - if (!private_key_) { - return TonlibError::EmptyField("private_key"); - } - TRY_RESULT(decrypted, SimpleEncryptionV2::decrypt_data(encrypted_message, private_key_.value(), salt)); - return decrypted.data.as_slice().str(); - }(); - if (r_decrypted_message.is_ok()) { - data = tonlib_api::make_object(r_decrypted_message.move_as_ok()); + if (r_body_message.is_ok()) { + if (type == 0) { + data = tonlib_api::make_object(r_body_message.move_as_ok()); } else { - data = tonlib_api::make_object(encrypted_message); + auto encrypted_message = r_body_message.move_as_ok(); + auto r_decrypted_message = [&]() -> td::Result { + if (!private_key_) { + return TonlibError::EmptyField("private_key"); + } + TRY_RESULT(decrypted, SimpleEncryptionV2::decrypt_data(encrypted_message, private_key_.value(), salt)); + return decrypted.data.as_slice().str(); + }(); + if (r_decrypted_message.is_ok()) { + data = tonlib_api::make_object(r_decrypted_message.move_as_ok()); + } else { + data = tonlib_api::make_object(encrypted_message); + } } } }