diff --git a/src/ssb.c b/src/ssb.c index 61822e7d..59efb628 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -963,7 +963,7 @@ bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* ou } return true; } - else if (out_sequence_before_author) + else { JSValue reordered = JS_NewObject(context); JS_SetPropertyStr(context, reordered, "previous", JS_GetPropertyStr(context, val, "previous")); @@ -977,7 +977,10 @@ bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* ou JS_FreeValue(context, reordered); if (result) { - *out_sequence_before_author = true; + if (out_sequence_before_author) + { + *out_sequence_before_author = true; + } return true; } } @@ -3373,25 +3376,33 @@ tf_ssb_blob_wants_t* tf_ssb_connection_get_blob_wants_state(tf_ssb_connection_t* return connection ? &connection->blob_wants : NULL; } -bool tf_ssb_verify_strip_and_store_message(tf_ssb_t* ssb, JSValue value) +bool tf_ssb_verify_strip_and_store_message(tf_ssb_t* ssb, JSValue value, bool* out_is_new) { JSContext* context = tf_ssb_get_context(ssb); char signature[crypto_sign_BYTES + 128] = { 0 }; char id[crypto_hash_sha256_BYTES * 2 + 1] = { 0 }; bool sequence_before_author = false; + if (out_is_new) + { + *out_is_new = false; + } if (tf_ssb_verify_and_strip_signature(context, value, id, sizeof(id), signature, sizeof(signature), &sequence_before_author)) { if (tf_ssb_db_store_message(ssb, context, id, value, signature, sequence_before_author)) { tf_ssb_notify_message_added(ssb, id); - return true; + if (out_is_new) + { + *out_is_new = true; + } } + return true; } else { tf_printf("failed to verify message\n"); + return false; } - return false; } bool tf_ssb_connection_get_sent_clock(tf_ssb_connection_t* connection) diff --git a/src/ssb.h b/src/ssb.h index 305b33bf..62a515ea 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -108,7 +108,7 @@ bool tf_ssb_id_bin_to_str(char* str, size_t str_size, const uint8_t* bin); bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* out_id, size_t out_id_size, char* out_signature, size_t out_signature_size, bool* out_sequence_before_author); void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_id, size_t out_id_size); -bool tf_ssb_verify_strip_and_store_message(tf_ssb_t* ssb, JSValue value); +bool tf_ssb_verify_strip_and_store_message(tf_ssb_t* ssb, JSValue value, bool* out_is_new); bool tf_ssb_connection_is_client(tf_ssb_connection_t* connection); const char* tf_ssb_connection_get_host(tf_ssb_connection_t* connection); diff --git a/src/ssb.js.c b/src/ssb.js.c index b2ff6185..29ea4225 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -642,7 +642,15 @@ static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int a static JSValue _tf_ssb_storeMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); - return tf_ssb_verify_strip_and_store_message(ssb, argv[0]) ? JS_TRUE : JS_FALSE; + bool is_new = false; + if (tf_ssb_verify_strip_and_store_message(ssb, argv[0], &is_new)) + { + return is_new ? JS_TRUE : JS_FALSE; + } + else + { + return JS_UNDEFINED; + } } typedef struct _broadcasts_t diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index db825207..a2461941 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -937,7 +937,7 @@ static void _tf_ssb_rpc_ebt_replicate(tf_ssb_connection_t* connection, uint8_t f if (!JS_IsUndefined(author)) { /* Looks like a message. */ - tf_ssb_verify_strip_and_store_message(ssb, args); + tf_ssb_verify_strip_and_store_message(ssb, args, NULL); } else {