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.
From Bytes
- Kit
- Web3.js v3
- Rust
- Python
- Go
- Web3.js (legacy)
Kit
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);
Example output
Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
Web3.js v3
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());
Example output
Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
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(())
}
Example output
Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
Python
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())
Example output
Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
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())
}
Example output
Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
Web3.js (legacy)
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide 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.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());
Example output
Output
24PNhTaNtomHhoy3fTRaMhAFCRj4uHqhZEEoWrKDbR5p
From Base58 String
- Kit
- Web3.js v3
- Rust
- Python
- Go
- Web3.js (legacy)
Kit
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);
Example output
Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
Web3.js v3
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());
Example output
Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
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(())
}
Example output
Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
Python
from solders.keypair import Keypair
keypair_base58 = "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"
signer = Keypair.from_base58_string(keypair_base58)
print(signer.pubkey())
Example output
Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
Go
package main
import (
"fmt"
"github.com/solana-foundation/solana-go/v2"
)
func main() {
keypairBase58 := "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG"
signer := solana.MustPrivateKeyFromBase58(keypairBase58)
fmt.Println(signer.PublicKey())
}
Example output
Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG
Web3.js (legacy)
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide 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.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());
Example output
Output
5pVyoAeURQHNMVU7DmfMHvCDNmTEYXWfEwc136GYhTKG