From ae646417aa2bb6e7fdacf9b491af95f0541023fb Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 18 Mar 2024 23:32:55 -0600 Subject: [PATCH] jr: generating new keypair works --- jrx/src/b_lib.ts | 121 ++++++++++++++++++++++++++++++++++++++--------- jrx/src/popup.ts | 2 +- 2 files changed, 99 insertions(+), 24 deletions(-) diff --git a/jrx/src/b_lib.ts b/jrx/src/b_lib.ts index 7c4e7b4..fb37de1 100644 --- a/jrx/src/b_lib.ts +++ b/jrx/src/b_lib.ts @@ -67,6 +67,7 @@ export type { export { // popup script api p_get_data, + p_generate_keypair, // background script api b_main }; @@ -115,30 +116,32 @@ p_get_data () : Promise { - console.log('popup p_data'); + console.log('b_lib p_get_data'); - // type bi_state - // = {keypairs: Array}; - // type bi_nacl_keypair - // = {secretKey : Uint8Array, - // publicKey : Uint8Array}; - let jr_state : bi_state = await bi_get_state(); - - // accumulator - let akstrs : Array = []; - - for (let this_keypair of jr_state.keypairs) { - let this_pubkey : Uint8Array = this_keypair.publicKey; - let this_ak_str : string = await vdk_aeser.pubkey2ak_str(this_pubkey); - akstrs.push(this_ak_str); - } - - // type p_data = - // {pubkey_ak_strs : Array}; - return {pubkey_ak_strs: akstrs}; + return await browser.runtime.sendMessage({frum: 'popup', + data: {method : 'p_get_data', + args : {}}}) as p_data; } + +/** + * Called when user clicks "generate keypair" button in popup window + * + * Destructively updates jr state + */ +async function +p_generate_keypair + () + : Promise +{ + console.log('b_lib p_generate_keypair'); + + return await browser.runtime.sendMessage({frum: 'popup', + data: {method : 'p_generate_keypair', + args : {}}}) as void; +} + //============================================================================= //============================================================================= // API: BACKGROUND SCRIPT @@ -748,11 +751,20 @@ bi_mk_w2a_msg_err type bi_popup_calldata - = 'init'; + = bi_pc_p_get_data + | bi_pc_p_generate_keypair; +type bi_pc_p_get_data = + {method : 'p_get_data', + args : {}}; + +type bi_pc_p_generate_keypair = + {method : 'p_generate_keypair', + args : {}}; type bi_popup_return_data - = 'die'; + = p_data + | void; /** @@ -768,11 +780,74 @@ bi_msg_handler_popup : Promise { console.log('jr bg_msg_handler_popup msg', msg); - return 'die'; + + switch(msg.method) { + case 'p_get_data': + return await bi_p_get_data(msg.args); + case 'p_generate_keypair': + return await bi_p_generate_keypair(msg.args); + default: + throw new Error('unsupported popup call method'); + } } +async function +bi_p_get_data + (args: {}) + : Promise +{ + console.log('b_lib bi_p_get_data'); + // type bi_state + // = {keypairs: Array}; + // type bi_nacl_keypair + // = {secretKey : Uint8Array, + // publicKey : Uint8Array}; + let jr_state : bi_state = await bi_get_state(); + + // accumulator + let akstrs : Array = []; + + for (let this_keypair of jr_state.keypairs) { + let this_pubkey : Uint8Array = this_keypair.publicKey; + let this_ak_str : string = await vdk_aeser.pubkey2ak_str(this_pubkey); + akstrs.push(this_ak_str); + } + + // type p_data = + // {pubkey_ak_strs : Array}; + return {pubkey_ak_strs: akstrs}; +} + + + +async function +bi_p_generate_keypair + (args: {}) + : Promise +{ + console.log('b_lib bi_p_generate_keypair'); + + // get state + let state : bi_state = await bi_get_state(); + + // type bi_state + // = {keypairs: Array}; + // type bi_nacl_keypair + // = {secretKey : Uint8Array, + // publicKey : Uint8Array}; + // @ts-ignore fuck off typescript + let new_keypair : bi_nacl_keypair = nacl.sign.keyPair() as bi_nacl_keypair; + state.keypairs.push(new_keypair); + + // set new state + await bi_set_state(state); +} + + + + //============================================================================= //============================================================================= // INTERNALS: STORAGE API diff --git a/jrx/src/popup.ts b/jrx/src/popup.ts index dc49e58..3678235 100644 --- a/jrx/src/popup.ts +++ b/jrx/src/popup.ts @@ -38,7 +38,7 @@ p_main let onclick_generate = async function () { console.log('popup p_main onclick_generate'); - // await b_lib.p_generate_keypair(); + await b_lib.p_generate_keypair(); pi_repop(); }; document