diff --git a/jrx/popup.js b/jrx/popup.js index fd44802..f2efe4a 100644 --- a/jrx/popup.js +++ b/jrx/popup.js @@ -8,22 +8,25 @@ * runs whenever the popup opens */ async function -main() { +main() +{ // detect button action document.getElementById('mk-detectable').onclick = mk_detectable; + document.getElementById('generate').onclick = generate_keypair; // delete keypairs // browser.storage.local.clear(); - // look for keypairs - browser.storage.local.get('keypairs').then(handle_keypairs, handle_error); + relist_keypairs(); } + /** * Tells the content script to spam the event bus with detection messages */ async function -mk_detectable() { +mk_detectable() +{ let ati = await active_tab_id(); browser.tabs.sendMessage(ati, // active tab 'mk-detectable'); // message @@ -36,12 +39,39 @@ mk_detectable() { * gets the id of the current tab */ async function -active_tab_id() { +active_tab_id() +{ let active_tabs = await browser.tabs.query({active: true, currentWindow: true}); 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) * @@ -75,7 +105,8 @@ active_tab_id() { * ``` */ function -handle_keypairs(obj) { +handle_keypairs(obj) +{ logln('handle_keypairs'); // branch on if there are no keypairs // no 'keypairs' key @@ -96,64 +127,71 @@ handle_keypairs(obj) { * function called if there are no keypairs */ function -no_keypairs() { +no_keypairs() +{ logln('no keypairs!'); document.getElementById('no-keypairs').hidden = false; } -//function -//generate_keypair() { -// logln('generating a keypair'); -// let keypair = nacl.sign.keyPair(); -// keypair.name = "Untitled Keypair 1"; -// browser.storage.local.set({'keypairs': [keypair]}); -// ls_keypairs([keypair]); -//} + +/** + * This function is called when the user clicks the button to generate a keypair + */ +function +generate_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 * it renders the list of keypairs */ function -ls_keypairs(keypairs) { +ls_keypairs(keypairs) +{ logln('keypairs!'); console.log('keypairs: ', keypairs) // make keypair list visible - document.getElementById('keypairs').hidden = false; + let keypairs_ul = document.getElementById('keypairs'); + keypairs_ul.hidden = false; // render each keypair for (let kp of keypairs) { console.log(kp); - render_keypair(kp); + render_keypair(keypairs_ul, kp); } //let keypairs_json = JSON.stringify(keypairs, undefined, 4); //document.getElementById('keypairs').innerHTML = keypairs_json; } + /** * This renders a single keypair item in the list of keypairs */ function -render_keypair(parent_ul, {name, publicKey, current}) { +render_keypair(parent_ul, {name, publicKey}) +{ // create the element let li = document.createElement('li'); li.innerHTML += name; parent_ul.appendChild(li); } + + /** - * This function is called in the init phase (after the user clicks the popup) - * in the case when querying the keypairs fails - * - * Have not encountered this in practice + * Console.log the message and put it in the log thing */ function -handle_error(error) { - console.error('keypair fetch failed:', error); -} - - -function -logln(message) { +logln(message) +{ console.log(message); document.getElementById('log').innerHTML += message; document.getElementById('log').innerHTML += '\n'; diff --git a/utils/vw/src/vsp.erl b/utils/vw/src/vsp.erl new file mode 100644 index 0000000..f0bbea5 --- /dev/null +++ b/utils/vw/src/vsp.erl @@ -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) -> + <> = crypto:hash(sha256, Key).