From 18128303b67ceef12ee8d624e9709398e3fa7fd7 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 13 Jul 2023 00:20:12 +0000 Subject: [PATCH] Appending a message produces the ID. And bump the version. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4344 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- Makefile | 6 +++--- src/android/AndroidManifest.xml | 4 ++-- src/ssb.c | 11 ++++++++++- src/ssb.h | 2 +- src/ssb.js.c | 4 +++- src/ssb.tests.c | 14 ++++++++------ src/version.h | 4 ++-- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index e893d6a6..204929b7 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,9 @@ MAKEFLAGS += --warn-undefined-variables MAKEFLAGS += --no-builtin-rules -VERSION_CODE := 8 -VERSION_NUMBER := 0.0.8 -VERSION_NAME := The secret ingredient is love. +VERSION_CODE := 9 +VERSION_NUMBER := 0.0.9-wip +VERSION_NAME := Failure is the only opportunity to begin again. PROJECT = tildefriends BUILD_DIR ?= out diff --git a/src/android/AndroidManifest.xml b/src/android/AndroidManifest.xml index 029683f3..46361df4 100644 --- a/src/android/AndroidManifest.xml +++ b/src/android/AndroidManifest.xml @@ -1,8 +1,8 @@ + versionCode="9" + versionName="0.0.9"> diff --git a/src/ssb.c b/src/ssb.c index a6da193c..9059139d 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -1666,7 +1666,7 @@ static bool _tf_ssb_connection_box_stream_recv(tf_ssb_connection_t* connection) return true; } -void tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message) +bool tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message, char* out_id, size_t out_id_size) { char previous_id[crypto_hash_sha256_BYTES * 2]; int64_t previous_sequence = 0; @@ -1718,12 +1718,15 @@ void tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const ui JS_FreeCString(context, json); JS_FreeValue(context, jsonval); + bool stored = false; char id[sodium_base64_ENCODED_LEN(crypto_hash_sha256_BYTES, sodium_base64_VARIANT_ORIGINAL) + 7 + 1]; if (valid && tf_ssb_verify_and_strip_signature(ssb->context, root, id, sizeof(id), NULL, 0, NULL)) { if (tf_ssb_db_store_message(ssb, ssb->context, id, root, signature_base64, false)) { tf_ssb_notify_message_added(ssb, id); + snprintf(out_id, out_id_size, "%s", id); + stored = true; } else { @@ -1735,7 +1738,13 @@ void tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const ui tf_printf("Failed to verify message signature.\n"); } + if (!stored && out_id && out_id_size) + { + *out_id = '\0'; + } + JS_FreeValue(context, root); + return stored; } static void _tf_ssb_connection_dispatch_scheduled(tf_ssb_connection_t* connection) diff --git a/src/ssb.h b/src/ssb.h index 569f8855..034b6db0 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -87,7 +87,7 @@ JSContext* tf_ssb_get_context(tf_ssb_t* ssb); void tf_ssb_broadcast_listener_start(tf_ssb_t* ssb, bool linger); void tf_ssb_broadcast_sender_start(tf_ssb_t* ssb); void tf_ssb_run(tf_ssb_t* ssb); -void tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message); +bool tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message, char* out_id, size_t out_id_size); bool tf_ssb_whoami(tf_ssb_t* ssb, char* out_id, size_t out_id_size); void tf_ssb_visit_broadcasts(tf_ssb_t* ssb, void (*callback)(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data), void* user_data); diff --git a/src/ssb.js.c b/src/ssb.js.c index c7b12e2a..5c98cf7a 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -118,7 +118,9 @@ static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueCons uint8_t private_key[crypto_sign_SECRETKEYBYTES]; if (tf_ssb_db_identity_get_private_key(ssb, user, id, private_key, sizeof(private_key))) { - tf_ssb_append_message_with_keys(ssb, id, private_key, argv[2]); + char id[k_id_base64_len] = { 0 }; + tf_ssb_append_message_with_keys(ssb, id, private_key, argv[2], id, sizeof(id)); + result = JS_NewString(context, id); } else { diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 8881df31..df127c53 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -170,17 +170,19 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) b = tf_ssb_db_blob_store(ssb0, (const uint8_t*)k_blob, strlen(k_blob), blob_id, sizeof(blob_id), NULL); assert(b); + char message_id[k_id_base64_len] = { 0 }; JSContext* context0 = tf_ssb_get_context(ssb0); JSValue obj = JS_NewObject(context0); JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post")); JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Hello, world!")); - tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj); + tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, message_id, sizeof(message_id)); JS_FreeValue(context0, obj); + printf("appended %s\n", message_id); obj = JS_NewObject(context0); JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post")); JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post.")); - tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj); + tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0); JS_FreeValue(context0, obj); obj = JS_NewObject(context0); @@ -191,7 +193,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) JS_SetPropertyStr(context0, mention, "link", JS_NewString(context0, blob_id)); JS_SetPropertyUint32(context0, mentions, 0, mention); JS_SetPropertyStr(context0, obj, "mentions", mentions); - tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj); + tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0); JS_FreeValue(context0, obj); uint8_t* b0; @@ -247,7 +249,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) obj = JS_NewObject(context0); JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post")); JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Message to self.")); - tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj); + tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0); JS_FreeValue(context0, obj); while (count0 == 0) @@ -497,7 +499,7 @@ void tf_ssb_test_following(const tf_test_options_t* options) JS_SetPropertyStr(context, message, "type", JS_NewString(context, "contact")); \ JS_SetPropertyStr(context, message, "contact", JS_NewString(context, id)); \ JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \ - tf_ssb_append_message_with_keys(ssb, id, priv, message); \ + tf_ssb_append_message_with_keys(ssb, id, priv, message, NULL, 0); \ JS_FreeValue(context, message); \ context = NULL @@ -573,7 +575,7 @@ void tf_ssb_test_bench(const tf_test_options_t* options) JS_SetPropertyStr(tf_ssb_get_context(ssb0), obj, "text", JS_NewString(tf_ssb_get_context(ssb0), "Hello, world!")); for (int i = 0; i < k_messages; i++) { - tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj); + tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0); } JS_FreeValue(tf_ssb_get_context(ssb0), obj); clock_gettime(CLOCK_REALTIME, &end_time); diff --git a/src/version.h b/src/version.h index 8690f9f8..003b1814 100644 --- a/src/version.h +++ b/src/version.h @@ -1,3 +1,3 @@ -#define VERSION_NUMBER "0.0.8" -#define VERSION_NAME "The secret ingredient is love." +#define VERSION_NUMBER "0.0.9-wip" +#define VERSION_NAME "Failure is the only opportunity to begin again."