Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Jan 22, 2025
1 parent 7042f2a commit 442696a
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 117 deletions.
176 changes: 96 additions & 80 deletions auth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1046,79 +1046,93 @@ <h2>Message log</h2>
* This is useful for debugging as well.
*/
var addDOMEventListeners = function () {
var addDOMEventListeners = function () {
document.getElementById("inject").addEventListener(
"click",
async function (e) {
e.preventDefault();
window.postMessage({
type: "INJECT_CREDENTIAL_BUNDLE",
value: document.getElementById("credential-bundle").value,
});
},
false
);
document.getElementById("stamp").addEventListener(
"click",
async function (e) {
e.preventDefault();
window.postMessage({
type: "STAMP_REQUEST",
value: document.getElementById("payload").value,
});
},
false
);
document.getElementById("reset").addEventListener(
"click",
async function (e) {
e.preventDefault();
window.postMessage({ type: "RESET_EMBEDDED_KEY" });
},
false
);
};
var addDOMEventListeners = function () {
document.getElementById("inject").addEventListener(
"click",
async function (e) {
e.preventDefault();
window.postMessage({
type: "INJECT_CREDENTIAL_BUNDLE",
value: document.getElementById("credential-bundle").value,
});
},
false
);
document.getElementById("stamp").addEventListener(
"click",
async function (e) {
e.preventDefault();
window.postMessage({
type: "STAMP_REQUEST",
value: document.getElementById("payload").value,
});
},
false
);
document.getElementById("reset").addEventListener(
"click",
async function (e) {
e.preventDefault();
window.postMessage({ type: "RESET_EMBEDDED_KEY" });
},
false
);
};
};

/**
* Message Event Handlers to process messages from the parent frame
*/
var messageEventListener = async function (event) {
var messageEventListener = async function (event) {
if (
event.data &&
(event.data["type"] == "INJECT_CREDENTIAL_BUNDLE" ||
event.data["type"] == "INJECT_RECOVERY_BUNDLE")
) {
TKHQ.logMessage(
`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}`
);
try {
await onInjectBundle(event.data["value"], event.data["requestId"]);
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString(), event.data["requestId"]);
var messageEventListener = async function (event) {
if (
event.data &&
(event.data["type"] == "INJECT_CREDENTIAL_BUNDLE" ||
event.data["type"] == "INJECT_RECOVERY_BUNDLE")
) {
TKHQ.logMessage(
`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}`
);
try {
await onInjectBundle(
event.data["value"],
event.data["requestId"]
);
} catch (e) {
TKHQ.sendMessageUp(
"ERROR",
e.toString(),
event.data["requestId"]
);
}
}
}
if (event.data && event.data["type"] == "STAMP_REQUEST") {
TKHQ.logMessage(
`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}`
);
try {
await onStampRequest(event.data["value"], event.data["requestId"]);
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString(), event.data["requestId"]);
if (event.data && event.data["type"] == "STAMP_REQUEST") {
TKHQ.logMessage(
`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}`
);
try {
await onStampRequest(
event.data["value"],
event.data["requestId"]
);
} catch (e) {
TKHQ.sendMessageUp(
"ERROR",
e.toString(),
event.data["requestId"]
);
}
}
}
// This is localized just to the button on the actual page
if (event.data && event.data["type"] == "RESET_EMBEDDED_KEY") {
TKHQ.logMessage(`⬇️ Received message ${event.data["type"]}`);
try {
TKHQ.onResetEmbeddedKey();
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString());
// This is localized just to the button on the actual page
if (event.data && event.data["type"] == "RESET_EMBEDDED_KEY") {
TKHQ.logMessage(`⬇️ Received message ${event.data["type"]}`);
try {
TKHQ.onResetEmbeddedKey();
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString());
}
}
}
};
};
};

/**
Expand All @@ -1145,23 +1159,25 @@ <h2>Message log</h2>
false
);

window.addEventListener("message", async function (event) {
/**
* @turnkey/iframe-stamper >= v2.1.0 is using a MessageChannel to communicate with the parent frame.
* The parent frame sends a TURNKEY_INIT_MESSAGE_CHANNEL event with the MessagePort.
* If we receive this event, we want to remove the message event listener that was added in the DOMContentLoaded event to avoid processing messages twice.
* We persist the MessagePort so we can use it to communicate with the parent window in subsequent calls to TKHQ.sendMessageUp
*/
if (
event.data &&
event.data["type"] == "TURNKEY_INIT_MESSAGE_CHANNEL" &&
event.ports?.[0]
) {
// remove the message event listener that was added in the DOMContentLoaded event
messageListenerController.abort();
window.addEventListener(
"message",
async function (event) {
/**
* @turnkey/iframe-stamper >= v2.1.0 is using a MessageChannel to communicate with the parent frame.
* The parent frame sends a TURNKEY_INIT_MESSAGE_CHANNEL event with the MessagePort.
* If we receive this event, we want to remove the message event listener that was added in the DOMContentLoaded event to avoid processing messages twice.
* We persist the MessagePort so we can use it to communicate with the parent window in subsequent calls to TKHQ.sendMessageUp
*/
if (
event.data &&
event.data["type"] == "TURNKEY_INIT_MESSAGE_CHANNEL" &&
event.ports?.[0]
) {
// remove the message event listener that was added in the DOMContentLoaded event
messageListenerController.abort();

iframeMessagePort = event.ports[0];
iframeMessagePort.onmessage = messageEventListener;
iframeMessagePort = event.ports[0];
iframeMessagePort.onmessage = messageEventListener;

TKHQ.setParentFrameMessageChannelPort(iframeMessagePort);

Expand Down
43 changes: 25 additions & 18 deletions export/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ <h2>Message log</h2>
event.data["requestId"]
);
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString(), event.data["requestId"]);
TKHQ.sendMessageUp("ERROR", e.toString(), event.data["requestId"]);
}
}
if (event.data && event.data["type"] == "INJECT_WALLET_EXPORT_BUNDLE") {
Expand Down Expand Up @@ -992,23 +992,25 @@ <h2>Message log</h2>
false
);

window.addEventListener("message", async function (event) {
/**
* @turnkey/iframe-stamper >= v2.1.0 is using a MessageChannel to communicate with the parent frame.
* The parent frame sends a TURNKEY_INIT_MESSAGE_CHANNEL event with the MessagePort.
* If we receive this event, we want to remove the message event listener that was added in the DOMContentLoaded event to avoid processing messages twice.
* We persist the MessagePort so we can use it to communicate with the parent window in subsequent calls to TKHQ.sendMessageUp
*/
if (
event.data &&
event.data["type"] == "TURNKEY_INIT_MESSAGE_CHANNEL" &&
event.ports?.[0]
) {
// remove the message event listener that was added in the DOMContentLoaded event
messageListenerController.abort();
window.addEventListener(
"message",
async function (event) {
/**
* @turnkey/iframe-stamper >= v2.1.0 is using a MessageChannel to communicate with the parent frame.
* The parent frame sends a TURNKEY_INIT_MESSAGE_CHANNEL event with the MessagePort.
* If we receive this event, we want to remove the message event listener that was added in the DOMContentLoaded event to avoid processing messages twice.
* We persist the MessagePort so we can use it to communicate with the parent window in subsequent calls to TKHQ.sendMessageUp
*/
if (
event.data &&
event.data["type"] == "TURNKEY_INIT_MESSAGE_CHANNEL" &&
event.ports?.[0]
) {
// remove the message event listener that was added in the DOMContentLoaded event
messageListenerController.abort();

iframeMessagePort = event.ports[0];
iframeMessagePort.onmessage = messageEventListener;
iframeMessagePort = event.ports[0];
iframeMessagePort.onmessage = messageEventListener;

TKHQ.setParentFrameMessageChannelPort(iframeMessagePort);

Expand Down Expand Up @@ -1154,7 +1156,12 @@ <h2>Message log</h2>
* @param {string} organizationId
* @param {string} requestId
*/
async function onInjectKeyBundle(bundle, keyFormat, organizationId, requestId) {
async function onInjectKeyBundle(
bundle,
keyFormat,
organizationId,
requestId
) {
// Decrypt the export bundle
const keyBytes = await decryptBundle(bundle, organizationId);

Expand Down
53 changes: 34 additions & 19 deletions import/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -640,23 +640,25 @@
false
);

window.addEventListener("message", async function (event) {
/**
* @turnkey/iframe-stamper >= v2.1.0 is using a MessageChannel to communicate with the parent frame.
* The parent frame sends a TURNKEY_INIT_MESSAGE_CHANNEL event with the MessagePort.
* If we receive this event, we want to remove the message event listener that was added in the DOMContentLoaded event to avoid processing messages twice.
* We persist the MessagePort so we can use it to communicate with the parent window in subsequent calls to TKHQ.sendMessageUp
*/
if (
event.data &&
event.data["type"] == "TURNKEY_INIT_MESSAGE_CHANNEL" &&
event.ports?.[0]
) {
// remove the message event listener that was added in the DOMContentLoaded event
messageListenerController.abort();
window.addEventListener(
"message",
async function (event) {
/**
* @turnkey/iframe-stamper >= v2.1.0 is using a MessageChannel to communicate with the parent frame.
* The parent frame sends a TURNKEY_INIT_MESSAGE_CHANNEL event with the MessagePort.
* If we receive this event, we want to remove the message event listener that was added in the DOMContentLoaded event to avoid processing messages twice.
* We persist the MessagePort so we can use it to communicate with the parent window in subsequent calls to TKHQ.sendMessageUp
*/
if (
event.data &&
event.data["type"] == "TURNKEY_INIT_MESSAGE_CHANNEL" &&
event.ports?.[0]
) {
// remove the message event listener that was added in the DOMContentLoaded event
messageListenerController.abort();

iframeMessagePort = event.ports[0];
iframeMessagePort.onmessage = messageEventListener;
iframeMessagePort = event.ports[0];
iframeMessagePort.onmessage = messageEventListener;

TKHQ.setParentFrameMessageChannelPort(iframeMessagePort);

Expand Down Expand Up @@ -684,7 +686,12 @@
* @param {string} userId
* @param {string} requestId
*/
async function onInjectImportBundle(bundle, organizationId, userId, requestId) {
async function onInjectImportBundle(
bundle,
organizationId,
userId,
requestId
) {
let targetPublicBuf;
let verified;

Expand Down Expand Up @@ -805,7 +812,11 @@
TKHQ.resetTargetEmbeddedKey();

// Send up ENCRYPTED_BUNDLE_EXTRACTED message
TKHQ.sendMessageUp("ENCRYPTED_BUNDLE_EXTRACTED", encryptedBundle, requestId);
TKHQ.sendMessageUp(
"ENCRYPTED_BUNDLE_EXTRACTED",
encryptedBundle,
requestId
);
}

/**
Expand Down Expand Up @@ -843,7 +854,11 @@
TKHQ.resetTargetEmbeddedKey();

// Send up ENCRYPTED_BUNDLE_EXTRACTED message
TKHQ.sendMessageUp("ENCRYPTED_BUNDLE_EXTRACTED", encryptedBundle, requestId);
TKHQ.sendMessageUp(
"ENCRYPTED_BUNDLE_EXTRACTED",
encryptedBundle,
requestId
);
}

/**
Expand Down

0 comments on commit 442696a

Please sign in to comment.