Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sha512 ESM #29

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions export/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ <h2>Inject Key Export Bundle</h2>
<option value="HEXADECIMAL">Hexadecimal (Default)</option>
<option value="SOLANA">Solana</option>
</select>
<br>
<label>Public Key (Optional)</label>
<input type="text" name="key-export-public-key" id="key-export-public-key"/>
</form>
<br>
<h2>Inject Wallet Export Bundle</h2>
Expand Down Expand Up @@ -452,6 +449,9 @@ <h2>Message log</h2>
// TODO: this should be bundled at build time or replaced with code written by Turnkey entirely.
import * as hpke from "https://esm.sh/@hpke/core";
import * as ed from "https://esm.sh/@noble/ed25519";
import { sha512 } from 'https://esm.sh/@noble/hashes/sha512';

ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));

document.addEventListener("DOMContentLoaded", async () => {
await TKHQ.initEmbeddedKey();
Expand All @@ -465,9 +465,9 @@ <h2>Message log</h2>
// We do not want to arbitrarily receive messages from all origins.
window.addEventListener("message", async function(event) {
if (event.data && event.data["type"] == "INJECT_KEY_EXPORT_BUNDLE") {
TKHQ.logMessage(`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}, ${event.data["keyFormat"]}, ${event.data["publicKey"]}`);
TKHQ.logMessage(`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}, ${event.data["keyFormat"]}`);
try {
await onInjectKeyBundle(event.data["value"], event.data["keyFormat"], event.data["publicKey"])
await onInjectKeyBundle(event.data["value"], event.data["keyFormat"])
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString());
}
Expand Down Expand Up @@ -501,7 +501,6 @@ <h2>Message log</h2>
"type": "INJECT_KEY_EXPORT_BUNDLE",
"value": document.getElementById("key-export-bundle").value,
"keyFormat": document.getElementById("key-export-format").value,
"publicKey": document.getElementById("key-export-public-key").value,
})
}, false);
document.getElementById("inject-wallet").addEventListener("click", async e => {
Expand Down Expand Up @@ -568,10 +567,6 @@ <h2>Message log</h2>
});
}

const getEd25519PublicKey = async (privateKeyHex) => {
return await ed.getPublicKey(privateKeyHex);
};

/**
* Function triggered when INJECT_KEY_EXPORT_BUNDLE event is received.
* @param {string} bundle
Expand All @@ -581,13 +576,14 @@ <h2>Message log</h2>
const keyBytes = await decryptBundle(bundle);

// Parse the decrypted key bytes
var key;
const privateKeyBytes = new Uint8Array(keyBytes);
if (keyFormat === "SOLANA") {
const privateKeyHex = TKHQ.uint8arrayToHexString(privateKeyBytes.subarray(0,32));
const publicKeyBytes = getEd25519PublicKey(privateKeyHex);
const key = await TKHQ.encodeKey(privateKeyBytes, keyFormat, publicKeyBytes);
const publicKeyBytes = ed.getPublicKey(privateKeyHex);
key = await TKHQ.encodeKey(privateKeyBytes, keyFormat, publicKeyBytes);
} else {
const key = await TKHQ.encodeKey(privateKeyBytes, keyFormat);
key = await TKHQ.encodeKey(privateKeyBytes, keyFormat);
}

// Display only the key
Expand Down
Loading