writeKeyPairSigner

function writeKeyPairSigner(signer, path, config?): Promise<void>;

Writes the CryptoKeyPair backing a KeyPairSigner to disk as a JSON array of 64 bytes, matching the format produced by solana-keygen. The first 32 bytes are the raw Ed25519 seed (private key) and the last 32 bytes are the raw public key.

Any missing parent directories are created automatically. The written file uses mode 0600 (owner read/write only) to match solana-keygen.

This helper requires a writable filesystem and will throw in environments that don't provide one (such as browsers or React Native).

Parameters

ParameterTypeDescription
signerKeyPairSignerA KeyPairSigner whose underlying CryptoKeyPair is extractable (i.e. created via generateKeyPairSigner(true) or createKeyPairSignerFromBytes(bytes, true)).
pathstringThe destination path on disk.
config?Readonly<{ unsafelyOverwriteExistingKeyPair?: boolean; }>See WriteKeyPairConfig.

Returns

Promise<void>

Throws

A SolanaError of code SOLANA_ERROR__KEYS__WRITE_KEY_PAIR_UNSUPPORTED_ENVIRONMENT when called in an environment without a writable filesystem.

Throws

A SolanaError of code SOLANA_ERROR__SUBTLE_CRYPTO__CANNOT_EXPORT_NON_EXTRACTABLE_KEY when the signer's underlying key pair is not extractable.

Examples

import { generateKeyPairSigner, writeKeyPairSigner } from '@solana/signers';
 
// Generate an extractable signer so its bytes can be persisted.
const signer = await generateKeyPairSigner(true);
await writeKeyPairSigner(signer, './my-keypair.json');

Overwriting an existing file requires an explicit opt-in, because doing so permanently destroys the previous key and any funds controlled by it:

import { writeKeyPairSigner } from '@solana/signers';
 
await writeKeyPairSigner(signer, './my-keypair.json', {
    unsafelyOverwriteExistingKeyPair: true,
});

See

  • writeKeyPair — the lower-level helper from @solana/keys that operates on a raw CryptoKeyPair.
  • createKeyPairSignerFromBytes — the inverse helper that loads a signer from a 64-byte buffer.

On this page