Skip to content

Commit

Permalink
Merge pull-request #439
Browse files Browse the repository at this point in the history
  • Loading branch information
moe-dev committed Dec 9, 2024
2 parents cefc9eb + 668edfa commit 748561e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curly-eyes-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@turnkey/crypto": minor
---

Add keyformat to decryptExportBundle for displaying Solana private keys
24 changes: 23 additions & 1 deletion packages/crypto/src/turnkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import {
uncompressRawPublicKey,
} from "./crypto";

import { ed25519 } from "@noble/curves/ed25519";

interface DecryptExportBundleParams {
exportBundle: string;
organizationId: string;
embeddedKey: string;
dangerouslyOverrideSignerPublicKey?: string; // Optional override for signer key
keyFormat?: "SOLANA" | "HEXADECIMAL";
returnMnemonic: boolean;
}
interface EncryptPrivateKeyToBundleParams {
Expand Down Expand Up @@ -99,6 +102,7 @@ export const decryptExportBundle = async ({
embeddedKey,
organizationId,
dangerouslyOverrideSignerPublicKey,
keyFormat,
returnMnemonic,
}: DecryptExportBundleParams): Promise<string> => {
try {
Expand Down Expand Up @@ -139,10 +143,28 @@ export const decryptExportBundle = async ({
receiverPriv: embeddedKey,
});

if (keyFormat === "SOLANA" && !returnMnemonic) {
if (decryptedData.length !== 32) {
throw new Error(
`invalid private key length. Expected 32 bytes. Got ${decryptedData.length}.`
);
}
const publicKeyBytes = ed25519.getPublicKey(decryptedData);
if (publicKeyBytes.length !== 32) {
throw new Error(
`invalid public key length. Expected 32 bytes. Got ${publicKeyBytes.length}.`
);
}
const concatenatedBytes = new Uint8Array(64);
concatenatedBytes.set(decryptedData, 0);
concatenatedBytes.set(publicKeyBytes, 32);
return bs58.encode(concatenatedBytes);
}

const decryptedDataHex = uint8ArrayToHexString(decryptedData);
return returnMnemonic ? hexToAscii(decryptedDataHex) : decryptedDataHex;
} catch (error) {
throw new Error(`"Error decrypting bundle:", ${error}`);
throw new Error(`Error decrypting bundle: ${error}`);
}
};

Expand Down

0 comments on commit 748561e

Please sign in to comment.