Skip to content

Commit

Permalink
book: add NIP-05 python examples
Browse files Browse the repository at this point in the history
* Added NIP-05 Python Example
* Updated book page for NIP-05
* Included minor print statement updates to: `json.py`, `keys.py`, `nip44.py` and `nip59.py`

Closes #435

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
RydalWater authored and yukibtc committed May 24, 2024
1 parent 56e5e43 commit 169695c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 11 deletions.
2 changes: 2 additions & 0 deletions book/snippets/nostr/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from src.event.json import event_json
from src.event.builder import event_builder
from src.nip01 import nip01
from src.nip05 import nip05
from src.nip44 import nip44
from src.nip59 import nip59

Expand All @@ -15,6 +16,7 @@ def main():
event_json()
event_builder()
nip01()
nip05()
nip44()
nip59()

Expand Down
3 changes: 2 additions & 1 deletion book/snippets/nostr/python/src/event/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ def event_json():

# Serialize as json
json = event.as_json()
print(f"{json}")
print("\nEvent JSON:")
print(f" {json}")
8 changes: 4 additions & 4 deletions book/snippets/nostr/python/src/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def generate():
print(" Public keys:")
print(f" hex: {public_key.to_hex()}")
print(f" bech32: {public_key.to_bech32()}")
print(" Secret keys:")
print("\n Secret keys:")
print(f" hex: {secret_key.to_hex()}")
print(f" bech32: {secret_key.to_bech32()}")

Expand All @@ -34,7 +34,7 @@ def restore():
# ANCHOR: vanity
def vanity():
keys = Keys.vanity(["yuk0"], True, 8)

print(f"Public keys: {keys.public_key().to_bech32()}")
print(f"Secret keys: {keys.secret_key().to_bech32()}")
print("\n Vanity:")
print(f" Public keys: {keys.public_key().to_bech32()}")
print(f" Secret keys: {keys.secret_key().to_bech32()}")
# ANCHOR_END: vanity
39 changes: 39 additions & 0 deletions book/snippets/nostr/python/src/nip05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from nostr_protocol import Keys, Metadata, EventBuilder, PublicKey, verify_nip05, get_nip05_profile


def nip05():
# ANCHOR: set-metadata
# Create metadata object with name and NIP05
metadata = Metadata() \
.set_name("TestName") \
.set_nip05("[email protected]")
# ANCHOR_END: set-metadata

print()

# ANCHOR: verify-nip05
print("Verify NIP-05:")
nip_05 = "[email protected]"
public_key = PublicKey.parse("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet")
proxy = None
try:
verify_nip05(public_key, nip_05, proxy)
print(f" '{nip_05}' verified, for {public_key.to_bech32()}")
except Exception as e:
print(f" Unable to verify NIP-05, for {public_key.to_bech32()}: {e}")
# ANCHOR_END: verify-nip05

# TODO: replace above code with the following one (due to changes to NIP-05 verify func)
# if verify_nip05(public_key, nip_05, proxy):
# print(f" '{nip_05}' verified, for {public_key.to_bech32()}")
# else:
# print(f" Unable to verify NIP-05, for {public_key.to_bech32()}")

print()

# ANCHOR: nip05-profile
print("Profile NIP-05:")
nip_05 = "[email protected]"
profile = get_nip05_profile(nip_05)
print(f" {nip_05} Profile: {profile.to_bech32()}")
# ANCHOR_END: nip05-profile
5 changes: 3 additions & 2 deletions book/snippets/nostr/python/src/nip44.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from nostr_protocol import Keys, PublicKey, nip44_encrypt, nip44_decrypt, Nip44Version

def nip44():
print("\nEncrypting and Decrypting Messages (NIP-44):")
keys = Keys.generate()

pk = PublicKey.from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")

ciphertext = nip44_encrypt(keys.secret_key(), pk, "my message", Nip44Version.V2)
print(f"Encrypted: {ciphertext}")
print(f" Encrypted: {ciphertext}")

plaintext = nip44_decrypt(keys.secret_key(), pk, ciphertext)
print(f"Decrypted: {plaintext}")
print(f" Decrypted: {plaintext}")
8 changes: 5 additions & 3 deletions book/snippets/nostr/python/src/nip59.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


def nip59():
print("\nGift Wrapping (NIP-59):")
# Sender Keys
alice_keys = Keys.parse("5c0c523f52a5b6fad39ed2403092df8cebc36318b39383bca6c00808626fab3a")

Expand All @@ -13,11 +14,12 @@ def nip59():

# Build gift wrap with sender keys
gw: Event = gift_wrap(alice_keys, bob_keys.public_key(), rumor, None)
print(f"Gift Wrap: {gw.as_json()}")
print(f" Gift Wrap:\n{gw.as_json()}")

# Extract rumor from gift wrap with receiver keys
print("\n Unwrapped Gift:")
unwrapped_gift = UnwrappedGift.from_gift_wrap(bob_keys, gw)
sender = unwrapped_gift.sender()
rumor: UnsignedEvent = unwrapped_gift.rumor()
print(f"Sender: {sender.to_bech32()}")
print(f"Rumor: {rumor.as_json()}")
print(f" Sender: {sender.to_bech32()}")
print(f" Rumor: {rumor.as_json()}")
63 changes: 62 additions & 1 deletion book/src/nostr/06-nip05.md
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
# NIP-05
## NIP-05

As a part of the kind 0 metadata events the optional key `nip05` is used to set and internet identifier value (e.g. `[email protected]`).
Clients can then use this information to make GET requests with the form `https://<domain>/.well-known/nostr.json?name=<local-part>`.

### Mapping Nostr keys to DNS-based internet identifiers (NIP-05)

<custom-tabs category="lang">

<div slot="title">Rust</div>
<section>

TODO

</section>

<div slot="title">Python</div>
<section>

Using the `Metadata` class to build the metadata object and incorporate the NIP-05 identifier with the `set_nip05()` function.

For more details on metadata (or general) events please refer back to the [examples](06-nip01.md) provided for NIP-01.

```python,ignore
{{#include ../../snippets/nostr/python/src/nip05.py:set-metadata}}
```

For verification of NIP-05 identifiers associated with a given `PublicKey` object we can the `verify_nip05()` function as follows:

```python,ignore
{{#include ../../snippets/nostr/python/src/nip05.py:verify-nip05}}
```

To retrieve a sharable profile identifier (as specified in NIP-19) the `get_nip05_profile()` function can be called with the NIP-05 value passed as an argument.

```python,ignore
{{#include ../../snippets/nostr/python/src/nip05.py:nip05-profile}}
```

</section>

<div slot="title">JavaScript</div>
<section>

TODO

</section>

<div slot="title">Kotlin</div>
<section>

TODO

</section>

<div slot="title">Swift</div>
<section>

TODO

</section>
</custom-tabs>

0 comments on commit 169695c

Please sign in to comment.