267 Commits

Author SHA1 Message Date
fiatjaf
9775016bf1
implement nip-42 AUTH event validation. v0.11.1 2023-01-15 22:00:38 -03:00
barkyq
a37ffacc74
added sub.mutex handling in the relay Connect() function (#37) 2023-01-15 09:19:00 -03:00
Dylan Cant
635c1b0132 fixed small typo 2023-01-14 00:27:18 -03:00
Dylan Cant
ea968c79d1 typo... 2023-01-14 00:27:18 -03:00
Dylan Cant
f149ee8b4e nip19, subscription, publication code examples added to README.md
fixed typo in example/example.go
2023-01-14 00:27:18 -03:00
Dylan Cant
92ce379649 added example/example.go with subscription and publication code example 2023-01-13 22:08:32 -03:00
fiatjaf
c64078e5cc
remove outdated docs. must add new ones later. 2023-01-13 14:28:56 -03:00
fiatjaf
0397395261
force a deadline of 7 seconds when connecting to relays (if not set).
must add documentation to these calls.
v0.11.0
2023-01-03 14:47:21 -03:00
fiatjaf
3349b2a52b
fix encoding and decoding of event "extra" fields.
fixes https://github.com/nbd-wtf/go-nostr/issues/31
2023-01-03 14:46:50 -03:00
fiatjaf
4a62a753e6
contexts everywhere. 2023-01-01 20:58:43 -03:00
fiatjaf
c18de89dd3
remove relaypool because it is considered harmful. 2023-01-01 20:10:05 -03:00
fiatjaf
4dbbcec80a
one more nip19 test. 2022-12-29 09:35:05 -03:00
fiatjaf
e4c5dfbebb
nip19. v0.10.0 2022-12-27 07:49:26 -03:00
alex
28663f21f0 relaypool: add a unique events subscription smoke test
was trying to reproduce the issue described in
https://github.com/nbd-wtf/go-nostr/issues/23

no success in reproducing that specific problem, but i believe the test
can still be useful to help in avoiding regression bugs in the future.
2022-12-26 16:03:00 -03:00
alex
435579dc75 publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:

    ["OK", event-id, false, "blocked"]

this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:

    func (r Relay) Publish(event Event) chan Status {
      // uses a **copy** of statusChans here:
      r.statusChans.Store(event.ID, statusChan)
      ...
    }

the bugfix is a one character change:

    func (r *Relay) Publish(event Event) chan Status

but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:

    // ok, which is a command status from earlier, is shadowed here:
    if statusChan, ok := r.statusChans.Load(eventId); ok {
      statusChan <- ...
    }

as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 14:44:40 -03:00
alex
5bfb398f4d nip13: check and generate proof of work
implementation as per NIP-13
https://github.com/nostr-protocol/nips/blob/e79c84ae/13.md

some benchmark results:

    $ go test -bench . ./nip13
    goos: linux
    goarch: amd64
    pkg: github.com/nbd-wtf/go-nostr/nip13
    cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
    BenchmarkCheck-8                        144905385                8.328 ns/op
    BenchmarkGenerateOneIteration-8           467874              2451 ns/op
    BenchmarkGenerate/8bits-8                   4045           1033196 ns/op
    BenchmarkGenerate/16bits-8                    16         101939954 ns/op
    BenchmarkGenerate/24bits-8                     1        11411513764 ns/op
    PASS
    ok      github.com/nbd-wtf/go-nostr/nip13       22.220s
2022-12-26 08:05:40 -03:00
Stan Stacker
bb1138a2fa problem: panic if calling unsub more than once
problem: panic if calling unsub more than once

It's a bit dangerous if calling unsub more than once causes a fatal panic.
2022-12-25 14:34:14 -03:00
Wes van der Vleuten
3f2c3f1bd8 fix: nip04 remove extra padding 2022-12-23 11:35:24 -03:00
fiatjaf
6aab32ef4f
Merge pull request #20 from stanstacks/cantunsub
problem: can't unsub
2022-12-23 11:33:11 -03:00
stanstacks
6452f7de09 problem: example is broken 2022-12-23 10:12:22 +08:00
stanstacks
c94ae093c1 problem: close channel to unsub is confusing 2022-12-23 10:03:40 +08:00
stanstacks
6f1c60f66c problem: can't unsub 2022-12-22 18:48:41 +08:00
mlctrez
72ef03f238 use crypto/rand instead of math/rand 2022-12-20 08:00:22 -03:00
fiatjaf
6c13dc7969
remove tlv from npub in nip19. 2022-12-19 15:10:23 -03:00
fiatjaf
b3aa4eba29
remove unused dependency. v0.9.0 2022-12-18 06:37:54 -03:00
alex
c327f622f3 relay: introduce ConnectContext for better control over network latency
A websocket dial may hand for an unreasonably long time and a nostr client
has no control over this when trying to connect to a relay.

Go started introducing context in networking since 2014 -
see https://go.dev/blog/context - and by now many net functions have
XxxContext equivalent, such as DialContext.

Example usage of the change introduced by this commit:

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()
    r, err := nostr.RelayConnectContext(ctx, "ws://relay.example.org")

The code above makes RelayConnectContext last at most 3 sec, returning
an error if a connection cannot be established in the given time.
This helps whenever a tight control over connection latency is required,
such as distributed systems.

The change is backwards-compatible except the case where RelayPool.Add
sent an error over the returned channel without actually closing said
channel. I believe it was a bug.
2022-12-17 22:33:05 -03:00
fiatjaf
ad71e083d8
add some extra padding to decoy super small messages. 2022-12-14 16:17:37 -03:00
fiatjaf
8a8fd7c2b0
fix nip04 decryption padding and add tests. 2022-12-14 16:10:10 -03:00
fiatjaf
ac25c2071e
another event serialization test, now reversed. 2022-12-14 13:35:13 -03:00
fiatjaf
4a3aea6d4b
update nip04 to btcec/v2 2022-12-11 19:01:36 -03:00
fiatjaf
fb29445ef9
omitempty metadata fields. 2022-12-11 16:35:04 -03:00
fiatjaf
ea15550039
kind-0 metadata helper type. 2022-11-26 19:32:16 -03:00
fiatjaf
0c39530d57
QuerySync() relay method. 2022-11-26 19:32:03 -03:00
fiatjaf
e8bc11e4f9
support nip-35. 2022-11-26 19:31:49 -03:00
fiatjaf
2d01aa8630
prevent sending on closed channel for subscription. v0.8.2 2022-11-26 09:25:51 -03:00
fiatjaf
67d8f26d8a
some stringifiers. 2022-11-26 09:25:31 -03:00
fiatjaf
b0ae497656
allow more fine-grained control over subscription filters. 2022-11-19 14:00:29 -03:00
fiatjaf
8bc91a894c
sub.GetFilters() 2022-11-19 07:19:36 -03:00
fiatjaf
b2885d57cd
fix blank case on NormalizeURL(). 2022-11-19 07:19:10 -03:00
fiatjaf
89cb5ad461
fix Tag.Relay(). 2022-11-18 14:18:37 -03:00
fiatjaf
e09f6b4bff
fix event extra: create a map when it doesn't exist. 2022-11-18 14:18:37 -03:00
fiatjaf
0d7a4b258a
fix normalize and add a test. 2022-11-18 14:18:37 -03:00
fiatjaf
37b3bf7a8a
helpers for dealing with extra values in events. 2022-11-17 10:58:45 -03:00
fiatjaf
480ee0ef87
PublishEvent() fixes. 2022-11-17 09:28:45 -03:00
fiatjaf
381ee2cc01
tag helpers in a separate file and nip10 helpers. 2022-11-17 09:19:55 -03:00
fiatjaf
69ccfbaa08
protect against faulty relays that send more than one EOSE halting us
using sync.Once{} to only emit to the EndOfStoredEvents channel once
(it has capacity 1 so anything over that would halt).
2022-11-16 10:07:37 -03:00
fiatjaf
7538f1108d
stop halting at EOSE. 2022-11-16 10:05:28 -03:00
fiatjaf
14e81a756a
fix unique logic (move it to a helper function, we can't return two channels because that will break if the caller doesn't read from both.) 2022-11-15 16:40:17 -03:00
fiatjaf
2ec7957409
fix nip19 bech32 encoding and decoding. 2022-11-15 16:29:37 -03:00
fiatjaf
dd0571229b
RelayConnect() ensures there will be a connection, and handle connection errors better. v0.8.1 2022-11-15 07:53:50 -03:00