jr: now enforcing uniqueness of account names

This commit is contained in:
2024-03-19 02:53:21 -06:00
parent 96c45bbe68
commit 0e6ba80983
2 changed files with 84 additions and 8 deletions
+80 -8
View File
@@ -69,6 +69,7 @@ export {
// popup script api // popup script api
p_get_data, p_get_data,
p_generate_keypair, p_generate_keypair,
p_get_unique_name,
// background script api // background script api
b_main b_main
}; };
@@ -136,6 +137,8 @@ p_get_data
* Called when user clicks "generate keypair" button in popup window * Called when user clicks "generate keypair" button in popup window
* *
* Destructively updates jr state * Destructively updates jr state
*
* TODO: send back error if name is not unique
*/ */
async function async function
p_generate_keypair p_generate_keypair
@@ -150,6 +153,22 @@ p_generate_keypair
} }
/**
* Get unique name for new keypair
*/
async function
p_get_unique_name
()
: Promise<string>
{
console.log('b_lib p_generate_keypair');
return await browser.runtime.sendMessage({frum: 'popup',
data: {method : 'p_get_unique_name',
args : {}}}) as string;
}
//============================================================================= //=============================================================================
//============================================================================= //=============================================================================
// API: BACKGROUND SCRIPT // API: BACKGROUND SCRIPT
@@ -880,19 +899,27 @@ bi_mk_w2a_msg_err
type bi_popup_calldata type bi_popup_calldata
= bi_pc_p_get_data = bi_pc_p_get_data
| bi_pc_p_generate_keypair; | bi_pc_p_generate_keypair
| bi_pc_p_get_unique_name
;
type bi_pc_p_get_data = type bi_pc_p_get_data
{method : 'p_get_data', = {method : 'p_get_data',
args : {}}; args : {}};
type bi_pc_p_generate_keypair = type bi_pc_p_generate_keypair
{method : 'p_generate_keypair', = {method : 'p_generate_keypair',
args : {name: string}}; args : {name: string}};
type bi_pc_p_get_unique_name
= {method : 'p_get_unique_name',
args : {}};
type bi_popup_return_data type bi_popup_return_data
= p_data = p_data
| void; | void
| string
;
/** /**
@@ -914,6 +941,8 @@ bi_msg_handler_popup
return await bi_p_get_data(msg.args); return await bi_p_get_data(msg.args);
case 'p_generate_keypair': case 'p_generate_keypair':
return await bi_p_generate_keypair(msg.args); return await bi_p_generate_keypair(msg.args);
case 'p_get_unique_name':
return await bi_p_get_unique_name(msg.args);
default: default:
throw new Error('unsupported popup call method'); throw new Error('unsupported popup call method');
} }
@@ -987,6 +1016,10 @@ bi_p_generate_keypair
// if there are no keypairs, the first one is active // if there are no keypairs, the first one is active
let active : boolean = bi_no_keypairs(state); let active : boolean = bi_no_keypairs(state);
// if name is not unique change it
if (!bi_name_unique(state, args.name))
args.name = await bi_p_get_unique_name({});
// update state // update state
state.rich_keypairs.push({nacl_keypair : new_keypair, state.rich_keypairs.push({nacl_keypair : new_keypair,
active : active, active : active,
@@ -997,6 +1030,45 @@ bi_p_generate_keypair
} }
async function
bi_p_get_unique_name
(args: {})
: Promise<string>
{
console.log('b_lib bi_p_get_unique_name');
// get state
let istate : bi_state = await bi_get_state();
let default_name = 'default';
let i : number = 0;
while (true)
{
if (bi_name_unique(istate, 'default' + i))
return 'default' + i;
else
i = i + 1;
}
}
function
bi_name_unique
(istate : bi_state,
name : string)
: boolean
{
for (let this_irkp of istate.rich_keypairs)
{
if (name === this_irkp.name)
return false;
}
return true;
}
//============================================================================= //=============================================================================
+4
View File
@@ -34,6 +34,10 @@ p_main
await pi_repop(); await pi_repop();
// add unique name for default
// @ts-ignore fuck off
document.getElementById('generate-name')!.value = await b_lib.p_get_unique_name();
// add hooks for each button // add hooks for each button
let onclick_generate = let onclick_generate =
async function () { async function () {