1010#include < variant>
1111
1212#include " internal.hpp"
13+ #include " session/blinding.hpp"
1314#include " session/config/contacts.h"
1415#include " session/config/error.h"
1516#include " session/export.h"
@@ -308,12 +309,16 @@ size_t Contacts::size() const {
308309blinded_contact_info::blinded_contact_info (
309310 std::string_view community_base_url,
310311 std::span<const unsigned char > community_pubkey,
311- std::string_view blinded_id,
312- bool legacy_blinding) :
312+ std::string_view blinded_id) :
313313 comm{community (
314- std::move (community_base_url), blinded_id.substr (2 ), std::move (community_pubkey))},
315- legacy_blinding{legacy_blinding} {
316- check_session_id (blinded_id, legacy_blinding ? " 15" : " 25" );
314+ std::move (community_base_url), blinded_id.substr (2 ), std::move (community_pubkey))} {
315+ auto prefix = get_session_id_prefix (blinded_id);
316+ legacy_blinding = (prefix == session::SessionIDPrefix::community_blinded_legacy);
317+
318+ if (prefix != session::SessionIDPrefix::community_blinded &&
319+ prefix != session::SessionIDPrefix::community_blinded_legacy)
320+ throw std::invalid_argument{
321+ " Invalid blinded ID: Expected '15' or '25' prefix; got " + std::string{blinded_id}};
317322}
318323
319324void blinded_contact_info::load (const dict& info_dict) {
@@ -378,6 +383,10 @@ void blinded_contact_info::set_base_url(std::string_view base_url) {
378383}
379384
380385void blinded_contact_info::set_room (std::string_view room) {
386+ if (room.size () != 64 || !oxenc::is_hex (room))
387+ throw std::invalid_argument{
388+ fmt::format (" Invalid room: expected 64 hex digits; got {}" , room)};
389+
381390 comm.set_room (room);
382391}
383392
@@ -403,9 +412,8 @@ ConfigBase::DictFieldProxy Contacts::blinded_contact_field(
403412
404413using any_blinded_contact = std::variant<blinded_contact_info>;
405414
406- std::optional<blinded_contact_info> Contacts::get_blinded (
407- std::string_view blinded_id_hex, bool legacy_blinding) const {
408- check_session_id (blinded_id_hex, legacy_blinding ? " 15" : " 25" );
415+ std::optional<blinded_contact_info> Contacts::get_blinded (std::string_view blinded_id_hex) const {
416+ get_session_id_prefix (blinded_id_hex);
409417
410418 if (auto * b = data[" b" ].dict ()) {
411419 auto comm = comm_iterator_helper{b->begin (), b->end ()};
@@ -426,16 +434,12 @@ std::optional<blinded_contact_info> Contacts::get_blinded(
426434blinded_contact_info Contacts::get_or_construct_blinded (
427435 std::string_view community_base_url,
428436 std::string_view community_pubkey_hex,
429- std::string_view blinded_id_hex,
430- bool legacy_blinding) {
431- if (auto maybe = get_blinded (blinded_id_hex, legacy_blinding))
437+ std::string_view blinded_id_hex) {
438+ if (auto maybe = get_blinded (blinded_id_hex))
432439 return *std::move (maybe);
433440
434441 return blinded_contact_info{
435- community_base_url,
436- to_span (oxenc::from_hex (community_pubkey_hex)),
437- blinded_id_hex,
438- legacy_blinding};
442+ community_base_url, to_span (oxenc::from_hex (community_pubkey_hex)), blinded_id_hex};
439443}
440444
441445std::vector<blinded_contact_info> Contacts::blinded () const {
@@ -475,9 +479,13 @@ void Contacts::set_blinded(const blinded_contact_info& bc) {
475479 set_ts (info[" j" ], bc.created );
476480}
477481
478- bool Contacts::erase_blinded (
479- std::string_view base_url_, std::string_view blinded_id, bool legacy_blinding) {
480- check_session_id (blinded_id, legacy_blinding ? " 15" : " 25" );
482+ bool Contacts::erase_blinded (std::string_view base_url_, std::string_view blinded_id) {
483+ auto prefix = get_session_id_prefix (blinded_id);
484+
485+ if (prefix != session::SessionIDPrefix::community_blinded &&
486+ prefix != session::SessionIDPrefix::community_blinded_legacy)
487+ throw std::invalid_argument{
488+ " Invalid blinded ID: Expected '15' or '25' prefix; got " + std::string{blinded_id}};
481489
482490 auto base_url = community::canonical_url (base_url_);
483491 auto pk = std::string (blinded_id.substr (2 ));
@@ -588,14 +596,11 @@ LIBSESSION_C_API size_t contacts_size(const config_object* conf) {
588596}
589597
590598LIBSESSION_C_API bool contacts_get_blinded (
591- config_object* conf,
592- const char * blinded_id,
593- bool legacy_blinding,
594- contacts_blinded_contact* blinded_contact) {
599+ config_object* conf, const char * blinded_id, contacts_blinded_contact* blinded_contact) {
595600 return wrap_exceptions (
596601 conf,
597602 [&] {
598- if (auto bc = unbox<Contacts>(conf)->get_blinded (blinded_id, legacy_blinding )) {
603+ if (auto bc = unbox<Contacts>(conf)->get_blinded (blinded_id)) {
599604 bc->into (*blinded_contact);
600605 return true ;
601606 }
@@ -609,17 +614,13 @@ LIBSESSION_C_API bool contacts_get_or_construct_blinded(
609614 const char * community_base_url,
610615 const char * community_pubkey_hex,
611616 const char * blinded_id,
612- bool legacy_blinding,
613617 contacts_blinded_contact* blinded_contact) {
614618 return wrap_exceptions (
615619 conf,
616620 [&] {
617621 unbox<Contacts>(conf)
618622 ->get_or_construct_blinded (
619- community_base_url,
620- community_pubkey_hex,
621- blinded_id,
622- legacy_blinding)
623+ community_base_url, community_pubkey_hex, blinded_id)
623624 .into (*blinded_contact);
624625 return true ;
625626 },
@@ -682,13 +683,9 @@ LIBSESSION_C_API bool contacts_set_blinded(
682683}
683684
684685LIBSESSION_C_API bool contacts_erase_blinded (
685- config_object* conf,
686- const char * community_base_url,
687- const char * blinded_id,
688- bool legacy_blinding) {
686+ config_object* conf, const char * community_base_url, const char * blinded_id) {
689687 try {
690- return unbox<Contacts>(conf)->erase_blinded (
691- community_base_url, blinded_id, legacy_blinding);
688+ return unbox<Contacts>(conf)->erase_blinded (community_base_url, blinded_id);
692689 } catch (...) {
693690 return false ;
694691 }
0 commit comments