|
|
|
@ -108,13 +108,60 @@ static JSValue _tf_ssb_createIdentity(JSContext* context, JSValueConst this_val,
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct _add_identity_t
|
|
|
|
|
{
|
|
|
|
|
uint8_t key[crypto_sign_SECRETKEYBYTES / 2];
|
|
|
|
|
bool added;
|
|
|
|
|
JSValue promise[2];
|
|
|
|
|
char user[];
|
|
|
|
|
} add_identity_t;
|
|
|
|
|
|
|
|
|
|
static void _tf_ssb_add_identity_work(tf_ssb_t* ssb, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
add_identity_t* work = user_data;
|
|
|
|
|
uint8_t public_key[crypto_sign_PUBLICKEYBYTES];
|
|
|
|
|
unsigned char seed[crypto_sign_SEEDBYTES];
|
|
|
|
|
uint8_t secret_key[crypto_sign_SECRETKEYBYTES] = { 0 };
|
|
|
|
|
memcpy(secret_key, work->key, sizeof(secret_key) / 2);
|
|
|
|
|
if (crypto_sign_ed25519_sk_to_seed(seed, secret_key) == 0 && crypto_sign_seed_keypair(public_key, secret_key, seed) == 0)
|
|
|
|
|
{
|
|
|
|
|
char public_key_b64[512];
|
|
|
|
|
tf_base64_encode(public_key, sizeof(public_key), public_key_b64, sizeof(public_key_b64));
|
|
|
|
|
snprintf(public_key_b64 + strlen(public_key_b64), sizeof(public_key_b64) - strlen(public_key_b64), ".ed25519");
|
|
|
|
|
|
|
|
|
|
uint8_t combined[crypto_sign_SECRETKEYBYTES];
|
|
|
|
|
memcpy(combined, work->key, sizeof(work->key));
|
|
|
|
|
memcpy(combined + sizeof(work->key), public_key, sizeof(public_key));
|
|
|
|
|
char combined_b64[512];
|
|
|
|
|
tf_base64_encode(combined, sizeof(combined), combined_b64, sizeof(combined_b64));
|
|
|
|
|
snprintf(combined_b64 + strlen(combined_b64), sizeof(combined_b64) - strlen(combined_b64), ".ed25519");
|
|
|
|
|
|
|
|
|
|
work->added = tf_ssb_db_identity_add(ssb, work->user, public_key_b64, combined_b64);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void _tf_ssb_add_identity_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
add_identity_t* work = user_data;
|
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
|
JSValue result = work->added ? JS_TRUE : JS_UNDEFINED;
|
|
|
|
|
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
|
|
|
JS_FreeValue(context, result);
|
|
|
|
|
tf_util_report_error(context, error);
|
|
|
|
|
JS_FreeValue(context, error);
|
|
|
|
|
JS_FreeValue(context, work->promise[0]);
|
|
|
|
|
JS_FreeValue(context, work->promise[1]);
|
|
|
|
|
tf_free(work);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_addIdentity(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]);
|
|
|
|
|
size_t user_length = 0;
|
|
|
|
|
const char* user = JS_ToCStringLen(context, &user_length, argv[0]);
|
|
|
|
|
|
|
|
|
|
JSValue buffer = JS_UNDEFINED;
|
|
|
|
|
size_t length = 0;
|
|
|
|
@ -134,67 +181,73 @@ static JSValue _tf_ssb_addIdentity(JSContext* context, JSValueConst this_val, in
|
|
|
|
|
{
|
|
|
|
|
if (length == crypto_sign_SECRETKEYBYTES / 2)
|
|
|
|
|
{
|
|
|
|
|
uint8_t public_key[crypto_sign_PUBLICKEYBYTES];
|
|
|
|
|
unsigned char seed[crypto_sign_SEEDBYTES];
|
|
|
|
|
uint8_t secret_key[crypto_sign_SECRETKEYBYTES] = { 0 };
|
|
|
|
|
memcpy(secret_key, array, sizeof(secret_key) / 2);
|
|
|
|
|
if (crypto_sign_ed25519_sk_to_seed(seed, secret_key) == 0 && crypto_sign_seed_keypair(public_key, secret_key, seed) == 0)
|
|
|
|
|
{
|
|
|
|
|
char public_key_b64[512];
|
|
|
|
|
tf_base64_encode(public_key, sizeof(public_key), public_key_b64, sizeof(public_key_b64));
|
|
|
|
|
snprintf(public_key_b64 + strlen(public_key_b64), sizeof(public_key_b64) - strlen(public_key_b64), ".ed25519");
|
|
|
|
|
|
|
|
|
|
uint8_t combined[crypto_sign_SECRETKEYBYTES];
|
|
|
|
|
memcpy(combined, array, length);
|
|
|
|
|
memcpy(combined + length, public_key, sizeof(public_key));
|
|
|
|
|
char combined_b64[512];
|
|
|
|
|
tf_base64_encode(combined, sizeof(combined), combined_b64, sizeof(combined_b64));
|
|
|
|
|
snprintf(combined_b64 + strlen(combined_b64), sizeof(combined_b64) - strlen(combined_b64), ".ed25519");
|
|
|
|
|
|
|
|
|
|
if (tf_ssb_db_identity_add(ssb, user, public_key_b64, combined_b64))
|
|
|
|
|
{
|
|
|
|
|
result = JS_TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tf_printf("Unable to add the identity.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
add_identity_t* work = tf_malloc(sizeof(add_identity_t) + user_length + 1);
|
|
|
|
|
*work = (add_identity_t) { 0 };
|
|
|
|
|
memcpy(work->key, array, sizeof(work->key));
|
|
|
|
|
memcpy(work->user, user, user_length + 1);
|
|
|
|
|
result = JS_NewPromiseCapability(context, work->promise);
|
|
|
|
|
tf_ssb_run_work(ssb, _tf_ssb_add_identity_work, _tf_ssb_add_identity_after_work, work);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tf_printf("Unexpected private key size: %zd vs. %d\n", length, crypto_sign_SECRETKEYBYTES);
|
|
|
|
|
result = JS_ThrowInternalError(context, "Unexpected private key size: %d vs. %d\n", (int)length, crypto_sign_SECRETKEYBYTES);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tf_printf("didn't find array\n");
|
|
|
|
|
result = JS_ThrowInternalError(context, "Expected array argument.");
|
|
|
|
|
}
|
|
|
|
|
JS_FreeValue(context, buffer);
|
|
|
|
|
|
|
|
|
|
JS_FreeCString(context, user);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tf_printf("no ssb\n");
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct _delete_identity_t
|
|
|
|
|
{
|
|
|
|
|
char id[k_id_base64_len];
|
|
|
|
|
bool deleted;
|
|
|
|
|
JSValue promise[2];
|
|
|
|
|
char user[];
|
|
|
|
|
} delete_identity_t;
|
|
|
|
|
|
|
|
|
|
static void _tf_ssb_delete_identity_work(tf_ssb_t* ssb, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
delete_identity_t* work = user_data;
|
|
|
|
|
work->deleted = tf_ssb_db_identity_delete(ssb, work->user, work->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void _tf_ssb_delete_identity_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
delete_identity_t* work = user_data;
|
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
|
JSValue result = work->deleted ? JS_TRUE : JS_FALSE;
|
|
|
|
|
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
|
|
|
JS_FreeValue(context, result);
|
|
|
|
|
tf_util_report_error(context, error);
|
|
|
|
|
JS_FreeValue(context, error);
|
|
|
|
|
JS_FreeValue(context, work->promise[0]);
|
|
|
|
|
JS_FreeValue(context, work->promise[1]);
|
|
|
|
|
tf_free(work);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_deleteIdentity(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]);
|
|
|
|
|
size_t user_length = 0;
|
|
|
|
|
const char* user = JS_ToCStringLen(context, &user_length, argv[0]);
|
|
|
|
|
const char* id = JS_ToCString(context, argv[1]);
|
|
|
|
|
if (id && user)
|
|
|
|
|
{
|
|
|
|
|
if (tf_ssb_db_identity_delete(ssb, user, *id == '@' ? id + 1 : id))
|
|
|
|
|
{
|
|
|
|
|
result = JS_TRUE;
|
|
|
|
|
}
|
|
|
|
|
delete_identity_t* work = tf_malloc(sizeof(delete_identity_t) + user_length + 1);
|
|
|
|
|
*work = (delete_identity_t) { 0 };
|
|
|
|
|
snprintf(work->id, sizeof(work->id), "%s", *id == '@' ? id + 1 : id);
|
|
|
|
|
memcpy(work->user, user, user_length + 1);
|
|
|
|
|
result = JS_NewPromiseCapability(context, work->promise);
|
|
|
|
|
tf_ssb_run_work(ssb, _tf_ssb_delete_identity_work, _tf_ssb_delete_identity_after_work, work);
|
|
|
|
|
}
|
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
|
JS_FreeCString(context, user);
|
|
|
|
@ -337,17 +390,53 @@ static JSValue _tf_ssb_getIdentities(JSContext* context, JSValueConst this_val,
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct _get_private_key_t
|
|
|
|
|
{
|
|
|
|
|
JSContext* context;
|
|
|
|
|
JSValue promise[2];
|
|
|
|
|
char id[k_id_base64_len];
|
|
|
|
|
uint8_t private_key[crypto_sign_SECRETKEYBYTES];
|
|
|
|
|
bool got_private_key;
|
|
|
|
|
char user[];
|
|
|
|
|
} get_private_key_t;
|
|
|
|
|
|
|
|
|
|
static void _tf_ssb_get_private_key_work(tf_ssb_t* ssb, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
get_private_key_t* work = user_data;
|
|
|
|
|
work->got_private_key = tf_ssb_db_identity_get_private_key(ssb, work->user, work->id, work->private_key, sizeof(work->private_key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void _tf_ssb_get_private_key_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
get_private_key_t* work = user_data;
|
|
|
|
|
JSValue result = JS_UNDEFINED;
|
|
|
|
|
JSContext* context = work->context;
|
|
|
|
|
if (work->got_private_key)
|
|
|
|
|
{
|
|
|
|
|
result = tf_util_new_uint8_array(context, work->private_key, sizeof(work->private_key) / 2);
|
|
|
|
|
}
|
|
|
|
|
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
|
|
|
JS_FreeValue(context, result);
|
|
|
|
|
tf_util_report_error(context, error);
|
|
|
|
|
JS_FreeValue(context, error);
|
|
|
|
|
JS_FreeValue(context, work->promise[0]);
|
|
|
|
|
JS_FreeValue(context, work->promise[1]);
|
|
|
|
|
tf_free(work);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_getPrivateKey(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* user = JS_ToCString(context, argv[0]);
|
|
|
|
|
size_t user_length = 0;
|
|
|
|
|
const char* user = JS_ToCStringLen(context, &user_length, 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)))
|
|
|
|
|
{
|
|
|
|
|
result = tf_util_new_uint8_array(context, private_key, sizeof(private_key) / 2);
|
|
|
|
|
}
|
|
|
|
|
get_private_key_t* work = tf_malloc(sizeof(get_private_key_t) + user_length + 1);
|
|
|
|
|
*work = (get_private_key_t) { .context = context };
|
|
|
|
|
memcpy(work->user, user, user_length + 1);
|
|
|
|
|
snprintf(work->id, sizeof(work->id), "%s", id);
|
|
|
|
|
JSValue result = JS_NewPromiseCapability(context, work->promise);
|
|
|
|
|
tf_ssb_run_work(ssb, _tf_ssb_get_private_key_work, _tf_ssb_get_private_key_after_work, work);
|
|
|
|
|
|
|
|
|
|
JS_FreeCString(context, user);
|
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
|
return result;
|
|
|
|
@ -610,8 +699,15 @@ static JSValue _tf_ssb_getIdentityInfo(JSContext* context, JSValueConst this_val
|
|
|
|
|
|
|
|
|
|
typedef struct _append_message_t
|
|
|
|
|
{
|
|
|
|
|
char id[k_id_base64_len];
|
|
|
|
|
uint8_t private_key[crypto_sign_SECRETKEYBYTES];
|
|
|
|
|
bool got_private_key;
|
|
|
|
|
char previous_id[512];
|
|
|
|
|
int64_t previous_sequence;
|
|
|
|
|
JSContext* context;
|
|
|
|
|
JSValue promise[2];
|
|
|
|
|
JSValue message;
|
|
|
|
|
char user[];
|
|
|
|
|
} append_message_t;
|
|
|
|
|
|
|
|
|
|
static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success, JSValue result)
|
|
|
|
@ -619,6 +715,7 @@ static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success,
|
|
|
|
|
JSValue error = JS_Call(async->context, success ? async->promise[0] : async->promise[1], JS_UNDEFINED, 1, &result);
|
|
|
|
|
tf_util_report_error(async->context, error);
|
|
|
|
|
JS_FreeValue(async->context, error);
|
|
|
|
|
JS_FreeValue(async->context, async->message);
|
|
|
|
|
JS_FreeValue(async->context, async->promise[0]);
|
|
|
|
|
JS_FreeValue(async->context, async->promise[1]);
|
|
|
|
|
tf_free(async);
|
|
|
|
@ -635,35 +732,50 @@ static void _tf_ssb_appendMessageWithIdentity_callback(const char* id, bool veri
|
|
|
|
|
_tf_ssb_appendMessage_finish(async, verified, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
|
static void _tf_ssb_append_message_with_identity_get_key_work(tf_ssb_t* ssb, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
append_message_t* async = tf_malloc(sizeof(append_message_t));
|
|
|
|
|
*async = (append_message_t) { .context = context };
|
|
|
|
|
JSValue result = JS_NewPromiseCapability(context, async->promise);
|
|
|
|
|
append_message_t* work = user_data;
|
|
|
|
|
work->got_private_key = tf_ssb_db_identity_get_private_key(ssb, work->user, work->id, work->private_key, sizeof(work->private_key));
|
|
|
|
|
tf_ssb_db_get_latest_message_by_author(ssb, work->id, &work->previous_sequence, work->previous_id, sizeof(work->previous_id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
|
if (ssb)
|
|
|
|
|
static void _tf_ssb_append_message_with_identity_get_key_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
|
|
|
{
|
|
|
|
|
append_message_t* work = user_data;
|
|
|
|
|
if (work->got_private_key)
|
|
|
|
|
{
|
|
|
|
|
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)))
|
|
|
|
|
{
|
|
|
|
|
JSValue signed_message = tf_ssb_sign_message(ssb, id, private_key, argv[2]);
|
|
|
|
|
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _tf_ssb_appendMessageWithIdentity_callback, async);
|
|
|
|
|
JS_FreeValue(context, signed_message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_tf_ssb_appendMessage_finish(async, false, JS_ThrowInternalError(context, "Unable to get private key for user %s with identity %s.", user, id));
|
|
|
|
|
}
|
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
|
JS_FreeCString(context, user);
|
|
|
|
|
JSValue signed_message = tf_ssb_sign_message(ssb, work->id, work->private_key, work->message, work->previous_id, work->previous_sequence);
|
|
|
|
|
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _tf_ssb_appendMessageWithIdentity_callback, work);
|
|
|
|
|
JS_FreeValue(work->context, signed_message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_tf_ssb_appendMessage_finish(async, false, JS_ThrowInternalError(context, "No SSB instance."));
|
|
|
|
|
_tf_ssb_appendMessage_finish(work, false, JS_ThrowInternalError(work->context, "Unable to get private key for user %s with identity %s.", work->user, work->id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
|
{
|
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
|
if (!ssb)
|
|
|
|
|
{
|
|
|
|
|
return JS_ThrowInternalError(context, "No SSB instance.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t user_length = 0;
|
|
|
|
|
const char* user = JS_ToCStringLen(context, &user_length, argv[0]);
|
|
|
|
|
const char* id = JS_ToCString(context, argv[1]);
|
|
|
|
|
|
|
|
|
|
append_message_t* work = tf_malloc(sizeof(append_message_t) + user_length + 1);
|
|
|
|
|
*work = (append_message_t) { .context = context, .message = JS_DupValue(context, argv[2]) };
|
|
|
|
|
memcpy(work->user, user, user_length + 1);
|
|
|
|
|
snprintf(work->id, sizeof(work->id), "%s", id);
|
|
|
|
|
|
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
|
JS_FreeCString(context, user);
|
|
|
|
|
|
|
|
|
|
JSValue result = JS_NewPromiseCapability(context, work->promise);
|
|
|
|
|
tf_ssb_run_work(ssb, _tf_ssb_append_message_with_identity_get_key_work, _tf_ssb_append_message_with_identity_get_key_after_work, work);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|