With the current version of @solana/web3.js, we can recreate a keypair from a saved secretkey:

const keypair = Keypair.fromSecretKey(secretKey)

The new version of @solana/web3.js has a generateKeyPair() function that returns a CryptoKeyPair.

const keypair = await generateKeyPair();
interface CryptoKeyPair {
    privateKey: CryptoKey;
    publicKey: CryptoKey;

How do you convert a secretkey to a CryptoKeyPair that can be used with the new version of @solana/web3.js?

2 Answers 2


Thanks Buffalo Joe for the help!

There is a fromLegacyKeypair helper function here, which is part of a lib called @solana/compat.

Install with npm i @solana/compat@experimental

As Serban mentioned in the previous comment, it uses the importKey method

 * Convert from a Legacy Web3 JS Keypair to a CryptoKeyPair
 * @param keypair   The Keypair to convert
 * @returns         A CryptoKeyPair
export async function fromLegacyKeypair(keypair: Keypair, extractable?: boolean): Promise<CryptoKeyPair> {
    const publicKey: CryptoKey = await crypto.subtle.importKey('raw', keypair.publicKey.toBytes(), 'Ed25519', true, [
    const privateKeyBytesPkcs8 = addPkcs8Header(keypair.secretKey.slice(0, 32));
    const privateKey: CryptoKey = await crypto.subtle.importKey(
        extractable ?? false,
    return {
    } as CryptoKeyPair;

For example:

const secretKey = new Uint8Array([...]);
const legacyKeypair = Keypair.fromSecretKey(secretKey);
const cryptoKeypair = await fromLegacyKeypair(legacyKeypair);

It doesn't seem there is currently a helper function to convert bytes directly to a CryptoKeypair, but the fromLegacyKeypair helper function can be used as a reference.


CryptoKeyPair is an object native to browsers that is part of the Web Crypto API.

Solana uses Ed25519, which is not supported on all browsers yet, but the Solana team have offered a polyfill for it. You can find it here along with an example.

To import an existing key, create an Uint8Array of your secret key and use the importKey method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.