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
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
void generate() {
  final keys = Keys.generate();

  final publicKey = keys.publicKey();
  final secretKey = keys.secretKey();

  print("Public key (hex): ${publicKey.toHex()}");
  print("Secret key (hex): ${secretKey.toSecretHex()}");

  print("Public key (bech32): ${publicKey.toBech32()}");
  print("Secret key (bech32): ${secretKey.toBech32()}");
}

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.parse("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")
    keys = Keys(secret_key)
JavaScript
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.parse("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")
    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
void restore() {
  // Parse keys directly from secret key (hex or bech32)
  var keys = Keys.parse(
    secretKey:
        "nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99",
  );

  // Parse secret key and construct keys
  var secretKey = SecretKey.parse(
    secretKey:
        "6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e",
  );
  keys = Keys(secretKey: secretKey);

  print(keys);
}