From 0e6ba80983fd967d252d87768719959fb45ee0d0 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 19 Mar 2024 02:53:21 -0600 Subject: [PATCH] jr: now enforcing uniqueness of account names --- jrx/src/b_lib.ts | 88 +++++++++++++++++++++++++++++++++++++++++++----- jrx/src/popup.ts | 4 +++ 2 files changed, 84 insertions(+), 8 deletions(-) diff --git a/jrx/src/b_lib.ts b/jrx/src/b_lib.ts index 5de802b..45a34cf 100644 --- a/jrx/src/b_lib.ts +++ b/jrx/src/b_lib.ts @@ -69,6 +69,7 @@ export { // popup script api p_get_data, p_generate_keypair, + p_get_unique_name, // background script api b_main }; @@ -136,6 +137,8 @@ p_get_data * Called when user clicks "generate keypair" button in popup window * * Destructively updates jr state + * + * TODO: send back error if name is not unique */ async function p_generate_keypair @@ -150,6 +153,22 @@ p_generate_keypair } +/** + * Get unique name for new keypair + */ +async function +p_get_unique_name + () + : Promise +{ + 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 @@ -880,19 +899,27 @@ bi_mk_w2a_msg_err type bi_popup_calldata = 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 = - {method : 'p_get_data', - args : {}}; +type bi_pc_p_get_data + = {method : 'p_get_data', + args : {}}; -type bi_pc_p_generate_keypair = - {method : 'p_generate_keypair', - args : {name: string}}; +type bi_pc_p_generate_keypair + = {method : 'p_generate_keypair', + args : {name: string}}; + +type bi_pc_p_get_unique_name + = {method : 'p_get_unique_name', + args : {}}; type bi_popup_return_data = p_data - | void; + | void + | string + ; /** @@ -914,6 +941,8 @@ bi_msg_handler_popup return await bi_p_get_data(msg.args); case 'p_generate_keypair': return await bi_p_generate_keypair(msg.args); + case 'p_get_unique_name': + return await bi_p_get_unique_name(msg.args); default: 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 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 state.rich_keypairs.push({nacl_keypair : new_keypair, active : active, @@ -997,6 +1030,45 @@ bi_p_generate_keypair } +async function +bi_p_get_unique_name + (args: {}) + : Promise +{ + 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; +} + + + //============================================================================= diff --git a/jrx/src/popup.ts b/jrx/src/popup.ts index 565a190..6d0074d 100644 --- a/jrx/src/popup.ts +++ b/jrx/src/popup.ts @@ -34,6 +34,10 @@ p_main 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 let onclick_generate = async function () {