package main // Verify Worker - BIP-340 Schnorr signature verification. // // Wire format: // supervisor -> worker: ["V_CHECK", reqID, evJSON] // worker -> supervisor: ["V_RESULT", reqID, 1|0] // // Pure compute: no IDB, no WebSocket, no signer access. // Four instances run in parallel; the Verify Supervisor distributes round-robin. import ( "git.smesh.lol/smesh/web/common/helpers" "git.smesh.lol/smesh/web/common/jsbridge/verify" "git.smesh.lol/smesh/web/common/nostr" nosig "git.smesh.lol/smesh/web/common/nostr/sig" ) func main() { verify.WorkerOnMessage(handleMessage) } func handleMessage(msg string) { w := newMW(msg) if w.str() != "V_CHECK" { return } reqID := w.num() evJSON := w.raw() ev := nostr.ParseEvent(evJSON) var valid int64 if ev != nil && (*nosig.Event)(ev).CheckSig() { valid = 1 } verify.WorkerPost(`["V_RESULT",` | helpers.Itoa(reqID) | `,` | helpers.Itoa(valid) | `]`) } // mw: minimal JSON-array walker type mw struct { s string i int32 } func newMW(s string) (v mw) { w := mw{s: s} for w.i < len(s) && s[w.i] != '[' { w.i++ } if w.i < len(s) { w.i++ } return w } func (w *mw) sep() { for w.i < len(w.s) { c := w.s[w.i] if c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != ',' { break } w.i++ } } func (w *mw) str() (s string) { w.sep() if w.i >= len(w.s) || w.s[w.i] != '"' { return "" } w.i++ start := w.i for w.i < len(w.s) && w.s[w.i] != '"' { if w.s[w.i] == '\\' { w.i++ } w.i++ } if w.i >= len(w.s) { return "" } result := w.s[start:w.i] w.i++ return result } func (w *mw) num() (n int64) { w.sep() neg := false if w.i < len(w.s) && w.s[w.i] == '-' { neg = true w.i++ } var n int64 for w.i < len(w.s) && w.s[w.i] >= '0' && w.s[w.i] <= '9' { n = n*10 + int64(w.s[w.i]-'0') w.i++ } if neg { n = -n } return n } func (w *mw) raw() (s string) { w.sep() start := w.i w.i = skipval(w.s, w.i) if w.i < 0 { w.i = len(w.s) return "" } return w.s[start:w.i] } func skipval(s string, i int32) (n int32) { if i >= len(s) { return -1 } c := s[i] if c == '"' { i++ for i < len(s) && s[i] != '"' { if s[i] == '\\' { i++ } i++ } if i >= len(s) { return -1 } return i + 1 } if c == '{' || c == '[' { open := c close := byte('}') if open == '[' { close = ']' } depth := 1 i++ for i < len(s) && depth > 0 { if s[i] == '"' { i++ for i < len(s) && s[i] != '"' { if s[i] == '\\' { i++ } i++ } if i >= len(s) { return -1 } i++ continue } if s[i] == open { depth++ } else if s[i] == close { depth-- if depth == 0 { return i + 1 } } i++ } return -1 } for i < len(s) { c := s[i] if c == ',' || c == ']' || c == '}' { return i } i++ } return i }