|
1 | 1 | import pytest |
2 | 2 | import time |
| 3 | + |
| 4 | +from nostr import bech32 |
3 | 5 | from nostr.event import Event, EncryptedDirectMessage |
4 | 6 | from nostr.key import PrivateKey |
5 | 7 |
|
@@ -36,6 +38,44 @@ def test_event_id_recomputes(self): |
36 | 38 | assert event.id != event_id |
37 | 39 |
|
38 | 40 |
|
| 41 | + def test_note_id_bech32_conversion(self): |
| 42 | + """ should convert the event id to its `note`-prepended bech32 form """ |
| 43 | + event = Event(content="some event") |
| 44 | + assert event.note_id.startswith("note") |
| 45 | + |
| 46 | + # reverse the bech32 encoding |
| 47 | + hrp, data, spec = bech32.bech32_decode(event.note_id) |
| 48 | + raw_event_id = bech32.convertbits(data, 5, 8)[:-1] |
| 49 | + |
| 50 | + # Should get the same event_id back |
| 51 | + assert event.id == bytes(raw_event_id).hex() |
| 52 | + |
| 53 | + |
| 54 | + def test_note_id_conformity(self): |
| 55 | + """ should produce the same note ID as a known published note """ |
| 56 | + """ |
| 57 | + A real-world note from fiatjaf: |
| 58 | + { |
| 59 | + "id": "deb8b23368b6c658c36cf16396927a045dee0b7707b4133d714fb67264cc10cc", |
| 60 | + "kind": 1, |
| 61 | + "pubkey": "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", |
| 62 | + "created_at": 1673361254, |
| 63 | + "content": "hello", |
| 64 | + "tags": [], |
| 65 | + "sig": "f5e5e8a477c6749ef8562c23cdfec7a6917c975ec55075489cb3319b8a2ccb78317335a6850fb3a3714777b1c22611419d6c81ce4b0b88db86e2d1662bb17540" |
| 66 | + } |
| 67 | + """ |
| 68 | + event = Event( |
| 69 | + content="hello", |
| 70 | + public_key="3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", |
| 71 | + created_at=1673361254, |
| 72 | + kind=1, |
| 73 | + signature="f5e5e8a477c6749ef8562c23cdfec7a6917c975ec55075489cb3319b8a2ccb78317335a6850fb3a3714777b1c22611419d6c81ce4b0b88db86e2d1662bb17540" |
| 74 | + ) |
| 75 | + assert event.id == "deb8b23368b6c658c36cf16396927a045dee0b7707b4133d714fb67264cc10cc" |
| 76 | + assert event.note_id == "note1m6utyvmgkmr93smv793edyn6q3w7uzmhq76px0t3f7m8yexvzrxqw46k83" |
| 77 | + |
| 78 | + |
39 | 79 | def test_add_event_ref(self): |
40 | 80 | """ should add an 'e' tag for each event_ref added """ |
41 | 81 | some_event_id = "some_event_id" |
|
0 commit comments