Extract event signing into its own class

This commit is contained in:
Wilson Silva
2023-02-25 15:00:04 +07:00
parent 97037e6e30
commit 79567fcf22
8 changed files with 167 additions and 34 deletions

33
spec/nostr/crypto_spec.rb Normal file
View File

@@ -0,0 +1,33 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Nostr::Crypto do
let(:crypto) { described_class.new }
describe '#sign_event' do
let(:keypair) do
Nostr::KeyPair.new(
public_key: '8a9d69c56e3c691bec8f9565e4dcbe38ae1d88fffeec3ce66b9f47558a3aa8ca',
private_key: '3185a47e3802f956ca5a2b4ea606c1d51c7610f239617e8f0f218d55bdf2b757'
)
end
let(:event) do
Nostr::Event.new(
kind: Nostr::EventKind::TEXT_NOTE,
content: 'Your feedback is appreciated, now pay $8',
pubkey: keypair.public_key
)
end
it 'signs an event' do
signed_event = crypto.sign_event(event, keypair.private_key)
aggregate_failures do
expect(signed_event.id.length).to eq(64)
expect(signed_event.sig.length).to eq(128)
end
end
end
end

View File

@@ -121,6 +121,15 @@ RSpec.describe Nostr::Event do
end
end
describe '#id=' do
it 'sets the event id' do
new_id = '20f31a9b2a0ced48a167add9732ccade1dca5e34b44316e37da4af33bc8946a9'
event.id = new_id
expect(event.id).to eq(new_id)
end
end
describe '#pubkey' do
it 'exposes the event pubkey' do
expect(event.pubkey).to eq('ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460')
@@ -156,6 +165,41 @@ RSpec.describe Nostr::Event do
end
end
describe '#sig=' do
it 'sets the event signature' do
new_signature = '058613f8d34c053294cc28b7f9e1f8f0e80fd1ac94fb20f2da6ca514e7360b39' \
'63d0086171f842ffebf1f7790ce147b4811a15ef3f59c76ec1324b970cc57ffe'
event.sig = new_signature
expect(event.sig).to eq(new_signature)
end
end
describe '#sign' do
let(:event) do
described_class.new(
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
kind: Nostr::EventKind::TEXT_NOTE,
content: 'Your feedback is appreciated, now pay $8'
)
end
let(:keypair) do
Nostr::KeyPair.new(
public_key: '8a9d69c56e3c691bec8f9565e4dcbe38ae1d88fffeec3ce66b9f47558a3aa8ca',
private_key: '3185a47e3802f956ca5a2b4ea606c1d51c7610f239617e8f0f218d55bdf2b757'
)
end
it 'signs the event' do
event.sign(keypair.private_key)
aggregate_failures do
expect(event.id.length).to eq(64)
expect(event.sig.length).to eq(128)
end
end
end
describe '#tags' do
it 'exposes the event tags' do
expect(event.tags).to eq(