Add NIP-01 compliant client code

This commit is contained in:
Wilson Silva
2023-01-12 17:13:45 +07:00
parent e344ebda82
commit 5b1786ddec
30 changed files with 2260 additions and 2 deletions

View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::ClientMessageType do
describe '::EVENT' do
it 'is a string' do
expect(described_class::EVENT).to eq('EVENT')
end
end
describe '::REQ' do
it 'is a string' do
expect(described_class::REQ).to eq('REQ')
end
end
describe '::CLOSE' do
it 'is a string' do
expect(described_class::CLOSE).to eq('CLOSE')
end
end
end

309
spec/nostr/client_spec.rb Normal file
View File

@@ -0,0 +1,309 @@
# frozen_string_literal: true
require 'spec_helper'
# This test suite does not use let blocks because they don't work with EventMachine.
#
# EventMachine is a pain to work with, hence the use of sleep statements and instance variables.
# I'll come back to fix this once I'm more familiar with it.
RSpec.describe Nostr::Client do
def server(port)
@echo_server = EchoServer.new
@echo_server.listen(port)
end
def stop
@echo_server.stop
end
let(:client) { described_class.new }
let(:relay) { Nostr::Relay.new(url: plain_text_url, name: 'localhost') }
let(:port) { 4180 }
let(:plain_text_url) { "ws://0.0.0.0:#{port}/" }
before { server port }
after { stop }
describe '.new' do
it 'creates an instance of a relay' do
client = described_class.new
expect(client).to be_an_instance_of(described_class)
end
end
describe '#connect' do
it 'connects to the relay' do
connected = false
client.on :connect do
connected = true
end
client.connect(relay)
sleep 0.02
expect(connected).to be(true)
end
end
describe '#on' do
context 'when the connection is opened' do
it 'fires the :connect event' do
connect_event_fired = false
client.on :connect do
connect_event_fired = true
end
client.connect(relay)
sleep 0.02
expect(connect_event_fired).to be(true)
end
end
context 'when the client receives a message' do
it 'fires the :message event' do
received_message = nil
client.on :message do |_message|
received_message = 'hello'
end
client.connect(relay)
sleep 0.1
@echo_server.send('hello')
sleep 0.1
expect(received_message).to eq('hello')
end
end
context "when there's a connection error" do
it 'fires the :error event' do
connection_error_event = nil
client.on :error do |event|
connection_error_event = event
end
relay = Nostr::Relay.new(url: 'musk', name: 'localhost')
client.connect(relay)
sleep 0.1
expect(connection_error_event).to eq('Network error: musk: musk is not a valid WebSocket URL')
end
end
context 'when the connection is closed' do
it 'fires the :close event' do
connection_closed_code = nil
connection_closed_reason = nil
client.on :close do |code, reason|
connection_closed_code = code
connection_closed_reason = reason
end
client.connect(relay)
sleep 0.1
@echo_server.close(1000, 'We are done')
sleep 0.1
aggregate_failures do
expect(connection_closed_code).to eq(1000)
expect(connection_closed_reason).to eq('We are done')
end
end
end
end
describe '#subscribe' do
context 'when given a subscription id' do
it 'sends a REQ message to the relay, asking for all events and returns a subscription with the same id' do
id = '16605b59b539f6e86762f28fb57db2fd'
client = described_class.new
sent_message = nil
subscription = nil
client.on :message do |message|
sent_message = message
end
client.on :connect do
subscription = client.subscribe(subscription_id: id)
end
relay = Nostr::Relay.new(url: plain_text_url, name: 'localhost')
client.connect(relay)
sleep 0.01
aggregate_failures do
expect(sent_message).to eq('["REQ","16605b59b539f6e86762f28fb57db2fd",{}]')
expect(subscription).to eq(Nostr::Subscription.new(id:, filter: nil))
end
end
end
context 'when given a filter' do
it 'sends a REQ message to the relay, asking for filtered events and returns a subscription with same filter' do
allow(SecureRandom).to receive(:hex).and_return('16605b59b539f6e86762f28fb57db2fd')
filter = Nostr::Filter.new(since: 1_230_981_305)
client = described_class.new
sent_message = nil
subscription = nil
client.on :message do |message|
sent_message = message
end
client.on :connect do
subscription = client.subscribe(filter:)
end
relay = Nostr::Relay.new(url: plain_text_url, name: 'localhost')
client.connect(relay)
sleep 0.01
aggregate_failures do
expect(sent_message).to eq('["REQ","16605b59b539f6e86762f28fb57db2fd",{"since":1230981305}]')
expect(subscription).to eq(Nostr::Subscription.new(id: '16605b59b539f6e86762f28fb57db2fd', filter:))
end
end
end
context 'when given a subscription id and a filter' do
it 'sends a REQ message to the relay, asking for filtered events and returns a subscription with the same id' do
id = '16605b59b539f6e86762f28fb57db2fd'
filter = Nostr::Filter.new(since: 1_230_981_305)
client = described_class.new
sent_message = nil
subscription = nil
client.on :message do |message|
sent_message = message
end
client.on :connect do
subscription = client.subscribe(subscription_id: id, filter:)
end
relay = Nostr::Relay.new(url: plain_text_url, name: 'localhost')
client.connect(relay)
sleep 0.01
aggregate_failures do
expect(sent_message).to eq('["REQ","16605b59b539f6e86762f28fb57db2fd",{"since":1230981305}]')
expect(subscription).to eq(Nostr::Subscription.new(id:, filter:))
end
end
end
context 'when not given a subscription id nor a filter' do
it 'sends a REQ message to the relay, asking for all events and returns a subscription with a random id' do
allow(SecureRandom).to receive(:hex).and_return('16605b59b539f6e86762f28fb57db2fd')
client = described_class.new
sent_message = nil
subscription = nil
client.on :message do |message|
sent_message = message
end
client.on :connect do
subscription = client.subscribe
end
relay = Nostr::Relay.new(url: plain_text_url, name: 'localhost')
client.connect(relay)
sleep 0.01
aggregate_failures do
expect(sent_message).to eq('["REQ","16605b59b539f6e86762f28fb57db2fd",{}]')
expect(subscription).to eq(Nostr::Subscription.new(id: '16605b59b539f6e86762f28fb57db2fd', filter: nil))
end
end
end
end
describe '#unsubscribe' do
it 'sends a CLOSE message to the relay, asking it to stop a subscription' do
subscription_id = '16605b59b539f6e86762f28fb57db2fd'
client = described_class.new
sent_message = nil
client.on :message do |message|
sent_message = message
end
client.on :connect do
client.unsubscribe(subscription_id:)
end
relay = Nostr::Relay.new(url: plain_text_url, name: 'localhost')
client.connect(relay)
sleep 0.01
expect(sent_message).to eq(['CLOSE', { subscription_id: }].to_json)
end
end
describe '#publish' do
it 'sends a message to the relay' do
relay = Nostr::Relay.new(url: plain_text_url, name: 'localhost')
client = described_class.new
event = Nostr::Event.new(
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8',
sig: '970fea8d213da86c583804522c45d04e61c18c433704b62f793f187bca82091c' \
'3884d6207c6511c0966ecf6230082179a49257b03e5a4d2d08da9124a190f1bb'
)
received_message = nil
client.on :message do |message|
received_message = message
end
client.on :connect do
client.publish(event)
end
client.connect(relay)
sleep 0.01
expect(received_message).to eq(['EVENT', event.to_h].to_json)
end
end
end

View File

@@ -0,0 +1,90 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::EventFragment do
let(:event_fragment) do
described_class.new(
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
created_at: 1_230_981_305,
kind: 1,
tags: [
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
],
content: 'Your feedback is appreciated, now pay $8'
)
end
describe '.new' do
it 'creates an instance of an event fragment' do
event_fragment = described_class.new(
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
created_at: 1_230_981_305,
kind: 1,
tags: [
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
],
content: 'Your feedback is appreciated, now pay $8'
)
expect(event_fragment).to be_an_instance_of(described_class)
end
end
describe '#content' do
it 'exposes the event fragment content' do
expect(event_fragment.content).to eq('Your feedback is appreciated, now pay $8')
end
end
describe '#created_at' do
it 'exposes the event fragment creation date' do
expect(event_fragment.created_at).to eq(1_230_981_305)
end
end
describe '#kind' do
it 'exposes the event fragment kind' do
expect(event_fragment.kind).to eq(1)
end
end
describe '#pubkey' do
it 'exposes the event fragment pubkey' do
expect(event_fragment.pubkey).to eq('ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460')
end
end
describe '#serialize' do
it 'serializes the event fragment according to NIP-01' do
serialized_event_fragment = event_fragment.serialize
expect(serialized_event_fragment).to eq(
[
0,
'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
1_230_981_305,
1,
[
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
],
'Your feedback is appreciated, now pay $8'
]
)
end
end
describe '#tags' do
it 'exposes the event fragment tags' do
expect(event_fragment.tags).to eq(
[
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
]
)
end
end
end

View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::EventKind do
describe '::SET_METADATA' do
it 'is an integer' do
expect(described_class::SET_METADATA).to eq(0)
end
end
describe '::TEXT_NOTE' do
it 'is an integer' do
expect(described_class::TEXT_NOTE).to eq(1)
end
end
describe '::RECOMMEND_SERVER' do
it 'is an integer' do
expect(described_class::RECOMMEND_SERVER).to eq(2)
end
end
end

185
spec/nostr/event_spec.rb Normal file
View File

@@ -0,0 +1,185 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::Event do
let(:event) do
described_class.new(
id: '20f31a9b2a0ced48a167add9732ccade1dca5e34b44316e37da4af33bc8946a9',
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
created_at: 1_230_981_305,
kind: 1,
tags: [
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
],
content: 'Your feedback is appreciated, now pay $8',
sig: '058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39' \
'63d0086171f842ffebf1f7790ce147b4811a15ef3f59c76ec1324b970cc57ffe'
)
end
describe '#==' do
context 'when both events have the same attributes' do
it 'returns true' do
event1 = described_class.new(
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8',
sig: '970fea8d213da86c583804522c45d04e61c18c433704b62f793f187bca82091c' \
'3884d6207c6511c0966ecf6230082179a49257b03e5a4d2d08da9124a190f1bb'
)
event2 = described_class.new(
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8',
sig: '970fea8d213da86c583804522c45d04e61c18c433704b62f793f187bca82091c' \
'3884d6207c6511c0966ecf6230082179a49257b03e5a4d2d08da9124a190f1bb'
)
expect(event1).to eq(event2)
end
end
context 'when both events have at least one different attribute' do
it 'returns false' do
event1 = described_class.new(
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8',
sig: '970fea8d213da86c583804522c45d04e61c18c433704b62f793f187bca82091c' \
'3884d6207c6511c0966ecf6230082179a49257b03e5a4d2d08da9124a190f1bb'
)
event2 = described_class.new(
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8',
sig: '058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39' \
'63d0086171f842ffebf1f7790ce147b4811a15ef3f59c76ec1324b970cc57ffe'
)
expect(event1).not_to eq(event2)
end
end
end
describe '.new' do
it 'creates an instance of an event' do
event = described_class.new(
id: '20f31a9b2a0ced48a167add9732ccade1dca5e34b44316e37da4af33bc8946a9',
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
created_at: 1_230_981_305,
kind: 1,
tags: [
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
],
content: 'Your feedback is appreciated, now pay $8',
sig: '058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39' \
'63d0086171f842ffebf1f7790ce147b4811a15ef3f59c76ec1324b970cc57ffe'
)
expect(event).to be_an_instance_of(described_class)
end
end
describe '#content' do
it 'exposes the event content' do
expect(event.content).to eq('Your feedback is appreciated, now pay $8')
end
end
describe '#created_at' do
it 'exposes the event creation date' do
expect(event.created_at).to eq(1_230_981_305)
end
end
describe '#kind' do
it 'exposes the event kind' do
expect(event.kind).to eq(1)
end
end
describe '#id' do
it 'exposes the event id' do
expect(event.id).to eq('20f31a9b2a0ced48a167add9732ccade1dca5e34b44316e37da4af33bc8946a9')
end
end
describe '#pubkey' do
it 'exposes the event pubkey' do
expect(event.pubkey).to eq('ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460')
end
end
describe '#serialize' do
it 'serializes the event according to NIP-01' do
serialized_event = event.serialize
expect(serialized_event).to eq(
[
0,
'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
1_230_981_305,
1,
[
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
],
'Your feedback is appreciated, now pay $8'
]
)
end
end
describe '#sig' do
it 'exposes the event signature' do
expect(event.sig).to eq(
'058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39' \
'63d0086171f842ffebf1f7790ce147b4811a15ef3f59c76ec1324b970cc57ffe'
)
end
end
describe '#tags' do
it 'exposes the event tags' do
expect(event.tags).to eq(
[
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
]
)
end
end
describe '#to_h' do
it 'converts the event to a hash' do
expect(event.to_h).to eq(
id: '20f31a9b2a0ced48a167add9732ccade1dca5e34b44316e37da4af33bc8946a9',
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]],
content: 'Your feedback is appreciated, now pay $8',
sig: '058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39' \
'63d0086171f842ffebf1f7790ce147b4811a15ef3f59c76ec1324b970cc57ffe'
)
end
end
end

143
spec/nostr/filter_spec.rb Normal file
View File

@@ -0,0 +1,143 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::Filter do
let(:filter) do
described_class.new(
ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
kinds: [0, 1, 2],
since: 1_230_981_305,
until: 1_292_190_341,
e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
)
end
describe '#==' do
context 'when both filters have the same attributes' do
it 'returns true' do
filter1 = described_class.new(
ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
kinds: [0, 1, 2],
since: 1_230_981_305,
until: 1_292_190_341,
e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
)
filter2 = described_class.new(
ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
kinds: [0, 1, 2],
since: 1_230_981_305,
until: 1_292_190_341,
e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
)
expect(filter1).to eq(filter2)
end
end
context 'when both filters have at least one different attribute' do
it 'returns false' do
filter1 = described_class.new(
ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
kinds: [0, 1, 2],
since: 1_230_981_305,
until: 1_292_190_341,
e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
)
filter2 = described_class.new(
ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
kinds: [1],
since: 1_230_981_305,
until: 1_292_190_341,
e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
)
expect(filter1).not_to eq(filter2)
end
end
end
describe '.new' do
it 'creates an instance of a filter' do
filter = described_class.new(
ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
kinds: [0, 1, 2],
since: 1_230_981_305,
until: 1_292_190_341,
e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
)
expect(filter).to be_an_instance_of(described_class)
end
end
describe '#ids' do
it 'exposes the filter ids' do
expect(filter.ids).to eq(['c24881c305c5cfb7c1168be7e9b0e150'])
end
end
describe '#authors' do
it 'exposes the filter authors' do
expect(filter.authors).to eq(['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'])
end
end
describe '#kinds' do
it 'exposes the filter kinds' do
expect(filter.kinds).to eq([0, 1, 2])
end
end
describe '#since' do
it 'exposes the filter since' do
expect(filter.since).to eq(1_230_981_305)
end
end
describe '#until' do
it 'exposes the filter until' do
expect(filter.until).to eq(1_292_190_341)
end
end
describe '#e' do
it 'exposes the filter e' do
expect(filter.e).to eq(['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'])
end
end
describe '#p' do
it 'exposes the filter p' do
expect(filter.p).to eq(['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'])
end
end
describe '#to_h' do
it 'converts the filter to a hash' do
expect(filter.to_h).to eq(
ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
kinds: [0, 1, 2],
'#e': ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
'#p': ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
since: 1_230_981_305,
until: 1_292_190_341
)
end
end
end

View File

@@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::KeyPair do
let(:keypair) do
described_class.new(
private_key: '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900',
public_key: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'
)
end
describe '.new' do
it 'creates an instance of a key pair' do
keypair = described_class.new(
private_key: '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900',
public_key: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'
)
expect(keypair).to be_an_instance_of(described_class)
end
end
describe '#private_key' do
it 'exposes the private key' do
expect(keypair.private_key).to eq('893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900')
end
end
describe '#public_key' do
it 'exposes the public key' do
expect(keypair.public_key).to eq('2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558')
end
end
end

43
spec/nostr/keygen_spec.rb Normal file
View File

@@ -0,0 +1,43 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::Keygen do
let(:keygen) { described_class.new }
describe '.new' do
it 'creates an instance of a keygen' do
keygen = described_class.new
expect(keygen).to be_an_instance_of(described_class)
end
end
describe '#generate_key_pair' do
it 'generates a private/public key pair' do
keypair = keygen.generate_key_pair
aggregate_failures do
expect(keypair.private_key).to match(/[a-f0-9]{64}/)
expect(keypair.public_key).to match(/[a-f0-9]{64}/)
end
end
end
describe '#generate_private_key' do
it 'generates a private key' do
private_key = keygen.generate_private_key
expect(private_key).to match(/[a-f0-9]{64}/)
end
end
describe '#extract_public_key' do
it 'extracts a public key from a private key' do
private_key = '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900'
public_key = keygen.extract_public_key(private_key)
expect(public_key).to eq('2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558')
end
end
end

29
spec/nostr/relay_spec.rb Normal file
View File

@@ -0,0 +1,29 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::Relay do
let(:relay) do
described_class.new(url: 'wss://relay.damus.io', name: 'Damus')
end
describe '.new' do
it 'creates an instance of a relay' do
relay = described_class.new(url: 'wss://relay.damus.io', name: 'Damus')
expect(relay).to be_an_instance_of(described_class)
end
end
describe '#name' do
it 'exposes the relay name' do
expect(relay.name).to eq('Damus')
end
end
describe '#public_key' do
it 'exposes the relay URL' do
expect(relay.url).to eq('wss://relay.damus.io')
end
end
end

View File

@@ -0,0 +1,81 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::Subscription do
let(:filter) do
Nostr::Filter.new(since: 1_230_981_305)
end
let(:subscription) do
described_class.new(id: 'c24881c305c5cfb7c1168be7e9b0e150', filter:)
end
describe '#==' do
context 'when both subscriptions have the same attributes' do
it 'returns true' do
subscription1 = described_class.new(id: 'c24881c305c5cfb7c1168be7e9b0e150', filter:)
subscription2 = described_class.new(id: 'c24881c305c5cfb7c1168be7e9b0e150', filter:)
expect(subscription1).to eq(subscription2)
end
end
context 'when both subscriptions have a different id' do
it 'returns false' do
subscription1 = described_class.new(id: 'c24881c305c5cfb7c1168be7e9b0e150', filter:)
subscription2 = described_class.new(id: '16605b59b539f6e86762f28fb57db2fd', filter:)
expect(subscription1).not_to eq(subscription2)
end
end
context 'when both subscriptions have a different filter' do
let(:other_filter) do
Nostr::Filter.new(since: 1_230_981_305, until: 1_292_190_341)
end
it 'returns false' do
subscription1 = described_class.new(id: 'c24881c305c5cfb7c1168be7e9b0e150', filter:)
subscription2 = described_class.new(id: 'c24881c305c5cfb7c1168be7e9b0e150', filter: other_filter)
expect(subscription1).not_to eq(subscription2)
end
end
end
describe '.new' do
context 'when no id is provided' do
it 'creates an instance of a subscription using a randomly generated id' do
allow(SecureRandom).to receive(:hex).and_return('a_random_string')
subscription = described_class.new(filter:)
expect(subscription.id).to eq('a_random_string')
end
end
context 'when an id is provided' do
it 'creates an instance of a subscription using that ID' do
subscription = described_class.new(
id: 'c24881c305c5cfb7c1168be7e9b0e150',
filter:
)
expect(subscription.id).to eq('c24881c305c5cfb7c1168be7e9b0e150')
end
end
end
describe '#filter' do
it 'exposes the subscription filter' do
expect(subscription.filter).to eq(filter)
end
end
describe '#id' do
it 'exposes the subscription id' do
expect(subscription.id).to eq('c24881c305c5cfb7c1168be7e9b0e150')
end
end
end

126
spec/nostr/user_spec.rb Normal file
View File

@@ -0,0 +1,126 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::User do
let(:keypair) do
Nostr::KeyPair.new(
private_key: '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900',
public_key: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'
)
end
let(:user) do
described_class.new(keypair:)
end
describe '.new' do
context 'when no key pair is provided' do
let(:nostr_keygen) do
instance_double(Nostr::Keygen, generate_key_pair: keypair)
end
it 'creates an instance of a user with a new key pair' do
allow(Nostr::Keygen).to receive(:new).and_return(nostr_keygen)
user = described_class.new
aggregate_failures do
expect(user).to be_an_instance_of(described_class)
expect(user.keypair).to eq(keypair)
end
end
end
context 'when a key pair is provided' do
it 'creates an instance of a user using the provided key pair' do
user = described_class.new(keypair:)
expect(user.keypair).to eq(keypair)
end
end
end
describe '#keypair' do
it 'exposes the user key pair' do
expect(user.keypair).to eq(keypair)
end
end
describe '#create_event' do
context 'when created_at is missing' do
let(:now) { instance_double(Time, to_i: 1_230_981_305) }
before { allow(Time).to receive(:now).and_return(now) }
it 'builds and signs an event using the user key pair and the current time' do
event = user.create_event(
kind: Nostr::EventKind::TEXT_NOTE,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8'
)
expect(event).to eq(
Nostr::Event.new(
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8',
sig: '970fea8d213da86c583804522c45d04e61c18c433704b62f793f187bca82091c' \
'3884d6207c6511c0966ecf6230082179a49257b03e5a4d2d08da9124a190f1bb'
)
)
end
end
context 'when tags is missing' do
it 'builds and signs an event using the user key pair and empty tags' do
event = user.create_event(
kind: Nostr::EventKind::TEXT_NOTE,
tags: [],
created_at: 1_230_981_305,
content: 'Your feedback is appreciated, now pay $8'
)
expect(event).to eq(
Nostr::Event.new(
id: 'ed35331e66dc166109d45daff39b8c1bf83d4c0c7a59a9a8a23b240dc126d526',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [],
content: 'Your feedback is appreciated, now pay $8',
sig: 'f5a2cdc29723c888df52afd6f8c6e260110f74ed23fee3edbf39fff4a9f1b9f1' \
'c93284b02d4eba0481325bb5555624ddf969d5905b63f17191f9132a0ddd97b0'
)
)
end
end
context 'when all attributes are present' do
it 'builds and signs an event using the user key pair' do
event = user.create_event(
created_at: 1_230_981_305,
kind: Nostr::EventKind::TEXT_NOTE,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8'
)
expect(event).to eq(
Nostr::Event.new(
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
created_at: 1_230_981_305,
kind: 1,
tags: [%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]],
content: 'Your feedback is appreciated, now pay $8',
sig: '970fea8d213da86c583804522c45d04e61c18c433704b62f793f187bca82091c' \
'3884d6207c6511c0966ecf6230082179a49257b03e5a4d2d08da9124a190f1bb'
)
)
end
end
end
end

View File

@@ -20,6 +20,8 @@ end
require 'nostr'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'

View File

@@ -0,0 +1,43 @@
# frozen_string_literal: true
require 'puma'
require 'puma/binder'
require 'puma/events'
class EchoServer
def call(env)
@socket = Faye::WebSocket.new(env, ['echo'])
@socket.onmessage = lambda do |event|
@socket.send(event.data)
end
@socket.rack_response
end
def send(message)
@socket.send(message)
end
def close(code, reason)
@socket.close(code, reason)
end
def log(*args); end
def listen(port)
events = Puma::Events.new(StringIO.new, StringIO.new)
binder = Puma::Binder.new(events)
binder.parse(["tcp://0.0.0.0:#{port}"], self)
@server = Puma::Server.new(self, events)
@server.binder = binder
@server.run
end
def stop
case @server
when Puma::Server then @server.stop(true)
else @server.stop
end
end
end