Keys
Generate new random keys
To generate a new key pair use the generate()
method:
Rust
pub fn generate() -> Result<()> {
let keys = Keys::generate();
let public_key = keys.public_key();
let secret_key = keys.secret_key();
println!("Public key (hex): {}", public_key);
println!("Public key (bech32): {}", public_key.to_bech32()?);
println!("Secret key (hex): {}", keys.secret_key().to_secret_hex());
println!("Secret key (bech32): {}", secret_key.to_bech32()?);
Ok(())
}
Python
def generate():
keys = Keys.generate()
public_key = keys.public_key()
secret_key = keys.secret_key()
print("Keys:")
print(" Public keys:")
print(f" hex: {public_key.to_hex()}")
print(f" bech32: {public_key.to_bech32()}")
print()
print(" Secret keys:")
print(f" hex: {secret_key.to_hex()}")
print(f" bech32: {secret_key.to_bech32()}")
JavaScript
export function generate() {
// Generate new random keys
let keys = Keys.generate();
console.log("Public key (hex): ", keys.publicKey.toHex());
console.log("Secret key (hex): ", keys.secretKey.toHex());
console.log("Public key (bech32): ", keys.publicKey.toBech32());
console.log("Secret key (bech32): ", keys.secretKey.toBech32());
}
Kotlin
fun generate() {
val keys = Keys.generate();
val publicKey = keys.publicKey();
val secretKey = keys.secretKey();
println("Public key (hex): ${publicKey.toHex()}");
println("Public key (bech32): ${publicKey.toBech32()}");
println("Secret key (hex): ${secretKey.toHex()}");
println("Secret key (bech32): ${secretKey.toHex()}");
}
Swift
import NostrSDK
import Foundation
func keys() {
// TODO
}
Flutter
TODO
Parsing
Rust
pub fn restore() -> Result<()> {
// Parse keys directly from secret key
let keys = Keys::parse("secret-key")?;
// Parse secret key and construct keys
let secret_key =
SecretKey::parse("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")?;
let keys = Keys::new(secret_key);
// Restore from bech32
let secret_key =
SecretKey::from_bech32("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;
let keys = Keys::new(secret_key);
// Restore from hex
let secret_key =
SecretKey::from_hex("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")?;
let keys = Keys::new(secret_key);
Ok(())
}
Python
def restore():
keys = Keys.parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")
secret_key = SecretKey.from_hex("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")
keys = Keys(secret_key)
secret_key = SecretKey.from_bech32("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")
keys = Keys(secret_key)
JavaScript
export function restore() {
// Parse directly from secret key
let keys = Keys.parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85");
console.log("Secret key (hex): ", keys.secretKey.toHex());
}
Kotlin
fun restore() {
var keys = Keys.parse("hex or bech32 secret key")
// Parse from hex
var secretKey = SecretKey.fromHex("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")
keys = Keys(secretKey = secretKey)
secretKey = SecretKey.fromBech32("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")
keys = Keys(secretKey = secretKey)
}
Swift
TODO
Flutter
TODO
Generate vanity keys
Rust
pub fn vanity() -> Result<()> {
let keys = Keys::vanity(vec!["0000", "yuk", "yuk0"], true, 8)?;
println!("Secret key: {}", keys.secret_key().to_bech32()?);
println!("Public key: {}", keys.public_key().to_bech32()?);
Ok(())
}
Python
def vanity():
keys = Keys.vanity(["yuk0"], True, 8)
print(" Vanity:")
print(f" Public keys: {keys.public_key().to_bech32()}")
print(f" Secret keys: {keys.secret_key().to_bech32()}")
JavaScript
export function vanity() {
// NOTE: NOT SUPPORTED YET!
// Generate vanity keys
// let keys = Keys.vanity(["yuk0"], true, 1);
// console.log("Public key (bech32): ", keys.publicKey.toBech32());
// console.log("Secret key (bech32): ", keys.secretKey.toBech32());
}
Kotlin
fun vanity() {
val keys = Keys.vanity(listOf("yuk0"), true, 4u)
println("Public key: ${keys.publicKey().toBech32()}");
println("Secret key: ${keys.secretKey().toHex()}");
}
Swift
import NostrSDK
import Foundation
func vanity() {
// TODO
}
Flutter
TODO