Skip to main content
added 715 characters in body
Source Link

Updated answer

I have now gotten a job at Solana Foundation and created the Solana Helpers library with the rest of the Developer Relations team and some well known third parties.

Since the Solana CLI uses 'array of numbers' format, let's do the same:

await addKeypairToEnvFile(testKeypair, "SECRET_KEY");

Will save testKeypair to your .env using the variable name SECRET_KEY.

To restore:

const keypair = await getKeypairFromEnvironment("SECRET_KEY");

See the Solana Helpers docs for more information on both functions.

Original Answer

Answering my own question: this works, but it's better to simply avoid keyPair.secretKey.toString(). Instead save the base58 encoded value of the secretKey:

export const secretKeyToHex = (secretKey: Uint8Array) => {
  return base58.encode(secretKey);
};

And then import that:

export const getKeypairFromString = (secretKeyString: string) => {
  let decodedSecretKey: Uint8Array;
  try {
    decodedSecretKey = base58.decode(secretKeyString);
  } catch (throwObject) {
    throw new Error("Invalid secret key! See README.md");
  }
  return Keypair.fromSecretKey(decodedSecretKey);
};

Answering my own question: this works, but it's better to simply avoid keyPair.secretKey.toString(). Instead save the base58 encoded value of the secretKey:

export const secretKeyToHex = (secretKey: Uint8Array) => {
  return base58.encode(secretKey);
};

And then import that:

export const getKeypairFromString = (secretKeyString: string) => {
  let decodedSecretKey: Uint8Array;
  try {
    decodedSecretKey = base58.decode(secretKeyString);
  } catch (throwObject) {
    throw new Error("Invalid secret key! See README.md");
  }
  return Keypair.fromSecretKey(decodedSecretKey);
};

Updated answer

I have now gotten a job at Solana Foundation and created the Solana Helpers library with the rest of the Developer Relations team and some well known third parties.

Since the Solana CLI uses 'array of numbers' format, let's do the same:

await addKeypairToEnvFile(testKeypair, "SECRET_KEY");

Will save testKeypair to your .env using the variable name SECRET_KEY.

To restore:

const keypair = await getKeypairFromEnvironment("SECRET_KEY");

See the Solana Helpers docs for more information on both functions.

Original Answer

Answering my own question: this works, but it's better to simply avoid keyPair.secretKey.toString(). Instead save the base58 encoded value of the secretKey:

export const secretKeyToHex = (secretKey: Uint8Array) => {
  return base58.encode(secretKey);
};

And then import that:

export const getKeypairFromString = (secretKeyString: string) => {
  let decodedSecretKey: Uint8Array;
  try {
    decodedSecretKey = base58.decode(secretKeyString);
  } catch (throwObject) {
    throw new Error("Invalid secret key! See README.md");
  }
  return Keypair.fromSecretKey(decodedSecretKey);
};
Source Link

Answering my own question: this works, but it's better to simply avoid keyPair.secretKey.toString(). Instead save the base58 encoded value of the secretKey:

export const secretKeyToHex = (secretKey: Uint8Array) => {
  return base58.encode(secretKey);
};

And then import that:

export const getKeypairFromString = (secretKeyString: string) => {
  let decodedSecretKey: Uint8Array;
  try {
    decodedSecretKey = base58.decode(secretKeyString);
  } catch (throwObject) {
    throw new Error("Invalid secret key! See README.md");
  }
  return Keypair.fromSecretKey(decodedSecretKey);
};