Add NIP-01 compliant client code
This commit is contained in:
11
lib/nostr.rb
11
lib/nostr.rb
@@ -1,6 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'nostr/version'
|
||||
require_relative 'nostr/keygen'
|
||||
require_relative 'nostr/client_message_type'
|
||||
require_relative 'nostr/filter'
|
||||
require_relative 'nostr/subscription'
|
||||
require_relative 'nostr/relay'
|
||||
require_relative 'nostr/key_pair'
|
||||
require_relative 'nostr/event_kind'
|
||||
require_relative 'nostr/event_fragment'
|
||||
require_relative 'nostr/event'
|
||||
require_relative 'nostr/client'
|
||||
require_relative 'nostr/user'
|
||||
|
||||
# Encapsulates all the gem's logic
|
||||
module Nostr
|
||||
|
||||
176
lib/nostr/client.rb
Normal file
176
lib/nostr/client.rb
Normal file
@@ -0,0 +1,176 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'event_emitter'
|
||||
require 'faye/websocket'
|
||||
|
||||
module Nostr
|
||||
# Clients can talk with relays and can subscribe to any set of events using a subscription filters.
|
||||
# The filter represents all the set of nostr events that a client is interested in.
|
||||
#
|
||||
# There is no sign-up or account creation for a client. Every time a client connects to a relay, it submits its
|
||||
# subscription filters and the relay streams the "interested events" to the client as long as they are connected.
|
||||
#
|
||||
class Client
|
||||
include EventEmitter
|
||||
|
||||
# Instantiates a new Client
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Instantiating a client that logs all the events it sends and receives
|
||||
# client = Nostr::Client.new(debug: true)
|
||||
#
|
||||
def initialize
|
||||
@subscriptions = {}
|
||||
|
||||
initialize_channels
|
||||
end
|
||||
|
||||
# Connects to the Relay's websocket endpoint
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Connecting to a relay
|
||||
# relay = Nostr::Relay.new(url: 'wss://relay.damus.io', name: 'Damus')
|
||||
# client.connect(relay)
|
||||
#
|
||||
# @param [Relay] relay The relay to connect to
|
||||
#
|
||||
# @return [void]
|
||||
#
|
||||
def connect(relay)
|
||||
execute_within_an_em_thread do
|
||||
client = Faye::WebSocket::Client.new(relay.url, [], { tls: { verify_peer: false } })
|
||||
parent_to_child_channel.subscribe { |msg| client.send(msg) }
|
||||
|
||||
client.on :open do
|
||||
child_to_parent_channel.push(type: :open)
|
||||
end
|
||||
|
||||
client.on :message do |event|
|
||||
child_to_parent_channel.push(type: :message, data: event.data)
|
||||
end
|
||||
|
||||
client.on :error do |event|
|
||||
child_to_parent_channel.push(type: :error, message: event.message)
|
||||
end
|
||||
|
||||
client.on :close do |event|
|
||||
child_to_parent_channel.push(type: :close, code: event.code, reason: event.reason)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Subscribes to a set of events using a filter
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Creating a subscription with no id and no filters
|
||||
# subscription = client.subscribe
|
||||
#
|
||||
# @example Creating a subscription with an ID
|
||||
# subscription = client.subscribe(subscription_id: 'my-subscription')
|
||||
#
|
||||
# @example Subscribing to all events created after a certain time
|
||||
# subscription = client.subscribe(filter: Nostr::Filter.new(since: 1230981305))
|
||||
#
|
||||
# @param subscription_id [String] The subscription id. A random string.
|
||||
# @param filter [Filter] A set of attributes that represent the events that the client is interested in.
|
||||
#
|
||||
# @return [Subscription] The subscription object
|
||||
#
|
||||
def subscribe(subscription_id: SecureRandom.hex, filter: Filter.new)
|
||||
subscriptions[subscription_id] = Subscription.new(id: subscription_id, filter:)
|
||||
parent_to_child_channel.push([ClientMessageType::REQ, subscription_id, filter.to_h].to_json)
|
||||
subscriptions[subscription_id]
|
||||
end
|
||||
|
||||
# Stops a previous subscription
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Stopping a subscription
|
||||
# client.unsubscribe(subscription.id)
|
||||
#
|
||||
# @example Stopping a subscription
|
||||
# client.unsubscribe('my-subscription')
|
||||
#
|
||||
# @param subscription_id [String] ID of a previously created subscription.
|
||||
#
|
||||
# @return [void]
|
||||
#
|
||||
def unsubscribe(subscription_id)
|
||||
subscriptions.delete(subscription_id)
|
||||
parent_to_child_channel.push([ClientMessageType::CLOSE, subscription_id].to_json)
|
||||
end
|
||||
|
||||
# Sends an event to a Relay
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Sending an event to a relay
|
||||
# client.publish(event)
|
||||
#
|
||||
# @param event [Event] The event to be sent to a Relay
|
||||
#
|
||||
# @return [void]
|
||||
#
|
||||
def publish(event)
|
||||
parent_to_child_channel.push([ClientMessageType::EVENT, event.to_h].to_json)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# The subscriptions that the client has created
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# @return [Hash{String=>Subscription}>]
|
||||
#
|
||||
attr_reader :subscriptions
|
||||
|
||||
# The channel that the parent thread uses to send messages to the child thread
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# @return [EventMachine::Channel]
|
||||
#
|
||||
attr_reader :parent_to_child_channel
|
||||
|
||||
# The channel that the child thread uses to send messages to the parent thread
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# @return [EventMachine::Channel]
|
||||
#
|
||||
attr_reader :child_to_parent_channel
|
||||
|
||||
# Executes a block of code within the EventMachine thread
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# @return [Thread]
|
||||
#
|
||||
def execute_within_an_em_thread(&block)
|
||||
Thread.new { EventMachine.run(block) }
|
||||
end
|
||||
|
||||
# Creates the communication channels between threads
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# @return [void]
|
||||
#
|
||||
def initialize_channels
|
||||
@parent_to_child_channel = EventMachine::Channel.new
|
||||
@child_to_parent_channel = EventMachine::Channel.new
|
||||
|
||||
child_to_parent_channel.subscribe do |msg|
|
||||
emit :connect if msg[:type] == :open
|
||||
emit :message, msg[:data] if msg[:type] == :message
|
||||
emit :error, msg[:message] if msg[:type] == :error
|
||||
emit :close, msg[:code], msg[:reason] if msg[:type] == :close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
15
lib/nostr/client_message_type.rb
Normal file
15
lib/nostr/client_message_type.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# Clients can send 3 types of messages, which must be JSON arrays
|
||||
module ClientMessageType
|
||||
# @return [String] Used to publish events
|
||||
EVENT = 'EVENT'
|
||||
|
||||
# @return [String] Used to request events and subscribe to new updates
|
||||
REQ = 'REQ'
|
||||
|
||||
# @return [String] Used to stop previous subscriptions
|
||||
CLOSE = 'CLOSE'
|
||||
end
|
||||
end
|
||||
93
lib/nostr/event.rb
Normal file
93
lib/nostr/event.rb
Normal file
@@ -0,0 +1,93 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# The only object type that exists in Nostr is an event. Events are immutable.
|
||||
class Event < EventFragment
|
||||
# 32-bytes sha256 of the the serialized event data.
|
||||
# To obtain the event.id, we sha256 the serialized event. The serialization is done over the UTF-8 JSON-serialized
|
||||
# string (with no white space or line breaks)
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Getting the event id
|
||||
# event.id # => 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :id
|
||||
|
||||
# 64-bytes signature of the sha256 hash of the serialized event data, which is
|
||||
# the same as the "id" field
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Getting the event signature
|
||||
# event.sig # => ''
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :sig
|
||||
|
||||
# Instantiates a new Event
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Instantiating a new event
|
||||
# Nostr::Event.new(
|
||||
# id: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
|
||||
# pubkey: '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e',
|
||||
# created_at: 1230981305,
|
||||
# kind: 1,
|
||||
# tags: [],
|
||||
# content: 'Your feedback is appreciated, now pay $8',
|
||||
# sig: '123ac2923b792ce730b3da34f16155470ab13c8f97f9c53eaeb334f1fb3a5dc9a7f643
|
||||
# 937c6d6e9855477638f5655c5d89c9aa5501ea9b578a66aced4f1cd7b3'
|
||||
# )
|
||||
#
|
||||
#
|
||||
# @param id [String] 32-bytes sha256 of the the serialized event data.
|
||||
# @param sig [String] 64-bytes signature of the sha256 hash of the serialized event data, which is
|
||||
# the same as the "id" field
|
||||
#
|
||||
def initialize(id:, sig:, **kwargs)
|
||||
super(**kwargs)
|
||||
|
||||
@id = id
|
||||
@sig = sig
|
||||
end
|
||||
|
||||
# Converts the event to a hash
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Converting the event to a hash
|
||||
# event.to_h
|
||||
#
|
||||
# @return [Hash] The event as a hash.
|
||||
#
|
||||
def to_h
|
||||
{
|
||||
id:,
|
||||
pubkey:,
|
||||
created_at:,
|
||||
kind:,
|
||||
tags:,
|
||||
content:,
|
||||
sig:
|
||||
}
|
||||
end
|
||||
|
||||
# Compares two events. Returns true if all attributes are equal and false otherwise
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# event1 == event2 # => true
|
||||
#
|
||||
# @return [Boolean] True if all attributes are equal and false otherwise
|
||||
#
|
||||
def ==(other)
|
||||
to_h == other.to_h
|
||||
end
|
||||
end
|
||||
end
|
||||
111
lib/nostr/event_fragment.rb
Normal file
111
lib/nostr/event_fragment.rb
Normal file
@@ -0,0 +1,111 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# Part of an +Event+. A complete +Event+ must have an +id+ and a +sig+.
|
||||
class EventFragment
|
||||
# 32-bytes hex-encoded public key of the event creator
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# event.pubkey # => '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :pubkey
|
||||
|
||||
# Date of the creation of the vent. A UNIX timestamp, in seconds
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# event.created_at # => 1230981305
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
attr_reader :created_at
|
||||
|
||||
# The kind of the event. An integer from 0 to 2
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# event.kind # => 1
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
attr_reader :kind
|
||||
|
||||
# An array of tags. Each tag is an array of strings
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Tags referencing an event
|
||||
# event.tags #=> [["e", "event_id", "relay URL"]]
|
||||
#
|
||||
# @example Tags referencing a key
|
||||
# event.tags #=> [["p", "event_id", "relay URL"]]
|
||||
#
|
||||
# @return [Array<Array>]
|
||||
#
|
||||
attr_reader :tags
|
||||
|
||||
# An arbitrary string
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# event.content # => 'Your feedback is appreciated, now pay $8'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :content
|
||||
|
||||
# Instantiates a new EventFragment
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# Nostr::EventFragment.new(
|
||||
# pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
|
||||
# created_at: 1230981305,
|
||||
# kind: 1,
|
||||
# tags: [['e', '189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408']],
|
||||
# content: 'Your feedback is appreciated, now pay $8'
|
||||
# )
|
||||
#
|
||||
# @param pubkey [String] 32-bytes hex-encoded public key of the event creator.
|
||||
# @param created_at [Integer] Date of the creation of the vent. A UNIX timestamp, in seconds.
|
||||
# @param kind [Integer] The kind of the event. An integer from 0 to 2.
|
||||
# @param tags [Array<Array>] An array of tags. Each tag is an array of strings.
|
||||
# @param content [String] Arbitrary string.
|
||||
#
|
||||
def initialize(pubkey:, kind:, content:, created_at: Time.now.to_i, tags: [])
|
||||
@pubkey = pubkey
|
||||
@created_at = created_at
|
||||
@kind = kind
|
||||
@tags = tags
|
||||
@content = content
|
||||
end
|
||||
|
||||
# Serializes the event fragment, to obtain a SHA256 hash of it
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Converting the event to a hash
|
||||
# event_fragment.serialize
|
||||
#
|
||||
# @return [Array] The event fragment as an array.
|
||||
#
|
||||
def serialize
|
||||
[
|
||||
0,
|
||||
pubkey,
|
||||
created_at,
|
||||
kind,
|
||||
tags,
|
||||
content
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
28
lib/nostr/event_kind.rb
Normal file
28
lib/nostr/event_kind.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# Defines the event kinds that can be emitted by clients.
|
||||
module EventKind
|
||||
# The content is set to a stringified JSON object +{name: <username>, about: <string>,
|
||||
# picture: <url, string>}+ describing the user who created the event. A relay may delete past set_metadata
|
||||
# events once it gets a new one for the same pubkey.
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
SET_METADATA = 0
|
||||
|
||||
# The content is set to the text content of a note (anything the user wants to say).
|
||||
# Non-plaintext notes should instead use kind 1000-10000 as described in NIP-16.
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
TEXT_NOTE = 1
|
||||
|
||||
# The content is set to the URL (e.g., wss://somerelay.com) of a relay the event creator wants to
|
||||
# recommend to its followers.
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
RECOMMEND_SERVER = 2
|
||||
end
|
||||
end
|
||||
172
lib/nostr/filter.rb
Normal file
172
lib/nostr/filter.rb
Normal file
@@ -0,0 +1,172 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# A filter determines what events will be sent in a subscription.
|
||||
class Filter
|
||||
# A list of event ids or prefixes
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.ids # => ['c24881c305c5cfb7c1168be7e9b0e150', '35deb2612efdb9e13e8b0ca4fc162341']
|
||||
#
|
||||
# @return [Array<String>, nil]
|
||||
#
|
||||
attr_reader :ids
|
||||
|
||||
# A list of pubkeys or prefixes, the pubkey of an event must be one of these
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.authors # => ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
|
||||
#
|
||||
# @return [Array<String>, nil]
|
||||
#
|
||||
attr_reader :authors
|
||||
|
||||
# A list of a kind numbers
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.kinds # => [0, 1, 2]
|
||||
#
|
||||
# @return [Array<Integer>, nil]
|
||||
#
|
||||
attr_reader :kinds
|
||||
|
||||
# A list of event ids that are referenced in an "e" tag
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.e # => ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2']
|
||||
#
|
||||
# @return [Array<String>, nil]
|
||||
#
|
||||
attr_reader :e
|
||||
|
||||
# A list of pubkeys that are referenced in a "p" tag
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.p # => ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
|
||||
#
|
||||
# @return [Array<String>, nil]
|
||||
#
|
||||
attr_reader :p
|
||||
|
||||
# A timestamp, events must be newer than this to pass
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.since # => 1230981305
|
||||
#
|
||||
# @return [Integer, nil]
|
||||
#
|
||||
attr_reader :since
|
||||
|
||||
# A timestamp, events must be older than this to pass
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.until # => 1292190341
|
||||
#
|
||||
# @return [Integer, nil]
|
||||
#
|
||||
attr_reader :until
|
||||
|
||||
# Maximum number of events to be returned in the initial query
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.limit # => 420
|
||||
#
|
||||
# @return [Integer, nil]
|
||||
#
|
||||
attr_reader :limit
|
||||
|
||||
# Instantiates a new Filter
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# Nostr::Filter.new(
|
||||
# ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
|
||||
# authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
|
||||
# kinds: [0, 1, 2],
|
||||
# since: 1230981305,
|
||||
# until: 1292190341,
|
||||
# e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
|
||||
# p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
|
||||
# )
|
||||
#
|
||||
# @param kwargs [Hash]
|
||||
# @option kwargs [Array<String>, nil] ids A list of event ids or prefixes
|
||||
# @option kwargs [Array<String>, nil] authors A list of pubkeys or prefixes, the pubkey of an event must be one
|
||||
# of these
|
||||
# @option kwargs [Array<Integer>, nil] kinds A list of a kind numbers
|
||||
# @option kwargs [Array<String>, nil] e A list of event ids that are referenced in an "e" tag
|
||||
# @option kwargs [Array<String, nil>] p A list of pubkeys that are referenced in a "p" tag
|
||||
# @option kwargs [Integer, nil] since A timestamp, events must be newer than this to pass
|
||||
# @option kwargs [Integer, nil] until A timestamp, events must be older than this to pass
|
||||
# @option kwargs [Integer, nil] limit Maximum number of events to be returned in the initial query
|
||||
#
|
||||
def initialize(**kwargs)
|
||||
@ids = kwargs[:ids]
|
||||
@authors = kwargs[:authors]
|
||||
@kinds = kwargs[:kinds]
|
||||
@e = kwargs[:e]
|
||||
@p = kwargs[:p]
|
||||
@since = kwargs[:since]
|
||||
@until = kwargs[:until]
|
||||
@limit = kwargs[:limit]
|
||||
end
|
||||
|
||||
# Converts the filter to a hash, removing all empty attributes
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter.to_h # => {:ids=>["c24881c305c5cfb7c1168be7e9b0e150"],
|
||||
# :authors=>["000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7"],
|
||||
# :kinds=>[0, 1, 2],
|
||||
# :"#e"=>["7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2"],
|
||||
# :"#p"=>["000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7"],
|
||||
# :since=>1230981305,
|
||||
# :until=>1292190341}
|
||||
#
|
||||
# @return [Hash] The filter as a hash.
|
||||
#
|
||||
def to_h
|
||||
{
|
||||
ids:,
|
||||
authors:,
|
||||
kinds:,
|
||||
'#e': e,
|
||||
'#p': p,
|
||||
since:,
|
||||
until: self.until,
|
||||
limit:
|
||||
}.compact
|
||||
end
|
||||
|
||||
# Compares two filters. Returns true if all attributes are equal and false otherwise
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# filter == filter # => true
|
||||
#
|
||||
# @return [Boolean] True if all attributes are equal and false otherwise
|
||||
#
|
||||
def ==(other)
|
||||
to_h == other.to_h
|
||||
end
|
||||
end
|
||||
end
|
||||
46
lib/nostr/key_pair.rb
Normal file
46
lib/nostr/key_pair.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# A pair of public and private keys
|
||||
class KeyPair
|
||||
# 32-bytes hex-encoded private key
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# keypair.private_key # => '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :private_key
|
||||
|
||||
# 32-bytes hex-encoded public key
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# keypair.public_key # => '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :public_key
|
||||
|
||||
# Instantiates a key pair
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# keypair = Nostr::KeyPair.new(
|
||||
# private_key: '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900',
|
||||
# public_key: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
|
||||
# )
|
||||
#
|
||||
# @param private_key [String] 32-bytes hex-encoded private key.
|
||||
# @param public_key [String] 32-bytes hex-encoded public key.
|
||||
#
|
||||
def initialize(private_key:, public_key:)
|
||||
@private_key = private_key
|
||||
@public_key = public_key
|
||||
end
|
||||
end
|
||||
end
|
||||
78
lib/nostr/keygen.rb
Normal file
78
lib/nostr/keygen.rb
Normal file
@@ -0,0 +1,78 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'ecdsa'
|
||||
require 'securerandom'
|
||||
|
||||
module Nostr
|
||||
# Generates private keys, public keys and key pairs.
|
||||
class Keygen
|
||||
# Instantiates a new keygen
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# keygen = Nostr::Keygen.new
|
||||
#
|
||||
def initialize
|
||||
@group = ECDSA::Group::Secp256k1
|
||||
end
|
||||
|
||||
# Generates a pair of private and public keys
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# keypair = keygen.generate_keypair
|
||||
# keypair # #<Nostr::KeyPair:0x0000000107bd3550
|
||||
# @private_key="893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900",
|
||||
# @public_key="2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558">
|
||||
#
|
||||
# @return [KeyPair] An object containing a private key and a public key.
|
||||
#
|
||||
def generate_key_pair
|
||||
private_key = generate_private_key
|
||||
public_key = extract_public_key(private_key)
|
||||
|
||||
KeyPair.new(private_key:, public_key:)
|
||||
end
|
||||
|
||||
# Generates a private key
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# private_key = keygen.generate_private_key
|
||||
# private_key # => '893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900'
|
||||
#
|
||||
# @return [String] A 32-bytes hex-encoded private key.
|
||||
#
|
||||
def generate_private_key
|
||||
(SecureRandom.random_number(group.order - 1) + 1).to_s(16)
|
||||
end
|
||||
|
||||
# Extracts a public key from a private key
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# private_key = keygen.generate_private_key
|
||||
# public_key = keygen.extract_public_key(private_key)
|
||||
# public_key # => '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558'
|
||||
#
|
||||
# @return [String] A 32-bytes hex-encoded public key.
|
||||
#
|
||||
def extract_public_key(private_key)
|
||||
group.generator.multiply_by_scalar(private_key.to_i(16)).x.to_s(16)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# The elliptic curve group. Used to generate public and private keys
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# @return [ECDSA::Group]
|
||||
#
|
||||
attr_reader :group
|
||||
end
|
||||
end
|
||||
43
lib/nostr/relay.rb
Normal file
43
lib/nostr/relay.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# Relays expose a websocket endpoint to which clients can connect.
|
||||
class Relay
|
||||
# The websocket URL of the relay
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# relay.url # => 'wss://relay.damus.io'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :url
|
||||
|
||||
# The name of the relay
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# relay.name # => 'Damus'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :name
|
||||
|
||||
# Instantiates a new Relay
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# relay = Nostr::Relay.new(url: 'wss://relay.damus.io', name: 'Damus')
|
||||
#
|
||||
# @return [String] The websocket URL of the relay
|
||||
# @return [String] The name of the relay
|
||||
#
|
||||
def initialize(url:, name:)
|
||||
@url = url
|
||||
@name = name
|
||||
end
|
||||
end
|
||||
end
|
||||
65
lib/nostr/subscription.rb
Normal file
65
lib/nostr/subscription.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'securerandom'
|
||||
|
||||
module Nostr
|
||||
# A subscription the result of a request to receive events from a relay
|
||||
class Subscription
|
||||
# A random string that should be used to represent a subscription
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# subscription.id # => 'c24881c305c5cfb7c1168be7e9b0e150'
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
attr_reader :id
|
||||
|
||||
# An object that determines what events will be sent in the subscription
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# subscription.filter # => #<Nostr::Subscription:0x0000000110eea460 @filter=#<Nostr::Filter:0x0000000110c24de8>,
|
||||
# @id="0dd7f3fa06cd5f797438dd0b7477f3c7">
|
||||
#
|
||||
# @return [Filter]
|
||||
#
|
||||
attr_reader :filter
|
||||
|
||||
# Initializes a subscription
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Creating a subscription with no id and no filters
|
||||
# subscription = Nostr::Subscription.new
|
||||
#
|
||||
# @example Creating a subscription with an ID
|
||||
# subscription = Nostr::Subscription.new(id: 'c24881c305c5cfb7c1168be7e9b0e150')
|
||||
#
|
||||
# @example Subscribing to all events created after a certain time
|
||||
# subscription = Nostr::Subscription.new(filter: Nostr::Filter.new(since: 1230981305))
|
||||
#
|
||||
# @param id [String] A random string that should be used to represent a subscription
|
||||
# @param filter [Filter] An object that determines what events will be sent in that subscription
|
||||
#
|
||||
def initialize(filter:, id: SecureRandom.hex)
|
||||
@id = id
|
||||
@filter = filter
|
||||
end
|
||||
|
||||
# Compares two subscriptions. Returns true if all attributes are equal and false otherwise
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# subscription1 == subscription1 # => true
|
||||
#
|
||||
# @return [Boolean] True if all attributes are equal and false otherwise
|
||||
#
|
||||
def ==(other)
|
||||
id == other.id && filter == other.filter
|
||||
end
|
||||
end
|
||||
end
|
||||
92
lib/nostr/user.rb
Normal file
92
lib/nostr/user.rb
Normal file
@@ -0,0 +1,92 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'schnorr'
|
||||
require 'json'
|
||||
|
||||
module Nostr
|
||||
# Each user has a keypair. Signatures, public key, and encodings are done according to the
|
||||
# Schnorr signatures standard for the curve secp256k1.
|
||||
class User
|
||||
# A pair of private and public keys
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example
|
||||
# user.keypair # #<Nostr::KeyPair:0x0000000107bd3550
|
||||
# @private_key="893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900",
|
||||
# @public_key="2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558">
|
||||
#
|
||||
# @return [KeyPair]
|
||||
#
|
||||
attr_reader :keypair
|
||||
|
||||
# Instantiates a user
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Creating a user with no keypair
|
||||
# user = Nostr::User.new
|
||||
#
|
||||
# @example Creating a user with a keypair
|
||||
# user = Nostr::User.new(keypair: keypair)
|
||||
#
|
||||
# @param keypair [Keypair] A pair of private and public keys
|
||||
# @param keygen [Keygen] A private key and public key generator
|
||||
#
|
||||
def initialize(keypair: nil, keygen: Keygen.new)
|
||||
@keypair = keypair || keygen.generate_key_pair
|
||||
end
|
||||
|
||||
# Builds an Event
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Creating a note event
|
||||
# event = user.create_event(
|
||||
# kind: Nostr::EventKind::TEXT_NOTE,
|
||||
# content: 'Your feedback is appreciated, now pay $8'
|
||||
# )
|
||||
#
|
||||
# @param event_attributes [Hash]
|
||||
# @option event_attributes [String] :pubkey 32-bytes hex-encoded public key of the event creator.
|
||||
# @option event_attributes [Integer] :created_at Date of the creation of the vent. A UNIX timestamp, in seconds.
|
||||
# @option event_attributes [Integer] :kind The kind of the event. An integer from 0 to 2.
|
||||
# @option event_attributes [Array<Array>] :tags An array of tags. Each tag is an array of strings.
|
||||
# @option event_attributes [String] :content Arbitrary string.
|
||||
#
|
||||
# @return [Event]
|
||||
#
|
||||
def create_event(event_attributes)
|
||||
event_fragment = EventFragment.new(**event_attributes.merge(pubkey: keypair.public_key))
|
||||
event_sha256 = Digest::SHA256.hexdigest(JSON.dump(event_fragment.serialize))
|
||||
|
||||
signature = sign(event_sha256)
|
||||
|
||||
Event.new(
|
||||
id: event_sha256,
|
||||
pubkey: event_fragment.pubkey,
|
||||
created_at: event_fragment.created_at,
|
||||
kind: event_fragment.kind,
|
||||
tags: event_fragment.tags,
|
||||
content: event_fragment.content,
|
||||
sig: signature
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Signs an event with the user's private key
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# @param event_sha256 [String] The SHA256 hash of the event.
|
||||
#
|
||||
# @return [String] The signature of the event.
|
||||
#
|
||||
def sign(event_sha256)
|
||||
hex_private_key = Array(keypair.private_key).pack('H*')
|
||||
hex_message = Array(event_sha256).pack('H*')
|
||||
Schnorr.sign(hex_message, hex_private_key).encode.unpack1('H*')
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Nostr
|
||||
# The version of the gem
|
||||
VERSION = '0.1.0'
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user