From 546f990b97c49bc78182181c01b5c0dbc1d8b181 Mon Sep 17 00:00:00 2001 From: Peter Harpending Date: Mon, 23 Oct 2023 13:55:53 -0600 Subject: [PATCH] [wip] done genericizing popup (works!) now moving on to mansplaining --- jrx/jex.eterms | 2 +- jrx/src/b_lib.ts | 59 ++++++++++++--------------------- libs/vdk_aeser/jex.eterms | 2 +- libs/vdk_aeser/src/vdk_aeser.ts | 22 +++++++++++- 4 files changed, 44 insertions(+), 41 deletions(-) diff --git a/jrx/jex.eterms b/jrx/jex.eterms index 92fea89..ba5d888 100644 --- a/jrx/jex.eterms +++ b/jrx/jex.eterms @@ -5,5 +5,5 @@ {deps, ["local-tweetnacl-1.0.3", "local-awcp-0.2.3", "local-vdk_aecrypt-0.1.2", - "local-vdk_aeser-0.1.0", + "local-vdk_aeser-0.1.1", "local-vdk_binary-0.1.0"]}. diff --git a/jrx/src/b_lib.ts b/jrx/src/b_lib.ts index 3505e13..7f3cb8e 100644 --- a/jrx/src/b_lib.ts +++ b/jrx/src/b_lib.ts @@ -53,7 +53,7 @@ import * as awcp from './jex_include/local-awcp-0.2.3/dist/awcp.js'; import * as vdk_aecrypt from './jex_include/local-vdk_aecrypt-0.1.2/dist/vdk_aecrypt.js'; -import * as vdk_aeser from './jex_include/local-vdk_aeser-0.1.0/dist/vdk_aeser.js'; +import * as vdk_aeser from './jex_include/local-vdk_aeser-0.1.1/dist/vdk_aeser.js'; import * as vdk_binary from './jex_include/local-vdk_binary-0.1.0/dist/vdk_binary.js'; @@ -69,6 +69,14 @@ export { b_main }; +// INTERNAL CONSTANTS ALL IN ONE PLACE +let GB_ITER_MS = 5; // [MS / ITER] 5 ms every 1 iteration +let GB_ITERS = 1; +let GB_SEC = 200*GB_ITERS; // [ITER]; 1iter / 5ms = 200iter / sec +let GB_MIN = 60*GB_SEC; // [ITER] + +let MSG_SIGN_TIMEOUT = 30*GB_MIN; +let TX_SIGN_TIMEOUT = 30*GB_MIN; //============================================================================= @@ -381,12 +389,10 @@ msg_sign | {result : 'bad'} > { - let result: 'good' | 'bad' - = await gb('Do you want to sign this message?', - msg_str, - 30*GB_MIN); + let result : gb = await gb('Do you want to sign this message?', msg_str, MSG_SIGN_TIMEOUT); - // if confirm + // if the user wants us to sign the transaction, return the signed transaction + // confirm if (result === 'good') { // use nacl detached signatures @@ -401,6 +407,7 @@ msg_sign return {result : 'good', signature : signature_str}; } + // otherwise don't else { return {result : 'bad'}; } @@ -420,41 +427,19 @@ msg_sign */ async function tx_sign - (tx_str : string, + (tx_str : string, secret_key : Uint8Array) : Promise< {result : 'good', signedTransaction : string} | {result : 'bad'} > { - // does the user want to sign the message - let confirm_window = await browser.windows.create({url : '../pages/tx_confirm.html', - type : 'popup'}); + let mansplained_tx : string = await vdk_aeser.mansplain_str(tx_str); + let result : gb = await gb('Do you want to sign this transaction?', + mansplained_tx, + TX_SIGN_TIMEOUT); - // @ts-ignore shut the fuck up - let tabid : number = confirm_window.tabs[0].id; - - // stupid hack because otherwise the message gets sent before the listener in the page script is created - await sleep(200); - - // debug: show tx - let mansplained_tx : object = await vdk_aeser.mansplain(tx_str); - console.log('mansplained_tx,', mansplained_tx); - - type browser_send_data = {tx_str : string, - tx_data : object}; - - let result: 'good' | 'bad' - = await browser.tabs.sendMessage(tabid, - {tx_str : tx_str, - tx_data : mansplained_tx}); - - - console.log('result', result); - - // close the popup - browser.tabs.remove(tabid); - // if we're supposed to sign the tx + // if we're supposed to sign the tx, then sign it if ('good' === result) { let tx_bytes : Uint8Array = (await vdk_aeser.unbaseNcheck(tx_str)).bytes; @@ -463,6 +448,7 @@ tx_sign // we sign <> // SerializedObject can either be the object or the hash of the object // let's stick with hash for now + // FIXME: get network id from application state let network_id : Uint8Array = vdk_binary.encode_utf8('ae_uat'); // let tx_hash_bytes : Uint8Array = hash(tx_bytes); let sign_data : Uint8Array = vdk_binary.bytes_concat(network_id, tx_bytes); @@ -483,10 +469,7 @@ tx_sign } -let GB_ITER_MS = 5; // [MS / ITER] 5 ms every 1 iteration -let GB_ITERS = 1; -let GB_SEC = 200*GB_ITERS; // [ITER]; 1iter / 5ms = 200iter / sec -let GB_MIN = 60*GB_SEC; // [ITER] + /** * This pops up a window for the luser and asks if the thing is good or bad. diff --git a/libs/vdk_aeser/jex.eterms b/libs/vdk_aeser/jex.eterms index 30b6647..fd960e0 100644 --- a/libs/vdk_aeser/jex.eterms +++ b/libs/vdk_aeser/jex.eterms @@ -1,7 +1,7 @@ {type, library}. {realm, local}. {name, vdk_aeser}. -{version, "0.1.0"}. +{version, "0.1.1"}. {deps, ["local-vdk_base58-0.1.0", "local-vdk_base64-0.1.0", "local-vdk_rlp-0.1.0", diff --git a/libs/vdk_aeser/src/vdk_aeser.ts b/libs/vdk_aeser/src/vdk_aeser.ts index 648a1c5..7c5cb63 100644 --- a/libs/vdk_aeser/src/vdk_aeser.ts +++ b/libs/vdk_aeser/src/vdk_aeser.ts @@ -22,7 +22,8 @@ export { unbaseNcheck, baseNcheck, signed_tx, - mansplain + mansplain, + mansplain_str } export type { @@ -290,6 +291,24 @@ signed_tx +/** + * Take apart some API-encoded data and return a string that explains it to the + * user. + * + * Mostly for the "confirm transaction" popup + */ +async function +mansplain_str + (api_str : string) + : Promise +{ + let mansplained_obj : object = await mansplain(api_str) + + return JSON.stringify(mansplained_obj, undefined, 4); +} + + + /** * Take apart some API-encoded data and show its component parts * @@ -314,6 +333,7 @@ mansplain } + /** * Take RLP encoded bytes and mansplain them */