Skip to content

Commit

Permalink
New tag for encrypted messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SpyCheese committed Jun 27, 2023
1 parent 9c67f80 commit 13c540f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crypto/smc-envelope/WalletInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
47 changes: 24 additions & 23 deletions tonlib/tonlib/TonlibClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2197,30 +2197,31 @@ struct ToRawTransactions {

auto get_data = [body = std::move(body), body_cell, this](td::Slice salt) mutable {
tonlib_api::object_ptr<tonlib_api::msg_Data> data;
if (body->size() >= 32 && static_cast<td::uint32>(body->prefetch_long(32)) <= 1) {
auto type = body.write().fetch_long(32);
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();

if (r_body_message.is_ok()) {
if (type == 0) {
data = tonlib_api::make_object<tonlib_api::msg_dataText>(r_body_message.move_as_ok());
} else {
LOG(ERROR) << "TRY DECRYPT";
auto encrypted_message = r_body_message.move_as_ok();
auto r_decrypted_message = [&]() -> td::Result<std::string> {
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<tonlib_api::msg_dataDecryptedText>(r_decrypted_message.move_as_ok());
if (body->size() >= 32) {
auto type = static_cast<td::uint32>(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();

if (r_body_message.is_ok()) {
if (type == 0) {
data = tonlib_api::make_object<tonlib_api::msg_dataText>(r_body_message.move_as_ok());
} else {
data = tonlib_api::make_object<tonlib_api::msg_dataEncryptedText>(encrypted_message);
auto encrypted_message = r_body_message.move_as_ok();
auto r_decrypted_message = [&]() -> td::Result<std::string> {
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<tonlib_api::msg_dataDecryptedText>(r_decrypted_message.move_as_ok());
} else {
data = tonlib_api::make_object<tonlib_api::msg_dataEncryptedText>(encrypted_message);
}
}
}
}
Expand Down

0 comments on commit 13c540f

Please sign in to comment.