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!("Secret key (hex): {}", secret_key.to_secret_hex());
println!("Public key (bech32): {}", public_key.to_bech32()?);
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(f"Public key (hex): {public_key.to_hex()}")
print(f"Secret key (hex): {secret_key.to_hex()}")
print(f"Public key (bech32): {public_key.to_bech32()}")
print(f"Secret key (bech32): {secret_key.to_bech32()}")
JavaScript
export function generate() {
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("Secret key (hex): ${secretKey.toHex()}")
println("Public key (bech32): ${publicKey.toBech32()}")
println("Secret key (bech32): ${secretKey.toBech32()}")
}
Swift
func generate() throws {
let keys = Keys.generate()
let publicKey = keys.publicKey()
let secretKey = keys.secretKey()
print("Public key (hex): \(publicKey.toHex())")
print("Secret key (hex): \(secretKey.toHex())")
print("Public key (bech32): \(try publicKey.toBech32())")
print("Secret key (bech32): \(try secretKey.toBech32())")
}
Flutter
TODO
Parsing
Rust
pub fn restore() -> Result<()> {
// Parse keys directly from secret key
let keys = Keys::parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;
// Parse secret key and construct keys
let secret_key =
SecretKey::parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;
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() {
let keys = Keys.parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85");
console.log("Secret key (hex): ", keys.secretKey.toHex());
}
Kotlin
fun restore() {
// Parse secret key
var keys = Keys.parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")
// Parse from hex
var secretKey = SecretKey.fromHex("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")
keys = Keys(secretKey = secretKey)
// Parse from bech32
secretKey = SecretKey.fromBech32("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")
keys = Keys(secretKey = secretKey)
}
Swift
func restore() throws {
let keys = try Keys.parse(secretKey: "nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")
let publicKey = keys.publicKey()
print("Public key: \(try publicKey.toBech32())")
}
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().toBech32()}")
}
Swift
func vanity() throws {
let keys = try Keys.vanity(prefixes: ["0000", "yuk", "yuk0"], bech32: true, numCores: 8)
let publicKey = keys.publicKey()
let secretKey = keys.secretKey()
print("Public key: \(try publicKey.toBech32())")
print("Secret key: \(try secretKey.toBech32())")
}
Flutter
TODO