From 39c942a205e0fea9b1c7f4a20143918773972095 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 6 Jan 2024 19:22:49 +0000 Subject: [PATCH] Support deleting identities, too. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4736 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/identity.json | 2 +- apps/identity/app.js | 83 ++++++++++++++++++++++++++++++++------------ core/core.js | 9 +++++ src/ssb.db.c | 25 +++++++++++++ src/ssb.db.h | 1 + src/ssb.js.c | 22 ++++++++++++ 6 files changed, 118 insertions(+), 24 deletions(-) diff --git a/apps/identity.json b/apps/identity.json index 3e7350052..91328bcbb 100644 --- a/apps/identity.json +++ b/apps/identity.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🪪", - "previous": "&BOmGofChv5mvgZq61xy5mSS0fshtttcG68uL1ILu+qo=.sha256" + "previous": "&kgukkyDk1RxgfzgMH6H/0QeDPIuwPZypLuAFax21ljk=.sha256" } \ No newline at end of file diff --git a/apps/identity/app.js b/apps/identity/app.js index 51d214b30..3e24026a7 100644 --- a/apps/identity/app.js +++ b/apps/identity/app.js @@ -3,48 +3,85 @@ import * as tfrpc from '/tfrpc.js'; tfrpc.register(async function get_private_key(id) { return bip39Words(await ssb.getPrivateKey(id)); }); +tfrpc.register(async function create_id(id) { + return await ssb.createIdentity(); +}); tfrpc.register(async function add_id(id) { return await ssb.addIdentity(bip39Bytes(id)); }); +tfrpc.register(async function delete_id(id) { + return await ssb.deleteIdentity(id); +}); +tfrpc.register(async function reload() { + await main(); +}); async function main() { let ids = await ssb.getIdentities(); await app.setDocument(` +

SSB Identity Management

+

Create a new identity

+

Import an SSB Identity from 12 BIP39 English Words

- -

Export Identities

`+ - ids.map(id => `
`).join('\n')+ - ``); + +

Identities

+ + `); } main(); \ No newline at end of file diff --git a/core/core.js b/core/core.js index c70aff828..f31519611 100644 --- a/core/core.js +++ b/core/core.js @@ -404,6 +404,15 @@ async function getProcessBlob(blobId, key, options) { }); } }; + imports.ssb.deleteIdentity = function(id) { + if (process.credentials && + process.credentials.session && + process.credentials.session.name) { + return Promise.resolve(imports.core.permissionTest('ssb_id_delete')).then(function() { + return ssb.deleteIdentity(process.credentials.session.name, id); + }); + } + }; imports.ssb.getOwnerIdentities = function() { if (options.packageOwner) { return ssb.getIdentities(options.packageOwner); diff --git a/src/ssb.db.c b/src/ssb.db.c index c51a97963..0dde7f253 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -1222,6 +1222,31 @@ bool tf_ssb_db_identity_add(tf_ssb_t* ssb, const char* user, const char* public_ return added; } +bool tf_ssb_db_identity_delete(tf_ssb_t* ssb, const char* user, const char* public_key) +{ + bool removed = false; + sqlite3* db = tf_ssb_acquire_db_writer(ssb); + sqlite3_stmt* statement = NULL; + tf_printf("deleting [%s] [%s]\n", user, public_key); + if (sqlite3_prepare(db, "DELETE FROM identities WHERE user = ? AND public_key = ?", -1, &statement, NULL) == SQLITE_OK) + { + if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK && + sqlite3_bind_text(statement, 2, public_key, -1, NULL) == SQLITE_OK) + { + removed = + sqlite3_step(statement) == SQLITE_DONE && + sqlite3_changes(db) != 0; + if (!removed) + { + tf_printf("Unable to delete identity: %s.\n", sqlite3_errmsg(db)); + } + } + sqlite3_finalize(statement); + } + tf_ssb_release_db_writer(ssb, db); + return removed; +} + void tf_ssb_db_identity_visit(tf_ssb_t* ssb, const char* user, void (*callback)(const char* identity, void* user_data), void* user_data) { sqlite3* db = tf_ssb_acquire_db_reader(ssb); diff --git a/src/ssb.db.h b/src/ssb.db.h index a0b7ccdfe..64dda5fda 100644 --- a/src/ssb.db.h +++ b/src/ssb.db.h @@ -31,6 +31,7 @@ bool tf_ssb_db_check(sqlite3* db, const char* author); int tf_ssb_db_identity_get_count_for_user(tf_ssb_t* ssb, const char* user); bool tf_ssb_db_identity_create(tf_ssb_t* ssb, const char* user, uint8_t* out_public_key, uint8_t* out_private_key); +bool tf_ssb_db_identity_delete(tf_ssb_t* ssb, const char* user, const char* public_key); bool tf_ssb_db_identity_add(tf_ssb_t* ssb, const char* user, const char* public_key, const char* private_key); void tf_ssb_db_identity_visit(tf_ssb_t* ssb, const char* user, void (*callback)(const char* identity, void* user_data), void* user_data); void tf_ssb_db_identity_visit_all(tf_ssb_t* ssb, void (*callback)(const char* identity, void* user_data), void* user_data); diff --git a/src/ssb.js.c b/src/ssb.js.c index 63956217e..74651fb08 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -139,6 +139,27 @@ static JSValue _tf_ssb_addIdentity(JSContext* context, JSValueConst this_val, in return result; } +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]); + 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; + } + } + JS_FreeCString(context, id); + JS_FreeCString(context, user); + } + return result; +} + static JSValue _set_server_following_internal(tf_ssb_t* ssb, JSValueConst this_val, JSValue id, JSValue following) { JSContext* context = tf_ssb_get_context(ssb); @@ -1720,6 +1741,7 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) /* Requires an identity. */ JS_SetPropertyStr(context, object, "createIdentity", JS_NewCFunction(context, _tf_ssb_createIdentity, "createIdentity", 1)); JS_SetPropertyStr(context, object, "addIdentity", JS_NewCFunction(context, _tf_ssb_addIdentity, "addIdentity", 2)); + JS_SetPropertyStr(context, object, "deleteIdentity", JS_NewCFunction(context, _tf_ssb_deleteIdentity, "deleteIdentity", 2)); JS_SetPropertyStr(context, object, "setServerFollowingMe", JS_NewCFunction(context, _tf_ssb_set_server_following_me, "setServerFollowingMe", 3)); JS_SetPropertyStr(context, object, "getIdentities", JS_NewCFunction(context, _tf_ssb_getIdentities, "getIdentities", 1)); JS_SetPropertyStr(context, object, "getPrivateKey", JS_NewCFunction(context, _tf_ssb_getPrivateKey, "getPrivateKey", 2));