- Kit
- Web3.js v3
- Rust
- Python
- Go
- Web3.js (legacy)
Kit
import {
generateKeyPair,
signBytes,
verifySignature,
getUtf8Encoder,
getBase58Decoder,
} from "@solana/kit";
const keys = await generateKeyPair();
const message = getUtf8Encoder().encode("Hello, World!");
const signedBytes = await signBytes(keys.privateKey, message);
const decoded = getBase58Decoder().decode(signedBytes);
console.log("Signature:", decoded);
const verified = await verifySignature(keys.publicKey, signedBytes, message);
console.log("Verified:", verified);
Example output
Output
Signature: 4u1mRhCwXhzRwiprmXQ6vZbR5PdHpq92YN274wbFBbe1oPbmbrC5uFYxhgHkvZnDaJoVexky99EYTy7jUZ9r6NMw
Verified: true
Web3.js v3
import { Keypair } from "@solana/web3.js";
const keypair = await Keypair.fromSecretKey(
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 message = "The quick brown fox jumps over the lazy dog";
const messageBytes = new TextEncoder().encode(message);
const signature = await keypair.signBytes(messageBytes);
const result = await keypair.verifySignature(signature, messageBytes);
console.log("Verified:", result);
Example output
Output
Verified: true
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[..])?;
let message = "The quick brown fox jumps over the lazy dog";
let signature = keypair.sign_message(message.as_bytes());
let is_valid_signature = signature.verify(&keypair.pubkey().to_bytes(), message.as_bytes());
println!("Verified: {:?}", is_valid_signature);
Ok(())
}
Example output
Output
Verified: true
Python
from solders.keypair import Keypair
keys = Keypair()
message = "Hello, World!".encode("utf-8")
signed_bytes = keys.sign_message(message)
print("Signature:", signed_bytes)
verified = signed_bytes.verify(keys.pubkey(), message)
print("Verified:", verified)
Example output
Output
Signature: 5Z77czQpmhPZ4mzr8HhPjFgfae6SWQtaXguxf67Sphc89GF2kG8zAnA8bqxsitATj9HDzpiEniESs9KHbK4fQb4u
Verified: True
Go
package main
import (
"fmt"
"github.com/solana-foundation/solana-go/v2"
)
func main() {
keys := solana.NewWallet().PrivateKey
message := []byte("Hello, World!")
signedBytes, err := keys.Sign(message)
if err != nil {
panic(err)
}
fmt.Println("Signature:", signedBytes)
verified := signedBytes.Verify(keys.PublicKey(), message)
fmt.Println("Verified:", verified)
}
Example output
Output
Signature: 2ScgLbWw2dnaY4RifZA7rSpLtHjVPFok8QuoYRTDcspU8wQm9HRiuE3sN2Lu13C9FHCT9U2ycAbMXhnb5wTpMf9J
Verified: true
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.// In Solana Web3.js v1, we can use the TweetNaCl crypto library:
import { Keypair } from "@solana/web3.js";
import nacl from "tweetnacl";
import naclUtil from "tweetnacl-util";
const keypair = Keypair.fromSecretKey(
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 message = "The quick brown fox jumps over the lazy dog";
const messageBytes = naclUtil.decodeUTF8(message);
const signature = nacl.sign.detached(messageBytes, keypair.secretKey);
const result = nacl.sign.detached.verify(
messageBytes,
signature,
keypair.publicKey.toBytes(),
);
console.log("Verified:", result);
Example output
Output
Verified: true