tildefriends/apps/identity/app.js

39 lines
1.2 KiB
JavaScript

import * as tfrpc from '/tfrpc.js';
tfrpc.register(async function get_private_key(id) {
return bip39Words(await ssb.getPrivateKey(id));
});
tfrpc.register(async function add_id(id) {
return await ssb.addIdentity(bip39Bytes(id));
});
async function main() {
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;
}
async function add_id(event) {
let id = document.getElementById('add_id').value;
await tfrpc.rpc.add_id(id);
}
window.addEventListener('load', function() {
for (let button of document.getElementsByTagName('button')) {
if (button.id == "add") {
button.onclick = add_id;
} else {
button.onclick = export_id;
}
}
});
</script>
<input type="text" id="add_id"></input><button id="add">Add ID</button>`+
ids.map(id => `<div><button>${id}</button></div>`).join('\n')+
`</body>`);
}
main();