ssb: Work in progress invite support. We can generate them. We can connect using an invite code. We can't yet invite.use().

This commit is contained in:
2025-01-19 16:00:37 -05:00
parent 11564a5292
commit fd09a766d2
9 changed files with 388 additions and 37 deletions

View File

@ -9,6 +9,8 @@
#include "ow-crypt.h"
#include "sodium/crypto_hash_sha256.h"
#include "sodium/crypto_sign.h"
#include "sodium/crypto_sign_ed25519.h"
#include "sodium/randombytes.h"
#include "sqlite3.h"
#include "uv.h"
@ -168,6 +170,14 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
_tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS identities_user ON identities (user, public_key)");
_tf_ssb_db_exec(db, "DELETE FROM identities WHERE user = ':auth'");
_tf_ssb_db_exec(db,
"CREATE TABLE IF NOT EXISTS invites ("
" invite_public_key TEXT PRIMARY KEY,"
" account TEXT,"
" use_count INTEGER,"
" expires INTEGER"
")");
bool populate_fts = false;
if (!_tf_ssb_db_has_rows(db, "PRAGMA table_list('messages_fts')"))
{
@ -2137,3 +2147,44 @@ const char* tf_ssb_db_get_profile_name(sqlite3* db, const char* id)
}
return result;
}
bool tf_ssb_db_generate_invite(sqlite3* db, const char* id, const char* host, int port, int use_count, int expires_seconds, char* out_invite, size_t size)
{
if (use_count < -1 || use_count == 0)
{
return false;
}
uint8_t public_key[crypto_sign_ed25519_PUBLICKEYBYTES] = { 0 };
uint8_t secret_key[crypto_sign_ed25519_SECRETKEYBYTES] = { 0 };
uint8_t seed[crypto_sign_ed25519_SEEDBYTES] = { 0 };
randombytes_buf(seed, sizeof(seed));
crypto_sign_ed25519_seed_keypair(public_key, secret_key, seed);
char public[k_id_base64_len];
tf_ssb_id_bin_to_str(public, sizeof(public), public_key);
tf_printf("invite is for public key %s\n", public);
char seed_b64[64];
tf_base64_encode(seed, sizeof(seed), seed_b64, sizeof(seed_b64));
bool inserted = false;
sqlite3_stmt* statement;
if (sqlite3_prepare(db,
"INSERT INTO invites (invite_public_key, account, use_count, expires) VALUES (?, ?, ?, ?)",
-1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, public, -1, NULL) == SQLITE_OK &&
sqlite3_bind_text(statement, 2, id, -1, NULL) == SQLITE_OK &&
sqlite3_bind_int(statement, 3, use_count) == SQLITE_OK &&
sqlite3_bind_int64(statement, 4, (int64_t)time(NULL) + expires_seconds) == SQLITE_OK)
{
inserted = sqlite3_step(statement) == SQLITE_DONE;
}
sqlite3_finalize(statement);
}
snprintf(out_invite, size, "%s:%d:%s~%s", host, port, id, seed_b64);
return inserted;
}