Skip to content

Commit 921d282

Browse files
committed
Removed 'legacy_blinding' flag from convo info volatile functions
1 parent 551a48b commit 921d282

File tree

4 files changed

+64
-72
lines changed

4 files changed

+64
-72
lines changed

include/session/config/convo_info_volatile.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -368,23 +368,20 @@ LIBSESSION_EXPORT bool convo_info_volatile_get_or_construct_legacy_group(
368368
/// [in] config_object* conf,
369369
/// [out] convo_info_volatile_blinded_1to1* convo,
370370
/// [in] const char* blinded_session_id
371-
/// [in] bool legacy_blinding
372371
/// );
373372
/// ```
374373
///
375374
/// Inputs:
376375
/// - `conf` -- [in] Pointer to the config object
377376
/// - `convo` -- [out] Pointer to conversation info
378377
/// - `blinded_session_id` -- [in] Null terminated hex string of the session_id
379-
/// - `legacy_blinding` -- flag indicating whether this blinded contact should use legacy blinding
380378
///
381379
/// Outputs:
382380
/// - `bool` - Returns true if the conversation exists
383381
LIBSESSION_EXPORT bool convo_info_volatile_get_blinded_1to1(
384382
config_object* conf,
385383
convo_info_volatile_blinded_1to1* convo,
386-
const char* blinded_session_id,
387-
bool legacy_blinding) LIBSESSION_WARN_UNUSED;
384+
const char* blinded_session_id) LIBSESSION_WARN_UNUSED;
388385

389386
/// API: convo_info_volatile/convo_info_volatile_get_or_construct_blinded_1to1
390387
///
@@ -405,23 +402,20 @@ LIBSESSION_EXPORT bool convo_info_volatile_get_blinded_1to1(
405402
/// [in] config_object* conf,
406403
/// [out] convo_info_volatile_blinded_1to1* convo,
407404
/// [in] const char* blinded_session_id
408-
/// [in] bool legacy_blinding
409405
/// );
410406
/// ```
411407
///
412408
/// Inputs:
413409
/// - `conf` -- [in] Pointer to the config object
414410
/// - `convo` -- [out] Pointer to conversation info
415411
/// - `blinded_session_id` -- [in] Null terminated hex string of the blinded session id
416-
/// - `legacy_blinding` -- flag indicating whether this blinded contact should use legacy blinding
417412
///
418413
/// Outputs:
419414
/// - `bool` - Returns true if the conversation exists
420415
LIBSESSION_EXPORT bool convo_info_volatile_get_or_construct_blinded_1to1(
421416
config_object* conf,
422417
convo_info_volatile_blinded_1to1* convo,
423-
const char* blinded_session_id,
424-
bool legacy_blinding) LIBSESSION_WARN_UNUSED;
418+
const char* blinded_session_id) LIBSESSION_WARN_UNUSED;
425419

426420
/// API: convo_info_volatile/convo_info_volatile_set_1to1
427421
///
@@ -630,19 +624,17 @@ LIBSESSION_EXPORT bool convo_info_volatile_erase_legacy_group(
630624
/// BOOL convo_info_volatile_erase_blinded_1to1(
631625
/// [in] config_object* conf,
632626
/// [in] const char* blinded_session_id
633-
/// [in] bool legacy_blinding
634627
/// );
635628
/// ```
636629
///
637630
/// Inputs:
638631
/// - `conf` -- [in] Pointer to the config object
639632
/// - `blinded_session_id` -- [in] Null terminated hex string
640-
/// - `legacy_blinding` -- flag indicating whether the blinded contact used legacy blinding
641633
///
642634
/// Outputs:
643635
/// - `bool` - Returns true if conversation was found and removed
644636
LIBSESSION_EXPORT bool convo_info_volatile_erase_blinded_1to1(
645-
config_object* conf, const char* blinded_session_id, bool legacy_blinding);
637+
config_object* conf, const char* blinded_session_id);
646638

647639
/// API: convo_info_volatile/convo_info_volatile_size
648640
///

include/session/config/convo_info_volatile.hpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ namespace convo {
174174
///
175175
/// Inputs:
176176
/// - `blinded_session_id` -- Hex string of the blinded session id
177-
/// - `legacy_blinding` -- flag indicating whether this blinded contact should use legacy
178-
/// blinding
179-
explicit blinded_one_to_one(std::string&& blinded_session_id, bool legacy_blinding);
180-
explicit blinded_one_to_one(std::string_view blinded_session_id, bool legacy_blinding);
177+
explicit blinded_one_to_one(std::string&& blinded_session_id);
178+
explicit blinded_one_to_one(std::string_view blinded_session_id);
181179

182180
// Internal ctor/method for C API implementations:
183181
blinded_one_to_one(const struct convo_info_volatile_blinded_1to1& c); // From c struct
@@ -341,13 +339,11 @@ class ConvoInfoVolatile : public ConfigBase {
341339
///
342340
/// Inputs:
343341
/// - `blinded_session_id` -- Hex string of the blinded Session ID
344-
/// - `legacy_blinding` -- flag indicating whether this blinded contact should use legacy
345-
/// blinding
346342
///
347343
/// Outputs:
348344
/// - `std::optional<convo::blinded_one_to_one>` - Returns a contact
349345
std::optional<convo::blinded_one_to_one> get_blinded_1to1(
350-
std::string_view blinded_session_id, bool legacy_blinding) const;
346+
std::string_view blinded_session_id) const;
351347

352348
/// API: convo_info_volatile/ConvoInfoVolatile::get_or_construct_1to1
353349
///
@@ -444,13 +440,11 @@ class ConvoInfoVolatile : public ConfigBase {
444440
///
445441
/// Inputs:
446442
/// - `blinded_session_id` -- Hex string blinded Session ID
447-
/// - `legacy_blinding` -- flag indicating whether this blinded contact should use legacy
448-
/// blinding
449443
///
450444
/// Outputs:
451445
/// - `convo::blinded_one_to_one` - Returns a blinded contact
452446
convo::blinded_one_to_one get_or_construct_blinded_1to1(
453-
std::string_view blinded_session_id, bool legacy_blinding) const;
447+
std::string_view blinded_session_id) const;
454448

455449
/// API: convo_info_volatile/ConvoInfoVolatile::set
456450
///
@@ -545,11 +539,10 @@ class ConvoInfoVolatile : public ConfigBase {
545539
///
546540
/// Inputs:
547541
/// - `pubkey` -- hex blinded session id
548-
/// - `legacy_blinding` -- flag indicating whether this blinded contact is using legacy blinding
549542
///
550543
/// Outputs:
551544
/// - `bool` - Returns true if found and removed, otherwise false
552-
bool erase_blinded_1to1(std::string_view pubkey, bool legacy_blinding);
545+
bool erase_blinded_1to1(std::string_view pubkey);
553546

554547
/// API: convo_info_volatile/ConvoInfoVolatile::erase
555548
///

src/config/convo_info_volatile.cpp

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,23 @@ namespace convo {
8282
c.unread = unread;
8383
}
8484

85-
blinded_one_to_one::blinded_one_to_one(std::string&& sid, bool legacy_blinding) :
86-
blinded_session_id{std::move(sid)}, legacy_blinding{legacy_blinding} {
87-
check_session_id(blinded_session_id, legacy_blinding ? "15" : "25");
85+
blinded_one_to_one::blinded_one_to_one(std::string&& sid) : blinded_session_id{std::move(sid)} {
86+
auto prefix = get_session_id_prefix(blinded_session_id);
87+
legacy_blinding = (prefix == session::SessionIDPrefix::community_blinded_legacy);
88+
89+
if (prefix != session::SessionIDPrefix::community_blinded &&
90+
prefix != session::SessionIDPrefix::community_blinded_legacy)
91+
throw std::invalid_argument{
92+
"Invalid blinded ID: Expected '15' or '25' prefix; got " + blinded_session_id};
8893
}
89-
blinded_one_to_one::blinded_one_to_one(std::string_view sid, bool legacy_blinding) :
90-
blinded_session_id{sid}, legacy_blinding{legacy_blinding} {
91-
check_session_id(blinded_session_id, legacy_blinding ? "15" : "25");
94+
blinded_one_to_one::blinded_one_to_one(std::string_view sid) : blinded_session_id{sid} {
95+
auto prefix = get_session_id_prefix(blinded_session_id);
96+
legacy_blinding = (prefix == session::SessionIDPrefix::community_blinded_legacy);
97+
98+
if (prefix != session::SessionIDPrefix::community_blinded &&
99+
prefix != session::SessionIDPrefix::community_blinded_legacy)
100+
throw std::invalid_argument{
101+
"Invalid blinded ID: Expected '15' or '25' prefix; got " + blinded_session_id};
92102
}
93103
blinded_one_to_one::blinded_one_to_one(const convo_info_volatile_blinded_1to1& c) :
94104
base{c.last_read, c.unread},
@@ -234,25 +244,31 @@ convo::legacy_group ConvoInfoVolatile::get_or_construct_legacy_group(
234244
}
235245

236246
std::optional<convo::blinded_one_to_one> ConvoInfoVolatile::get_blinded_1to1(
237-
std::string_view pubkey_hex, bool legacy_blinding) const {
238-
std::string pubkey = session_id_to_bytes(pubkey_hex, legacy_blinding ? "15" : "25");
247+
std::string_view pubkey_hex) const {
248+
auto prefix = get_session_id_prefix(pubkey_hex);
249+
250+
if (prefix != session::SessionIDPrefix::community_blinded &&
251+
prefix != session::SessionIDPrefix::community_blinded_legacy)
252+
throw std::invalid_argument{
253+
"Invalid blinded ID: Expected '15' or '25' prefix; got " + std::string{pubkey_hex}};
254+
255+
std::string pubkey = session_id_to_bytes(pubkey_hex, to_string(prefix));
239256

240257
auto* info_dict = data["b"][pubkey].dict();
241258
if (!info_dict)
242259
return std::nullopt;
243260

244-
auto result =
245-
std::make_optional<convo::blinded_one_to_one>(std::string{pubkey_hex}, legacy_blinding);
261+
auto result = std::make_optional<convo::blinded_one_to_one>(std::string{pubkey_hex});
246262
result->load(*info_dict);
247263
return result;
248264
}
249265

250266
convo::blinded_one_to_one ConvoInfoVolatile::get_or_construct_blinded_1to1(
251-
std::string_view pubkey_hex, bool legacy_blinding) const {
252-
if (auto maybe = get_blinded_1to1(pubkey_hex, legacy_blinding))
267+
std::string_view pubkey_hex) const {
268+
if (auto maybe = get_blinded_1to1(pubkey_hex))
253269
return *std::move(maybe);
254270

255-
return convo::blinded_one_to_one{std::string{pubkey_hex}, legacy_blinding};
271+
return convo::blinded_one_to_one{std::string{pubkey_hex}};
256272
}
257273

258274
void ConvoInfoVolatile::set(const convo::one_to_one& c) {
@@ -386,9 +402,8 @@ bool ConvoInfoVolatile::erase_group(std::string_view id) {
386402
bool ConvoInfoVolatile::erase_legacy_group(std::string_view id) {
387403
return erase(convo::legacy_group{id});
388404
}
389-
bool ConvoInfoVolatile::erase_blinded_1to1(
390-
std::string_view blinded_session_id, bool legacy_blinding) {
391-
return erase(convo::blinded_one_to_one{blinded_session_id, legacy_blinding});
405+
bool ConvoInfoVolatile::erase_blinded_1to1(std::string_view blinded_session_id) {
406+
return erase(convo::blinded_one_to_one{blinded_session_id});
392407
}
393408

394409
size_t ConvoInfoVolatile::size_1to1() const {
@@ -489,11 +504,7 @@ class val_loader {
489504

490505
if (k.size() == 33 && (k[0] == prefix || (legacy_prefix && k[0] == *legacy_prefix))) {
491506
if (auto* info_dict = std::get_if<dict>(&v)) {
492-
if constexpr (std::is_same_v<ConvoType, convo::blinded_one_to_one>)
493-
val = std::make_shared<convo::any>(ConvoType{
494-
oxenc::to_hex(k), (legacy_prefix && k[0] == *legacy_prefix)});
495-
else
496-
val = std::make_shared<convo::any>(ConvoType{oxenc::to_hex(k)});
507+
val = std::make_shared<convo::any>(ConvoType{oxenc::to_hex(k)});
497508
std::get<ConvoType>(*val).load(*info_dict);
498509
return true;
499510
}
@@ -693,13 +704,11 @@ LIBSESSION_C_API bool convo_info_volatile_get_or_construct_legacy_group(
693704
LIBSESSION_C_API bool convo_info_volatile_get_blinded_1to1(
694705
config_object* conf,
695706
convo_info_volatile_blinded_1to1* convo,
696-
const char* blinded_session_id,
697-
bool legacy_blinding) {
707+
const char* blinded_session_id) {
698708
return wrap_exceptions(
699709
conf,
700710
[&] {
701-
if (auto c = unbox<ConvoInfoVolatile>(conf)->get_blinded_1to1(
702-
blinded_session_id, legacy_blinding)) {
711+
if (auto c = unbox<ConvoInfoVolatile>(conf)->get_blinded_1to1(blinded_session_id)) {
703712
c->into(*convo);
704713
return true;
705714
}
@@ -711,13 +720,12 @@ LIBSESSION_C_API bool convo_info_volatile_get_blinded_1to1(
711720
LIBSESSION_C_API bool convo_info_volatile_get_or_construct_blinded_1to1(
712721
config_object* conf,
713722
convo_info_volatile_blinded_1to1* convo,
714-
const char* blinded_session_id,
715-
bool legacy_blinding) {
723+
const char* blinded_session_id) {
716724
return wrap_exceptions(
717725
conf,
718726
[&] {
719727
unbox<ConvoInfoVolatile>(conf)
720-
->get_or_construct_blinded_1to1(blinded_session_id, legacy_blinding)
728+
->get_or_construct_blinded_1to1(blinded_session_id)
721729
.into(*convo);
722730
return true;
723731
},
@@ -799,13 +807,10 @@ LIBSESSION_C_API bool convo_info_volatile_erase_legacy_group(
799807
false);
800808
}
801809
LIBSESSION_C_API bool convo_info_volatile_erase_blinded_1to1(
802-
config_object* conf, const char* blinded_session_id, bool legacy_blinding) {
810+
config_object* conf, const char* blinded_session_id) {
803811
return wrap_exceptions(
804812
conf,
805-
[&] {
806-
return unbox<ConvoInfoVolatile>(conf)->erase_blinded_1to1(
807-
blinded_session_id, legacy_blinding);
808-
},
813+
[&] { return unbox<ConvoInfoVolatile>(conf)->erase_blinded_1to1(blinded_session_id); },
809814
false);
810815
}
811816

tests/test_config_convo_info_volatile.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ TEST_CASE("Conversations", "[config][conversations]") {
9595
g.unread = true;
9696
convos.set(g);
9797

98-
CHECK_FALSE(convos.get_blinded_1to1(legacy_blinded_id, true));
99-
CHECK_FALSE(convos.get_blinded_1to1(blinded_id, false));
98+
CHECK_FALSE(convos.get_blinded_1to1(legacy_blinded_id));
99+
CHECK_FALSE(convos.get_blinded_1to1(blinded_id));
100100

101-
auto lb = convos.get_or_construct_blinded_1to1(legacy_blinded_id, true);
101+
auto lb = convos.get_or_construct_blinded_1to1(legacy_blinded_id);
102102
CHECK(lb.blinded_session_id == legacy_blinded_id);
103103
CHECK(lb.last_read == 0);
104104
CHECK_FALSE(lb.unread);
@@ -107,7 +107,7 @@ TEST_CASE("Conversations", "[config][conversations]") {
107107
lb.unread = true;
108108
convos.set(lb);
109109

110-
auto b = convos.get_or_construct_blinded_1to1(blinded_id, false);
110+
auto b = convos.get_or_construct_blinded_1to1(blinded_id);
111111
CHECK(b.blinded_session_id == blinded_id);
112112
CHECK(b.last_read == 0);
113113
CHECK_FALSE(b.unread);
@@ -153,14 +153,14 @@ TEST_CASE("Conversations", "[config][conversations]") {
153153
CHECK(x3->last_read == now_ms);
154154
CHECK(x3->unread);
155155

156-
auto x4 = convos2.get_blinded_1to1(legacy_blinded_id, true);
156+
auto x4 = convos2.get_blinded_1to1(legacy_blinded_id);
157157
REQUIRE(x4);
158158
CHECK(x4->blinded_session_id ==
159159
"150000000000000000000000000000000000101010111010000110100001210000");
160160
CHECK(x4->last_read == now_ms);
161161
CHECK(x4->unread);
162162

163-
auto x5 = convos2.get_blinded_1to1(blinded_id, false);
163+
auto x5 = convos2.get_blinded_1to1(blinded_id);
164164
REQUIRE(x5);
165165
CHECK(x5->blinded_session_id ==
166166
"255000000000000000000000000000000000101010111010000110100001210000");
@@ -178,7 +178,7 @@ TEST_CASE("Conversations", "[config][conversations]") {
178178
convos2.set(c3);
179179

180180
auto c4 = convos2.get_or_construct_blinded_1to1(
181-
"2512345ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", false);
181+
"2512345ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc");
182182
c4.unread = true;
183183
convos2.set(c4);
184184

@@ -250,8 +250,7 @@ TEST_CASE("Conversations", "[config][conversations]") {
250250
convos.erase_1to1("052000000000000000000000000000000000000000000000000000000000000000");
251251
CHECK_FALSE(convos.needs_push());
252252
convos.erase_1to1("055000000000000000000000000000000000000000000000000000000000000000");
253-
convos.erase_blinded_1to1(
254-
"255000000000000000000000000000000000101010111010000110100001210000", false);
253+
convos.erase_blinded_1to1("255000000000000000000000000000000000101010111010000110100001210000");
255254
CHECK(convos.needs_push());
256255
CHECK(convos.size() == 6);
257256
CHECK(convos.size_1to1() == 1);
@@ -287,6 +286,12 @@ TEST_CASE("Conversations", "[config][conversations]") {
287286
"150000000000000000000000000000000000101010111010000110100001210000",
288287
"2512345ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
289288
});
289+
290+
// Ensure that we throw correctly when giving invalid blinded ids
291+
auto invalid_id_1 = "072222222222222222222222222222222222222222222222222222222222222222"sv;
292+
auto invalid_id_2 = "992222222222222222222222222222222222222222222222222222222222222222"sv;
293+
CHECK_THROWS(convos.get_or_construct_blinded_1to1(invalid_id_1));
294+
CHECK_THROWS(convos.get_or_construct_blinded_1to1(invalid_id_2));
290295
}
291296

292297
TEST_CASE("Conversations (C API)", "[config][conversations][c]") {
@@ -382,8 +387,8 @@ TEST_CASE("Conversations (C API)", "[config][conversations][c]") {
382387
const char* const blinded_id =
383388
"150000000000000000000000000000000000101010111010000110100001210000";
384389
convo_info_volatile_blinded_1to1 b1;
385-
REQUIRE_FALSE(convo_info_volatile_get_blinded_1to1(conf, &b1, blinded_id, true));
386-
REQUIRE(convo_info_volatile_get_or_construct_blinded_1to1(conf, &b1, blinded_id, true));
390+
REQUIRE_FALSE(convo_info_volatile_get_blinded_1to1(conf, &b1, blinded_id));
391+
REQUIRE(convo_info_volatile_get_or_construct_blinded_1to1(conf, &b1, blinded_id));
387392
b1.last_read = now_ms;
388393
convo_info_volatile_set_blinded_1to1(conf, &b1);
389394

@@ -438,10 +443,7 @@ TEST_CASE("Conversations (C API)", "[config][conversations][c]") {
438443

439444
convo_info_volatile_blinded_1to1 b2;
440445
REQUIRE(convo_info_volatile_get_or_construct_blinded_1to1(
441-
conf2,
442-
&b2,
443-
"2512345ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
444-
false));
446+
conf2, &b2, "2512345ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"));
445447
b2.unread = true;
446448
convo_info_volatile_set_blinded_1to1(conf2, &b2);
447449
CHECK(config_needs_push(conf2));
@@ -515,7 +517,7 @@ TEST_CASE("Conversations (C API)", "[config][conversations][c]") {
515517
convo_info_volatile_erase_1to1(
516518
conf, "055000000000000000000000000000000000000000000000000000000000000000");
517519
convo_info_volatile_erase_blinded_1to1(
518-
conf, "2512345ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", false);
520+
conf, "2512345ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc");
519521
CHECK(config_needs_push(conf));
520522
CHECK(convo_info_volatile_size(conf) == 4);
521523
CHECK(convo_info_volatile_size_1to1(conf) == 1);

0 commit comments

Comments
 (0)