[wip] state management and keypair generation

This commit is contained in:
2023-01-14 14:56:01 -07:00
parent 279bd888e3
commit da0fa03869
2 changed files with 82 additions and 30 deletions
+68 -30
View File
@@ -8,22 +8,25 @@
* runs whenever the popup opens * runs whenever the popup opens
*/ */
async function async function
main() { main()
{
// detect button action // detect button action
document.getElementById('mk-detectable').onclick = mk_detectable; document.getElementById('mk-detectable').onclick = mk_detectable;
document.getElementById('generate').onclick = generate_keypair;
// delete keypairs // delete keypairs
// browser.storage.local.clear(); // browser.storage.local.clear();
// look for keypairs relist_keypairs();
browser.storage.local.get('keypairs').then(handle_keypairs, handle_error);
} }
/** /**
* Tells the content script to spam the event bus with detection messages * Tells the content script to spam the event bus with detection messages
*/ */
async function async function
mk_detectable() { mk_detectable()
{
let ati = await active_tab_id(); let ati = await active_tab_id();
browser.tabs.sendMessage(ati, // active tab browser.tabs.sendMessage(ati, // active tab
'mk-detectable'); // message 'mk-detectable'); // message
@@ -36,12 +39,39 @@ mk_detectable() {
* gets the id of the current tab * gets the id of the current tab
*/ */
async function async function
active_tab_id() { active_tab_id()
{
let active_tabs = await browser.tabs.query({active: true, currentWindow: true}); let active_tabs = await browser.tabs.query({active: true, currentWindow: true});
return active_tabs[0].id; return active_tabs[0].id;
} }
/**
* clear the list of keypairs and relist
*
* clearing is so this can be a re-entry call
*/
function
relist_keypairs()
{
// delete all list items
pfizer(document.getElementById('keypairs'));
// look for keypairs then relist
browser.storage.local.get('keypairs').then(handle_keypairs, (err) => { console.error(err); } );
}
/**
* kill all child nodes in the dom
*/
function
pfizer(elt)
{
while (elt.firstChild) {
elt.removeChild(elt.firstChild);
}
}
/** /**
* This function is called in the init phase (after the user clicks the popup) * This function is called in the init phase (after the user clicks the popup)
* *
@@ -75,7 +105,8 @@ active_tab_id() {
* ``` * ```
*/ */
function function
handle_keypairs(obj) { handle_keypairs(obj)
{
logln('handle_keypairs'); logln('handle_keypairs');
// branch on if there are no keypairs // branch on if there are no keypairs
// no 'keypairs' key // no 'keypairs' key
@@ -96,64 +127,71 @@ handle_keypairs(obj) {
* function called if there are no keypairs * function called if there are no keypairs
*/ */
function function
no_keypairs() { no_keypairs()
{
logln('no keypairs!'); logln('no keypairs!');
document.getElementById('no-keypairs').hidden = false; document.getElementById('no-keypairs').hidden = false;
} }
//function
//generate_keypair() { /**
// logln('generating a keypair'); * This function is called when the user clicks the button to generate a keypair
// let keypair = nacl.sign.keyPair(); */
// keypair.name = "Untitled Keypair 1"; function
// browser.storage.local.set({'keypairs': [keypair]}); generate_keypair()
// ls_keypairs([keypair]); {
//} logln('generating a keypair');
let keypair = nacl.sign.keyPair();
keypair.name = "Untitled Keypair 1";
// fixme: ADD keypair (factor out into separate function)
browser.storage.local.set({'keypairs': [keypair]});
relist_keypairs();
}
/** /**
* This function is called in the case where there is at least one keypair, and * This function is called in the case where there is at least one keypair, and
* it renders the list of keypairs * it renders the list of keypairs
*/ */
function function
ls_keypairs(keypairs) { ls_keypairs(keypairs)
{
logln('keypairs!'); logln('keypairs!');
console.log('keypairs: ', keypairs) console.log('keypairs: ', keypairs)
// make keypair list visible // make keypair list visible
document.getElementById('keypairs').hidden = false; let keypairs_ul = document.getElementById('keypairs');
keypairs_ul.hidden = false;
// render each keypair // render each keypair
for (let kp of keypairs) { for (let kp of keypairs) {
console.log(kp); console.log(kp);
render_keypair(kp); render_keypair(keypairs_ul, kp);
} }
//let keypairs_json = JSON.stringify(keypairs, undefined, 4); //let keypairs_json = JSON.stringify(keypairs, undefined, 4);
//document.getElementById('keypairs').innerHTML = keypairs_json; //document.getElementById('keypairs').innerHTML = keypairs_json;
} }
/** /**
* This renders a single keypair item in the list of keypairs * This renders a single keypair item in the list of keypairs
*/ */
function function
render_keypair(parent_ul, {name, publicKey, current}) { render_keypair(parent_ul, {name, publicKey})
{
// create the element // create the element
let li = document.createElement('li'); let li = document.createElement('li');
li.innerHTML += name; li.innerHTML += name;
parent_ul.appendChild(li); parent_ul.appendChild(li);
} }
/** /**
* This function is called in the init phase (after the user clicks the popup) * Console.log the message and put it in the log thing
* in the case when querying the keypairs fails
*
* Have not encountered this in practice
*/ */
function function
handle_error(error) { logln(message)
console.error('keypair fetch failed:', error); {
}
function
logln(message) {
console.log(message); console.log(message);
document.getElementById('log').innerHTML += message; document.getElementById('log').innerHTML += message;
document.getElementById('log').innerHTML += '\n'; document.getElementById('log').innerHTML += '\n';
+14
View File
@@ -0,0 +1,14 @@
% Seed phrases
% See: https://en.bitcoin.it/wiki/BIP_0039#Reference_Implementation
-module(vsp).
-compile([export_all, nowarn_export_all]).
-spec encode(SecretKey) -> SeedPhrase
when SecretKey :: <<_:128>>,
SeedPhrase :: [string()].
encode(SecretKey) ->
<<CheckBytes:4/binary, _/binary>> = crypto:hash(sha256, Key).