diff --git a/jrx/src/b_lib.ts b/jrx/src/b_lib.ts index eee194f..42cbdd9 100644 --- a/jrx/src/b_lib.ts +++ b/jrx/src/b_lib.ts @@ -71,6 +71,7 @@ export { p_generate_keypair, p_get_unique_name, p_activate_key, + p_delete_key, // background script api b_main }; @@ -178,7 +179,7 @@ p_activate_key (name: string) : Promise { - console.log('b_lib p_activate_key'); + console.log('b_lib p_activate_key', {name: name}); return await browser.runtime.sendMessage({frum: 'popup', data: {method : 'p_activate_key', @@ -186,6 +187,22 @@ p_activate_key } +/** + * Delete given key + */ +async function +p_delete_key + (name: string) + : Promise +{ + console.log('b_lib p_delete_key', {name: name}); + + return await browser.runtime.sendMessage({frum: 'popup', + data: {method : 'p_delete_key', + args : {name: name}}}) as void; +} + + //============================================================================= //============================================================================= // API: BACKGROUND SCRIPT @@ -919,6 +936,7 @@ type bi_popup_calldata | bi_pc_p_generate_keypair | bi_pc_p_get_unique_name | bi_pc_p_activate_key + | bi_pc_p_delete_key ; type bi_pc_p_get_data @@ -937,6 +955,11 @@ type bi_pc_p_activate_key = {method : 'p_activate_key', args : {name: string}}; +type bi_pc_p_delete_key + = {method : 'p_delete_key', + args : {name: string}}; + + type bi_popup_return_data = p_data | void @@ -967,6 +990,8 @@ bi_msg_handler_popup return await bi_p_get_unique_name(msg.args); case 'p_activate_key': return await bi_p_activate_key(msg.args); + case 'p_delete_key': + return await bi_p_delete_key(msg.args); default: throw new Error('unsupported popup call method'); } @@ -1124,6 +1149,51 @@ bi_p_activate_key } +async function +bi_p_delete_key + (args: {name: string}) + : Promise +{ + console.log('b_lib bi_p_delete_key ' + args.name); + + // get state + let istate : bi_state = await bi_get_state(); + + // find key with name + // type bi_rich_keypair + // = {nacl_keypair : bi_nacl_keypair, + // name : string, + // active : boolean}; + // type bi_state + // = {rich_keypairs: Array}; + let new_rich_keypairs : Array = []; + let deleting_active_key : boolean = false; + for (let this_irkp of istate.rich_keypairs) { + // if this is the name, don't push + if (args.name === this_irkp.name) { + deleting_active_key = this_irkp.active; + continue; + } + else + new_rich_keypairs.push(this_irkp); + } + + // if the result is that no keys are active + // two cases: + // 1. no keys left, in which case do nothing + // 2. at least one key left, in which case, make the first one active + // cannot have a situation where there are keys and no key is active + if (deleting_active_key && !(0 === new_rich_keypairs.length)) { + // grab first keypair and set it to active + new_rich_keypairs[0].active = true; + } + + let new_state : bi_state = {rich_keypairs: new_rich_keypairs}; + await bi_set_state(new_state); +} + + + //============================================================================= diff --git a/jrx/src/popup.ts b/jrx/src/popup.ts index eead36d..0a56bec 100644 --- a/jrx/src/popup.ts +++ b/jrx/src/popup.ts @@ -122,17 +122,32 @@ pi_repop let addr_li = document.createElement('li'); addr_li.innerHTML = 'Addr: '; + // explorer link for li let addr_a = document.createElement('a'); addr_a.href = 'https://explorer.qpq.swiss/accounts/' + this_rkp.akstr; addr_a.style.fontSize = 'xx-small'; addr_a.innerHTML = this_rkp.akstr; + // add + addr_li.appendChild(addr_a); - // add + // add delete button + let del_li = document.createElement('li'); + + let del_a = document.createElement('a'); + del_a.href = '#' + del_a.innerHTML = this_rkp.akstr; + del_a.innerHTML = 'Delete this key'; + del_a.onclick = + async function () { + await b_lib.p_delete_key(this_rkp.name); + await pi_repop(); + }; + del_li.appendChild(del_a); // add everything inside-out - addr_li.appendChild(addr_a); rkp_ul.appendChild(addr_li); rkp_ul.appendChild(name_li); + rkp_ul.appendChild(del_li); this_li.appendChild(rkp_ul);