SimpleNostr

From Traxel Wiki
Jump to navigation Jump to search


Run From Local

Make a tunnel:

ssh -L 7777:127.0.0.1:7777 admin@52.11.172.130 -N
sudo apt install python3-venv
pip3 install virtualenv
git clone https://github.com/ksfi/simple-nostr.git
cd simple-nostr
python3 -m venv bobenv
source ./bobenv/bin/activate
pip3 install websockets secp256k1 bech32
emacs -nw message.py # change line 111, add message.encode('utf-8')
python message.py
...
deactivate

Try it

$ python message.py
ENTER A RELAY: (default: wss://relay.damus.io)
ws://127.0.0.1:7777
ws://127.0.0.1:7777
[msg] OR [msg] // [privkey] // [pubkey] OR q to quit OR enter to watch:
test
test
dGVzdA==
content: test
content: test
["OK","c8da3f0fea09589570263875f9101ea9539684a13965844f3668b78a565acb52",true,""]
[msg] OR [msg] // [privkey] // [pubkey] OR q to quit OR enter to watch:

First Try

sudo apt install python3-venv
pip3 install virtualenv
git clone https://github.com/ksfi/simple-nostr.git
cd simple-nostr
python3 -m venv bobenv
source ./bobenv/bin/activate
pip3 install websockets secp256k1 bech32
emacs -nw message.py # change line 111, add message.encode('utf-8')
python message.py
...
deactivate

Run and error:

python message.py 
ENTER A RELAY: (default: wss://relay.damus.io)
wss://127.0.0.1:7777
wss://127.0.0.1:7777
[msg] OR [msg] // [privkey] // [pubkey] OR q to quit OR enter to watch:
test
test
Traceback (most recent call last):
  File "/home/admin/projects/nostr/simple-nostr/message.py", line 181, in <module>
    run()
  File "/home/admin/projects/nostr/simple-nostr/message.py", line 178, in run
    asyncio.run(m.sendNote())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/admin/projects/nostr/simple-nostr/message.py", line 124, in sendNote
    note = json.dumps(["EVENT", self.to_json(dm_message, iv)], separators=(',', ':'), ensure_ascii=False)
  File "/home/admin/projects/nostr/simple-nostr/message.py", line 111, in to_json
    print(base64.b64encode(self.message).decode('utf-8'))
  File "/usr/lib/python3.9/base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'

Debug

Make it work with Python3

$ git diff
diff --git a/message.py b/message.py
index 9e88376..d2d2c87 100644
--- a/message.py
+++ b/message.py
@@ -108,7 +108,7 @@ class Message:
             kind = 1
             tags = []
         print(self.message)
-        print(base64.b64encode(self.message).decode('utf-8'))
+        print(base64.b64encode(self.message.encode('utf-8')).decode('utf-8'))
         ret = {
           "id": self.get_id(dm_message, iv).hex(),
           "pubkey": pubk,

Grabbed a compiled copy of Websocat from here: https://github.com/vi/websocat/releases

As I was fiddling with it, I noticed that there was wss:// and ws:// - that led me to think "Secure!" I bet wss means TLS encrypted.

Problem solved:

$ python message.py 
ENTER A RELAY: (default: wss://relay.damus.io)
ws://127.0.0.1:7777
ws://127.0.0.1:7777
[msg] OR [msg] // [privkey] // [pubkey] OR q to quit OR enter to watch:
test
test
dGVzdA==
content: test
content: test
["OK","d13b724fea64796c31cec9077fa2e6096bd5b78d7de72430a07d80e2ab24f6aa",true,""]
[msg] OR [msg] // [privkey] // [pubkey] OR q to quit OR enter to watch: