[wip] getting keypair stuff to work
This commit is contained in:
+73
-14
@@ -17,20 +17,11 @@ function post_detect_msg() {
|
|||||||
window.postMessage(detect_awcp_msg, '*');
|
window.postMessage(detect_awcp_msg, '*');
|
||||||
}
|
}
|
||||||
|
|
||||||
function post_connect_msg(msg_ident) {
|
|
||||||
// : EventData_W2A_connection_open
|
|
||||||
// http://localhost:6969/local-awcp-0.2.1/types/EventData_W2A_connection_open.html
|
|
||||||
let connect_response =
|
|
||||||
{type: "to_aepp",
|
|
||||||
data: {jsonrpc: "2.0",
|
|
||||||
id: msg_ident,
|
|
||||||
method: "connection.open",
|
|
||||||
result: detect_msg}};
|
|
||||||
window.postMessage(connect_response, '*');
|
|
||||||
}
|
|
||||||
|
|
||||||
async function mk_detectable() {
|
async function mk_detectable() {
|
||||||
while (true) {
|
//while (true) {
|
||||||
|
// 3 seconds times 40 is 2 minutes
|
||||||
|
for (let i=1; i<=40; i++) {
|
||||||
console.error('pee');
|
console.error('pee');
|
||||||
post_detect_msg();
|
post_detect_msg();
|
||||||
await sleep(3000);
|
await sleep(3000);
|
||||||
@@ -42,6 +33,9 @@ function sleep(ms) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This handles messages from *other parts of the extension*
|
||||||
|
*/
|
||||||
function handler(msg) {
|
function handler(msg) {
|
||||||
console.error('message: ', msg);
|
console.error('message: ', msg);
|
||||||
switch(msg) {
|
switch(msg) {
|
||||||
@@ -53,6 +47,9 @@ function handler(msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This handles messages from page scripts
|
||||||
|
*/
|
||||||
function window_message_handler(msg) {
|
function window_message_handler(msg) {
|
||||||
console.error('the science is coming', msg);
|
console.error('the science is coming', msg);
|
||||||
// example science:
|
// example science:
|
||||||
@@ -80,6 +77,15 @@ function window_message_handler(msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* window_message_handler handles all messages sent into the event bus,
|
||||||
|
* including messages that we sent (yay js). window_message_handler branches on
|
||||||
|
* whether the message is for us or not (is the science good or bad?). If the
|
||||||
|
* message is for us (the science is good), then this function is triggered.
|
||||||
|
*
|
||||||
|
* The bottom line is this is the function that *actually* handles messages
|
||||||
|
* from the window
|
||||||
|
*/
|
||||||
function the_science_is_good(awcp_rcp_data) {
|
function the_science_is_good(awcp_rcp_data) {
|
||||||
// example science data:
|
// example science data:
|
||||||
// // layer 3: json rpc
|
// // layer 3: json rpc
|
||||||
@@ -92,19 +98,72 @@ function the_science_is_good(awcp_rcp_data) {
|
|||||||
// can assume it is a call
|
// can assume it is a call
|
||||||
let msg_ident = awcp_rcp_data.id;
|
let msg_ident = awcp_rcp_data.id;
|
||||||
let msg_method = awcp_rcp_data.method;
|
let msg_method = awcp_rcp_data.method;
|
||||||
|
// branch here on the "method" field
|
||||||
switch (msg_method) {
|
switch (msg_method) {
|
||||||
case "connection.open":
|
case "connection.open":
|
||||||
post_connect_msg(msg_ident);
|
post_connect_msg(msg_ident);
|
||||||
return;
|
break;
|
||||||
|
case "address.subscribe":
|
||||||
|
bg_address_subscribe(msg_ident);
|
||||||
|
break;
|
||||||
|
case "transaction.sign":
|
||||||
|
bg_tx_sign(msg_ident, awcp_rcp_data.params);
|
||||||
|
break;
|
||||||
|
case "message.sign":
|
||||||
|
bg_msg_sign(msg_ident, awcp_rcp_data.params);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('the science is worse than i initially thought');
|
console.error('the science is worse than i initially thought');
|
||||||
console.error("we're going to have to put the science to sleep");
|
console.error("we're going to have to put the science to sleep");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called in response to "connection.open" requests from page scripts
|
||||||
|
*
|
||||||
|
* FIXME: this should query the user to see if he wants to connect
|
||||||
|
*/
|
||||||
|
function post_connect_msg(msg_ident) {
|
||||||
|
// : EventData_W2A_connection_open
|
||||||
|
// http://localhost:6969/local-awcp-0.2.1/types/EventData_W2A_connection_open.html
|
||||||
|
let connect_response =
|
||||||
|
{type: "to_aepp",
|
||||||
|
data: {jsonrpc: "2.0",
|
||||||
|
id: msg_ident,
|
||||||
|
method: "connection.open",
|
||||||
|
result: detect_msg}};
|
||||||
|
window.postMessage(connect_response, '*');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called in response to "address.subscribe" requests from page scripts
|
||||||
|
*
|
||||||
|
* this is to get the wallet's address
|
||||||
|
*/
|
||||||
|
function bg_address_subscribe(msg_ident) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called in response to "transaction.sign" requests from page scripts
|
||||||
|
*
|
||||||
|
* user wants us to sign a transaction
|
||||||
|
*/
|
||||||
|
function bg_tx_sign(msg_ident, params) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called in response to "message.sign" requests from page scripts
|
||||||
|
*
|
||||||
|
* user wants us to sign a message
|
||||||
|
*/
|
||||||
|
function bg_msg_sign(msg_ident, params) {
|
||||||
|
}
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(handler);
|
browser.runtime.onMessage.addListener(handler);
|
||||||
window.addEventListener('message', window_message_handler);
|
window.addEventListener('message', window_message_handler);
|
||||||
//window.addEventListener('message', function(evt) { console.error('BYYYYYYYYYYYYYYY', evt) });
|
//window.addEventListener('message', function(evt) { console.error('BYYYYYYYYYYYYYYY', evt) });
|
||||||
window.addEventListener('click', function() { console.error('HERRRO'); });
|
//window.addEventListener('click', function() { console.error('HERRRO'); });
|
||||||
|
|
||||||
console.error('poop');
|
console.error('poop');
|
||||||
|
|||||||
+2
-1
@@ -8,4 +8,5 @@
|
|||||||
"js" : ["content.js"]}],
|
"js" : ["content.js"]}],
|
||||||
"browser_action" : {"default_icon" : "icons/jrx-icon-32.png",
|
"browser_action" : {"default_icon" : "icons/jrx-icon-32.png",
|
||||||
"default_title" : "Jaeck Russell",
|
"default_title" : "Jaeck Russell",
|
||||||
"default_popup" : "popup.html"}}
|
"default_popup" : "popup.html"},
|
||||||
|
"permissions" : ["storage"]}
|
||||||
|
|||||||
+1178
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,12 @@
|
|||||||
|
MODEL
|
||||||
|
|
||||||
|
popup.html/popup.js
|
||||||
|
- this is our user interface, generally
|
||||||
|
raseev loops:
|
||||||
|
- raseevs data from the content script
|
||||||
|
- raseevs click events from end-user
|
||||||
|
|
||||||
|
content.js
|
||||||
|
- this is basically a process that
|
||||||
|
|
||||||
|
background script
|
||||||
+18
-1
@@ -5,9 +5,26 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<button id="mk-detectable">Make Wallet Detectable</button>
|
|
||||||
|
<h1>Jack Russell</h1>
|
||||||
|
|
||||||
|
<button id="mk-detectable">Make Wallet Detectable for 2 minutes</button>
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
<h2>Keypairs</h2>
|
||||||
|
<p id="no-keypairs" hidden>No keypairs yet. Click the button below to generate one.
|
||||||
|
</p>
|
||||||
|
<ul id="keypairs" hidden>
|
||||||
|
</ul>
|
||||||
|
<button id="generate">Generate new keypair</button>
|
||||||
|
|
||||||
|
<h2>Log</h2>
|
||||||
|
<pre id="log"></pre>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- including nacl here -->
|
||||||
|
<script src="nacl.js"></script>
|
||||||
|
|
||||||
<script type="module" src="popup.js"></script>
|
<script type="module" src="popup.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
+159
-7
@@ -1,15 +1,167 @@
|
|||||||
async function active_tab_id() {
|
/**
|
||||||
let active_tabs = await browser.tabs.query({active: true, currentWindow: true});
|
* This is the script for the popup window
|
||||||
return active_tabs[0].id;
|
*/
|
||||||
|
|
||||||
|
//import {default as nacl} from './nacl.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* runs whenever the popup opens
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
main() {
|
||||||
|
// detect button action
|
||||||
|
document.getElementById('mk-detectable').onclick = mk_detectable;
|
||||||
|
|
||||||
|
// delete keypairs
|
||||||
|
// browser.storage.local.clear();
|
||||||
|
// look for keypairs
|
||||||
|
browser.storage.local.get('keypairs').then(handle_keypairs, handle_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function mk_detectable() {
|
|
||||||
|
/**
|
||||||
|
* Tells the content script to spam the event bus with detection messages
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
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
|
||||||
document.getElementById('mk-detectable').disabled = true;
|
document.getElementById('mk-detectable').disabled = true;
|
||||||
|
// FIXME: update button with detect info
|
||||||
}
|
}
|
||||||
|
|
||||||
// detect button action
|
|
||||||
document.getElementById('mk-detectable').onclick = mk_detectable;
|
/**
|
||||||
|
* gets the id of the current tab
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
active_tab_id() {
|
||||||
|
let active_tabs = await browser.tabs.query({active: true, currentWindow: true});
|
||||||
|
return active_tabs[0].id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function is called in the init phase (after the user clicks the popup)
|
||||||
|
*
|
||||||
|
* If we have keypairs, put them in the keypairs element in the popup window
|
||||||
|
*
|
||||||
|
* Example call objects:
|
||||||
|
*
|
||||||
|
* Case of no keypairs:
|
||||||
|
*
|
||||||
|
* ```js
|
||||||
|
* obj = {}
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Case of a named keypair
|
||||||
|
*
|
||||||
|
* ```js
|
||||||
|
* obj = {
|
||||||
|
* keypairs: [
|
||||||
|
* {
|
||||||
|
* name: string,
|
||||||
|
* publicKey: uint8array
|
||||||
|
* secretKey: uint8array
|
||||||
|
* },
|
||||||
|
* {
|
||||||
|
* name: string,
|
||||||
|
* publicKey: uint8array
|
||||||
|
* secretKey: uint8array
|
||||||
|
* }
|
||||||
|
* ]
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
handle_keypairs(obj) {
|
||||||
|
logln('handle_keypairs');
|
||||||
|
// branch on if there are no keypairs
|
||||||
|
// no 'keypairs' key
|
||||||
|
if (!!!obj.keypairs) {
|
||||||
|
no_keypairs();
|
||||||
|
}
|
||||||
|
// 'keypairs' key but points to empty array
|
||||||
|
else if (0 === obj.keypairs.length) {
|
||||||
|
no_keypairs();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ls_keypairs(obj.keypairs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function called if there are no keypairs
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
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 in the case where there is at least one keypair, and
|
||||||
|
* it renders the list of keypairs
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
ls_keypairs(keypairs) {
|
||||||
|
logln('keypairs!');
|
||||||
|
console.log('keypairs: ', keypairs)
|
||||||
|
// make keypair list visible
|
||||||
|
document.getElementById('keypairs').hidden = false;
|
||||||
|
// render each keypair
|
||||||
|
for (let kp of keypairs) {
|
||||||
|
console.log(kp);
|
||||||
|
render_keypair(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}) {
|
||||||
|
// 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
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
handle_error(error) {
|
||||||
|
console.error('keypair fetch failed:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function
|
||||||
|
logln(message) {
|
||||||
|
console.log(message);
|
||||||
|
document.getElementById('log').innerHTML += message;
|
||||||
|
document.getElementById('log').innerHTML += '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* send the given message to the tab
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
main();
|
||||||
|
|||||||
Reference in New Issue
Block a user