/** * This is the script for the popup window */ import * as b_lib from './b_lib.js'; p_main(); /** * runs whenever the popup opens * * also whenever a button is clicked it appears the page re-renders which * re-runs this * * suggests the following structure: * * 1. Contact the background script and say "I'm a popup". * 2. Get all relevant data. * 3. Render * * On click: * * 1. Send relevant information back to background script * 2. Epstein */ async function p_main () : Promise { //let scratch = document.getElementById('scratch')!; //scratch.innerText += 'line 17 hello\n'; console.log('popup 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 () { console.log('popup p_main onclick_generate'); // @ts-ignore fuck off let name : string = document.getElementById('generate-name')!.value; await b_lib.p_generate_keypair(name); pi_repop(); }; document .getElementById('generate-btn')! .addEventListener('click', onclick_generate); } /** * query background library for popup display data and populate fields with it */ async function pi_repop () : Promise { console.log('popup pi_repop'); let popup_data: b_lib.p_data = await b_lib.p_get_data(); // show keypairs // step 1: delete the existing list of keypairs let kill_all_children = function (elt : HTMLElement) { // from: https://www.geeksforgeeks.org/remove-all-the-child-elements-of-a-dom-node-in-javascript/ // kill the last child repeatedly let child = elt.lastElementChild; while (child) { elt.removeChild(child); child = elt.lastElementChild; } }; // get the ul let pubkeys_ul = document.getElementById('keypairs')!; // delete the existing list of keypairs // we will repopulate it in a minute // this isn't necessary right now but in the future we will add a delete // keypair function so this is prophylaxis kill_all_children(pubkeys_ul); // type p_data // = {rich_pubkeys: Array}; // type p_rich_pubkey // = {akstr : string, // active : boolean, // name : string}; // keypairs let rkps : Array = popup_data.rich_pubkeys; let no_keypairs : boolean = (0 === rkps.length); // if the keypairs are empty unhide the "no keypairs" paragraph if (no_keypairs) document.getElementById('no-keypairs')!.hidden = false; // if the keypairs are not empty else { // hide the "no keypairs" paragraph document.getElementById('no-keypairs')!.hidden = true; // for each rich keypair // each one of these is an ak_... string for (let this_rkp of rkps) { // make an li let this_li = document.createElement('li'); // make a ul let rkp_ul = document.createElement('ul'); // make a li for the name let name_li = document.createElement('li'); name_li.innerHTML = 'Name: ' + this_rkp.name; // make li for address let addr_li = document.createElement('li'); addr_li.innerHTML = 'Addr: '; // explorer link for li let addr_a = document.createElement('a'); addr_a.href = 'https://explorer.qpq.swiss/accounts/' + this_rkp.akstr; addr_a.style.fontSize = 'xx-small'; addr_a.innerHTML = this_rkp.akstr; // add addr_li.appendChild(addr_a); // add delete button let del_li = document.createElement('li'); let del_a = document.createElement('a'); del_a.href = '#' del_a.innerHTML = this_rkp.akstr; del_a.innerHTML = 'Delete this key'; del_a.onclick = async function () { await b_lib.p_delete_key(this_rkp.name); await pi_repop(); }; del_li.appendChild(del_a); // add everything inside-out rkp_ul.appendChild(addr_li); rkp_ul.appendChild(name_li); rkp_ul.appendChild(del_li); this_li.appendChild(rkp_ul); // if active, make background green if (this_rkp.active) this_li.style.background = 'green'; // otherwise make background red and add activate li else { this_li.style.background = 'red'; let activate_li = document.createElement('li'); let activate_a = document.createElement('a'); activate_a.innerHTML = 'Switch to this key'; activate_a.href = '#'; // dirty but so is your mind so go fuck yourself activate_a.onclick = async function () { // tell controller that we want to activate this key await b_lib.p_activate_key(this_rkp.name); await pi_repop(); }; // add li and a activate_li.appendChild(activate_a); rkp_ul.appendChild(activate_li); } // add it to the list pubkeys_ul.appendChild(this_li); } } } /** * shorten the ak_... str */ function pi_shakstr (akstr : string) : string { console.log('popup shakstr'); return akstr.slice(0, 10) + '...'; } /** * get account balance of ak_... str */ async function pi_balance (akstr : string) : Promise< {ok : true, balance : number} | {ok : false, reason : string} > { let endpoint_init_url = 'http://demonet.qpq.swiss:3013'; let this_endpoint = endpoint_init_url + '/accounts/' + akstr; let input_params = {pubkey: akstr}; // fetch: https://developer.mozilla.org/en-US/docs/Web/API/fetch let response = await fetch(this_endpoint); // 200 = ok // 400 = pubkey malformed // 404 = account dne switch (response.status) { case 200: // unstringify the body let return_data = response.json(); // get the balance // @ts-ignore talking to foreign api let balance = return_data.balance; return {ok : true, balance : balance}; // pubkey malformed case 400: return {ok : false, reason : 'Pubkey Malformed'}; // account dne case 404: return {ok : false, reason : 'Account DNE'}; // wtf default: return {ok : false, reason : 'Unexpected response from node'}; } }