Skip to content

Commit

Permalink
book: complete Keys.swift snippet
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Nov 28, 2024
1 parent 49595b6 commit 4a6e7f3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 30 deletions.
3 changes: 1 addition & 2 deletions book/snippets/js/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Keys } from "@rust-nostr/nostr-sdk"

// ANCHOR: generate
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());

Expand All @@ -14,7 +14,6 @@ export function generate() {

// ANCHOR: restore
export function restore() {
// Parse directly from secret key
let keys = Keys.parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85");
console.log("Secret key (hex): ", keys.secretKey.toHex());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ fun generate() {
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()}")

println("Public key (bech32): ${publicKey.toBech32()}")
println("Secret key (bech32): ${secretKey.toBech32()}")
}
// ANCHOR_END: generate

// ANCHOR: restore
fun restore() {
var keys = Keys.parse("hex or bech32 secret key")
// 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)
}
Expand All @@ -35,6 +37,6 @@ fun vanity() {
val keys = Keys.vanity(listOf("yuk0"), true, 4u)

println("Public key: ${keys.publicKey().toBech32()}")
println("Secret key: ${keys.secretKey().toHex()}")
println("Secret key: ${keys.secretKey().toBech32()}")
}
// ANCHOR_END: vanity
14 changes: 6 additions & 8 deletions book/snippets/python/src/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
# ANCHOR: generate
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()}")
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()}")
# ANCHOR_END: generate

# ANCHOR: restore
Expand Down
7 changes: 4 additions & 3 deletions book/snippets/rust/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub fn generate() -> Result<()> {
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 (hex): {}", keys.secret_key().to_secret_hex());
println!("Secret key (bech32): {}", secret_key.to_bech32()?);

Ok(())
Expand All @@ -19,11 +20,11 @@ pub fn generate() -> Result<()> {
// ANCHOR: restore
pub fn restore() -> Result<()> {
// Parse keys directly from secret key
let keys = Keys::parse("secret-key")?;
let keys = Keys::parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;

// Parse secret key and construct keys
let secret_key =
SecretKey::parse("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")?;
SecretKey::parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;
let keys = Keys::new(secret_key);

// Restore from bech32
Expand Down
33 changes: 29 additions & 4 deletions book/snippets/swift/NostrSnippets/Sources/Keys.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import NostrSDK
// ANCHOR: full
import Foundation
import NostrSDK

func keys() throws {
// ANCHOR: generate
// ANCHOR: generate
func generate() throws {
let keys = Keys.generate()

let publicKey = keys.publicKey()
let secretKey = keys.secretKey()

Expand All @@ -12,5 +14,28 @@ func keys() throws {

print("Public key (bech32): \(try publicKey.toBech32())")
print("Secret key (bech32): \(try secretKey.toBech32())")
// ANCHOR_END: generate
}
// ANCHOR_END: generate

// ANCHOR: restore
func restore() throws {
let keys = try Keys.parse(secretKey: "nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")

let publicKey = keys.publicKey()

print("Public key: \(try publicKey.toBech32())")
}
// ANCHOR_END: restore

// ANCHOR: vanity
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())")
}
// ANCHOR_END: vanity
// ANCHOR_END: full
6 changes: 0 additions & 6 deletions book/snippets/swift/NostrSnippets/Sources/Vanity.swift

This file was deleted.

6 changes: 4 additions & 2 deletions book/src/sdk/signers/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ TODO
<div slot="title">Swift</div>
<section>

TODO
```swift,ignore
{{#include ../../../snippets/swift/NostrSnippets/Sources/Keys.swift:restore}}
```

</section>

Expand Down Expand Up @@ -158,7 +160,7 @@ TODO
<section>

```swift,ignore
{{#include ../../../snippets/swift/NostrSnippets/Sources/Vanity.swift}}
{{#include ../../../snippets/swift/NostrSnippets/Sources/Keys.swift:vanity}}
```

</section>
Expand Down

0 comments on commit 4a6e7f3

Please sign in to comment.