diff --git a/examples/sendE2E/index.html b/examples/sendE2E/index.html index 90c544f3085bf18d412c2e2466c6d378067abe55..27067671ba8e358138343310445fb490bd21dcab 100644 --- a/examples/sendE2E/index.html +++ b/examples/sendE2E/index.html @@ -9,9 +9,11 @@ <html lang="en"> <head> <meta charset="UTF-8"> - <title>xxDk WebAssembly Test</title> + <title>Receiver</title> <link rel="stylesheet" type="text/css" href="../styles.css"> + <link rel="shortcut icon" type="image/x-icon" href="receiver-favicon.ico" /> + <script type="text/javascript" src="xxdk.js"></script> <script type="text/javascript" src="../wasm_exec.js"></script> <script> @@ -40,6 +42,7 @@ </script> </head> <body> +<h1>Receiver</h1> <form id="start-cmix"> <div> <label for="ndf-input">Select NDF file to use <code>ndf</code> variable in JS (required):</label><br/> diff --git a/examples/sendE2E/index2.html b/examples/sendE2E/index2.html index 72443bc52ff3a5a3e953841f3133bed5b1f93f75..6665b33c9a44dab1e5974de6d742e075d92c2270 100644 --- a/examples/sendE2E/index2.html +++ b/examples/sendE2E/index2.html @@ -9,9 +9,10 @@ <html lang="en"> <head> <meta charset="UTF-8"> - <title>xxDk WebAssembly Test</title> + <title>Sender</title> <link rel="stylesheet" type="text/css" href="../styles.css"> + <link rel="shortcut icon" type="image/x-icon" href="sender-favicon.ico" /> <script type="text/javascript" src="xxdk.js"></script> <script type="text/javascript" src="../wasm_exec.js"></script> <script> @@ -23,7 +24,7 @@ LogLevel(0); - const recipientContactFile = '<xxc(2)qM22QBlbReXsTQ+zqDWr0ZSh7sFkDRjxqprUHyWe3aUDrgZ7Ugdw/BAr6cY/smgr1a48u+tl0EStWz84jY/oP+2BuftqQ5sW4gRxRsIHR+SG2lQb/NWIZt763UWGFTQAgCMCuSlhX3hVec972BhVY4igzVIazFbaDRZFONT8aeOSHIJFOgSQsFcfLX7s+ZZDSXVyx+4kZduyMRB1EhwtF4z4a5zebmoA9DB9H4B/jXB1H0pvkLXVS8QtWqtSP/9BHvSipXDXtnxlKxTwBaw6xNTWZnCY71nznRe2BElQrkLR7SyLWl3cMXeby2axbLO8nxsx5L9NKwvZOGlhDSlmpSfGaSoU8ETQ9F3LFj+wQ/xMrTR1bb0hUwkRkrLL4gmH9n0dNtZ/EblonRd7+v/lJnYHPCZCAPBMZQ9upqwJ7ZafgJkoP1dX/LT/mHoLB3Z8zeKvIXFC2IpT2notYxfMv1PRpqMna+FEK71dj/UVPxKS9eIOv7KnmUmOEWRQ66OB9MdKiINKMqik0zsTn3jSMj6OyePo2wshWcozHE/VXkvrHHhYVxFMjaN0Ysr2YYHQ/JGCNFkHsfHWQAAAAgA7j/wRIx1q2PjH8zBC6s8kmw==xxc>'; + const recipientContactFile = '<xxc(2)Q2uq69ry88AeNFKcnpOkCjuCtkYIdv/fgC3F4z6eNUMDrgZ7Ugdw/BAr6bG0KyMpEOL6fulH331JBFOD69e7iZLxkBr3BvZyfyTXL9QtQwpzaDnmPXjyRR5cjAQ6gfk5XQzsrw45Q8qsrKewwZsqGhac0NKJYPiXGa9DTQR6f1VMOY7cco4uquNN5BRo4ucz1gu9w2Ff3N+1Hdz9V1r7xulkkd/78IyoJqHQDgp1eO3q6NZJT55DNsSuS2ZuYQc3yQUyTHH/G7gIF7EwRTU6dhRTGMYpfOQhRT67EVidHOT4e7GSpkWoGzi5XUBA+9N1llRCAwzRJ65lQqJZIC/vNgxJhHR5Cd4AOfHc+KuGlXMpCQkeYjd7a+ggh4RUuPsMW59QlKaBbZ1mbGHPzphFUrJIS63TMcJRrWbGwXHJrvZnD1NYNOM/x/qSR4pTkv8soIHFzgvFbRME5VTfxWfWKSCG/qu3TaWfetnYXmsqhdmnURekF4iSMtyDpVch+86zK5YmuO9Ap18MvdwmnfX5r9xhyPRga/QqmkdEI6ZebuXVYJQGr6Lzsz4z+4AGI0v87j//APsGxvi8fwAAAgA7w6N/pgZWFlB+p/Muirk+VQ==xxc>'; const myContactFileName = 'theirE2eContact.xxc'; const statePath = 'statePathSender'; const statePass = 'password'; @@ -40,6 +41,7 @@ </script> </head> <body> +<h1>Sender</h1> <form id="start-cmix"> <div> <label for="ndf-input">Select NDF file to use <code>ndf</code> variable in JS:</label><br/> diff --git a/examples/sendE2E/receiver-favicon.ico b/examples/sendE2E/receiver-favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..3a7b5826dfdad175084ea63406202334dfe29bf4 Binary files /dev/null and b/examples/sendE2E/receiver-favicon.ico differ diff --git a/examples/sendE2E/sender-favicon.ico b/examples/sendE2E/sender-favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..149d6cb2fdb5e20b4a4f80a5797878f73e14d25e Binary files /dev/null and b/examples/sendE2E/sender-favicon.ico differ diff --git a/examples/sendE2E/xxdk.js b/examples/sendE2E/xxdk.js index 9a0840c04cb38ea03bb09fefb1a3815abd545ac6..533eead2b67038e3fbc4d16c13cdca758a3da86f 100644 --- a/examples/sendE2E/xxdk.js +++ b/examples/sendE2E/xxdk.js @@ -59,6 +59,7 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, let confirm = false; let confirmContact; + let e2eClient; let authCallbacks = { Confirm: function (contact, receptionId, ephemeralId, roundId) { confirm = true; @@ -69,6 +70,15 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, console.log("ephemeralId: " + roundId.toString()); output.innerHTML += "Received confirmation from " + ephemeralId.toString() + "<br />" + }, + Request: function (contact, receptionId, ephemeralId, roundId) { + console.log("Request:"); + console.log("contact: " + dec.decode(contact)); + console.log("receptionId: " + ephemeralId.toString()); + console.log("ephemeralId: " + roundId.toString()); + + e2eClient.Confirm(contact) + output.innerHTML += "Received Request from " + ephemeralId.toString() + "<br />" } } @@ -76,7 +86,9 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, // Pass in auth object which controls auth callbacks for this client const params = GetDefaultE2EParams(); console.log("Using E2E parameters: " + dec.decode(params)); - let e2eClient = Login(net.GetID(), authCallbacks, identity, params); + e2eClient = Login(net.GetID(), authCallbacks, identity, params); + + e2eClient.DeleteAllRequests() //////////////////////////////////////////////////////////////////////////// @@ -88,21 +100,8 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, output.innerHTML += "Starting network follower<br />" - // Set up a wait for the network to be connected - let health = false - const n = 100 - let myPromise = new Promise(async function (myResolve, myReject) { - for (let i = 0; (health === false) && (i < n); i++) { - await sleep(100) - } - if (health === true) { - myResolve("OK"); - } else { - myReject("timed out waiting for healthy network"); - } - }); - // Provide a callback that will be signalled when network health status changes + let health = false net.AddHealthCallback({ Callback: function (healthy) { health = healthy; @@ -110,17 +109,18 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, }); await sleep(3000) - // Wait until connected or crash on timeout - myPromise.then( - function (value) { - output.innerHTML += "Network is healthy<br />" - console.log("network is healthy") - }, - function (error) { - output.innerHTML += "Network is not healthy<br />" - // throw error; - } - ); + + const n = 100 + for (let i = 0; (health === false) && (i < n); i++) { + await sleep(100) + } + + if (health === false) { + console.error("Continuing with unhealthy network") + output.innerHTML += "Network NOT healthy<br />" + } else { + output.innerHTML += "Network healthy<br />" + } //////////////////////////////////////////////////////////////////////////// @@ -154,13 +154,22 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, let exists = false; output.innerHTML += "getting ID from contact<br />" const recipientContactID = GetIDFromContact(recipientContactFile); - output.innerHTML += "Checking for " + btoa(recipientContactID) + "<br />" - let partners = e2eClient.GetAllPartnerIDs(); + console.log(typeof recipientContactID) + console.log("recipientContactID: " + recipientContactID) + + output.innerHTML += "Checking for " + recipientContactID + "<br />" + + + const partnerIDS = dec.decode(e2eClient.GetAllPartnerIDs()) + console.log("partnerIDS: " + partnerIDS) + let partners = JSON.parse(partnerIDS); for (let i = 0; i < partners.length; i++) { + const partnerBytes = base64ToArrayBuffer(partners[i]) + console.log("partner " + recipientContactID + " == " + i + " " + partnerBytes) - if (partners[i] === recipientContactID) { - console.log("partner " + btoa(recipientContactID) + " matches partner " + i + " " + btoa(partners[i])) + if (partnerBytes.toString() === recipientContactID.toString()) { + console.log("MATCH! partner " + recipientContactID + " matches partner " + i + " " + partnerBytes) exists = true; break } @@ -168,7 +177,7 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, // If the partner does not exist, send a request if (exists === false) { - output.innerHTML += "Request sent to " + btoa(recipientContactID) + "<br />" + output.innerHTML += "Partner does not exist, Request sent to " + recipientContactID + "<br />" const factList = enc.encode('[]') e2eClient.Request(enc.encode(recipientContactFile), factList) @@ -176,17 +185,19 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, await sleep(50) } if (confirm === false) { - output.innerHTML += "Checking for " + recipientContactID + "<br />" - throw new Error("timed out waiting for confirmation") + output.innerHTML += "Checking for " + recipientContactIDBase64 + "<br />" + console.error(new Error("timed out waiting for confirmation")) } const confirmContactID = GetIDFromContact(confirmContact) - if (recipientContactID !== confirmContactID) { + if (recipientContactID.toString() !== confirmContactID.toString()) { throw new Error("contact ID from confirmation " + btoa(dec.decode(confirmContactID)) + " does not match recipient ID " + - btoa(dec.decode(recipientContactID))) + recipientContactIDBase64) } + } else { + output.innerHTML += "Partner exists<br />" } //////////////////////////////////////////////////////////////////////////// @@ -197,8 +208,10 @@ async function SendE2e(ndf, recipientContactFile, myContactFileName, statePath, const msgBody = "If this message is sent successfully, we'll have established contact with the recipient." output.innerHTML += "Sending E2E message<br />" - const paramsObj = JSON.parse(dec.decode(params)) - const e2eSendReport = e2eClient.SendE2E(2, recipientContactID, enc.encode(msgBody), enc.encode(JSON.stringify(paramsObj.Base))) + // const paramsObj = JSON.parse(dec.decode(params)) + // const e2eParams = JSON.stringify(paramsObj.Base) + // console.log("e2eParams: " + e2eParams) + const e2eSendReport = e2eClient.SendE2E(2, recipientContactID, enc.encode(msgBody), params) console.log("e2e send report: " + dec.decode(e2eSendReport)) output.innerHTML += "Send e2e: " + dec.decode(e2eSendReport) + "<br />" @@ -223,3 +236,13 @@ function download(filename, text) { document.body.removeChild(element); } + +function base64ToArrayBuffer(base64) { + const binary_string = window.atob(base64); + const len = binary_string.length; + const bytes = new Uint8Array(len); + for (let i = 0; i < len; i++) { + bytes[i] = binary_string.charCodeAt(i); + } + return bytes; +} \ No newline at end of file diff --git a/wasm/e2e.go b/wasm/e2e.go index 9ae1551b29e59d70cd968cb41540a7898429e904..98203fc778d98eefa0af3fbfb6c0ce518c25e2e8 100644 --- a/wasm/e2e.go +++ b/wasm/e2e.go @@ -210,20 +210,23 @@ func newAuthCallbacks(value js.Value) *authCallbacks { func (a *authCallbacks) Request( contact, receptionId []byte, ephemeralId, roundId int64) { if a.request != nil { - a.request(contact, receptionId, ephemeralId, roundId) + a.request(CopyBytesToJS(contact), CopyBytesToJS(receptionId), + ephemeralId, roundId) } } func (a *authCallbacks) Confirm( contact, receptionId []byte, ephemeralId, roundId int64) { if a.confirm != nil { - a.confirm(contact, receptionId, ephemeralId, roundId) + a.confirm(CopyBytesToJS(contact), CopyBytesToJS(receptionId), + ephemeralId, roundId) } } func (a *authCallbacks) Reset( contact, receptionId []byte, ephemeralId, roundId int64) { if a.reset != nil { - a.reset(contact, receptionId, ephemeralId, roundId) + a.reset(CopyBytesToJS(contact), CopyBytesToJS(receptionId), + ephemeralId, roundId) } } diff --git a/wasm/utils.go b/wasm/utils.go index 7959879b487f59a53187cde114bd01eeaa6a708a..5c7a62ad757f73db07b6997ce2428ea1fd2bca6c 100644 --- a/wasm/utils.go +++ b/wasm/utils.go @@ -45,7 +45,7 @@ func WrapCB(parent js.Value, m string) func(args ...interface{}) js.Value { } return func(args ...interface{}) js.Value { - return parent.Call(m, args) + return parent.Call(m, args...) } }