jr: delete key works
This commit is contained in:
+71
-1
@@ -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<void>
|
||||
{
|
||||
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<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
|
||||
@@ -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<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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//=============================================================================
|
||||
|
||||
Reference in New Issue
Block a user