2024-01-03 19:21:15 -05:00
import * as tfrpc from '/tfrpc.js' ;
tfrpc . register ( async function get _private _key ( id ) {
return bip39Words ( await ssb . getPrivateKey ( id ) ) ;
} ) ;
2024-01-06 14:22:49 -05:00
tfrpc . register ( async function create _id ( id ) {
return await ssb . createIdentity ( ) ;
} ) ;
2024-01-03 20:17:30 -05:00
tfrpc . register ( async function add _id ( id ) {
return await ssb . addIdentity ( bip39Bytes ( id ) ) ;
} ) ;
2024-01-06 14:22:49 -05:00
tfrpc . register ( async function delete _id ( id ) {
return await ssb . deleteIdentity ( id ) ;
} ) ;
tfrpc . register ( async function reload ( ) {
await main ( ) ;
} ) ;
2024-01-03 19:21:15 -05:00
async function main ( ) {
let ids = await ssb . getIdentities ( ) ;
2024-02-22 09:36:45 -05:00
await app . setDocument (
` <body style="color: #fff">
2024-01-06 14:22:49 -05:00
< script > const handler = { } ; < / s c r i p t >
2024-01-03 19:21:15 -05:00
< script type = "module" >
import * as tfrpc from '/static/tfrpc.js' ;
2024-01-06 14:22:49 -05:00
handler . export _id = async function export _id ( event ) {
let id = event . srcElement . dataset . id ;
2024-01-06 13:40:57 -05:00
let element = document . createElement ( 'textarea' ) ;
element . value = await tfrpc . rpc . get _private _key ( id ) ;
element . style = 'width: 100%; read-only: true' ;
element . readOnly = true ;
2024-01-06 14:22:49 -05:00
event . srcElement . parentElement . appendChild ( element ) ;
event . srcElement . onclick = event => handler . hide _id ( event , element ) ;
2024-01-03 19:21:15 -05:00
}
2024-01-06 14:22:49 -05:00
handler . add _id = async function add _id ( event ) {
2024-01-03 20:17:30 -05:00
let id = document . getElementById ( 'add_id' ) . value ;
2024-01-06 14:22:49 -05:00
try {
let new _id = await tfrpc . rpc . add _id ( id ) ;
alert ( 'Successfully imported: ' + new _id ) ;
await tfrpc . rpc . reload ( ) ;
} catch ( e ) {
alert ( 'Error importing identity: ' + e ) ;
}
2024-01-03 20:17:30 -05:00
}
2024-01-06 14:22:49 -05:00
handler . create _id = async function create _id ( event ) {
try {
let id = await tfrpc . rpc . create _id ( ) ;
alert ( 'Successfully created: ' + id ) ;
await tfrpc . rpc . reload ( ) ;
} catch ( e ) {
alert ( 'Error creating identity: ' + e ) ;
}
}
handler . hide _id = function hide _id ( event , element ) {
2024-01-06 13:40:57 -05:00
element . parentNode . removeChild ( element ) ;
2024-01-06 14:22:49 -05:00
event . srcElement . onclick = handler . export _id ;
2024-01-06 13:40:57 -05:00
}
2024-01-06 14:22:49 -05:00
handler . delete _id = async function delete _id ( event ) {
let id = event . srcElement . dataset . id ;
try {
if ( prompt ( 'Are you sure you want to delete "' + id + '"? It cannot be recovered without the exported phrase.\\n\\nEnter the word "DELETE" to confirm you wish to delete it.' ) === 'DELETE' ) {
if ( await tfrpc . rpc . delete _id ( id ) ) {
alert ( 'Successfully deleted ID: ' + id ) ;
}
await tfrpc . rpc . reload ( ) ;
2024-01-03 20:17:30 -05:00
}
2024-01-06 14:22:49 -05:00
} catch ( e ) {
alert ( 'Error deleting ID: ' + e ) ;
2024-01-03 19:21:15 -05:00
}
2024-01-06 14:22:49 -05:00
}
2024-01-03 20:17:30 -05:00
< / s c r i p t >
2024-01-06 13:40:57 -05:00
< h1 > SSB Identity Management < / h 1 >
2024-01-06 14:22:49 -05:00
< h2 > Create a new identity < / h 2 >
2024-01-06 14:52:14 -05:00
< button id = "create_id" onclick = "handler.create_id()" > Create Identity < / b u t t o n >
2024-01-06 13:40:57 -05:00
< h2 > Import an SSB Identity from 12 BIP39 English Words < / h 2 >
2024-01-06 14:22:49 -05:00
< textarea id = "add_id" style = "width: 100%" rows = "4" > < / t e x t a r e a > < b u t t o n i d = " a d d " o n c l i c k = " h a n d l e r . a d d _ i d ( e v e n t ) " > I m p o r t I d e n t i t y < / b u t t o n >
< h2 > Identities < / h 2 >
2024-02-22 09:36:45 -05:00
< ul > ` +
ids
. map (
( id ) => ` <li>
2024-01-06 14:22:49 -05:00
< button onclick = "handler.export_id(event)" data - id = "${id}" > Export Identity < / b u t t o n >
< button onclick = "handler.delete_id(event)" data - id = "${id}" > Delete Identity < / b u t t o n >
$ { id }
2024-02-22 09:36:45 -05:00
< / l i > `
)
. join ( '\n' ) +
` </ul>
< / b o d y > `
) ;
2024-01-03 19:21:15 -05:00
}
2024-01-06 14:52:14 -05:00
main ( ) ;