Skip to content

Commit cd58ab3

Browse files
mpretty-cyrojagerman
authored andcommitted
Added unit tests for blinded contacts, bug fixes and cleanup
1 parent b42905c commit cd58ab3

5 files changed

Lines changed: 562 additions & 134 deletions

File tree

include/session/config/contacts.h

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ LIBSESSION_EXPORT size_t contacts_size(const config_object* conf);
251251
/// Outputs:
252252
/// - `contacts_blinded_contact_list*` -- pointer to the list of blinded contact structs; the
253253
/// pointer belongs to the caller and must be freed when done with it.
254-
LIBSESSION_EXPORT contacts_blinded_contact_list* contacts_blinded_contacts(
255-
const config_object* conf);
254+
LIBSESSION_EXPORT contacts_blinded_contact_list* contacts_blinded(const config_object* conf);
256255

257256
/// API: contacts/contacts_get_blinded_contact
258257
///
@@ -264,27 +263,68 @@ LIBSESSION_EXPORT contacts_blinded_contact_list* contacts_blinded_contacts(
264263
/// ```cpp
265264
/// BOOL contacts_get_blinded_contact(
266265
/// [in] config_object* conf,
267-
/// [in] const char* blinded_session_id,
266+
/// [in] const char* blinded_id,
268267
/// [in] bool legacy_blinding,
269268
/// [out] contacts_blinded_contact* blinded_contact
270269
/// );
271270
/// ```
272271
///
273272
/// Inputs:
274273
/// - `conf` -- [in] Pointer to the config object
275-
/// - `blinded_session_id` -- [in] null terminated hex string
274+
/// - `blinded_id` -- [in] null terminated hex string
276275
/// - `legacy_blinding` -- [in] null terminated hex string
277276
/// - `blinded_contact` -- [out] the blinded contact info data
278277
///
279278
/// Output:
280279
/// - `bool` -- Returns true if blinded contact exists
281-
LIBSESSION_EXPORT bool contacts_get_blinded_contact(
280+
LIBSESSION_EXPORT bool contacts_get_blinded(
282281
config_object* conf,
283-
const char* blinded_session_id,
282+
const char* blinded_id,
284283
bool legacy_blinding,
285284
contacts_blinded_contact* blinded_contact) LIBSESSION_WARN_UNUSED;
286285

287-
/// API: contacts/contacts_set_blinded_contact
286+
/// API: contacts/contacts_get_or_construct_blinded
287+
///
288+
/// Same as the above `contacts_get_blinded()` except that when the blinded contact does not exist,
289+
/// this sets all the contact fields to defaults and loads it with the given blinded_id.
290+
///
291+
/// Returns true as long as it is given a valid blinded_id. A false return is considered an error,
292+
/// and means the blinded_id was not a valid blinded_id.
293+
///
294+
/// This is the method that should usually be used to create or update a blinded contact, followed
295+
/// by setting fields in the blinded contact, and then giving it to contacts_set_blinded().
296+
///
297+
/// Declaration:
298+
/// ```cpp
299+
/// BOOL contacts_get_or_construct_blinded(
300+
/// [in] config_object* conf,
301+
/// [in] const char* community_base_url,
302+
/// [in] const char* community_pubkey_hex,
303+
/// [in] const char* blinded_id,
304+
/// [in] bool legacy_blinding,
305+
/// [out] contacts_blinded_contact* blinded_contact
306+
/// );
307+
/// ```
308+
///
309+
/// Inputs:
310+
/// - `conf` -- [in] Pointer to the config object
311+
/// - `community_base_url` -- [in] null terminated string
312+
/// - `community_pubkey_hex` -- [in] null terminated hex string
313+
/// - `blinded_id` -- [in] null terminated hex string
314+
/// - `legacy_blinding` -- [in] null terminated hex string
315+
/// - `blinded_contact` -- [out] the blinded contact info data
316+
///
317+
/// Output:
318+
/// - `bool` -- Returns true if contact exsts
319+
LIBSESSION_EXPORT bool contacts_get_or_construct_blinded(
320+
config_object* conf,
321+
const char* community_base_url,
322+
const char* community_pubkey_hex,
323+
const char* blinded_id,
324+
bool legacy_blinding,
325+
contacts_blinded_contact* blinded_contact) LIBSESSION_WARN_UNUSED;
326+
327+
/// API: contacts/contacts_set_blinded
288328
///
289329
/// Adds or updates a blinded contact from the given contact info struct.
290330
///
@@ -302,19 +342,19 @@ LIBSESSION_EXPORT bool contacts_get_blinded_contact(
302342
///
303343
/// Output:
304344
/// - `bool` -- Returns true if the call succeeds, false if an error occurs.
305-
LIBSESSION_EXPORT bool contacts_set_blinded_contact(
345+
LIBSESSION_EXPORT bool contacts_set_blinded(
306346
config_object* conf, const contacts_blinded_contact* bc);
307347

308-
/// API: contacts/contacts_erase_blinded_contact
348+
/// API: contacts/contacts_erase_blinded
309349
///
310350
/// Erases a blinded contact from the blinded contact list. blinded_id is in hex. Returns true if
311351
/// the blinded contact was found and removed, false if the blinded contact was not present.
312352
///
313353
/// Declaration:
314354
/// ```cpp
315-
/// BOOL contacts_erase_blinded_contact(
355+
/// BOOL contacts_erase_blinded(
316356
/// [in, out] config_object* conf,
317-
/// [in] const char* base_url,
357+
/// [in] const char* community_base_url,
318358
/// [in] const char* blinded_id,
319359
/// [in] bool legacy_blinding
320360
/// );
@@ -330,7 +370,10 @@ LIBSESSION_EXPORT bool contacts_set_blinded_contact(
330370
/// Outputs:
331371
/// - `bool` -- True if erasing was successful
332372
LIBSESSION_EXPORT bool contacts_erase_blinded_contact(
333-
config_object* conf, const char* base_url, const char* blinded_id, bool legacy_blinding);
373+
config_object* conf,
374+
const char* community_base_url,
375+
const char* blinded_id,
376+
bool legacy_blinding);
334377

335378
typedef struct contacts_iterator {
336379
void* _internals;

include/session/config/contacts.hpp

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ struct blinded_contact_info {
129129

130130
blinded_contact_info() = default;
131131
explicit blinded_contact_info(
132-
std::string_view base_url,
132+
std::string_view community_base_url,
133+
std::span<const unsigned char> community_pubkey,
133134
std::string_view blinded_id,
134-
std::span<const unsigned char> pubkey,
135135
bool legacy_blinding);
136136

137137
// Internal ctor/method for C API implementations:
@@ -427,48 +427,74 @@ class Contacts : public ConfigBase {
427427
std::span<const unsigned char>* get_pubkey = nullptr) const;
428428

429429
public:
430-
/// API: contacts/Contacts::blinded_contacts
430+
/// API: contacts/Contacts::blinded
431431
///
432432
/// Retrieves a list of all known blinded contacts.
433433
///
434434
/// Inputs: None
435435
///
436436
/// Outputs:
437437
/// - `std::vector<blinded_contact_info>` - Returns a list of blinded_contact_info
438-
std::vector<blinded_contact_info> blinded_contacts() const;
438+
std::vector<blinded_contact_info> blinded() const;
439439

440440
/// API: contacts/Contacts::get_blinded
441441
///
442442
/// Looks up and returns a blinded contact by blinded session ID (hex). Returns nullopt if the
443443
/// blinded session ID was not found, otherwise returns a filled out `blinded_contact_info`.
444444
///
445445
/// Inputs:
446-
/// - `pubkey_hex` -- hex string of the session id
446+
/// - `blinded_id_hex` -- hex string of the session id
447447
/// - `legacy_blinding` -- flag indicating whether the pubkey is using legacy blinding
448448
///
449449
/// Outputs:
450450
/// - `std::optional<blinded_contact_info>` - Returns nullopt if blinded session ID was not
451451
/// found, otherwise a filled out blinded_contact_info
452452
std::optional<blinded_contact_info> get_blinded(
453-
std::string_view pubkey_hex, bool legacy_blinding) const;
453+
std::string_view blinded_id_hex, bool legacy_blinding) const;
454+
455+
/// API: contacts/Contacts::get_or_construct_blinded
456+
///
457+
/// Similar to get_blinded(), but if the blinded ID does not exist this returns a filled-out
458+
/// blinded_contact_info containing the blinded_id, community info and legacy_blinded flag (all
459+
/// other fields will be empty/defaulted). This is intended to be combined with `set_blinded`
460+
/// to set-or-create a record.
461+
///
462+
/// NB: calling this does *not* add the blinded id to the blinded list when called: that
463+
/// requires also calling `set_blinded` with this value.
464+
///
465+
/// Inputs:
466+
/// - `community_base_url` -- String of the base URL for the community this blinded id
467+
/// originates from
468+
/// - `community_pubkey_hex` -- Hex string of the public key for the community this blinded id
469+
/// originates from
470+
/// - `blinded_id_hex` -- hex string of the blinded id
471+
/// - `legacy_blinding` -- flag indicating whether the pubkey is using legacy blinding
472+
///
473+
/// Outputs:
474+
/// - `blinded_contact_info` - Returns a filled out blinded_contact_info
475+
blinded_contact_info get_or_construct_blinded(
476+
std::string_view community_base_url,
477+
std::string_view community_pubkey_hex,
478+
std::string_view blinded_id_hex,
479+
bool legacy_blinding);
454480

455-
/// API: contacts/contacts::set_blinded_contact
481+
/// API: contacts/contacts::set_blinded
456482
///
457483
/// Sets or updates multiple blinded contact info values at once with the given info. The usual
458484
/// use is to access the current info, change anything desired, then pass it back into
459-
/// set_blinded_contact, e.g.:
485+
/// set_blinded, e.g.:
460486
///
461487
///```cpp
462488
/// auto c = contacts.get_blinded(pubkey, legacy_blinding);
463489
/// c.name = "Session User 42";
464-
/// contacts.set_blinded_contact(c);
490+
/// contacts.set_blinded(c);
465491
///```
466492
///
467493
/// Inputs:
468-
/// - `bc` -- set_blinded_contact value to set
469-
bool set_blinded_contact(const blinded_contact_info& bc);
494+
/// - `bc` -- set_blinded value to set
495+
void set_blinded(const blinded_contact_info& bc);
470496

471-
/// API: contacts/contacts::erase_blinded_contact
497+
/// API: contacts/contacts::erase_blinded
472498
///
473499
/// Removes a blinded contact, if present. Returns true if it was found and removed, false
474500
/// otherwise. Note that this removes all fields related to a blinded contact, even fields we do
@@ -481,7 +507,7 @@ class Contacts : public ConfigBase {
481507
///
482508
/// Outputs:
483509
/// - `bool` - Returns true if contact was found and removed, false otherwise
484-
bool erase_blinded_contact(
510+
bool erase_blinded(
485511
std::string_view base_url, std::string_view blinded_id, bool legacy_blinding);
486512

487513
struct iterator;

0 commit comments

Comments
 (0)