jr: generating new keypair works

This commit is contained in:
2024-03-18 23:32:55 -06:00
parent b6aa8fe7cc
commit ae646417aa
2 changed files with 99 additions and 24 deletions
+97 -22
View File
@@ -67,6 +67,7 @@ export type {
export { export {
// popup script api // popup script api
p_get_data, p_get_data,
p_generate_keypair,
// background script api // background script api
b_main b_main
}; };
@@ -115,30 +116,32 @@ p_get_data
() ()
: Promise<p_data> : Promise<p_data>
{ {
console.log('popup p_data'); console.log('b_lib p_get_data');
// type bi_state return await browser.runtime.sendMessage({frum: 'popup',
// = {keypairs: Array<bi_nacl_keypair>}; data: {method : 'p_get_data',
// type bi_nacl_keypair args : {}}}) as p_data;
// = {secretKey : Uint8Array,
// publicKey : Uint8Array};
let jr_state : bi_state = await bi_get_state();
// accumulator
let akstrs : Array<string> = [];
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<string>};
return {pubkey_ak_strs: akstrs};
}
/**
* Called when user clicks "generate keypair" button in popup window
*
* Destructively updates jr state
*/
async function
p_generate_keypair
()
: Promise<void>
{
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 // API: BACKGROUND SCRIPT
@@ -748,11 +751,20 @@ bi_mk_w2a_msg_err
type bi_popup_calldata 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 type bi_popup_return_data
= 'die'; = p_data
| void;
/** /**
@@ -768,8 +780,71 @@ bi_msg_handler_popup
: Promise<bi_popup_return_data> : Promise<bi_popup_return_data>
{ {
console.log('jr bg_msg_handler_popup msg', msg); 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<p_data>
{
console.log('b_lib bi_p_get_data');
// type bi_state
// = {keypairs: Array<bi_nacl_keypair>};
// type bi_nacl_keypair
// = {secretKey : Uint8Array,
// publicKey : Uint8Array};
let jr_state : bi_state = await bi_get_state();
// accumulator
let akstrs : Array<string> = [];
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<string>};
return {pubkey_ak_strs: akstrs};
}
async function
bi_p_generate_keypair
(args: {})
: Promise<void>
{
console.log('b_lib bi_p_generate_keypair');
// get state
let state : bi_state = await bi_get_state();
// type bi_state
// = {keypairs: Array<bi_nacl_keypair>};
// 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);
}
+1 -1
View File
@@ -38,7 +38,7 @@ p_main
let onclick_generate = let onclick_generate =
async function () { async function () {
console.log('popup p_main onclick_generate'); console.log('popup p_main onclick_generate');
// await b_lib.p_generate_keypair(); await b_lib.p_generate_keypair();
pi_repop(); pi_repop();
}; };
document document