tildefriends/apps/identity/app.js

50 lines
1.6 KiB
JavaScript
Raw Normal View History

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;
let element = document.createElement('textarea');
element.value = await tfrpc.rpc.get_private_key(id);
element.style = 'width: 100%; read-only: true';
element.readOnly = true;
document.body.insertBefore(element, event.srcElement.parentNode.nextSibling);
event.srcElement.onclick = event => hide_id(event, element);
}
async function add_id(event) {
let id = document.getElementById('add_id').value;
await tfrpc.rpc.add_id(id);
}
function hide_id(event, element) {
element.parentNode.removeChild(element);
event.srcElement.onclick = export_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>
<h1>SSB Identity Management</h1>
<h2>Import an SSB Identity from 12 BIP39 English Words</h2>
<textarea id="add_id" style="width: 100%" rows="4"></textarea><button id="add">Add ID</button>
<h2>Export Identities</h2>`+
ids.map(id => `<div><button>${id}</button></div>`).join('\n')+
`</body>`);
}
main();