Skip to content

Commit

Permalink
book: add keys section
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Sep 30, 2023
1 parent 37da756 commit a87b31d
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 11 deletions.
1 change: 1 addition & 0 deletions book/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ nav:
- Overview: nostr/01-index.md
- Installation: nostr/02-installation.md
- Features: nostr/03-features.md
- Keys: nostr/04-keys.md
- Nostr SDK:
- Overview: nostr-sdk/01-index.md
- Installation: nostr-sdk/02-installation.md
Expand Down
21 changes: 21 additions & 0 deletions book/src/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# Introduction

Collection of libraries to build fast and secure `nostr` application on `desktop`, `mobile` and `web` environments.

## Libraries

* [Nostr](./nostr/01-index.md): Implementation of the `nostr` protocol
* [Nostr SDK](./nostr-sdk/01-index.md): High level nostr client library

## State

**These libraries are in ALPHA state**, things that are implemented generally work but the API will change in breaking ways.

## License

This project is distributed under the MIT software license.

## Donations

⚡ Tips: <https://getalby.com/p/yuki>

⚡ Lightning Address: [email protected]
9 changes: 9 additions & 0 deletions book/src/nostr-sdk/01-index.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# Nostr SDK

This section include documentation for the `nostr-sdk` library for all the supported languages (Rust, Python, Swift and Kotlin).

This library depends on `nostr` library so, before continue, take a look to the [`nostr`](../nostr/01-index.md) docs.

If you're writing a typical Nostr client or bot, this is likely the library you need.

However, the library is designed in a modular way and depends on several
other lower-level crates. If you're attempting something more custom, you might be interested in [`nostr`](../nostr/01-index.md) library.
2 changes: 0 additions & 2 deletions book/src/nostr-sdk/02-installation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Installing the library

## Installing the library

=== "Rust"
Expand Down
4 changes: 3 additions & 1 deletion book/src/nostr/01-index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Nostr

This section include documentation for the `nostr` library (all supported languages).
This section include documentation for the `nostr` library for all the supported languages (Rust, Python, Swift and Kotlin).

If you're writing a typical Nostr client or bot, you may be interested in [nostr-sdk](../nostr-sdk/01-index.md).
24 changes: 21 additions & 3 deletions book/src/nostr/02-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
!!! note
To use a specific commit, use `rev` instead of `tag`.

Import the library in your code:

```rust
use nostr::prelude::*;
```

=== "Python"

The `nostr-protocol` package is available on the public PyPI:
Expand All @@ -27,6 +33,12 @@
pip install nostr-protocol
```

Import the library in your code:

```python
from nostr_protocol import *
```

=== "Kotlin"

To use the Kotlin language bindings for `nostr` in your Android project add the following to your gradle dependencies:
Expand All @@ -41,6 +53,12 @@
}
```

Import the library in your code:

```kotlin
import nostr.*
```

## Known issues

### JNA dependency
Expand Down Expand Up @@ -80,8 +98,8 @@
.package(url: "https://github.com/rust-nostr/nostr-swift.git", from: "0.0.4"),
```

=== "JavaScript"
Import the library in your code:

```bash
npm i @rust-nostr/nostr
```swift
import Nostr
```
21 changes: 20 additions & 1 deletion book/src/nostr/03-features.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
## Features
# Features

!!! note
This page is related **only** to the `rust` library.

Available features:

| Feature | Default | Description |
| ------------------- | :-----: | ---------------------------------------------------------------------------------------- |
| `std` | Yes | Enable `std` library |
| `alloc` | No | Needed to use this library in `no_std` context |
| `blocking` | No | Needed to use `NIP-05` and `NIP-11` features in not async/await context |
| `all-nips` | Yes | Enable all NIPs |
| `nip03` | No | Enable NIP-03: OpenTimestamps Attestations for Events |
| `nip04` | Yes | Enable NIP-04: Encrypted Direct Message |
| `nip05` | Yes | Enable NIP-05: Mapping Nostr keys to DNS-based internet identifiers |
| `nip06` | Yes | Enable NIP-06: Basic key derivation from mnemonic seed phrase |
| `nip11` | Yes | Enable NIP-11: Relay Information Document |
| `nip44` | No | Enable NIP-44: Encrypted Payloads (Versioned) - EXPERIMENTAL |
| `nip46` | Yes | Enable NIP-46: Nostr Connect |
| `nip47` | Yes | Enable NIP-47: Nostr Wallet Connect |
103 changes: 103 additions & 0 deletions book/src/nostr/04-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Keys

## Generate new random keys

=== "Rust"

```rust
use nostr::prelude::*;

fn main() -> 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()?.display_secret());
println!("Secret key (bech32): {}", secret_key.to_bech32()?);
}
```

=== "Python"

```python
from nostr_protocol import Keys

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(" Secret keys:")
print(f" hex: {secret_key.to_hex()}")
print(f" bech32: {secret_key.to_bech32()}")
```

=== "Kotlin"

```kotlin
TODO
```

=== "Swift"

``` swift
TODO
```

## Restore from **hex** and/or **bech32** secret key

=== "Rust"

```rust
use std::str::FromStr;

use nostr::prelude::*;

fn main() -> Result<()> {
// Restore from hex
let secret_key = SecretKey::from_str("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")?;
let keys = Keys::new(secret_key);

// Restore from bech32
let secret_key = SecretKey::from_bech32("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;
let keys = Keys::new(secret_key);

// Try from bech32 or hex
let keys = Keys::from_sk_str("hex or bech32 secret key")?;

// ...
}
```

=== "Python"

```python
from nostr_protocol import *

secret_key = SecretKey.from_hex("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")
keys = Keys(secret_key)

secret_key = SecretKey.from_bech32("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")
keys = Keys(secret_key)

keys = Keys.from_sk_str("hex or bech32 secret key")

# ..
```

=== "Kotlin"

```kotlin
TODO
```

=== "Swift"

``` swift
TODO
```
5 changes: 1 addition & 4 deletions crates/nostr/examples/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ fn main() -> Result<()> {

println!("Public key: {}", public_key);
println!("Public key bech32: {}", public_key.to_bech32()?);
println!(
"Secret key: {}",
keys.secret_key()?.display_secret().to_string()
);
println!("Secret key: {}", keys.secret_key()?.display_secret());
println!("Secret key bech32: {}", secret_key.to_bech32()?);

// Bech32 keys
Expand Down

0 comments on commit a87b31d

Please sign in to comment.