[wip] I need to think seriously about zod
This commit is contained in:
@@ -1,7 +1,94 @@
|
|||||||
/**
|
/**
|
||||||
* JR Controller
|
* JR Controller
|
||||||
*
|
*
|
||||||
|
* Note that this is a *thread*. We put our business and storage logic within
|
||||||
|
* this thread. The reason we do that is we don't want to deal with the
|
||||||
|
* problem of conflicting concurrent writes..
|
||||||
|
*
|
||||||
* @module
|
* @module
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
export {
|
||||||
|
// types
|
||||||
|
jr_data,
|
||||||
|
named_keypair,
|
||||||
|
keypair
|
||||||
|
// top-level getters/setters
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data structure we store
|
||||||
|
*/
|
||||||
|
type jr_data
|
||||||
|
= {version : 0,
|
||||||
|
named_keypairs : Array<named_keypair>};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the default {@link jr_data}.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
let jr_data_mzero = {version : 0,
|
||||||
|
named_keypairs : []};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every keypair has to have a name
|
||||||
|
*/
|
||||||
|
type named_keypair
|
||||||
|
= {name : string}
|
||||||
|
& keypair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* From NaCl
|
||||||
|
*/
|
||||||
|
type keypair
|
||||||
|
= {secretKey : Uint8Array,
|
||||||
|
publicKey : Uint8Array};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the state from the system
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
fetch_migrate
|
||||||
|
()
|
||||||
|
: Promise<jr_data>
|
||||||
|
{
|
||||||
|
// try to get data
|
||||||
|
let browser_data = await browser.storage.local.get('jr');
|
||||||
|
// test if it's there
|
||||||
|
if (!!(browser_data.jr))
|
||||||
|
return browser_data.jr;
|
||||||
|
// if the data is not there
|
||||||
|
else
|
||||||
|
return jr_data_mzero;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put state
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
put
|
||||||
|
(state : jr_data)
|
||||||
|
: Promise<void>
|
||||||
|
{
|
||||||
|
await browser.storage.local.set({jr: state});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+109
-28
@@ -1,34 +1,78 @@
|
|||||||
let detect_msg
|
/**
|
||||||
= {id : "urmom",
|
* JR Content Script
|
||||||
name : "JR",
|
*
|
||||||
networkId : "ae_uat",
|
* @module
|
||||||
origin : "ur dads balls",
|
*/
|
||||||
type : "extension"};
|
|
||||||
|
|
||||||
let detect_awcp_msg
|
|
||||||
= {type : "to_aepp",
|
|
||||||
data : {jsonrpc : "2.0",
|
|
||||||
method : "connection.announcePresence",
|
|
||||||
params : detect_msg}};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function post_detect_msg() {
|
/**
|
||||||
|
* This is the "meat" of what is spammed to the page script when the user makes
|
||||||
|
* the wallet detectable.
|
||||||
|
*
|
||||||
|
* If you go read the AWCP documentation, there's a bunch of layers. This is I
|
||||||
|
* think the innermost layer.
|
||||||
|
*/
|
||||||
|
let detect_msg = {id : "urmom",
|
||||||
|
name : "JR",
|
||||||
|
networkId : "ae_uat",
|
||||||
|
origin : "ur dads balls",
|
||||||
|
type : "extension"};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the message that we spam the page script with when the user clicks
|
||||||
|
* the "make wallet detectable" button.
|
||||||
|
*
|
||||||
|
* Contains {@link detect_msg} as a field
|
||||||
|
*/
|
||||||
|
let detect_awcp_msg = {type : "to_aepp",
|
||||||
|
data : {jsonrpc : "2.0",
|
||||||
|
method : "connection.announcePresence",
|
||||||
|
params : detect_msg}};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post {@link detect_awcp_msg} into the window message queue
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
post_detect_msg
|
||||||
|
()
|
||||||
|
{
|
||||||
window.postMessage(detect_awcp_msg, '*');
|
window.postMessage(detect_awcp_msg, '*');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function mk_detectable() {
|
|
||||||
|
/**
|
||||||
|
* For 2 minutes, make the wallet detectable to a page script
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
mk_detectable
|
||||||
|
()
|
||||||
|
{
|
||||||
//while (true) {
|
//while (true) {
|
||||||
// 3 seconds times 40 is 2 minutes
|
// 3 seconds times 40 is 2 minutes
|
||||||
for (let i=1; i<=40; i++) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sleep(ms: number) {
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sleep for the given number of ms
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
sleep
|
||||||
|
(ms: number)
|
||||||
|
: Promise<void>
|
||||||
|
{
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,9 +80,13 @@ function sleep(ms: number) {
|
|||||||
/**
|
/**
|
||||||
* This handles messages from *other parts of the extension*
|
* This handles messages from *other parts of the extension*
|
||||||
*/
|
*/
|
||||||
function handler(msg: any) {
|
function
|
||||||
|
handler
|
||||||
|
(msg: any)
|
||||||
|
{
|
||||||
console.error('message: ', msg);
|
console.error('message: ', msg);
|
||||||
switch(msg) {
|
switch(msg)
|
||||||
|
{
|
||||||
case 'mk-detectable':
|
case 'mk-detectable':
|
||||||
mk_detectable();
|
mk_detectable();
|
||||||
return;
|
return;
|
||||||
@@ -47,10 +95,15 @@ function handler(msg: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This handles messages from page scripts
|
* This handles messages from page scripts
|
||||||
*/
|
*/
|
||||||
function window_message_handler(msg: {data: any}) {
|
function
|
||||||
|
window_message_handler
|
||||||
|
(msg: {data: any})
|
||||||
|
{
|
||||||
console.error('the science is coming', msg);
|
console.error('the science is coming', msg);
|
||||||
// example science:
|
// example science:
|
||||||
// {
|
// {
|
||||||
@@ -68,15 +121,17 @@ function window_message_handler(msg: {data: any}) {
|
|||||||
let the_science = msg.data;
|
let the_science = msg.data;
|
||||||
console.error('THE SCIENCE: ', the_science);
|
console.error('THE SCIENCE: ', the_science);
|
||||||
console.error('is the science good or bad?');
|
console.error('is the science good or bad?');
|
||||||
if (the_science.type === 'to_waellet') {
|
if (the_science.type === 'to_waellet')
|
||||||
|
{
|
||||||
console.error('the science is good');
|
console.error('the science is good');
|
||||||
the_science_is_good(the_science.data);
|
the_science_is_good(the_science.data);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
console.error('the science is bad');
|
console.error('the science is bad');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* window_message_handler handles all messages sent into the event bus,
|
* window_message_handler handles all messages sent into the event bus,
|
||||||
* including messages that we sent (yay js). window_message_handler branches on
|
* including messages that we sent (yay js). window_message_handler branches on
|
||||||
@@ -86,7 +141,12 @@ function window_message_handler(msg: {data: any}) {
|
|||||||
* The bottom line is this is the function that *actually* handles messages
|
* The bottom line is this is the function that *actually* handles messages
|
||||||
* from the window
|
* from the window
|
||||||
*/
|
*/
|
||||||
function the_science_is_good(awcp_rcp_data: {id: string|number, method: string, params: object}) {
|
function
|
||||||
|
the_science_is_good
|
||||||
|
(awcp_rcp_data: {id : string | number,
|
||||||
|
method : string,
|
||||||
|
params : object})
|
||||||
|
{
|
||||||
// example science data:
|
// example science data:
|
||||||
// // layer 3: json rpc
|
// // layer 3: json rpc
|
||||||
// {jsonrpc : "2.0",
|
// {jsonrpc : "2.0",
|
||||||
@@ -99,7 +159,8 @@ function the_science_is_good(awcp_rcp_data: {id: string|number, method: string,
|
|||||||
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
|
// 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);
|
||||||
break;
|
break;
|
||||||
@@ -123,7 +184,10 @@ function the_science_is_good(awcp_rcp_data: {id: string|number, method: string,
|
|||||||
*
|
*
|
||||||
* FIXME: this should query the user to see if he wants to connect
|
* FIXME: this should query the user to see if he wants to connect
|
||||||
*/
|
*/
|
||||||
function post_connect_msg(msg_ident: string|number) {
|
function
|
||||||
|
post_connect_msg
|
||||||
|
(msg_ident : string | number)
|
||||||
|
{
|
||||||
// : EventData_W2A_connection_open
|
// : EventData_W2A_connection_open
|
||||||
// http://localhost:6969/local-awcp-0.2.1/types/EventData_W2A_connection_open.html
|
// http://localhost:6969/local-awcp-0.2.1/types/EventData_W2A_connection_open.html
|
||||||
let connect_response =
|
let connect_response =
|
||||||
@@ -136,31 +200,48 @@ function post_connect_msg(msg_ident: string|number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called in response to "address.subscribe" requests from page scripts
|
* Called in response to "address.subscribe" requests from page scripts
|
||||||
*
|
*
|
||||||
* this is to get the wallet's address
|
* this is to get the wallet's address
|
||||||
*/
|
*/
|
||||||
function bg_address_subscribe(msg_ident: string|number) {
|
function
|
||||||
|
bg_address_subscribe
|
||||||
|
(msg_ident: string | number)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called in response to "transaction.sign" requests from page scripts
|
* Called in response to "transaction.sign" requests from page scripts
|
||||||
*
|
*
|
||||||
* user wants us to sign a transaction
|
* user wants us to sign a transaction
|
||||||
*/
|
*/
|
||||||
function bg_tx_sign(msg_ident: string|number, params: object) {
|
function
|
||||||
|
bg_tx_sign
|
||||||
|
(msg_ident : string | number,
|
||||||
|
params : object)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called in response to "message.sign" requests from page scripts
|
* Called in response to "message.sign" requests from page scripts
|
||||||
*
|
*
|
||||||
* user wants us to sign a message
|
* user wants us to sign a message
|
||||||
*/
|
*/
|
||||||
function bg_msg_sign(msg_ident: string|number, params: object) {
|
function
|
||||||
|
bg_msg_sign
|
||||||
|
(msg_ident : string | number,
|
||||||
|
params : object)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// @ts-ignore browser
|
// @ts-ignore browser
|
||||||
browser.runtime.onMessage.addListener(handler);
|
browser.runtime.onMessage.addListener(handler);
|
||||||
window.addEventListener('message', window_message_handler);
|
window.addEventListener('message', window_message_handler);
|
||||||
|
|||||||
@@ -205,6 +205,8 @@ ls_keypairs
|
|||||||
//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
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user