> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# How to Restore a Keypair or Signer

> Learn how to restore keypairs from a secret key on Solana.

If you have an existing secret key, you can restore your Keypair from it. This
allows you to access your wallet and sign transactions in your dApp.

<Warning>
  Handle secret keys with care: never embed them in client-side code or commit
  them to source control. For backend services in production, use a
  key-management backend instead of a raw secret key — see [Signing in
  Production](/developers/docs/core/transactions/signing-in-production).
</Warning>

## From Bytes

<Tabs>
<Tab title="Kit">
<div className="sol-tab-label">Kit</div>

```ts
import { createKeyPairSignerFromBytes } from "@solana/kit";

const keypairBytes = new Uint8Array([
  174, 47, 154, 16, 202, 193, 206, 113, 199, 190, 53, 133, 169, 175, 31, 56,
  222, 53, 138, 189, 224, 216, 117, 173, 10, 149, 53, 45, 73, 251, 237, 246, 15,
  185, 186, 82, 177, 240, 148, 69, 241, 227, 167, 80, 141, 89, 240, 121, 121,
  35, 172, 247, 68, 251, 226, 218, 48, 63, 176, 109, 168, 89, 238, 135,
]);

const signer = await createKeyPairSignerFromBytes(keypairBytes);
console.log(signer.address);
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-bytes" data-tab="Kit">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
```

</div>
</div>

</Tab>
<Tab title="Web3.js v3">
<div className="sol-tab-label">Web3.js v3</div>

```ts
import { Keypair } from "@solana/web3.js";

const keypairBytes = Uint8Array.from([
  174, 47, 154, 16, 202, 193, 206, 113, 199, 190, 53, 133, 169, 175, 31, 56,
  222, 53, 138, 189, 224, 216, 117, 173, 10, 149, 53, 45, 73, 251, 237, 246, 15,
  185, 186, 82, 177, 240, 148, 69, 241, 227, 167, 80, 141, 89, 240, 121, 121,
  35, 172, 247, 68, 251, 226, 218, 48, 63, 176, 109, 168, 89, 238, 135,
]);

const keypair = await Keypair.fromSecretKey(keypairBytes);
console.log(keypair.publicKey.toString());
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-bytes" data-tab="Web3.js v3">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
```

</div>
</div>

</Tab>
<Tab title="Rust">
<div className="sol-tab-label">Rust</div>

```rust
use anyhow::Result;
use solana_sdk::{signature::Keypair, signer::Signer};

fn main() -> Result<()> {
    let keypair_bytes = [
        174, 47, 154, 16, 202, 193, 206, 113, 199, 190, 53, 133, 169, 175, 31, 56, 222, 53, 138,
        189, 224, 216, 117, 173, 10, 149, 53, 45, 73, 251, 237, 246, 15, 185, 186, 82, 177, 240,
        148, 69, 241, 227, 167, 80, 141, 89, 240, 121, 121, 35, 172, 247, 68, 251, 226, 218, 48,
        63, 176, 109, 168, 89, 238, 135,
    ];

    let keypair = Keypair::try_from(&keypair_bytes[..])?;
    println!("{}", keypair.pubkey());

    Ok(())
}
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-bytes" data-tab="Rust">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
```

</div>
</div>

</Tab>
<Tab title="Python">
<div className="sol-tab-label">Python</div>

```py
from solders.keypair import Keypair

keypair_bytes = bytes([
    174, 47, 154, 16, 202, 193, 206, 113, 199, 190, 53, 133, 169, 175, 31, 56,
    222, 53, 138, 189, 224, 216, 117, 173, 10, 149, 53, 45, 73, 251, 237, 246,
    15, 185, 186, 82, 177, 240, 148, 69, 241, 227, 167, 80, 141, 89, 240, 121,
    121, 35, 172, 247, 68, 251, 226, 218, 48, 63, 176, 109, 168, 89, 238, 135,
])

keypair = Keypair.from_bytes(keypair_bytes)
print(keypair.pubkey())
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-bytes" data-tab="Python">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
```

</div>
</div>

</Tab>
<Tab title="Go">
<div className="sol-tab-label">Go</div>

```go
package main

import (
	"fmt"

	"github.com/solana-foundation/solana-go/v2"
)

func main() {
	keypair := solana.PrivateKey([]byte{
		174, 47, 154, 16, 202, 193, 206, 113, 199, 190, 53, 133, 169, 175, 31, 56,
		222, 53, 138, 189, 224, 216, 117, 173, 10, 149, 53, 45, 73, 251, 237, 246,
		15, 185, 186, 82, 177, 240, 148, 69, 241, 227, 167, 80, 141, 89, 240, 121,
		121, 35, 172, 247, 68, 251, 226, 218, 48, 63, 176, 109, 168, 89, 238, 135,
	})

	fmt.Println(keypair.PublicKey())
}
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-bytes" data-tab="Go">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
```

</div>
</div>

</Tab>
<Tab title="Web3.js (legacy)">
<div className="sol-tab-label">Web3.js (legacy)</div>

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```ts
import { Keypair } from "@solana/web3.js";

const keypairBytes = Uint8Array.from([
  174, 47, 154, 16, 202, 193, 206, 113, 199, 190, 53, 133, 169, 175, 31, 56,
  222, 53, 138, 189, 224, 216, 117, 173, 10, 149, 53, 45, 73, 251, 237, 246, 15,
  185, 186, 82, 177, 240, 148, 69, 241, 227, 167, 80, 141, 89, 240, 121, 121,
  35, 172, 247, 68, 251, 226, 218, 48, 63, 176, 109, 168, 89, 238, 135,
]);

const keypair = Keypair.fromSecretKey(keypairBytes);
console.log(keypair.publicKey.toString());
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-bytes" data-tab="Web3.js (legacy)">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
```

</div>
</div>

</Tab>
</Tabs>

## From Base58 String

<Tabs>
<Tab title="Kit">
<div className="sol-tab-label">Kit</div>

```ts
import {
  createKeyPairFromBytes,
  createSignerFromKeyPair,
  getBase58Encoder,
} from "@solana/kit";

const keypairBase58 =
  "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG";

const keypair = await createKeyPairFromBytes(
  getBase58Encoder().encode(keypairBase58),
);
const signer = await createSignerFromKeyPair(keypair);

console.log(signer.address);
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-base58" data-tab="Kit">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
```

</div>
</div>

</Tab>
<Tab title="Web3.js v3">
<div className="sol-tab-label">Web3.js v3</div>

```ts
import { Keypair } from "@solana/web3.js";
import bs58 from "bs58";

const keypairBase58 =
  "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG";
const keypairBytes = bs58.decode(keypairBase58);
const keypair = await Keypair.fromSecretKey(keypairBytes);
console.log(keypair.publicKey.toBase58());
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-base58" data-tab="Web3.js v3">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
```

</div>
</div>

</Tab>
<Tab title="Rust">
<div className="sol-tab-label">Rust</div>

```rust
use anyhow::Result;
use solana_sdk::{signature::Keypair, signer::Signer};

fn main() -> Result<()> {
    let keypair_base58 =
        "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG";

    let keypair = Keypair::from_base58_string(keypair_base58);
    println!("{:?}", keypair.pubkey());

    Ok(())
}
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-base58" data-tab="Rust">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
```

</div>
</div>

</Tab>
<Tab title="Python">
<div className="sol-tab-label">Python</div>

```py
from solders.keypair import Keypair

keypair_base58 = "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"

signer = Keypair.from_base58_string(keypair_base58)

print(signer.pubkey())
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-base58" data-tab="Python">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
```

</div>
</div>

</Tab>
<Tab title="Go">
<div className="sol-tab-label">Go</div>

```go
package main

import (
	"fmt"

	"github.com/solana-foundation/solana-go/v2"
)

func main() {
	keypairBase58 := "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"

	signer := solana.MustPrivateKeyFromBase58(keypairBase58)

	fmt.Println(signer.PublicKey())
}
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-base58" data-tab="Go">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
```

</div>
</div>

</Tab>
<Tab title="Web3.js (legacy)">
<div className="sol-tab-label">Web3.js (legacy)</div>

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```ts
import { Keypair } from "@solana/web3.js";
import bs58 from "bs58";

const keypairBase58 =
  "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG";
const keypairBytes = bs58.decode(keypairBase58);
const keypair = Keypair.fromSecretKey(keypairBytes);
console.log(keypair.publicKey.toBase58());
```

<div className="sol-run" data-run="cookbook/wallets/restore-keypair/from-base58" data-tab="Web3.js (legacy)">
<div className="sol-run-bar"><button type="button" className="sol-run-btn">Run</button><span className="sol-run-note">Example output</span></div>
<div className="sol-run-out">

```text Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
```

</div>
</div>

</Tab>
</Tabs>
