2021-10-24 11:46:30 -04:00
|
|
|
#include "ssb.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
#include "database.js.h"
|
2022-06-04 13:04:51 -04:00
|
|
|
#include "mem.h"
|
2021-08-22 15:41:27 -04:00
|
|
|
#include "ssb.db.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "ssb.h"
|
|
|
|
#include "task.h"
|
2021-11-03 18:15:46 -04:00
|
|
|
#include "util.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
#include <base64c.h>
|
2021-09-06 13:50:38 -04:00
|
|
|
#include <sodium/crypto_hash_sha256.h>
|
|
|
|
#include <sodium/crypto_sign.h>
|
|
|
|
#include <string.h>
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
|
|
|
static JSClassID _tf_ssb_classId;
|
|
|
|
|
2021-12-22 14:57:34 -05:00
|
|
|
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);
|
|
|
|
|
2022-07-13 21:01:14 -04:00
|
|
|
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));
|
2022-08-03 19:52:13 -04:00
|
|
|
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
|
2022-07-13 21:01:14 -04:00
|
|
|
{
|
|
|
|
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;
|
2022-07-31 15:01:08 -04:00
|
|
|
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));
|
2022-07-13 21:01:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-31 15:01:08 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-13 21:01:14 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
int64_t sequence = 0;
|
|
|
|
JS_ToInt64(context, &sequence, argv[1]);
|
2022-02-11 20:44:11 -05:00
|
|
|
double timestamp = -1.0;
|
2021-01-02 13:10:00 -05:00
|
|
|
char* contents = NULL;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (tf_ssb_db_get_message_by_author_and_sequence(ssb, id, sequence, NULL, 0, ×tamp, &contents))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewObject(context);
|
2022-02-11 20:44:11 -05:00
|
|
|
JS_SetPropertyStr(context, result, "timestamp", JS_NewFloat64(context, timestamp));
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, result, "content", JS_NewString(context, contents));
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(contents);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (tf_ssb_db_blob_get(ssb, id, &blob, &size))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewArrayBufferCopy(context, blob, size);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(blob);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeCString(context, id);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
char id[512];
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsString(argv[0]))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* text = JS_ToCStringLen(context, &size, argv[0]);
|
2022-10-12 08:27:32 -04:00
|
|
|
if (tf_ssb_db_blob_store(ssb, (const uint8_t*)text, size, id, sizeof(id), NULL))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, text);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2021-11-03 18:15:46 -04:00
|
|
|
else if ((blob = tf_util_try_get_array_buffer(context, &size, argv[0])) != 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-10-12 08:27:32 -04:00
|
|
|
if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id), NULL))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 17:15:18 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t offset;
|
|
|
|
size_t element_size;
|
2021-11-03 18:15:46 -04:00
|
|
|
JSValue buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &size, &element_size);
|
2021-10-31 17:15:18 -04:00
|
|
|
if (!JS_IsException(buffer))
|
|
|
|
{
|
2021-11-03 18:15:46 -04:00
|
|
|
blob = tf_util_try_get_array_buffer(context, &size, buffer);
|
2021-10-31 17:15:18 -04:00
|
|
|
if (blob)
|
|
|
|
{
|
2022-10-12 08:27:32 -04:00
|
|
|
if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id), NULL))
|
2021-10-31 17:15:18 -04:00
|
|
|
{
|
|
|
|
result = JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, buffer);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-09 18:06:33 -05:00
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
2021-01-09 18:06:33 -05:00
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (tf_ssb_db_message_content_get(ssb, id, &blob, &size))
|
|
|
|
{
|
2021-01-09 18:06:33 -05:00
|
|
|
result = JS_NewArrayBufferCopy(context, blob, size);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(blob);
|
2021-01-09 18:06:33 -05:00
|
|
|
}
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeCString(context, id);
|
2021-01-09 18:06:33 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char** connections = tf_ssb_get_connection_ids(ssb);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (connections)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewArray(context);
|
|
|
|
uint32_t i = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
for (const char** p = connections; *p; p++, i++)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyUint32(context, result, i, JS_NewString(context, *p));
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(connections);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct _sqlStream_callback_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue callback;
|
|
|
|
} sqlStream_callback_t;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _tf_ssb_sqlStream_callback(JSValue row, void* user_data)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlStream_callback_t* info = user_data;
|
|
|
|
JSValue response = JS_Call(info->context, info->callback, JS_UNDEFINED, 1, &row);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(info->context, response);
|
2021-10-31 15:39:16 -04:00
|
|
|
JS_FreeValue(info->context, response);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_sqlStream(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2022-04-17 20:24:00 -04:00
|
|
|
JSValue result = JS_NULL;
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* query = JS_ToCString(context, argv[0]);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (query)
|
|
|
|
{
|
2022-04-17 20:24:00 -04:00
|
|
|
sqlStream_callback_t info =
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.context = context,
|
|
|
|
.callback = argv[2],
|
|
|
|
};
|
2022-04-17 20:24:00 -04:00
|
|
|
result = tf_ssb_db_visit_query(ssb, query, argv[1], _tf_ssb_sqlStream_callback, &info);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeCString(context, query);
|
|
|
|
}
|
|
|
|
}
|
2022-04-17 20:24:00 -04:00
|
|
|
return result;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
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);
|
|
|
|
char signature[crypto_sign_BYTES + 128];
|
|
|
|
char id[crypto_hash_sha256_BYTES * 2 + 1];
|
2022-02-02 21:00:05 -05:00
|
|
|
bool sequence_before_author = false;
|
2022-02-09 22:58:33 -05:00
|
|
|
if (tf_ssb_verify_and_strip_signature(context, argv[0], id, sizeof(id), signature, sizeof(signature), &sequence_before_author))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-02-02 21:00:05 -05:00
|
|
|
if (tf_ssb_db_store_message(ssb, context, id, argv[0], signature, sequence_before_author))
|
2021-11-17 18:47:55 -05:00
|
|
|
{
|
|
|
|
tf_ssb_notify_message_added(ssb, id);
|
|
|
|
}
|
2022-05-20 21:38:13 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Failed to store message.\n");
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-06 13:50:38 -04:00
|
|
|
printf("failed to verify message\n");
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct _broadcasts_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue array;
|
|
|
|
int length;
|
|
|
|
} broadcasts_t;
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
static void _tf_ssb_broadcasts_visit(const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
|
|
|
broadcasts_t* broadcasts = user_data;
|
|
|
|
JSValue entry = JS_NewObject(broadcasts->context);
|
|
|
|
char address[256];
|
|
|
|
char pubkey[k_id_base64_len];
|
|
|
|
uv_ip4_name(addr, address, sizeof(address));
|
|
|
|
tf_ssb_id_bin_to_str(pubkey, sizeof(pubkey), pub);
|
2022-11-02 19:34:44 -04:00
|
|
|
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, address));
|
|
|
|
JS_SetPropertyStr(broadcasts->context, entry, "port", JS_NewInt32(broadcasts->context, ntohs(addr->sin_port)));
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (ssb)
|
|
|
|
{
|
|
|
|
if (JS_IsString(args))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
|
|
|
uint8_t pubkey_bin[k_id_bin_len];
|
|
|
|
printf("Connecting to %s:%d\n", address_str, port_int);
|
|
|
|
tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str);
|
|
|
|
tf_ssb_connect(ssb, address_str, port_int, pubkey_bin);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
static JSValue _tf_ssb_rpc_send_json(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
JSValue request_val = JS_GetPropertyStr(context, this_val, "request_number");
|
|
|
|
int32_t request_number;
|
|
|
|
JS_ToInt32(context, &request_number, request_val);
|
|
|
|
JS_FreeValue(context, request_val);
|
|
|
|
|
2021-12-27 14:52:42 -05:00
|
|
|
JSValue flags_val = JS_GetPropertyStr(context, this_val, "flags");
|
|
|
|
int32_t flags_number;
|
|
|
|
JS_ToInt32(context, &flags_number, flags_val);
|
|
|
|
JS_FreeValue(context, flags_val);
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
JSValue message_val = JS_JSONStringify(context, argv[0], JS_NULL, JS_NULL);
|
|
|
|
size_t size;
|
|
|
|
const char* message = JS_ToCStringLen(context, &size, message_val);
|
|
|
|
|
|
|
|
tf_ssb_connection_rpc_send(
|
|
|
|
connection,
|
2021-12-27 14:52:42 -05:00
|
|
|
k_ssb_rpc_flag_json | (flags_number & ~k_ssb_rpc_mask_type),
|
2021-09-06 13:50:38 -04:00
|
|
|
-request_number,
|
|
|
|
(const uint8_t*)message,
|
|
|
|
size,
|
|
|
|
NULL,
|
2022-11-02 19:34:44 -04:00
|
|
|
NULL,
|
2021-09-06 13:50:38 -04:00
|
|
|
NULL);
|
|
|
|
JS_FreeValue(context, connection_val);
|
2021-09-06 14:23:22 -04:00
|
|
|
JS_FreeCString(context, message);
|
|
|
|
JS_FreeValue(context, message_val);
|
2021-09-06 13:50:38 -04:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:48:03 -05:00
|
|
|
static JSValue _tf_ssb_rpc_send_json_end(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
JSValue request_val = JS_GetPropertyStr(context, this_val, "request_number");
|
|
|
|
int32_t request_number;
|
|
|
|
JS_ToInt32(context, &request_number, request_val);
|
|
|
|
JS_FreeValue(context, request_val);
|
|
|
|
|
|
|
|
JSValue flags_val = JS_GetPropertyStr(context, this_val, "flags");
|
|
|
|
int32_t flags_number;
|
|
|
|
JS_ToInt32(context, &flags_number, flags_val);
|
|
|
|
JS_FreeValue(context, flags_val);
|
|
|
|
|
|
|
|
JSValue message_val = JS_JSONStringify(context, argv[0], JS_NULL, JS_NULL);
|
|
|
|
size_t size;
|
|
|
|
const char* message = JS_ToCStringLen(context, &size, message_val);
|
|
|
|
|
|
|
|
tf_ssb_connection_rpc_send(
|
|
|
|
connection,
|
|
|
|
k_ssb_rpc_flag_json | (flags_number & ~k_ssb_rpc_mask_type) | k_ssb_rpc_flag_end_error,
|
|
|
|
-request_number,
|
|
|
|
(const uint8_t*)message,
|
|
|
|
size,
|
|
|
|
NULL,
|
2022-11-02 19:34:44 -04:00
|
|
|
NULL,
|
2021-12-28 16:48:03 -05:00
|
|
|
NULL);
|
|
|
|
JS_FreeValue(context, connection_val);
|
|
|
|
JS_FreeCString(context, message);
|
|
|
|
JS_FreeValue(context, message_val);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-22 14:57:34 -05:00
|
|
|
static JSValue _tf_ssb_rpc_more(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
JSValue request_val = JS_GetPropertyStr(context, this_val, "request_number");
|
|
|
|
int32_t request_number;
|
|
|
|
JS_ToInt32(context, &request_number, request_val);
|
|
|
|
JS_FreeValue(context, request_val);
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
tf_ssb_connection_add_request(connection, -request_number, _tf_ssb_on_rpc, _tf_ssb_cleanup_value, JS_VALUE_GET_PTR(JS_DupValue(context, argv[0])));
|
2021-12-22 14:57:34 -05:00
|
|
|
|
|
|
|
JS_FreeValue(context, connection_val);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
static JSValue _tf_ssb_rpc_send_binary(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
JSValue request_val = JS_GetPropertyStr(context, this_val, "request_number");
|
|
|
|
int32_t request_number;
|
|
|
|
JS_ToInt32(context, &request_number, request_val);
|
|
|
|
JS_FreeValue(context, request_val);
|
|
|
|
|
|
|
|
size_t size;
|
2021-11-03 18:15:46 -04:00
|
|
|
uint8_t* message = tf_util_try_get_array_buffer(context, &size, argv[0]);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (message)
|
|
|
|
{
|
2021-09-06 13:50:38 -04:00
|
|
|
tf_ssb_connection_rpc_send(
|
|
|
|
connection,
|
|
|
|
k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream,
|
|
|
|
-request_number,
|
|
|
|
(const uint8_t*)message,
|
|
|
|
size,
|
|
|
|
NULL,
|
2022-11-02 19:34:44 -04:00
|
|
|
NULL,
|
2021-09-06 13:50:38 -04:00
|
|
|
NULL);
|
|
|
|
}
|
2021-12-28 16:48:03 -05:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
size_t total_size;
|
|
|
|
message = tf_util_try_get_array_buffer(context, &total_size, buffer);
|
|
|
|
if (message)
|
|
|
|
{
|
|
|
|
tf_ssb_connection_rpc_send(
|
|
|
|
connection,
|
|
|
|
k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream,
|
|
|
|
-request_number,
|
|
|
|
(const uint8_t*)message + offset,
|
|
|
|
size,
|
|
|
|
NULL,
|
2022-11-02 19:34:44 -04:00
|
|
|
NULL,
|
2021-12-28 16:48:03 -05:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, buffer);
|
|
|
|
}
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_FreeValue(context, connection_val);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
static JSValue _tf_ssb_rpc_add_room_attendant(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
tf_ssb_connection_add_room_attendant(connection, id);
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
JS_FreeValue(context, connection_val);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_rpc_remove_room_attendant(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
tf_ssb_connection_remove_room_attendant(connection, id);
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
JS_FreeValue(context, connection_val);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
|
|
|
|
JSValue object = JS_NewObject(context);
|
|
|
|
JSValue connection_object = JS_DupValue(context, tf_ssb_connection_get_object(connection));
|
|
|
|
JS_SetPropertyStr(context, object, "connection", connection_object);
|
|
|
|
JS_SetPropertyStr(context, object, "flags", JS_NewUint32(context, flags));
|
|
|
|
JS_SetPropertyStr(context, object, "request_number", JS_NewInt32(context, request_number));
|
|
|
|
JS_SetPropertyStr(context, object, "args", JS_GetPropertyStr(context, args, "args"));
|
2022-07-09 10:23:58 -04:00
|
|
|
JS_SetPropertyStr(context, object, "message", message && size ? JS_NewArrayBufferCopy(context, message, size) : JS_DupValue(context, args));
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_SetPropertyStr(context, object, "send_json", JS_NewCFunction(context, _tf_ssb_rpc_send_json, "send_json", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "send_binary", JS_NewCFunction(context, _tf_ssb_rpc_send_binary, "send_binary", 1));
|
2021-12-28 16:48:03 -05:00
|
|
|
JS_SetPropertyStr(context, object, "send_json_end", JS_NewCFunction(context, _tf_ssb_rpc_send_json_end, "send_json_end", 1));
|
2021-12-22 14:57:34 -05:00
|
|
|
JS_SetPropertyStr(context, object, "more", JS_NewCFunction(context, _tf_ssb_rpc_more, "more", 1));
|
2022-11-02 19:34:44 -04:00
|
|
|
JS_SetPropertyStr(context, object, "add_room_attendant", JS_NewCFunction(context, _tf_ssb_rpc_add_room_attendant, "add_room_attendant", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "remove_room_attendant", JS_NewCFunction(context, _tf_ssb_rpc_remove_room_attendant, "remove_room_attendant", 1));
|
2021-09-06 13:50:38 -04:00
|
|
|
|
|
|
|
JSValue result = JS_Call(context, callback, JS_UNDEFINED, 1, &object);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(context, result);
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_FreeValue(context, result);
|
|
|
|
JS_FreeValue(context, object);
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
static JSValue _tf_ssb_add_rpc(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
2021-09-06 13:50:38 -04:00
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (!JS_IsArray(context, argv[0]))
|
|
|
|
{
|
|
|
|
return JS_ThrowTypeError(context, "Expected argument 1 to be an array of strings.");
|
|
|
|
}
|
|
|
|
if (!JS_IsFunction(context, argv[1]))
|
|
|
|
{
|
|
|
|
return JS_ThrowTypeError(context, "Expected argument 2 to be a function.");
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { k_max_name_parts = 16 };
|
|
|
|
const char* name[k_max_name_parts + 1] = { 0 };
|
|
|
|
|
2022-07-09 11:13:35 -04:00
|
|
|
int length = tf_util_get_length(context, argv[0]);
|
2021-09-06 13:50:38 -04:00
|
|
|
if (length >= k_max_name_parts)
|
|
|
|
{
|
|
|
|
return JS_ThrowInternalError(context, "Too many parts to RPC name.");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
JSValue value = JS_GetPropertyUint32(context, argv[0], i);
|
|
|
|
name[i] = JS_ToCString(context, value);
|
|
|
|
JS_FreeValue(context, value);
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
tf_ssb_add_rpc_callback(ssb, name, _tf_ssb_on_rpc, _tf_ssb_cleanup_value, JS_VALUE_GET_PTR(JS_DupValue(context, argv[1])));
|
2021-09-06 13:50:38 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
JS_FreeCString(context, name[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-11-10 19:05:07 -05:00
|
|
|
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);
|
2021-12-27 16:48:16 -05:00
|
|
|
if (tf_util_report_error(context, response))
|
|
|
|
{
|
|
|
|
tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, user_data);
|
|
|
|
}
|
2021-11-10 19:05:07 -05:00
|
|
|
JS_FreeValue(context, response);
|
|
|
|
JS_FreeValue(context, string);
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
static void _tf_ssb_on_blob_want_added_callback(tf_ssb_t* ssb, const char* id, void* user_data)
|
2021-09-06 13:50:38 -04:00
|
|
|
{
|
|
|
|
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);
|
2021-12-27 16:48:16 -05:00
|
|
|
if (tf_util_report_error(context, response))
|
|
|
|
{
|
|
|
|
tf_ssb_remove_blob_want_added_callback(ssb, _tf_ssb_on_blob_want_added_callback, user_data);
|
|
|
|
}
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_FreeValue(context, response);
|
|
|
|
JS_FreeValue(context, string);
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
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)
|
2021-09-06 13:50:38 -04:00
|
|
|
{
|
|
|
|
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:
|
|
|
|
{
|
2021-09-06 14:23:22 -04:00
|
|
|
JSValue object = JS_DupValue(context, tf_ssb_connection_get_object(connection));
|
2021-09-06 13:50:38 -04:00
|
|
|
JSValue args[] =
|
|
|
|
{
|
|
|
|
JS_NewString(context, "add"),
|
|
|
|
object,
|
|
|
|
};
|
|
|
|
response = JS_Call(context, callback, JS_UNDEFINED, 2, args);
|
2021-12-27 16:48:16 -05:00
|
|
|
if (tf_util_report_error(context, response))
|
|
|
|
{
|
|
|
|
tf_ssb_remove_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, user_data);
|
|
|
|
}
|
2021-09-06 14:23:22 -04:00
|
|
|
JS_FreeValue(context, args[0]);
|
|
|
|
JS_FreeValue(context, object);
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case k_tf_ssb_change_remove:
|
|
|
|
{
|
2021-09-06 14:23:22 -04:00
|
|
|
JSValue object = JS_DupValue(context, tf_ssb_connection_get_object(connection));
|
2021-09-06 13:50:38 -04:00
|
|
|
JSValue args[] =
|
|
|
|
{
|
|
|
|
JS_NewString(context, "remove"),
|
|
|
|
object,
|
|
|
|
};
|
|
|
|
response = JS_Call(context, callback, JS_UNDEFINED, 2, args);
|
2021-12-27 16:48:16 -05:00
|
|
|
if (tf_util_report_error(context, response))
|
|
|
|
{
|
|
|
|
tf_ssb_remove_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, user_data);
|
|
|
|
}
|
2021-09-06 14:23:22 -04:00
|
|
|
JS_FreeValue(context, args[0]);
|
|
|
|
JS_FreeValue(context, object);
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, response);
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
static void _tf_ssb_on_broadcasts_changed_callback(tf_ssb_t* ssb, void* user_data)
|
2021-09-06 13:50:38 -04:00
|
|
|
{
|
2021-11-07 17:28:58 -05:00
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
|
2021-12-17 18:58:59 -05:00
|
|
|
JSValue argv = JS_UNDEFINED;
|
|
|
|
JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &argv);
|
2021-12-27 16:48:16 -05:00
|
|
|
if (tf_util_report_error(context, response))
|
|
|
|
{
|
2021-12-27 17:00:37 -05:00
|
|
|
tf_ssb_remove_broadcasts_changed_callback(ssb, _tf_ssb_on_broadcasts_changed_callback, user_data);
|
2021-12-27 16:48:16 -05:00
|
|
|
}
|
2021-11-07 17:28:58 -05:00
|
|
|
JS_FreeValue(context, response);
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-06-04 13:04:51 -04:00
|
|
|
source = tf_malloc(file_size + 1);
|
2022-09-09 21:42:15 -04:00
|
|
|
int bytes_read = fread(source, 1, file_size, file);
|
|
|
|
source[bytes_read] = '\0';
|
2021-09-06 13:50:38 -04:00
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
JSValue result = JS_Eval(context, source, file_size, file_name, 0);
|
|
|
|
if (JS_IsError(context, result))
|
|
|
|
{
|
|
|
|
printf("Error running %s.\n", file_name);
|
|
|
|
const char* value = JS_ToCString(context, result);
|
|
|
|
printf("ERROR: %s\n", value);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
JSValue stack = JS_GetPropertyStr(context, result, "stack");
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(stack))
|
|
|
|
{
|
2021-09-06 13:50:38 -04:00
|
|
|
const char* stack_str = JS_ToCString(context, stack);
|
|
|
|
printf("%s\n", stack_str);
|
|
|
|
JS_FreeCString(context, stack_str);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, stack);
|
|
|
|
}
|
|
|
|
else if (JS_IsException(result))
|
|
|
|
{
|
|
|
|
printf("Exception running %s.\n", file_name);
|
|
|
|
JSValue error = JS_GetException(context);
|
|
|
|
const char* value = JS_ToCString(context, error);
|
|
|
|
printf("Exception: %s\n", value);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
JS_FreeValue(context, error);
|
|
|
|
}
|
2021-09-08 20:15:57 -04:00
|
|
|
|
|
|
|
JSRuntime* runtime = JS_GetRuntime(context);
|
2021-10-10 17:51:38 -04:00
|
|
|
while (JS_IsJobPending(runtime))
|
|
|
|
{
|
2021-09-08 20:15:57 -04:00
|
|
|
JSContext* context2 = NULL;
|
|
|
|
int r = JS_ExecutePendingJob(runtime, &context2);
|
|
|
|
JSValue result = JS_GetException(context2);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(context, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (r == 0)
|
|
|
|
{
|
2021-09-08 20:15:57 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_FreeValue(context, result);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(source);
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
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);
|
|
|
|
}
|
2021-11-10 19:05:07 -05:00
|
|
|
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);
|
|
|
|
}
|
2021-11-07 17:28:58 -05:00
|
|
|
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);
|
|
|
|
}
|
2021-11-17 18:47:55 -05:00
|
|
|
else if (strcmp(event_name, "message") == 0)
|
2021-11-10 19:05:07 -05:00
|
|
|
{
|
|
|
|
void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback));
|
|
|
|
tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, ptr);
|
|
|
|
}
|
2021-11-07 17:28:58 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-02 19:34:44 -04:00
|
|
|
static JSValue _tf_ssb_createTunnel(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(argv[0], tf_ssb_get_connection_class_id());
|
|
|
|
int32_t request_number = 0;
|
|
|
|
JS_ToInt32(context, &request_number, argv[1]);
|
|
|
|
const char* target_id = JS_ToCString(context, argv[2]);
|
|
|
|
tf_ssb_connection_tunnel_create(connection, request_number, target_id);
|
|
|
|
JS_FreeCString(context, target_id);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct tunnel_t
|
|
|
|
{
|
|
|
|
tf_ssb_connection_t* connection;
|
|
|
|
int32_t request_number;
|
|
|
|
} tunnel_t;
|
|
|
|
|
|
|
|
void _tf_ssb_tunnel_rpc_callback(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
|
|
|
{
|
|
|
|
tunnel_t* tun = user_data;
|
|
|
|
tf_ssb_connection_rpc_send(tun->connection, flags, tun->request_number, message, size, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _tf_ssb_tunnel_cleanup(tf_ssb_t* ssb, void* user_data)
|
|
|
|
{
|
|
|
|
tf_free(user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_tunnel(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_connection_t* connection0 = JS_GetOpaque(argv[0], tf_ssb_get_connection_class_id());
|
|
|
|
int32_t request_number0 = 0;
|
|
|
|
JS_ToInt32(context, &request_number0, argv[1]);
|
|
|
|
|
|
|
|
tf_ssb_connection_t* connection1 = JS_GetOpaque(argv[2], tf_ssb_get_connection_class_id());
|
|
|
|
int32_t request_number1 = 0;
|
|
|
|
JS_ToInt32(context, &request_number1, argv[3]);
|
|
|
|
|
|
|
|
printf("TUNNEL %p %d <=> %p %d\n", connection0, request_number0, connection1, request_number1);
|
|
|
|
|
|
|
|
tunnel_t* data0 = tf_malloc(sizeof(tunnel_t));
|
|
|
|
*data0 = (tunnel_t)
|
|
|
|
{
|
|
|
|
.connection = connection1,
|
|
|
|
.request_number = request_number1,
|
|
|
|
};
|
|
|
|
tunnel_t* data1 = tf_malloc(sizeof(tunnel_t));
|
|
|
|
*data1 = (tunnel_t)
|
|
|
|
{
|
|
|
|
.connection = connection0,
|
|
|
|
.request_number = request_number0,
|
|
|
|
};
|
|
|
|
|
|
|
|
printf("ADD REQUEST %p %d\n", connection0, request_number0);
|
|
|
|
printf("ADD REQUEST %p %d\n", connection1, request_number1);
|
|
|
|
tf_ssb_connection_add_request(connection0, request_number0, _tf_ssb_tunnel_rpc_callback, _tf_ssb_tunnel_cleanup, data0);
|
|
|
|
tf_ssb_connection_add_request(connection1, request_number1, _tf_ssb_tunnel_rpc_callback, _tf_ssb_tunnel_cleanup, data1);
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
|
|
|
JS_NewClassID(&_tf_ssb_classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
JSClassDef def =
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.class_name = "ssb",
|
|
|
|
};
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _tf_ssb_classId, &def) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2022-07-31 15:01:08 -04:00
|
|
|
|
|
|
|
/* Requires an identity. */
|
2022-07-13 21:01:14 -04:00
|
|
|
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));
|
2022-09-28 19:52:44 -04:00
|
|
|
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));
|
2022-07-13 21:01:14 -04:00
|
|
|
|
2022-07-31 15:01:08 -04:00
|
|
|
/* Does not require an identity. */
|
|
|
|
JS_SetPropertyStr(context, object, "getAllIdentities", JS_NewCFunction(context, _tf_ssb_getAllIdentities, "getAllIdentities", 0));
|
2021-01-02 13:10:00 -05:00
|
|
|
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));
|
2021-01-09 18:06:33 -05:00
|
|
|
JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0));
|
2022-11-02 19:34:44 -04:00
|
|
|
JS_SetPropertyStr(context, object, "getConnection", JS_NewCFunction(context, _tf_ssb_getConnection, "getConnection", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, object, "sqlStream", JS_NewCFunction(context, _tf_ssb_sqlStream, "sqlStream", 3));
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
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));
|
2022-11-02 19:34:44 -04:00
|
|
|
JS_SetPropertyStr(context, object, "createTunnel", JS_NewCFunction(context, _tf_ssb_createTunnel, "createTunnel", 3));
|
|
|
|
JS_SetPropertyStr(context, object, "tunnel", JS_NewCFunction(context, _tf_ssb_tunnel, "tunnel", 4));
|
2021-11-07 17:28:58 -05:00
|
|
|
|
2022-07-31 15:01:08 -04:00
|
|
|
/* Should be trusted only. */
|
2021-11-07 17:28:58 -05:00
|
|
|
JS_SetPropertyStr(context, object, "addRpc", JS_NewCFunction(context, _tf_ssb_add_rpc, "addRpc", 2));
|
|
|
|
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));
|
2021-09-06 13:50:38 -04:00
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(context, global);
|
2021-09-06 13:50:38 -04:00
|
|
|
|
2021-11-03 18:15:46 -04:00
|
|
|
tf_util_register(context);
|
2021-10-24 11:46:30 -04:00
|
|
|
tf_database_register(context, tf_ssb_get_db(ssb));
|
2021-09-06 13:50:38 -04:00
|
|
|
tf_ssb_run_file(context, "core/ssb.js");
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|