Allow the verification of signatures and events

Added the methods:
- Event#verify_signature
- Crypto#check_sig!
- Crypto#valid_sig?
- Crypto#sign_message

Fixed a primitive obsession by introducing a Signature class to ensure that signatures are valid Nostr signatures.
This commit is contained in:
Wilson Silva
2024-03-14 22:03:26 +00:00
parent f8893f9b0e
commit 01010c763f
25 changed files with 637 additions and 47 deletions

View File

@@ -5,6 +5,62 @@ require 'spec_helper'
RSpec.describe Nostr::Crypto do
let(:crypto) { described_class.new }
describe '#check_sig!' do
let(:keypair) do
Nostr::KeyPair.new(
public_key: Nostr::PublicKey.new('15678d8fbc126fa326fac536acd5a6dcb5ef64b3d939abe31d6830cba6cd26d6'),
private_key: Nostr::PrivateKey.new('7d1e4219a5e7d8342654c675085bfbdee143f0eb0f0921f5541ef1705a2b407d')
)
end
let(:message) { 'Your feedback is appreciated, now pay $8' }
context 'when the signature is valid' do
it 'returns true' do
signature = crypto.sign_message(message, keypair.private_key)
expect(crypto.check_sig!(message, keypair.public_key, signature)).to be(true)
end
end
context 'when the signature is invalid' do
it 'raises an error' do
signature = Nostr::Signature.new('badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadb' \
'badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadb')
expect do
crypto.check_sig!(message, keypair.public_key, signature)
end.to raise_error(Schnorr::InvalidSignatureError, 'signature verification failed.')
end
end
end
describe '#valid_sig?' do
let(:keypair) do
Nostr::KeyPair.new(
public_key: Nostr::PublicKey.new('15678d8fbc126fa326fac536acd5a6dcb5ef64b3d939abe31d6830cba6cd26d6'),
private_key: Nostr::PrivateKey.new('7d1e4219a5e7d8342654c675085bfbdee143f0eb0f0921f5541ef1705a2b407d')
)
end
let(:message) { 'Your feedback is appreciated, now pay $8' }
context 'when the signature is valid' do
it 'returns true' do
signature = crypto.sign_message(message, keypair.private_key)
expect(crypto.valid_sig?(message, keypair.public_key, signature)).to be(true)
end
end
context 'when the signature is invalid' do
it 'returns false' do
signature = Nostr::Signature.new('badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadb' \
'badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadb')
expect(crypto.valid_sig?(message, keypair.public_key, signature)).to be(false)
end
end
end
describe '#sign_event' do
let(:keypair) do
Nostr::KeyPair.new(
@@ -31,6 +87,19 @@ RSpec.describe Nostr::Crypto do
end
end
describe '#sign_message' do
let(:private_key) { Nostr::PrivateKey.new('7d1e4219a5e7d8342654c675085bfbdee143f0eb0f0921f5541ef1705a2b407d') }
let(:message) { 'Your feedback is appreciated, now pay $8' }
it 'signs a message' do
signature = crypto.sign_message(message, private_key)
hex_signature = '0fa6d8e26f44ddad9eca5be2b8a25d09338c1767f8bfce384046c8eb771d1120e4bda5ca49' \
'27e74837f912d4810945af6abf8d38139c1347f2d71ba8c52b175b'
expect(signature).to eq(Nostr::Signature.new(hex_signature))
end
end
describe '#encrypt_text' do
let(:sender_keypair) do
Nostr::KeyPair.new(