#include "ssb.js.h" #include "database.js.h" #include "mem.h" #include "ssb.db.h" #include "ssb.h" #include "util.js.h" #include #include #include #include #include #include #include #include "quickjs-libc.h" static JSClassID _tf_ssb_classId; void _tf_ssb_on_rpc(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data); static JSValue _tf_ssb_createIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); JSValue result = JS_UNDEFINED; if (ssb) { const char* user = JS_ToCString(context, argv[0]); int count = tf_ssb_db_identity_get_count_for_user(ssb, user); if (count < 16) { char public[512]; char private[512]; tf_ssb_generate_keys_buffer(public, sizeof(public), private, sizeof(private)); if (tf_ssb_db_identity_add(ssb, user, public, private)) { char id[513]; snprintf(id, sizeof(id), "@%s", public); result = JS_NewString(context, id); } else { result = JS_ThrowInternalError(context, "Unable to add identity."); } } else { result = JS_ThrowInternalError(context, "Too many identities for user."); } JS_FreeCString(context, user); } return result; } typedef struct _identities_visit_t { JSContext* context; JSValue array; int count; } identities_visit_t; static void _tf_ssb_getIdentities_visit(const char* identity, void* data) { identities_visit_t* state = data; char id[k_id_base64_len]; snprintf(id, sizeof(id), "@%s", identity); JS_SetPropertyUint32(state->context, state->array, state->count++, JS_NewString(state->context, id)); } static JSValue _tf_ssb_getIdentities(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NewArray(context); tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { const char* user = JS_ToCString(context, argv[0]); identities_visit_t state = { .context = context, .array = result, }; tf_ssb_db_identity_visit(ssb, user, _tf_ssb_getIdentities_visit, &state); JS_FreeCString(context, user); } return result; } static JSValue _tf_ssb_getAllIdentities(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NewArray(context); tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { identities_visit_t state = { .context = context, .array = result, }; tf_ssb_db_identity_visit_all(ssb, _tf_ssb_getIdentities_visit, &state); } return result; } static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_UNDEFINED; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { const char* user = JS_ToCString(context, argv[0]); const char* id = JS_ToCString(context, argv[1]); 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]); } else { result = JS_ThrowInternalError(context, "Unable to get private key for user %s with identity %s.", user, id); } JS_FreeCString(context, id); JS_FreeCString(context, user); } return result; } static JSValue _tf_ssb_getMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { const char* id = JS_ToCString(context, argv[0]); int64_t sequence = 0; JS_ToInt64(context, &sequence, argv[1]); double timestamp = -1.0; char* contents = NULL; if (tf_ssb_db_get_message_by_author_and_sequence(ssb, id, sequence, NULL, 0, ×tamp, &contents)) { result = JS_NewObject(context); JS_SetPropertyStr(context, result, "timestamp", JS_NewFloat64(context, timestamp)); JS_SetPropertyStr(context, result, "content", JS_NewString(context, contents)); tf_free(contents); } JS_FreeCString(context, id); } return result; } static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { const char* id = JS_ToCString(context, argv[0]); uint8_t* blob = NULL; size_t size = 0; if (tf_ssb_db_blob_get(ssb, id, &blob, &size)) { result = JS_NewArrayBufferCopy(context, blob, size); tf_free(blob); } JS_FreeCString(context, id); } return result; } static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { uint8_t* blob = NULL; size_t size = 0; char id[512]; if (JS_IsString(argv[0])) { const char* text = JS_ToCStringLen(context, &size, argv[0]); if (tf_ssb_db_blob_store(ssb, (const uint8_t*)text, size, id, sizeof(id), NULL)) { result = JS_NewString(context, id); } JS_FreeCString(context, text); } else if ((blob = tf_util_try_get_array_buffer(context, &size, argv[0])) != 0) { if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id), NULL)) { result = JS_NewString(context, id); } } else { size_t offset; size_t element_size; JSValue buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &size, &element_size); if (!JS_IsException(buffer)) { blob = tf_util_try_get_array_buffer(context, &size, buffer); if (blob) { if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id), NULL)) { result = JS_NewString(context, id); } } } JS_FreeValue(context, buffer); } } return result; } static JSValue _tf_ssb_messageContentGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { const char* id = JS_ToCString(context, argv[0]); uint8_t* blob = NULL; size_t size = 0; if (tf_ssb_db_message_content_get(ssb, id, &blob, &size)) { result = JS_NewArrayBufferCopy(context, blob, size); tf_free(blob); } JS_FreeCString(context, id); } return result; } static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { const char** connections = tf_ssb_get_connection_ids(ssb); if (connections) { result = JS_NewArray(context); uint32_t i = 0; for (const char** p = connections; *p; p++, i++) { JS_SetPropertyUint32(context, result, i, JS_NewString(context, *p)); } tf_free(connections); } } return result; } static JSValue _tf_ssb_storedConnections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { int count = 0; tf_ssb_db_stored_connection_t* connections = tf_ssb_db_get_stored_connections(ssb, &count); result = JS_NewArray(context); for (int i = 0; i < count; i++) { JSValue connection = JS_NewObject(context); JS_SetPropertyStr(context, connection, "address", JS_NewString(context, connections[i].address)); JS_SetPropertyStr(context, connection, "port", JS_NewInt32(context, connections[i].port)); JS_SetPropertyStr(context, connection, "pubkey", JS_NewString(context, connections[i].pubkey)); JS_SetPropertyUint32(context, result, i, connection); } tf_free(connections); } return result; } static JSValue _tf_ssb_getConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); const char* id = JS_ToCString(context, argv[0]); tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, id); JS_FreeCString(context, id); return JS_DupValue(context, tf_ssb_connection_get_object(connection)); } static JSValue _tf_ssb_closeConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); const char* id = JS_ToCString(context, argv[0]); tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, id); if (connection) { tf_ssb_connection_close(connection); } JS_FreeCString(context, id); return connection ? JS_TRUE : JS_FALSE; } typedef struct _sqlStream_callback_t { JSContext* context; JSValue callback; } sqlStream_callback_t; static void _tf_ssb_sqlStream_callback(JSValue row, void* user_data) { sqlStream_callback_t* info = user_data; JSValue response = JS_Call(info->context, info->callback, JS_UNDEFINED, 1, &row); tf_util_report_error(info->context, response); JS_FreeValue(info->context, response); } static JSValue _tf_ssb_sqlStream(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { const char* query = JS_ToCString(context, argv[0]); if (query) { sqlStream_callback_t info = { .context = context, .callback = argv[2], }; result = tf_ssb_db_visit_query(ssb, query, argv[1], _tf_ssb_sqlStream_callback, &info); JS_FreeCString(context, query); } } return result; } typedef struct _sql_work_t { uv_work_t request; tf_ssb_t* ssb; const char* query; uint8_t* binds; size_t binds_count; uint8_t* rows; size_t rows_count; JSValue callback; JSValue promise[2]; int result; } sql_work_t; static void _tf_ssb_sql_append(uint8_t** rows, size_t* rows_count, const void* data, size_t size) { *rows = tf_resize_vec(*rows, *rows_count + size); memcpy(*rows + *rows_count, data, size); *rows_count += size; } static void _tf_ssb_sqlAsync_work(uv_work_t* work) { sql_work_t* sql_work = work->data; sqlite3* db = tf_ssb_acquire_db_reader(sql_work->ssb); sqlite3_stmt* statement = NULL; if (sqlite3_prepare(db, sql_work->query, -1, &statement, NULL) == SQLITE_OK) { const uint8_t* p = sql_work->binds; int column = 0; while (p < sql_work->binds + sql_work->binds_count) { switch (*p++) { case SQLITE_INTEGER: { int64_t value = 0; memcpy(&value, p, sizeof(value)); sqlite3_bind_int64(statement, column + 1, value); p += sizeof(value); } break; case SQLITE_TEXT: { size_t length = 0; memcpy(&length, p, sizeof(length)); p += sizeof(length); sqlite3_bind_text(statement, column + 1, (const char*)p, length, NULL); } break; case SQLITE_NULL: sqlite3_bind_null(statement, column + 1); break; default: abort(); } column++; } int r = SQLITE_OK; while ((r = sqlite3_step(statement)) == SQLITE_ROW) { _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &(uint8_t[]) { 'r' }, 1); for (int i = 0; i < sqlite3_column_count(statement); i++) { _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &(uint8_t[]) { 'c' }, 1); const char* name = sqlite3_column_name(statement, i); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, name, strlen(name) + 1); uint8_t type = sqlite3_column_type(statement, i); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &type, sizeof(type)); switch (type) { case SQLITE_INTEGER: { int64_t value = sqlite3_column_int64(statement, i); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &value, sizeof(value)); } break; case SQLITE_FLOAT: { double value = sqlite3_column_double(statement, i); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &value, sizeof(value)); } break; case SQLITE_TEXT: { size_t bytes = sqlite3_column_bytes(statement, i); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &bytes, sizeof(bytes)); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, sqlite3_column_text(statement, i), bytes); } break; case SQLITE_BLOB: { size_t bytes = sqlite3_column_bytes(statement, i); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &bytes, sizeof(bytes)); _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, sqlite3_column_blob(statement, i), bytes); } break; case SQLITE_NULL: break; default: abort(); } } } _tf_ssb_sql_append(&sql_work->rows, &sql_work->rows_count, &(uint8_t[]) { 0 }, 1); sqlite3_finalize(statement); } else { printf("prepare failed\n"); } tf_ssb_release_db_reader(sql_work->ssb, db); } static void _tf_ssb_sqlAsync_after_work(uv_work_t* work, int status) { sql_work_t* sql_work = work->data; JSContext* context = tf_ssb_get_context(sql_work->ssb); uint8_t* p = sql_work->rows; while (p < sql_work->rows + sql_work->rows_count) { if (*p++ == 'r') { JSValue row = JS_NewObject(context); while (*p == 'c') { p++; const char* column_name = (const char*)p; size_t length = strlen((char*)p); p += length + 1; switch (*p++) { case SQLITE_INTEGER: { int64_t value = 0; memcpy(&value, p, sizeof(value)); JS_SetPropertyStr(context, row, column_name, JS_NewInt64(context, value)); p += sizeof(value); } break; case SQLITE_FLOAT: { double value = 0.0; memcpy(&value, p, sizeof(value)); JS_SetPropertyStr(context, row, column_name, JS_NewFloat64(context, value)); p += sizeof(value); } break; case SQLITE_TEXT: case SQLITE_BLOB: { size_t length = 0; memcpy(&length, p, sizeof(length)); p += sizeof(length); JS_SetPropertyStr(context, row, column_name, JS_NewStringLen(context, (const char*)p, length)); p += length; } break; case SQLITE_NULL: JS_SetPropertyStr(context, row, column_name, JS_NULL); break; } } JSValue response = JS_Call(context, sql_work->callback, JS_UNDEFINED, 1, &row); tf_util_report_error(context, response); JS_FreeValue(context, row); } else { break; } } tf_free(sql_work->binds); tf_free(sql_work->rows); JSValue result = JS_Call(context, sql_work->promise[0], JS_UNDEFINED, 0, NULL); tf_util_report_error(context, result); JS_FreeValue(context, sql_work->promise[0]); JS_FreeValue(context, sql_work->promise[1]); JS_FreeValue(context, sql_work->callback); JS_FreeCString(context, sql_work->query); tf_free(sql_work); } static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); JSValue result = JS_UNDEFINED; if (ssb) { const char* query = JS_ToCString(context, argv[0]); sql_work_t* work = tf_malloc(sizeof(sql_work_t)); *work = (sql_work_t) { .request = { .data = work, }, .ssb = ssb, .callback = JS_DupValue(context, argv[2]), .query = query, }; result = JS_NewPromiseCapability(context, work->promise); int32_t length = tf_util_get_length(context, argv[1]); for (int i = 0; i < length; i++) { JSValue value = JS_GetPropertyUint32(context, argv[1], i); if (JS_IsNumber(value)) { uint8_t type = SQLITE_INTEGER; int64_t number = 0; JS_ToInt64(context, &number, value); _tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type)); _tf_ssb_sql_append(&work->binds, &work->binds_count, &number, sizeof(number)); } else if (JS_IsBool(value)) { uint8_t type = SQLITE_INTEGER; int64_t number = JS_ToBool(context, value) ? 1 : 0; _tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type)); _tf_ssb_sql_append(&work->binds, &work->binds_count, &number, sizeof(number)); } else if (JS_IsNumber(value)) { uint8_t type = SQLITE_NULL; _tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type)); } else { uint8_t type = SQLITE_TEXT; size_t length = 0; const char* string = JS_ToCStringLen(context, &length, value); if (!string) { string = ""; } _tf_ssb_sql_append(&work->binds, &work->binds_count, &type, sizeof(type)); _tf_ssb_sql_append(&work->binds, &work->binds_count, string, length); JS_FreeCString(context, string); } } int r = uv_queue_work(tf_ssb_get_loop(ssb), &work->request, _tf_ssb_sqlAsync_work, _tf_ssb_sqlAsync_after_work); if (r) { JSValue error = JS_ThrowInternalError(context, "uv_queue_work failed: %s", uv_strerror(r)); JSValue result = JS_Call(context, work->promise[1], JS_UNDEFINED, 1, &error); tf_util_report_error(context, result); JS_FreeValue(context, work->promise[0]); JS_FreeValue(context, work->promise[1]); JS_FreeValue(context, work->callback); JS_FreeValue(context, error); JS_FreeCString(context, query); tf_free(work->binds); tf_free(work); } } return result; } 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); tf_ssb_verify_strip_and_store_message(ssb, argv[0]); return JS_UNDEFINED; } typedef struct _broadcasts_t { JSContext* context; JSValue array; int length; } broadcasts_t; static void _tf_ssb_broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data) { broadcasts_t* broadcasts = user_data; JSValue entry = JS_NewObject(broadcasts->context); char pubkey[k_id_base64_len]; tf_ssb_id_bin_to_str(pubkey, sizeof(pubkey), pub); if (tunnel) { JS_SetPropertyStr(broadcasts->context, entry, "tunnel", JS_DupValue(broadcasts->context, tf_ssb_connection_get_object(tunnel))); } else { JS_SetPropertyStr(broadcasts->context, entry, "address", JS_NewString(broadcasts->context, host)); JS_SetPropertyStr(broadcasts->context, entry, "port", JS_NewInt32(broadcasts->context, ntohs(addr->sin_port))); } JS_SetPropertyStr(broadcasts->context, entry, "pubkey", JS_NewString(broadcasts->context, pubkey)); JS_SetPropertyUint32(broadcasts->context, broadcasts->array, broadcasts->length++, entry); } static JSValue _tf_ssb_getBroadcasts(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_UNDEFINED; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { result = JS_NewArray(context); broadcasts_t broadcasts = { .context = context, .array = result, .length = 0, }; tf_ssb_visit_broadcasts(ssb, _tf_ssb_broadcasts_visit, &broadcasts); } return result; } static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue args = argv[0]; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { if (JS_IsString(args)) { const char* address_str = JS_ToCString(context, args); printf("Connecting to %s\n", address_str); tf_ssb_connect_str(ssb, address_str); JS_FreeCString(context, address_str); } else { JSValue address = JS_GetPropertyStr(context, args, "address"); JSValue port = JS_GetPropertyStr(context, args, "port"); JSValue pubkey = JS_GetPropertyStr(context, args, "pubkey"); const char* address_str = JS_ToCString(context, address); int32_t port_int = 0; JS_ToInt32(context, &port_int, port); const char* pubkey_str = JS_ToCString(context, pubkey); if (pubkey_str) { printf("Connecting to %s:%d\n", address_str, port_int); uint8_t pubkey_bin[k_id_bin_len]; tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str); tf_ssb_connect(ssb, address_str, port_int, pubkey_bin); } else { printf("Not connecting to null.\n"); } JS_FreeCString(context, pubkey_str); JS_FreeCString(context, address_str); JS_FreeValue(context, address); JS_FreeValue(context, port); JS_FreeValue(context, pubkey); } } return JS_UNDEFINED; } static JSValue _tf_ssb_forgetStoredConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue args = argv[0]; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); if (ssb) { JSValue address = JS_GetPropertyStr(context, args, "address"); JSValue port = JS_GetPropertyStr(context, args, "port"); JSValue pubkey = JS_GetPropertyStr(context, args, "pubkey"); const char* address_str = JS_ToCString(context, address); int32_t port_int = 0; JS_ToInt32(context, &port_int, port); const char* pubkey_str = JS_ToCString(context, pubkey); if (pubkey_str) { tf_ssb_db_forget_stored_connection(ssb, address_str, port_int, pubkey_str); } JS_FreeCString(context, pubkey_str); JS_FreeCString(context, address_str); JS_FreeValue(context, address); JS_FreeValue(context, port); JS_FreeValue(context, pubkey); } return JS_UNDEFINED; } static void _tf_ssb_cleanup_value(tf_ssb_t* ssb, void* user_data) { JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data); JS_FreeValue(tf_ssb_get_context(ssb), callback); } static void _tf_ssb_on_message_added_callback(tf_ssb_t* ssb, const char* id, void* user_data) { JSContext* context = tf_ssb_get_context(ssb); JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data); JSValue string = JS_NewString(context, id); JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &string); if (tf_util_report_error(context, response)) { tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, user_data); } JS_FreeValue(context, response); JS_FreeValue(context, string); } static void _tf_ssb_on_blob_want_added_callback(tf_ssb_t* ssb, const char* id, void* user_data) { JSContext* context = tf_ssb_get_context(ssb); JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data); JSValue string = JS_NewString(context, id); JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &string); if (tf_util_report_error(context, response)) { tf_ssb_remove_blob_want_added_callback(ssb, _tf_ssb_on_blob_want_added_callback, user_data); } JS_FreeValue(context, response); JS_FreeValue(context, string); } static void _tf_ssb_on_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data) { JSContext* context = tf_ssb_get_context(ssb); JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data); JSValue response = JS_UNDEFINED; switch (change) { case k_tf_ssb_change_create: break; case k_tf_ssb_change_connect: { JSValue object = JS_DupValue(context, tf_ssb_connection_get_object(connection)); JSValue args[] = { JS_NewString(context, "add"), object, }; response = JS_Call(context, callback, JS_UNDEFINED, 2, args); if (tf_util_report_error(context, response)) { tf_ssb_remove_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, user_data); } JS_FreeValue(context, args[0]); JS_FreeValue(context, object); } break; case k_tf_ssb_change_remove: { JSValue object = JS_DupValue(context, tf_ssb_connection_get_object(connection)); JSValue args[] = { JS_NewString(context, "remove"), object, }; response = JS_Call(context, callback, JS_UNDEFINED, 2, args); if (tf_util_report_error(context, response)) { tf_ssb_remove_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, user_data); } JS_FreeValue(context, args[0]); JS_FreeValue(context, object); } break; } JS_FreeValue(context, response); } static void _tf_ssb_on_broadcasts_changed_callback(tf_ssb_t* ssb, void* user_data) { JSContext* context = tf_ssb_get_context(ssb); JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data); JSValue argv = JS_UNDEFINED; JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &argv); if (tf_util_report_error(context, response)) { tf_ssb_remove_broadcasts_changed_callback(ssb, _tf_ssb_on_broadcasts_changed_callback, user_data); } JS_FreeValue(context, response); } void tf_ssb_run_file(JSContext* context, const char* file_name) { FILE* file = fopen(file_name, "rb"); if (!file) { printf("Unable to open %s: %s.", file_name, strerror(errno)); return; } char* source = NULL; fseek(file, 0, SEEK_END); long file_size = ftell(file); fseek(file, 0, SEEK_SET); source = tf_malloc(file_size + 1); int bytes_read = fread(source, 1, file_size, file); source[bytes_read] = '\0'; fclose(file); JSValue result = JS_Eval(context, source, file_size, file_name, 0); if (tf_util_report_error(context, result)) { printf("Error running %s.\n", file_name); } JSRuntime* runtime = JS_GetRuntime(context); while (JS_IsJobPending(runtime)) { JSContext* context2 = NULL; int r = JS_ExecutePendingJob(runtime, &context2); JSValue result = JS_GetException(context2); tf_util_report_error(context, result); if (r == 0) { break; } } JS_FreeValue(context, result); tf_free(source); } static JSValue _tf_ssb_add_event_listener(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); const char* event_name = JS_ToCString(context, argv[0]); JSValue callback = argv[1]; JSValue result = JS_UNDEFINED; if (!event_name) { result = JS_ThrowTypeError(context, "Expected argument 1 to be a string event name."); } else if (!JS_IsFunction(context, callback)) { result = JS_ThrowTypeError(context, "Expected argument 2 to be a function."); } else { if (strcmp(event_name, "connections") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_add_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, _tf_ssb_cleanup_value, ptr); } else if (strcmp(event_name, "broadcasts") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_add_broadcasts_changed_callback(ssb, _tf_ssb_on_broadcasts_changed_callback, _tf_ssb_cleanup_value, ptr); } else if (strcmp(event_name, "message") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_add_message_added_callback(ssb, _tf_ssb_on_message_added_callback, _tf_ssb_cleanup_value, ptr); } else if (strcmp(event_name, "blob_want_added") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_add_blob_want_added_callback(ssb, _tf_ssb_on_blob_want_added_callback, _tf_ssb_cleanup_value, ptr); } } JS_FreeCString(context, event_name); return result; } static JSValue _tf_ssb_remove_event_listener(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); const char* event_name = JS_ToCString(context, argv[0]); JSValue callback = argv[1]; JSValue result = JS_UNDEFINED; if (!event_name) { result = JS_ThrowTypeError(context, "Expected argument 1 to be a string event name."); } else if (!JS_IsFunction(context, callback)) { result = JS_ThrowTypeError(context, "Expected argument 2 to be a function."); } else { if (strcmp(event_name, "connections") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_remove_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, ptr); } else if (strcmp(event_name, "broadcasts") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_remove_broadcasts_changed_callback(ssb, _tf_ssb_on_broadcasts_changed_callback, ptr); } else if (strcmp(event_name, "message") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, ptr); } else if (strcmp(event_name, "blob_want_added") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_remove_blob_want_added_callback(ssb, _tf_ssb_on_blob_want_added_callback, ptr); } } JS_FreeCString(context, event_name); return result; } static JSValue _tf_ssb_hmacsha256_sign(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_UNDEFINED; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); size_t payload_length = 0; const char* payload = JS_ToCStringLen(context, &payload_length, argv[0]); const char* user = JS_ToCString(context, argv[1]); const char* public_key = JS_ToCString(context, argv[2]); uint8_t private_key[crypto_sign_SECRETKEYBYTES]; if (tf_ssb_db_identity_get_private_key(ssb, user, public_key, private_key, sizeof(private_key))) { uint8_t signature[crypto_sign_BYTES]; unsigned long long siglen; if (crypto_sign_detached(signature, &siglen, (const uint8_t*)payload, payload_length, private_key) == 0) { char signature_base64[crypto_sign_BYTES * 2]; base64c_encode(signature, sizeof(signature), (uint8_t*)signature_base64, sizeof(signature_base64)); result = JS_NewString(context, signature_base64); } } else { result = JS_ThrowInternalError(context, "Private key not found."); } JS_FreeCString(context, public_key); JS_FreeCString(context, user); JS_FreeCString(context, payload); return result; } static JSValue _tf_ssb_hmacsha256_verify(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_UNDEFINED; size_t public_key_length = 0; const char* public_key = JS_ToCStringLen(context, &public_key_length, argv[0]); size_t payload_length = 0; const char* payload = JS_ToCStringLen(context, &payload_length, argv[1]); size_t signature_length = 0; const char* signature = JS_ToCStringLen(context, &signature_length, argv[2]); const char* public_key_start = public_key && *public_key == '@' ? public_key + 1 : public_key; const char* public_key_end = strstr(public_key_start, ".ed25519"); if (!public_key_end) { public_key_end = public_key_start + strlen(public_key_start); } uint8_t bin_public_key[crypto_sign_PUBLICKEYBYTES] = { 0 }; if (base64c_decode((const uint8_t*)public_key_start, public_key_end - public_key_start, bin_public_key, sizeof(bin_public_key)) > 0) { uint8_t bin_signature[crypto_sign_BYTES] = { 0 }; if (base64c_decode((const uint8_t*)signature, signature_length, bin_signature, sizeof(bin_signature)) > 0) { if (crypto_sign_verify_detached(bin_signature, (const uint8_t*)payload, payload_length, bin_public_key) == 0) { result = JS_TRUE; } } } JS_FreeCString(context, signature); JS_FreeCString(context, payload); JS_FreeCString(context, public_key); return result; } static JSValue _tf_ssb_createTunnel(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_UNDEFINED; tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); const char* portal_id = JS_ToCString(context, argv[0]); const char* target_id = JS_ToCString(context, argv[1]); tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, portal_id); if (connection) { // let request_number = await tfrpc.rpc.connectionSendJson(portal, {name: ['tunnel', 'connect'], args: [{portal: portal, target: target}], type: 'duplex'}); int32_t request_number = tf_ssb_connection_next_request_number(connection); JSValue message = JS_NewObject(context); JSValue name = JS_NewArray(context); JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "tunnel")); JS_SetPropertyUint32(context, name, 1, JS_NewString(context, "connect")); JS_SetPropertyStr(context, message, "name", name); JSValue arg = JS_NewObject(context); JS_SetPropertyStr(context, arg, "portal", JS_NewString(context, portal_id)); JS_SetPropertyStr(context, arg, "target", JS_NewString(context, target_id)); JSValue args = JS_NewArray(context); JS_SetPropertyUint32(context, args, 0, arg); JS_SetPropertyStr(context, message, "args", args); JS_SetPropertyStr(context, message, "type", JS_NewString(context, "duplex")); tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_stream, request_number, message, NULL, NULL, NULL); JS_FreeValue(context, message); tf_ssb_connection_tunnel_create(ssb, portal_id, request_number, target_id); result = JS_TRUE; } JS_FreeCString(context, target_id); JS_FreeCString(context, portal_id); return result; } static JSValue _tf_ssb_followingDeep(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { int depth = 2; if (!JS_IsArray(context, argv[0])) { return JS_ThrowTypeError(context, "Expected argument 1 to be an array of ids."); } if (JS_ToInt32(context, &depth, argv[1])) { return JS_ThrowTypeError(context, "Could not convert argument 2 to integer."); } int count = tf_util_get_length(context, argv[0]); const char** ids = tf_malloc(count * sizeof(char*)); for (int i = 0; i < count; i++) { JSValue id = JS_GetPropertyUint32(context, argv[0], i); ids[i] = JS_ToCString(context, id); JS_FreeValue(context, id); } tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); const char** following_deep = tf_ssb_db_following_deep(ssb, ids, count, depth); JSValue result = JS_NewArray(context); int index = 0; for (const char** id = following_deep; *id; id++) { JS_SetPropertyUint32(context, result, index++, JS_NewString(context, *id)); } tf_free(following_deep); for (int i = 0; i < count; i++) { JS_FreeCString(context, ids[i]); } tf_free(ids); return result; } void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) { JS_NewClassID(&_tf_ssb_classId); JSClassDef def = { .class_name = "ssb", }; if (JS_NewClass(JS_GetRuntime(context), _tf_ssb_classId, &def) != 0) { fprintf(stderr, "Failed to register ssb.\n"); } JSValue global = JS_GetGlobalObject(context); JSValue object = JS_NewObjectClass(context, _tf_ssb_classId); JS_SetPropertyStr(context, global, "ssb", object); JS_SetOpaque(object, ssb); /* Requires an identity. */ JS_SetPropertyStr(context, object, "createIdentity", JS_NewCFunction(context, _tf_ssb_createIdentity, "createIdentity", 1)); JS_SetPropertyStr(context, object, "getIdentities", JS_NewCFunction(context, _tf_ssb_getIdentities, "getIdentities", 1)); JS_SetPropertyStr(context, object, "appendMessageWithIdentity", JS_NewCFunction(context, _tf_ssb_appendMessageWithIdentity, "appendMessageWithIdentity", 3)); JS_SetPropertyStr(context, object, "hmacsha256sign", JS_NewCFunction(context, _tf_ssb_hmacsha256_sign, "hmacsha256sign", 3)); JS_SetPropertyStr(context, object, "hmacsha256verify", JS_NewCFunction(context, _tf_ssb_hmacsha256_verify, "hmacsha256verify", 3)); /* Does not require an identity. */ JS_SetPropertyStr(context, object, "getAllIdentities", JS_NewCFunction(context, _tf_ssb_getAllIdentities, "getAllIdentities", 0)); JS_SetPropertyStr(context, object, "getMessage", JS_NewCFunction(context, _tf_ssb_getMessage, "getMessage", 2)); JS_SetPropertyStr(context, object, "blobGet", JS_NewCFunction(context, _tf_ssb_blobGet, "blobGet", 1)); JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunction(context, _tf_ssb_blobStore, "blobStore", 2)); JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1)); JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0)); JS_SetPropertyStr(context, object, "storedConnections", JS_NewCFunction(context, _tf_ssb_storedConnections, "storedConnections", 0)); JS_SetPropertyStr(context, object, "getConnection", JS_NewCFunction(context, _tf_ssb_getConnection, "getConnection", 1)); JS_SetPropertyStr(context, object, "closeConnection", JS_NewCFunction(context, _tf_ssb_closeConnection, "closeConnection", 1)); JS_SetPropertyStr(context, object, "forgetStoredConnection", JS_NewCFunction(context, _tf_ssb_forgetStoredConnection, "forgetStoredConnection", 1)); JS_SetPropertyStr(context, object, "sqlStream", JS_NewCFunction(context, _tf_ssb_sqlStream, "sqlStream", 3)); JS_SetPropertyStr(context, object, "sqlAsync", JS_NewCFunction(context, _tf_ssb_sqlAsync, "sqlAsync", 3)); JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1)); JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0)); JS_SetPropertyStr(context, object, "connect", JS_NewCFunction(context, _tf_ssb_connect, "connect", 1)); JS_SetPropertyStr(context, object, "createTunnel", JS_NewCFunction(context, _tf_ssb_createTunnel, "createTunnel", 3)); JS_SetPropertyStr(context, object, "followingDeep", JS_NewCFunction(context, _tf_ssb_followingDeep, "followingDeep", 2)); /* Should be trusted only. */ JS_SetPropertyStr(context, object, "addEventListener", JS_NewCFunction(context, _tf_ssb_add_event_listener, "addEventListener", 2)); JS_SetPropertyStr(context, object, "removeEventListener", JS_NewCFunction(context, _tf_ssb_remove_event_listener, "removeEventListener", 2)); JS_FreeValue(context, global); }