Fixed unicode and slashes. json/write-str was escaping them.

This commit is contained in:
Robert C. Martin 2022-04-20 10:22:52 -05:00
parent bc2b912cab
commit 6760ba48a8
5 changed files with 35 additions and 22 deletions

View File

@ -115,6 +115,33 @@
(should (ecc/do-verify (ecc/hex-string->bytes id)
public-key
(ecc/hex-string->bytes sig)))))
(it "composes a message with a slash."
(let [private-key (ecc/num->bytes 64 42)
public-key (ecc/get-pub-key private-key)
reply-to nil
text "message/text"
event (compose-text-event private-key text reply-to)
{:keys [pubkey created_at kind tags content id sig]} (second event)
now (quot (System/currentTimeMillis) 1000)]
(should= "EVENT" (first event))
(should= (ecc/bytes->hex-string public-key) pubkey)
(should (<= 0 (- now created_at) 1)) ;within one second.
(should= 1 kind)
(should= [] tags)
(should= text content)
(should (ecc/do-verify (ecc/hex-string->bytes id)
public-key
(ecc/hex-string->bytes sig)))))
)
(describe "json"
(it "does not escape slashes"
(should= "\"/\"" (to-json "/")))
(it "does not escape unicode"
(should= "\"Ω\"" (to-json "\u03a9")))
)

View File

@ -2,8 +2,6 @@
;; - Add author/date, etc. to replies.
;; - Start checking sdefs in update.
;; - Clean up java schnorr library.
;; - Messages with / don't work. Signature is wrong somehow?
;; - Messages with unicode (\u) don't work either.
;; - Threading does not work quite right. Do some diagnosis.
;; - Reply
;; - Mark read and highlight properly.

View File

@ -23,6 +23,9 @@
(declare process-text-event
process-name-event)
(defn to-json [o]
(json/write-str o :escape-slash false :escape-unicode false))
(defn process-event [{:keys [application] :as state} event]
(let [{:keys [_articles nicknames]} application
_name-of (fn [pubkey] (get nicknames pubkey pubkey))
@ -108,7 +111,7 @@
(defn make-id
"returns byte array of id given the clojure form of the body"
[{:keys [pubkey created_at kind tags content]}]
(let [id-event (json/write-str [0 pubkey created_at kind tags content])
(let [id-event (to-json [0 pubkey created_at kind tags content])
id (ecc/sha-256 (.getBytes id-event StandardCharsets/UTF_8))]
id)
)

View File

@ -1,7 +1,8 @@
(ns more-speech.nostr.protocol
(:require [clojure.data.json :as json]
[more-speech.nostr.elliptic-signature :as ecc]
[clojure.core.async :as async])
[clojure.core.async :as async]
[more-speech.nostr.events :as events])
(:import (java.util Date)
(java.text SimpleDateFormat)
(java.net.http WebSocket HttpClient WebSocket$Listener)
@ -22,7 +23,7 @@
(defn send-to [^WebSocket conn msg]
(let [msg (json/write-str msg)]
(let [msg (events/to-json msg)]
(println "sending:" msg)
(.sendText conn msg true)))
@ -93,23 +94,6 @@
ws)
)
;(defn make-text [msg private-key]
; (let [pub-key (ecc/pub-key private-key)
; created-at (quot (System/currentTimeMillis) 1000)
; id-event (json/write-str [0 (ecc/bytes->hex-string pub-key) created-at 1 [] msg])
; message-bytes (.getBytes id-event StandardCharsets/UTF_8)
; id (ecc/sha-256 message-bytes)
; event ["EVENT" {"id" (ecc/bytes->hex-string id)
; "pubkey" (ecc/bytes->hex-string pub-key)
; "created_at" created-at
; "kind" 1
; "tags" []
; "content" msg
; "sig" (ecc/bytes->hex-string (ecc/sign private-key id))
; }]
; ]
; event))
(def private-key (ecc/sha-256 (.getBytes "I am Bob.")))
(defn get-events [events send-chan]

View File

@ -9,6 +9,7 @@
[clojure.core.async :as async]
[more-speech.ui.formatters :as formatters]
[more-speech.nostr.elliptic-signature :as ecc]))
;; edit-frame
;; - :text A vector containing the lines of the composed message
;; - :reply-id nil, or the numeric id of the article being replied to.