forked from cory/tildefriends
Tiny steps toward getting away from one global identity.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3932 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
85
src/ssb.js.c
85
src/ssb.js.c
@ -32,6 +32,87 @@ static JSValue _tf_ssb_whoami(JSContext* context, JSValueConst this_val, int arg
|
||||
return JS_NULL;
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
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;
|
||||
JS_SetPropertyUint32(state->context, state->array, state->count++, JS_NewString(state->context, identity));
|
||||
}
|
||||
|
||||
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_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;
|
||||
@ -765,6 +846,10 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
|
||||
JSValue object = JS_NewObjectClass(context, _tf_ssb_classId);
|
||||
JS_SetPropertyStr(context, global, "ssb", object);
|
||||
JS_SetOpaque(object, ssb);
|
||||
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, "whoami", JS_NewCFunction(context, _tf_ssb_whoami, "whoami", 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));
|
||||
|
Reference in New Issue
Block a user