-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidos-iframe.js
57 lines (41 loc) · 1.49 KB
/
idos-iframe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const base64ToArrayBuffer = (base64) => (
Uint8Array.from(atob(base64), c => c.charCodeAt(0)).buffer
);
const arrayBufferToBase64 = (bytes) => (
btoa(String.fromCharCode(...new Uint8Array(bytes)))
);
const dappUrl = new URL(document.referrer).origin;
let password;
const deriveKey = (password, humanId) => {
// stub: key derivation with salt
return `deriveKey("${password}", "${humanId}")`;
};
const decrypt = (key, message) => {
// stub: decryption with derived key
return "some decrypted data";
};
window.addEventListener("message", (event) => {
if (event.origin != dappUrl) { return; }
const { humanId, encryptedData } = event.data;
const key = deriveKey(password, humanId);
const decryptedData = decrypt(key, encryptedData);
document.querySelector("#decryption-request").innerText = encryptedData;
document.querySelector("#key").innerText = key;
window.parent.postMessage({ decryptedData: decryptedData }, dappUrl);
});
(async () => {
const publicKey = {
challenge: crypto.getRandomValues(new Uint8Array(10)),
//rpId: "idos.network",
};
const credentialId = window.localStorage.getItem("idos-credential-id");
if (credentialId !== null) {
publicKey.allowCredentials = [{
id: base64ToArrayBuffer(credentialId),
type: "public-key",
}];
}
const credential = await navigator.credentials.get({ publicKey });
password = new TextDecoder().decode(credential.response.userHandle);
document.querySelector("#password").innerText = password;
})();