forked from cory/tildefriends
ssb.addIdentity without hitting the DB from the main thread.
This commit is contained in:
parent
7997a739ab
commit
a09edfb612
90
src/ssb.js.c
90
src/ssb.js.c
@ -108,13 +108,60 @@ static JSValue _tf_ssb_createIdentity(JSContext* context, JSValueConst this_val,
|
|||||||
return result;
|
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)
|
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);
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
||||||
JSValue result = JS_UNDEFINED;
|
JSValue result = JS_UNDEFINED;
|
||||||
if (ssb)
|
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;
|
JSValue buffer = JS_UNDEFINED;
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
@ -134,50 +181,25 @@ static JSValue _tf_ssb_addIdentity(JSContext* context, JSValueConst this_val, in
|
|||||||
{
|
{
|
||||||
if (length == crypto_sign_SECRETKEYBYTES / 2)
|
if (length == crypto_sign_SECRETKEYBYTES / 2)
|
||||||
{
|
{
|
||||||
uint8_t public_key[crypto_sign_PUBLICKEYBYTES];
|
add_identity_t* work = tf_malloc(sizeof(add_identity_t) + user_length + 1);
|
||||||
unsigned char seed[crypto_sign_SEEDBYTES];
|
*work = (add_identity_t) { 0 };
|
||||||
uint8_t secret_key[crypto_sign_SECRETKEYBYTES] = { 0 };
|
memcpy(work->key, array, sizeof(work->key));
|
||||||
memcpy(secret_key, array, sizeof(secret_key) / 2);
|
memcpy(work->user, user, user_length + 1);
|
||||||
if (crypto_sign_ed25519_sk_to_seed(seed, secret_key) == 0 && crypto_sign_seed_keypair(public_key, secret_key, seed) == 0)
|
result = JS_NewPromiseCapability(context, work->promise);
|
||||||
{
|
tf_ssb_run_work(ssb, _tf_ssb_add_identity_work, _tf_ssb_add_identity_after_work, work);
|
||||||
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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tf_printf("Unexpected private key size: %zd vs. %d\n", length, crypto_sign_SECRETKEYBYTES);
|
result = JS_ThrowInternalError(context, "Unexpected private key size: %zd vs. %d\n", length, crypto_sign_SECRETKEYBYTES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tf_printf("didn't find array\n");
|
result = JS_ThrowInternalError(context, "Expected array argument.");
|
||||||
}
|
}
|
||||||
JS_FreeValue(context, buffer);
|
JS_FreeValue(context, buffer);
|
||||||
|
|
||||||
JS_FreeCString(context, user);
|
JS_FreeCString(context, user);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
tf_printf("no ssb\n");
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user