Go to file
Râu Cao d59f31a3c6
Make code compatible with Ruby 2.7
Not including specs etc.
2024-01-15 12:34:53 +03:00
.github/workflows Setup CodeClimate's quality reporting 2023-01-06 12:41:31 +07:00
bin Add and configure dotenv 2023-01-08 15:33:49 +07:00
docs Add full NIP-19 compatibility 2023-11-20 21:03:24 +07:00
lib Make code compatible with Ruby 2.7 2024-01-15 12:34:53 +03:00
sig Add full NIP-19 compatibility 2023-11-20 21:03:24 +07:00
spec Add full NIP-19 compatibility 2023-11-20 21:03:24 +07:00
.editorconfig Add high-level documentation 2023-11-13 17:48:17 +07:00
.gitignore Ignore my local copy of the nostr nips folder 2023-11-13 18:09:03 +07:00
.overcommit.yml Add Overcommit to prevent committing substandard code 2023-01-06 11:10:18 +07:00
.rspec Initial commit 2023-01-06 10:00:47 +07:00
.rubocop_todo.yml Add NIP-01 compliant client code 2023-01-12 17:21:17 +07:00
.rubocop.yml Update bip-schnorr, puma, rake, rubocop and rubocop-rspec 2023-11-20 10:32:27 +07:00
.tool-versions Update Ruby to version 3.2.2 2023-11-14 07:54:56 +07:00
.yardstick.yml Add yard, yardstick and yard-junk to document the gem 2023-01-06 11:15:17 +07:00
CHANGELOG.md Fix a few YARD example rendering issues 2023-11-20 21:43:01 +07:00
CODE_OF_CONDUCT.md Initial commit 2023-01-06 10:00:47 +07:00
Gemfile Fix RuboCop violations 2023-01-06 11:10:17 +07:00
Guardfile Add Guard with bundler, rspec and rubocop plugins 2023-01-06 11:10:19 +07:00
LICENSE.txt Initial commit 2023-01-06 10:00:47 +07:00
nostr.gemspec Make code compatible with Ruby 2.7 2024-01-15 12:34:53 +03:00
Rakefile Add a Rake task to assure the quality of the codebase 2023-01-06 11:16:27 +07:00
README.md Add full NIP-19 compatibility 2023-11-20 21:03:24 +07:00
Steepfile Implement NIP-04: Encrypted Direct Messages 2023-02-25 17:38:00 +07:00

Nostr

Gem Version Maintainability Test Coverage

Asynchronous Nostr client for Rubyists.

Table of contents

🔑 Key features

  • Asynchronous
  • Easy to use
  • Fully documented
  • Fully tested
  • Fully typed

📦 Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add nostr

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install nostr

Quickstart

Here is a quick example of how to use the gem. For more detailed documentation, please check the documentation website.

# Require the gem
require 'nostr'

# Instantiate a client
client = Nostr::Client.new

# a) Use an existing keypair
keypair = Nostr::KeyPair.new(
  private_key: Nostr::PrivateKey.new('add-your-hex-private-key-here'),
  public_key: Nostr::PublicKey.new('add-your-hex-public-key-here'),
)

# b) Or build a keypair from a private key
keygen = Nostr::Keygen.new
keypair = keygen.get_key_pair_from_private_key(
  Nostr::PrivateKey.new('add-your-hex-private-key-here')
)

# c) Or create a new keypair
keygen = Nostr::Keygen.new
keypair = keygen.generate_keypair

# Create a user with the keypair
user = Nostr::User.new(keypair: keypair)

# Create a signed event
text_note_event = user.create_event(
  kind: Nostr::EventKind::TEXT_NOTE,
  content: 'Your feedback is appreciated, now pay $8'
)

# Connect asynchronously to a relay
relay = Nostr::Relay.new(url: 'wss://nostr.wine', name: 'Wine')
client.connect(relay)

# Listen asynchronously for the connect event
client.on :connect do
  # Send the event to the Relay
  client.publish(text_note_event)

  # Create a filter to receive the first 20 text notes
  # and encrypted direct messages from the relay that
  # were created in the previous hour
  filter = Nostr::Filter.new(
    kinds: [
      Nostr::EventKind::TEXT_NOTE,
      Nostr::EventKind::ENCRYPTED_DIRECT_MESSAGE
    ],
    since: Time.now.to_i - 3600, # 1 hour ago
    until: Time.now.to_i,
    limit: 20,
  )

  # Subscribe to events matching conditions of a filter
  subscription = client.subscribe(filter: filter)

  # Unsubscribe from events matching the filter above
  client.unsubscribe(subscription.id)
end

# Listen for incoming messages and print them
client.on :message do |message|
  puts message
end

# Listen for error messages
client.on :error do |error_message|
  # Handle the error
end

# Listen for the close event
client.on :close do |code, reason|
  # You may attempt to reconnect to the relay here
end

📚 Documentation

I made a detailed documentation for this gem and it's usage. The code is also fully documented using YARD.

Implemented NIPs

🔨 Development

After checking out the repo, run bin/setup to install dependencies.

To install this gem onto your local machine, run bundle exec rake install.

You can also run bin/console for an interactive prompt that will allow you to experiment.

To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

The health and maintainability of the codebase is ensured through a set of Rake tasks to test, lint and audit the gem for security vulnerabilities and documentation:

rake build                    # Build nostr.gem into the pkg directory
rake build:checksum           # Generate SHA512 checksum if nostr.gem into the checksums directory
rake bundle:audit:check       # Checks the Gemfile.lock for insecure dependencies
rake bundle:audit:update      # Updates the bundler-audit vulnerability database
rake clean                    # Remove any temporary products
rake clobber                  # Remove any generated files
rake coverage                 # Run spec with coverage
rake install                  # Build and install nostr.gem into system gems
rake install:local            # Build and install nostr.gem into system gems without network access
rake qa                       # Test, lint and perform security and documentation audits
rake release[remote]          # Create a tag, build and push nostr.gem to rubygems.org
rake rubocop                  # Run RuboCop
rake rubocop:autocorrect      # Autocorrect RuboCop offenses (only when it's safe)
rake rubocop:autocorrect_all  # Autocorrect RuboCop offenses (safe and unsafe)
rake spec                     # Run RSpec code examples
rake verify_measurements      # Verify that yardstick coverage is at least 100%
rake yard                     # Generate YARD Documentation
rake yard:junk                # Check the junk in your YARD Documentation
rake yardstick_measure        # Measure docs in lib/**/*.rb with yardstick

Type checking

This gem leverages RBS, a language to describe the structure of Ruby programs. It is used to provide type checking and autocompletion in your editor. Run bundle exec typeprof FILENAME to generate an RBS definition for the given Ruby file. And validate all definitions using Steep with the command bundle exec steep check.

🐞 Issues & Bugs

If you find any issues or bugs, please report them here, I will be happy to have a look at them and fix them as soon as possible.

🤝 Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/wilsonsilva/nostr. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

📜 License

The gem is available as open source under the terms of the MIT License.

👔 Code of Conduct

Everyone interacting in the Nostr project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.