Some plumbing to export an SSB identity from Tilde Friends.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4732 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2024-01-04 00:21:15 +00:00
parent 4c8eab2692
commit 8ab53f2da3
4 changed files with 68 additions and 0 deletions

5
apps/identity.json Normal file
View File

@ -0,0 +1,5 @@
{
"type": "tildefriends-app",
"emoji": "🪪",
"previous": "&XHcSLE9zb9QH0hFGAza/5wuIv8jj2NKARlOvMBpMAzc=.sha256"
}

37
apps/identity/app.js Normal file
View File

@ -0,0 +1,37 @@
import * as tfrpc from '/tfrpc.js';
tfrpc.register(async function get_private_key(id) {
return bip39Words(await ssb.getPrivateKey(id));
});
async function main() {
/*
let words = 'body hair useful camp warm into cause riot two bamboo kick educate dinosaur advice seed type crisp where guilt avocado output rely lunch goddess';
let bytes = base64Decode('GO0Lv5BvcuuJJdHrokHoo0PmCDC/XjO/SZ6H+ddq4UvWd/VPW1RJrjd1aCUIfPIojFXrWMb8R54vVerU2TwjdQ==').slice(0, 32);
let data = {
ids: ids,
words: bip39Words(bytes),
bytes: base64Encode(bip39Bytes(words)),
round: bip39Words((await bip39Bytes(words)).slice(0, 32)),
privates: (await Promise.all(ids.map(id => ssb.getPrivateKey(id)))).map(x => bip39Words(x)),
};*/
let ids = await ssb.getIdentities();
await app.setDocument(`<body style="color: #fff">
<script type="module">
import * as tfrpc from '/static/tfrpc.js';
async function export_id(event) {
let id = event.srcElement.innerHTML;
document.body.insertBefore(document.createTextNode(await tfrpc.rpc.get_private_key(id)), event.srcElement.parentNode.nextSibling);
event.srcElement.disabled = true;
}
window.addEventListener('load', function() {
for (let button of document.getElementsByTagName('button')) {
button.onclick = export_id;
}
});
</script>`+
ids.map(id => `<div><button>${id}</button></div>`).join('\n')+
`</body>`);
}
main();

View File

@ -407,6 +407,15 @@ async function getProcessBlob(blobId, key, options) {
return ssb.getIdentities(process.credentials.session.name);
}
};
imports.ssb.getPrivateKey = function(id) {
if (process.credentials &&
process.credentials.session &&
process.credentials.session.name) {
return Promise.resolve(imports.core.permissionTest('ssb_id_export')).then(function() {
return ssb.getPrivateKey(process.credentials.session.name, id);
});
}
};
imports.ssb.appendMessageWithIdentity = function(id, message) {
if (process.credentials &&
process.credentials.session &&

View File

@ -162,6 +162,22 @@ static JSValue _tf_ssb_getIdentities(JSContext* context, JSValueConst this_val,
return result;
}
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]);
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);
}
JS_FreeCString(context, user);
JS_FreeCString(context, id);
return result;
}
static JSValue _tf_ssb_getServerIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_NewArray(context);
@ -1630,6 +1646,7 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
JS_SetPropertyStr(context, object, "createIdentity", JS_NewCFunction(context, _tf_ssb_createIdentity, "createIdentity", 1));
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));
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));
JS_SetPropertyStr(context, object, "privateMessageEncrypt", JS_NewCFunction(context, _tf_ssb_private_message_encrypt, "privateMessageEncrypt", 4));