msg confirm message working, need to figure out how to branch reply
This commit is contained in:
+19
-6
@@ -319,6 +319,8 @@ bi_msg_handler_content
|
|||||||
case "message.sign":
|
case "message.sign":
|
||||||
console.log('jr bg content message handler message sign');
|
console.log('jr bg content message handler message sign');
|
||||||
let msg_str : string = msg.data.params.message;
|
let msg_str : string = msg.data.params.message;
|
||||||
|
// TODO: need to break here on whether the msg signature was good
|
||||||
|
// (signed) or bad (not)
|
||||||
let resultt = await msg_sign(msg_str, secret_key)
|
let resultt = await msg_sign(msg_str, secret_key)
|
||||||
return w2a_ok(resultt);
|
return w2a_ok(resultt);
|
||||||
|
|
||||||
@@ -354,23 +356,30 @@ msg_sign
|
|||||||
secret_key : Uint8Array)
|
secret_key : Uint8Array)
|
||||||
: Promise<{signature : string}>
|
: Promise<{signature : string}>
|
||||||
{
|
{
|
||||||
console.log('a');
|
|
||||||
// does the user want to sign the message
|
// does the user want to sign the message
|
||||||
let confirm_window = await browser.windows.create({url : '../pages/msg_confirm.html',
|
let confirm_window = await browser.windows.create({url : '../pages/msg_confirm.html',
|
||||||
type : 'popup'});
|
type : 'popup'});
|
||||||
console.log('b');
|
//console.log(confirm_window.tabs);
|
||||||
console.log(confirm_window.tabs);
|
|
||||||
// @ts-ignore shut the fuck up
|
// @ts-ignore shut the fuck up
|
||||||
let tabid : number = confirm_window.tabs[0].id;
|
let tabid : number = confirm_window.tabs[0].id;
|
||||||
console.log('tabid', tabid);
|
//console.log('tabid', tabid);
|
||||||
|
|
||||||
// stupid hack because otherwise the message gets sent before the listener in the page script is created
|
// stupid hack because otherwise the message gets sent before the listener in the page script is created
|
||||||
await sleep(500);
|
await sleep(200);
|
||||||
console.log('slept');
|
console.log('slept');
|
||||||
|
|
||||||
let result = await browser.tabs.sendMessage(tabid, {msg_str : msg_str});
|
let result: 'good' | 'bad'
|
||||||
|
= await browser.tabs.sendMessage(tabid, {msg_str : msg_str});
|
||||||
|
//
|
||||||
console.log('result', result);
|
console.log('result', result);
|
||||||
|
|
||||||
|
// close the popup
|
||||||
|
browser.tabs.remove(tabid);
|
||||||
|
|
||||||
|
// if confirm
|
||||||
|
if
|
||||||
|
(result === 'good') {
|
||||||
// use nacl detached signatures
|
// use nacl detached signatures
|
||||||
// https://github.com/aeternity/aepp-sdk-js/blob/5df22dd297abebc0607710793a7234e6761570d4/src/utils/crypto.ts#L141-L143
|
// https://github.com/aeternity/aepp-sdk-js/blob/5df22dd297abebc0607710793a7234e6761570d4/src/utils/crypto.ts#L141-L143
|
||||||
// https://github.com/aeternity/aepp-sdk-js/blob/5df22dd297abebc0607710793a7234e6761570d4/src/utils/crypto.ts#L160-L167
|
// https://github.com/aeternity/aepp-sdk-js/blob/5df22dd297abebc0607710793a7234e6761570d4/src/utils/crypto.ts#L160-L167
|
||||||
@@ -380,6 +389,10 @@ msg_sign
|
|||||||
let signature_str : string = vdk_binary.bytes_to_hex_str(signature);
|
let signature_str : string = vdk_binary.bytes_to_hex_str(signature);
|
||||||
return {signature: signature_str};
|
return {signature: signature_str};
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+69
-13
@@ -5,29 +5,85 @@
|
|||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
||||||
|
|
||||||
async function
|
async function
|
||||||
main
|
main
|
||||||
()
|
()
|
||||||
: Promise<void>
|
: Promise<void>
|
||||||
{
|
{
|
||||||
console.log('msg_confirm main');
|
console.log('msg_confirm main');
|
||||||
var result = null;
|
|
||||||
document.getElementById('good')!.onclick = () => { console.log('click good'); result = 'good' }
|
|
||||||
document.getElementById('bad')!.onclick = () => { console.log('click bad'); result = 'bad' }
|
|
||||||
|
|
||||||
console.log('adding listener');
|
let result : '' | 'good' | 'bad'
|
||||||
browser.runtime.onMessage.addListener(listener);
|
= '';
|
||||||
console.log('added listener');
|
|
||||||
}
|
|
||||||
|
|
||||||
// for some reason the listener here never gets triggered
|
document.getElementById('good')!.onclick
|
||||||
async function
|
= function() {
|
||||||
listener
|
console.log('click good');
|
||||||
(msg : any,
|
result = 'good';
|
||||||
|
};
|
||||||
|
document.getElementById('bad')!.onclick
|
||||||
|
= function() {
|
||||||
|
console.log('click bad');
|
||||||
|
result = 'bad';
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
type bg_msg
|
||||||
|
= {msg_str : string};
|
||||||
|
|
||||||
|
async function listener
|
||||||
|
(msg : bg_msg,
|
||||||
_sender : any,
|
_sender : any,
|
||||||
_sendResponse : any)
|
_sendResponse : any)
|
||||||
|
: Promise<'good' | 'bad'>
|
||||||
|
{
|
||||||
|
console.log('listener triggered', msg);
|
||||||
|
console.log('msg_str:', msg.msg_str);
|
||||||
|
document.getElementById('message')!.innerHTML = msg.msg_str;
|
||||||
|
|
||||||
|
// every 5 ms check
|
||||||
|
// timeout of 10 minutes = 10*60 secs * 20 iterations =
|
||||||
|
// number of iters in a full second
|
||||||
|
let ITERS = 1;
|
||||||
|
let SEC = 200*ITERS;
|
||||||
|
let MIN = 60*SEC;
|
||||||
|
//let n_max = 10*MIN;
|
||||||
|
let n = 1;
|
||||||
|
let n_max = 30*SEC;
|
||||||
|
|
||||||
|
while
|
||||||
|
(result === '') {
|
||||||
|
// if haven't timed out yet
|
||||||
|
if (n <= n_max) {
|
||||||
|
await sleep(5);
|
||||||
|
n = n + 1;
|
||||||
|
}
|
||||||
|
// if timed out
|
||||||
|
else {
|
||||||
|
result = 'bad';
|
||||||
|
// this break is implied but you're not smart enough to figure
|
||||||
|
// that out yourself
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send result back
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add listener
|
||||||
|
browser.runtime.onMessage.addListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hack from stack overflow somewhere to sleep for the given number of ms
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
sleep
|
||||||
|
(ms: number)
|
||||||
: Promise<void>
|
: Promise<void>
|
||||||
{
|
{
|
||||||
console.log('listener triggered');
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
console.log(msg);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user