Skip to content

Commit

Permalink
Merge pull request #28 from tkhq/olivia/ed25519-esm
Browse files Browse the repository at this point in the history
Use Ed25519 package for solana public keys
  • Loading branch information
Olivia Thet authored Mar 15, 2024
2 parents 2164906 + 5e4af1f commit aeb6b9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
22 changes: 16 additions & 6 deletions export/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,16 @@ <h2>Message log</h2>
* hex-encoding if `keyFormat` isn't passed.
* @param {Uint8Array} privateKeyBytes
* @param {string} keyFormat Can be "HEXADECIMAL" or "SOLANA"
* @param {string} publicKey Hex-encoded public key, needed for Solana private keys
*/
const encodeKey = (privateKeyBytes, keyFormat, publicKey) => {
const encodeKey = async (privateKeyBytes, keyFormat, publicKeyBytes) => {
switch (keyFormat) {
case "SOLANA":
if (!publicKey) {
if (!publicKeyBytes) {
throw new Error("public key must be specified for SOLANA key format");
}
if (privateKeyBytes.length !== 32) {
throw new Error(`invalid private key length. Expected 32 bytes. Got ${privateKeyBytes.length}.`);
}
const publicKeyBytes = uint8arrayFromHexString(publicKey);
if (publicKeyBytes.length !== 32) {
throw new Error(`invalid public key length. Expected 32 bytes. Got ${publicKeyBytes.length}.`);
}
Expand Down Expand Up @@ -453,6 +451,7 @@ <h2>Message log</h2>
<script type="module">
// 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";

document.addEventListener("DOMContentLoaded", async () => {
await TKHQ.initEmbeddedKey();
Expand Down Expand Up @@ -569,16 +568,27 @@ <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
*/
const onInjectKeyBundle = async (bundle, keyFormat, publicKey) => {
const onInjectKeyBundle = async (bundle, keyFormat) => {
// Decrypt the export bundle
const keyBytes = await decryptBundle(bundle);

// Parse the decrypted key bytes
const key = TKHQ.encodeKey(new Uint8Array(keyBytes), keyFormat, publicKey);
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);
} else {
const key = await TKHQ.encodeKey(privateKeyBytes, keyFormat);
}

// Display only the key
displayKey(key);
Expand Down
8 changes: 3 additions & 5 deletions export/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ describe("TKHQ", () => {

it("encodes hex-encoded private key correctly by default", async () => {
const keyHex = "0x13eff5b3f9c63eab5d53cff5149f01606b69325496e0e98b53afa938d890cd2e";
const encodedKey = TKHQ.encodeKey(TKHQ.uint8arrayFromHexString(keyHex.slice(2)));
const encodedKey = await TKHQ.encodeKey(TKHQ.uint8arrayFromHexString(keyHex.slice(2)));
expect(encodedKey).toEqual(keyHex);
})

it("encodes hex-encoded private key correctly", async () => {
const keyHex = "0x13eff5b3f9c63eab5d53cff5149f01606b69325496e0e98b53afa938d890cd2e";
const encodedKey = TKHQ.encodeKey(TKHQ.uint8arrayFromHexString(keyHex.slice(2)), "HEXADECIMAL");
const encodedKey = await TKHQ.encodeKey(TKHQ.uint8arrayFromHexString(keyHex.slice(2)), "HEXADECIMAL");
expect(encodedKey).toEqual(keyHex);
})

Expand All @@ -102,9 +102,7 @@ describe("TKHQ", () => {
expect(keySolBytes.length).toEqual(64);
const keyPrivBytes = keySolBytes.subarray(0, 32);
const keyPubBytes = keySolBytes.subarray(32, 64);
const keyPubHex = TKHQ.uint8arrayToHexString(keyPubBytes);

const encodedKey = TKHQ.encodeKey(keyPrivBytes, "SOLANA", keyPubHex);
const encodedKey = await TKHQ.encodeKey(keyPrivBytes, "SOLANA", keyPubBytes);
expect(encodedKey).toEqual(keySol);
})

Expand Down

0 comments on commit aeb6b9c

Please sign in to comment.