jr: delete key works

This commit is contained in:
2024-03-19 03:49:54 -06:00
parent 2f4106b799
commit 46d0fecd03
2 changed files with 88 additions and 3 deletions
+71 -1
View File
@@ -71,6 +71,7 @@ export {
p_generate_keypair, p_generate_keypair,
p_get_unique_name, p_get_unique_name,
p_activate_key, p_activate_key,
p_delete_key,
// background script api // background script api
b_main b_main
}; };
@@ -178,7 +179,7 @@ p_activate_key
(name: string) (name: string)
: Promise<void> : Promise<void>
{ {
console.log('b_lib p_activate_key'); console.log('b_lib p_activate_key', {name: name});
return await browser.runtime.sendMessage({frum: 'popup', return await browser.runtime.sendMessage({frum: 'popup',
data: {method : 'p_activate_key', data: {method : 'p_activate_key',
@@ -186,6 +187,22 @@ p_activate_key
} }
/**
* Delete given key
*/
async function
p_delete_key
(name: string)
: Promise<void>
{
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 // API: BACKGROUND SCRIPT
@@ -919,6 +936,7 @@ type bi_popup_calldata
| bi_pc_p_generate_keypair | bi_pc_p_generate_keypair
| bi_pc_p_get_unique_name | bi_pc_p_get_unique_name
| bi_pc_p_activate_key | bi_pc_p_activate_key
| bi_pc_p_delete_key
; ;
type bi_pc_p_get_data type bi_pc_p_get_data
@@ -937,6 +955,11 @@ type bi_pc_p_activate_key
= {method : 'p_activate_key', = {method : 'p_activate_key',
args : {name: string}}; args : {name: string}};
type bi_pc_p_delete_key
= {method : 'p_delete_key',
args : {name: string}};
type bi_popup_return_data type bi_popup_return_data
= p_data = p_data
| void | void
@@ -967,6 +990,8 @@ bi_msg_handler_popup
return await bi_p_get_unique_name(msg.args); return await bi_p_get_unique_name(msg.args);
case 'p_activate_key': case 'p_activate_key':
return await bi_p_activate_key(msg.args); return await bi_p_activate_key(msg.args);
case 'p_delete_key':
return await bi_p_delete_key(msg.args);
default: default:
throw new Error('unsupported popup call method'); 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<void>
{
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<bi_rich_keypair>};
let new_rich_keypairs : Array<bi_rich_keypair> = [];
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);
}
//============================================================================= //=============================================================================
+17 -2
View File
@@ -122,17 +122,32 @@ pi_repop
let addr_li = document.createElement('li'); let addr_li = document.createElement('li');
addr_li.innerHTML = 'Addr: '; addr_li.innerHTML = 'Addr: ';
// explorer link for li
let addr_a = document.createElement('a'); let addr_a = document.createElement('a');
addr_a.href = 'https://explorer.qpq.swiss/accounts/' + this_rkp.akstr; addr_a.href = 'https://explorer.qpq.swiss/accounts/' + this_rkp.akstr;
addr_a.style.fontSize = 'xx-small'; addr_a.style.fontSize = 'xx-small';
addr_a.innerHTML = this_rkp.akstr; 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 // add everything inside-out
addr_li.appendChild(addr_a);
rkp_ul.appendChild(addr_li); rkp_ul.appendChild(addr_li);
rkp_ul.appendChild(name_li); rkp_ul.appendChild(name_li);
rkp_ul.appendChild(del_li);
this_li.appendChild(rkp_ul); this_li.appendChild(rkp_ul);