diff --git a/jrx/src/background.ts b/jrx/src/background.ts index 5ffe9f9..0e28a8b 100644 --- a/jrx/src/background.ts +++ b/jrx/src/background.ts @@ -1,7 +1,94 @@ /** * 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 */ +export { + // types + jr_data, + named_keypair, + keypair + // top-level getters/setters +}; + + + +/** + * The data structure we store + */ +type jr_data + = {version : 0, + named_keypairs : Array}; + + + +/** + * 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 +{ + // 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 +{ + await browser.storage.local.set({jr: state}); +} + + + + diff --git a/jrx/src/content.ts b/jrx/src/content.ts index 73431e1..24702d5 100644 --- a/jrx/src/content.ts +++ b/jrx/src/content.ts @@ -1,34 +1,78 @@ -let detect_msg - = {id : "urmom", - name : "JR", - networkId : "ae_uat", - origin : "ur dads balls", - type : "extension"}; - -let detect_awcp_msg - = {type : "to_aepp", - data : {jsonrpc : "2.0", - method : "connection.announcePresence", - params : detect_msg}}; +/** + * JR Content Script + * + * @module + */ -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, '*'); } -async function mk_detectable() { + +/** + * For 2 minutes, make the wallet detectable to a page script + */ +async function +mk_detectable + () +{ //while (true) { // 3 seconds times 40 is 2 minutes - for (let i=1; i<=40; i++) { + for (let i=1; i<=40; i++) + { console.error('pee'); post_detect_msg(); await sleep(3000); } } -function sleep(ms: number) { + + +/** + * sleep for the given number of ms + */ +async function +sleep + (ms: number) + : Promise +{ return new Promise(resolve => setTimeout(resolve, ms)); } @@ -36,9 +80,13 @@ function sleep(ms: number) { /** * This handles messages from *other parts of the extension* */ -function handler(msg: any) { +function +handler + (msg: any) +{ console.error('message: ', msg); - switch(msg) { + switch(msg) + { case 'mk-detectable': mk_detectable(); return; @@ -47,10 +95,15 @@ function handler(msg: any) { } } + + /** * 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); // example science: // { @@ -68,15 +121,17 @@ function window_message_handler(msg: {data: any}) { let the_science = msg.data; console.error('THE SCIENCE: ', the_science); 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'); the_science_is_good(the_science.data); } - else { + else console.error('the science is bad'); - } } + + /** * window_message_handler handles all messages sent into the event bus, * 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 * 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: // // layer 3: json rpc // {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_method = awcp_rcp_data.method; // branch here on the "method" field - switch (msg_method) { + switch (msg_method) + { case "connection.open": post_connect_msg(msg_ident); 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 */ -function post_connect_msg(msg_ident: string|number) { +function +post_connect_msg + (msg_ident : string | number) +{ // : EventData_W2A_connection_open // http://localhost:6969/local-awcp-0.2.1/types/EventData_W2A_connection_open.html 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 * * 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 * * 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 * * 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 browser.runtime.onMessage.addListener(handler); window.addEventListener('message', window_message_handler); diff --git a/jrx/src/popup.ts b/jrx/src/popup.ts index f56f4dc..1c43063 100644 --- a/jrx/src/popup.ts +++ b/jrx/src/popup.ts @@ -205,6 +205,8 @@ ls_keypairs //document.getElementById('keypairs').innerHTML = keypairs_json; } + + /** * This renders a single keypair item in the list of keypairs */