Merge pull request 'Add support for Lightning Zaps' (#190) from feature/170-nostr_zaps into master
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #190
This commit is contained in:
commit
d6d09b57b8
@ -18,6 +18,9 @@ LNDHUB_API_URL='http://localhost:3026'
|
||||
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
|
||||
LNDHUB_PUBLIC_KEY='024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946'
|
||||
|
||||
NOSTR_PRIVATE_KEY='7c3ef7e448505f0615137af38569d01807d3b05b5005d5ecf8aaafcd40323cea'
|
||||
NOSTR_PUBLIC_KEY='bdd76ce2934b2f591f9fad2ebe9da18f20d2921de527494ba00eeaa0a0efadcf'
|
||||
|
||||
RS_STORAGE_URL='https://storage.kosmos.org'
|
||||
RS_REDIS_URL='redis://localhost:6379/1'
|
||||
|
||||
|
@ -1,13 +1,15 @@
|
||||
class LnurlpayController < ApplicationController
|
||||
before_action :check_service_available
|
||||
before_action :find_user
|
||||
before_action :set_cors_access_control_headers, only: [:invoice]
|
||||
|
||||
MIN_SATS = 10
|
||||
MAX_SATS = 1_000_000
|
||||
MAX_COMMENT_CHARS = 100
|
||||
|
||||
# GET /.well-known/lnurlp/:username
|
||||
def index
|
||||
render json: {
|
||||
res = {
|
||||
status: "OK",
|
||||
callback: "https://#{Setting.accounts_domain}/lnurlpay/#{@user.cn}/invoice",
|
||||
tag: "payRequest",
|
||||
@ -16,8 +18,16 @@ class LnurlpayController < ApplicationController
|
||||
metadata: metadata(@user.address),
|
||||
commentAllowed: MAX_COMMENT_CHARS
|
||||
}
|
||||
|
||||
if Setting.nostr_enabled?
|
||||
res[:allowsNostr] = true
|
||||
res[:nostrPubkey] = Setting.nostr_public_key
|
||||
end
|
||||
|
||||
render json: res
|
||||
end
|
||||
|
||||
# GET /.well-known/keysend/:username
|
||||
def keysend
|
||||
http_status :not_found and return unless Setting.lndhub_keysend_enabled?
|
||||
|
||||
@ -32,8 +42,9 @@ class LnurlpayController < ApplicationController
|
||||
}
|
||||
end
|
||||
|
||||
# GET /lnurlpay/:username/invoice
|
||||
def invoice
|
||||
amount = params[:amount].to_i / 1000 # msats
|
||||
amount = params[:amount].to_i / 1000 # msats to sats
|
||||
comment = params[:comment] || ""
|
||||
address = @user.address
|
||||
|
||||
@ -42,40 +53,32 @@ class LnurlpayController < ApplicationController
|
||||
return
|
||||
end
|
||||
|
||||
if !valid_comment?(comment)
|
||||
render json: { status: "ERROR", reason: "Comment too long" }
|
||||
return
|
||||
if params[:nostr].present? && Setting.nostr_enabled?
|
||||
handle_zap_request amount, params[:nostr], params[:lnurl]
|
||||
else
|
||||
handle_pay_request address, amount, comment
|
||||
end
|
||||
|
||||
memo = "To #{address}"
|
||||
memo = "#{memo}: \"#{comment}\"" if comment.present?
|
||||
|
||||
payment_request = @user.ln_create_invoice({
|
||||
amount: amount, # we create invoices in sats
|
||||
memo: memo,
|
||||
description_hash: Digest::SHA2.hexdigest(metadata(address)),
|
||||
})
|
||||
|
||||
render json: {
|
||||
status: "OK",
|
||||
successAction: {
|
||||
tag: "message",
|
||||
message: "Sats received. Thank you!"
|
||||
},
|
||||
routes: [],
|
||||
pr: payment_request
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_cors_access_control_headers
|
||||
headers['Access-Control-Allow-Origin'] = "*"
|
||||
headers['Access-Control-Allow-Headers'] = "*"
|
||||
headers['Access-Control-Allow-Methods'] = "GET"
|
||||
end
|
||||
|
||||
def check_service_available
|
||||
http_status :not_found unless Setting.lndhub_enabled?
|
||||
end
|
||||
|
||||
def find_user
|
||||
@user = User.where(cn: params[:username], ou: Setting.primary_domain).first
|
||||
http_status :not_found if @user.nil?
|
||||
end
|
||||
|
||||
def metadata(address)
|
||||
"[[\"text/identifier\", \"#{address}\"], [\"text/plain\", \"Send sats, receive thanks.\"]]"
|
||||
"[[\"text/identifier\",\"#{address}\"],[\"text/plain\",\"Sats for #{address}\"]]"
|
||||
end
|
||||
|
||||
def valid_amount?(amount_in_sats)
|
||||
@ -86,9 +89,73 @@ class LnurlpayController < ApplicationController
|
||||
comment.length <= MAX_COMMENT_CHARS
|
||||
end
|
||||
|
||||
private
|
||||
def handle_pay_request(address, amount, comment)
|
||||
if !valid_comment?(comment)
|
||||
render json: { status: "ERROR", reason: "Comment too long" }
|
||||
return
|
||||
end
|
||||
|
||||
def check_service_available
|
||||
http_status :not_found unless Setting.lndhub_enabled?
|
||||
desc = "To #{address}"
|
||||
desc = "#{desc}: \"#{comment}\"" if comment.present?
|
||||
|
||||
invoice = LndhubManager::CreateUserInvoice.call(
|
||||
user: @user, payload: {
|
||||
amount: amount, # sats
|
||||
description: desc,
|
||||
description_hash: Digest::SHA256.hexdigest(metadata(address)),
|
||||
}
|
||||
)
|
||||
|
||||
render json: {
|
||||
status: "OK",
|
||||
successAction: {
|
||||
tag: "message",
|
||||
message: "Sats received. Thank you!"
|
||||
},
|
||||
routes: [],
|
||||
pr: invoice["payment_request"]
|
||||
}
|
||||
end
|
||||
|
||||
def nostr_event_from_payload(nostr_param)
|
||||
event_obj = JSON.parse(nostr_param).transform_keys(&:to_sym)
|
||||
Nostr::Event.new(**event_obj)
|
||||
rescue => e
|
||||
return nil
|
||||
end
|
||||
|
||||
def valid_zap_request?(amount, event, lnurl)
|
||||
NostrManager::VerifyZapRequest.call(
|
||||
amount: amount, event: event, lnurl: lnurl
|
||||
)
|
||||
end
|
||||
|
||||
def handle_zap_request(amount, nostr_param, lnurl_param)
|
||||
event = nostr_event_from_payload(nostr_param)
|
||||
|
||||
unless event.present? && valid_zap_request?(amount*1000, event, lnurl_param)
|
||||
render json: { status: "ERROR", reason: "Invalid zap request" }
|
||||
return
|
||||
end
|
||||
|
||||
# TODO might want to use the existing invoice and zap record if there are
|
||||
# multiple calls with the same zap request
|
||||
|
||||
desc = "Zap for #{@user.address}"
|
||||
desc = "#{desc}: \"#{event.content}\"" if event.content.present?
|
||||
|
||||
invoice = LndhubManager::CreateUserInvoice.call(
|
||||
user: @user, payload: {
|
||||
amount: amount, # sats
|
||||
description: desc,
|
||||
description_hash: Digest::SHA256.hexdigest(event.to_json),
|
||||
}
|
||||
)
|
||||
|
||||
@user.zaps.create! request: event,
|
||||
payment_request: invoice["payment_request"],
|
||||
amount: amount
|
||||
|
||||
render json: { status: "OK", pr: invoice["payment_request"] }
|
||||
end
|
||||
end
|
||||
|
@ -2,45 +2,66 @@ class WebhooksController < ApplicationController
|
||||
skip_forgery_protection
|
||||
|
||||
before_action :authorize_request
|
||||
before_action :process_payload
|
||||
|
||||
def lndhub
|
||||
begin
|
||||
payload = JSON.parse(request.body.read, symbolize_names: true)
|
||||
head :no_content and return unless payload[:type] == "incoming"
|
||||
rescue
|
||||
head :unprocessable_entity and return
|
||||
@user = User.find_by!(ln_account: @payload[:user_login])
|
||||
|
||||
if zap = @user.zaps.find_by(payment_request: @payload[:payment_request])
|
||||
zap_receipt = NostrManager::CreateZapReceipt.call(
|
||||
zap: zap,
|
||||
paid_at: Time.parse(@payload[:settled_at]).to_i,
|
||||
preimage: @payload[:preimage]
|
||||
)
|
||||
zap.update! receipt: zap_receipt.to_h
|
||||
NostrManager::PublishZapReceipt.call(zap: zap)
|
||||
end
|
||||
|
||||
user = User.find_by!(ln_account: payload[:user_login])
|
||||
notify = user.preferences[:lightning_notify_sats_received]
|
||||
case notify
|
||||
when "xmpp"
|
||||
notify_xmpp(user.address, payload[:amount], payload[:memo])
|
||||
when "email"
|
||||
NotificationMailer.with(user: user, amount_sats: payload[:amount])
|
||||
.lightning_sats_received.deliver_later
|
||||
end
|
||||
send_notifications
|
||||
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# TODO refactor into mailer-like generic class/service
|
||||
def notify_xmpp(address, amt_sats, memo)
|
||||
payload = {
|
||||
type: "normal",
|
||||
from: Setting.xmpp_notifications_from_address,
|
||||
to: address,
|
||||
subject: "Sats received!",
|
||||
body: "#{helpers.number_with_delimiter amt_sats} sats received in your Lightning wallet:\n> #{memo}"
|
||||
}
|
||||
XmppSendMessageJob.perform_later(payload)
|
||||
end
|
||||
|
||||
def authorize_request
|
||||
if !ENV['WEBHOOKS_ALLOWED_IPS'].split(',').include?(request.remote_ip)
|
||||
head :forbidden and return
|
||||
end
|
||||
end
|
||||
|
||||
def process_payload
|
||||
@payload = JSON.parse(request.body.read, symbolize_names: true)
|
||||
unless @payload[:type] == "incoming" &&
|
||||
@payload[:state] == "settled"
|
||||
head :no_content and return
|
||||
end
|
||||
rescue
|
||||
head :unprocessable_entity and return
|
||||
end
|
||||
|
||||
def send_notifications
|
||||
case @user.preferences[:lightning_notify_sats_received]
|
||||
when "xmpp"
|
||||
notify_xmpp
|
||||
when "email"
|
||||
notify_email
|
||||
end
|
||||
end
|
||||
|
||||
# TODO refactor into mailer-like generic class/service
|
||||
def notify_xmpp
|
||||
XmppSendMessageJob.perform_later({
|
||||
type: "normal",
|
||||
from: Setting.xmpp_notifications_from_address,
|
||||
to: @user.address,
|
||||
subject: "Sats received!",
|
||||
body: "#{helpers.number_with_delimiter @payload[:amount]} sats received in your Lightning wallet:\n> #{@payload[:memo]}"
|
||||
})
|
||||
end
|
||||
|
||||
def notify_email
|
||||
NotificationMailer.with(user: @user, amount_sats: @payload[:amount])
|
||||
.lightning_sats_received.deliver_later
|
||||
end
|
||||
end
|
||||
|
7
app/jobs/nostr_publish_event_job.rb
Normal file
7
app/jobs/nostr_publish_event_job.rb
Normal file
@ -0,0 +1,7 @@
|
||||
class NostrPublishEventJob < ApplicationJob
|
||||
queue_as :nostr
|
||||
|
||||
def perform(event:, relay_url:)
|
||||
NostrManager::PublishEvent.call(event: event, relay_url: relay_url)
|
||||
end
|
||||
end
|
@ -160,7 +160,17 @@ class Setting < RailsSettings::Base
|
||||
# Nostr
|
||||
#
|
||||
|
||||
field :nostr_enabled, type: :boolean, default: false
|
||||
field :nostr_enabled, type: :boolean,
|
||||
default: ENV["NOSTR_PRIVATE_KEY"].present?
|
||||
|
||||
field :nostr_private_key, type: :string,
|
||||
default: ENV["NOSTR_PRIVATE_KEY"].presence
|
||||
|
||||
field :nostr_public_key, type: :string,
|
||||
default: ENV["NOSTR_PUBLIC_KEY"].presence
|
||||
|
||||
field :nostr_zaps_relay_limit, type: :integer,
|
||||
default: 12
|
||||
|
||||
#
|
||||
# OpenCollective
|
||||
|
@ -17,16 +17,15 @@ class User < ApplicationRecord
|
||||
has_one :invitation, inverse_of: :invitee, foreign_key: 'invited_user_id'
|
||||
has_one :inviter, through: :invitation, source: :user
|
||||
has_many :invitees, through: :invitations
|
||||
|
||||
has_many :donations, dependent: :nullify
|
||||
has_many :remote_storage_authorizations
|
||||
has_many :zaps
|
||||
|
||||
has_one :lndhub_user, class_name: "LndhubUser", inverse_of: "user",
|
||||
primary_key: "ln_account", foreign_key: "login"
|
||||
|
||||
has_many :accounts, through: :lndhub_user
|
||||
|
||||
has_many :remote_storage_authorizations
|
||||
|
||||
#
|
||||
# Validations
|
||||
#
|
||||
@ -143,12 +142,6 @@ class User < ApplicationRecord
|
||||
enable_service Setting.default_services
|
||||
end
|
||||
|
||||
def ln_create_invoice(payload)
|
||||
lndhub = Lndhub.new
|
||||
lndhub.authenticate self
|
||||
lndhub.addinvoice payload
|
||||
end
|
||||
|
||||
def dn
|
||||
return @dn if defined?(@dn)
|
||||
@dn = Devise::LDAP::Adapter.get_dn(self.cn)
|
||||
|
17
app/models/zap.rb
Normal file
17
app/models/zap.rb
Normal file
@ -0,0 +1,17 @@
|
||||
class Zap < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
def request_event
|
||||
nostr_event_from_hash(request)
|
||||
end
|
||||
|
||||
def receipt_event
|
||||
nostr_event_from_hash(receipt)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def nostr_event_from_hash(hash)
|
||||
Nostr::Event.new(**hash.symbolize_keys)
|
||||
end
|
||||
end
|
13
app/services/lndhub_manager/create_user_invoice.rb
Normal file
13
app/services/lndhub_manager/create_user_invoice.rb
Normal file
@ -0,0 +1,13 @@
|
||||
module LndhubManager
|
||||
class CreateUserInvoice < LndhubV2
|
||||
def initialize(user:, payload:)
|
||||
@user = user
|
||||
@payload = payload
|
||||
end
|
||||
|
||||
def call
|
||||
authenticate @user
|
||||
create_invoice @payload
|
||||
end
|
||||
end
|
||||
end
|
25
app/services/nostr_manager/create_zap_receipt.rb
Normal file
25
app/services/nostr_manager/create_zap_receipt.rb
Normal file
@ -0,0 +1,25 @@
|
||||
module NostrManager
|
||||
class CreateZapReceipt < NostrManagerService
|
||||
def initialize(zap:, paid_at:, preimage:)
|
||||
@zap, @paid_at, @preimage = zap, paid_at, preimage
|
||||
end
|
||||
|
||||
def call
|
||||
request_tags = parse_tags(@zap.request_event.tags)
|
||||
|
||||
site_user.create_event(
|
||||
kind: 9735,
|
||||
created_at: @paid_at,
|
||||
content: "",
|
||||
tags: [
|
||||
["p", request_tags[:p].first],
|
||||
["e", request_tags[:e]&.first],
|
||||
["a", request_tags[:a]&.first],
|
||||
["bolt11", @zap.payment_request],
|
||||
["preimage", @preimage],
|
||||
["description", @zap.request_event.to_json]
|
||||
].reject { |t| t[1].nil? }
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
50
app/services/nostr_manager/publish_event.rb
Normal file
50
app/services/nostr_manager/publish_event.rb
Normal file
@ -0,0 +1,50 @@
|
||||
module NostrManager
|
||||
class PublishEvent < NostrManagerService
|
||||
def initialize(event:, relay_url:)
|
||||
relay_name = URI.parse(relay_url).host
|
||||
@relay = Nostr::Relay.new(url: relay_url, name: relay_name)
|
||||
|
||||
if event.is_a?(Nostr::Event)
|
||||
@event = event
|
||||
else
|
||||
@event = Nostr::Event.new(**event.symbolize_keys)
|
||||
end
|
||||
|
||||
@client = Nostr::Client.new
|
||||
end
|
||||
|
||||
def call
|
||||
client, relay, event = @client, @relay, @event
|
||||
log_prefix = "[nostr][#{relay.name}]"
|
||||
|
||||
thread = Thread.new do
|
||||
client.on :connect do
|
||||
puts "#{log_prefix} Publishing #{event.id}..."
|
||||
client.publish event
|
||||
end
|
||||
|
||||
client.on :error do |e|
|
||||
puts "#{log_prefix} Error: #{e}"
|
||||
puts "#{log_prefix} Closing thread..."
|
||||
thread.exit
|
||||
end
|
||||
|
||||
client.on :message do |m|
|
||||
puts "#{log_prefix} Message: #{m}"
|
||||
msg = JSON.parse(m) rescue []
|
||||
if msg[0] == "OK" && msg[1] == event.id && msg[2]
|
||||
puts "#{log_prefix} Event published. Closing thread..."
|
||||
else
|
||||
puts "#{log_prefix} Unexpected message from relay. Closing thread..."
|
||||
end
|
||||
thread.exit
|
||||
end
|
||||
|
||||
puts "#{log_prefix} Connecting to #{relay.url}..."
|
||||
client.connect relay
|
||||
end
|
||||
|
||||
thread.join
|
||||
end
|
||||
end
|
||||
end
|
19
app/services/nostr_manager/publish_zap_receipt.rb
Normal file
19
app/services/nostr_manager/publish_zap_receipt.rb
Normal file
@ -0,0 +1,19 @@
|
||||
module NostrManager
|
||||
class PublishZapReceipt < NostrManagerService
|
||||
def initialize(zap:, delayed: true)
|
||||
@zap, @delayed = zap, delayed
|
||||
end
|
||||
|
||||
def call
|
||||
tags = parse_tags(@zap.request_event.tags)
|
||||
|
||||
tags[:relays].take(Setting.nostr_zaps_relay_limit).each do |relay_url|
|
||||
if @delayed
|
||||
NostrPublishEventJob.perform_later(event: @zap.receipt, relay_url: relay_url)
|
||||
else
|
||||
NostrManager::PublishEvent.call(event: @zap.receipt_event, relay_url: relay_url)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -7,8 +7,9 @@ module NostrManager
|
||||
end
|
||||
|
||||
def call
|
||||
site_given = @event.tags.find{|t| t[0] == "site"}[1]
|
||||
challenge_given = @event.tags.find{|t| t[0] == "challenge"}[1]
|
||||
tags = parse_tags(@event.tags)
|
||||
site_given = tags[:site].first
|
||||
challenge_given = tags[:challenge].first
|
||||
|
||||
site_given == @site_expected &&
|
||||
challenge_given == @challenge_expected
|
||||
|
51
app/services/nostr_manager/verify_zap_request.rb
Normal file
51
app/services/nostr_manager/verify_zap_request.rb
Normal file
@ -0,0 +1,51 @@
|
||||
module NostrManager
|
||||
class VerifyZapRequest < NostrManagerService
|
||||
def initialize(amount:, event:, lnurl: nil)
|
||||
@amount, @event, @lnurl = amount, event, lnurl
|
||||
end
|
||||
|
||||
# https://github.com/nostr-protocol/nips/blob/27fef638e2460139cc9078427a0aec0ce4470517/57.md#appendix-d-lnurl-server-zap-request-validation
|
||||
def call
|
||||
tags = parse_tags(@event.tags)
|
||||
|
||||
@event.verify_signature &&
|
||||
@event.kind == 9734 &&
|
||||
tags.present? &&
|
||||
valid_p_tag?(tags[:p]) &&
|
||||
valid_e_tag?(tags[:e]) &&
|
||||
valid_a_tag?(tags[:a]) &&
|
||||
valid_amount_tag?(tags[:amount]) &&
|
||||
valid_lnurl_tag?(tags[:lnurl])
|
||||
end
|
||||
|
||||
def valid_p_tag?(tag)
|
||||
return false unless tag.present? && tag.length == 1
|
||||
key = Nostr::PublicKey.new(tag.first) rescue nil
|
||||
key.present?
|
||||
end
|
||||
|
||||
def valid_e_tag?(tag)
|
||||
return true unless tag.present?
|
||||
# TODO validate format of event ID properly
|
||||
tag.length == 1 && tag.first.is_a?(String)
|
||||
end
|
||||
|
||||
def valid_a_tag?(tag)
|
||||
return true unless tag.present?
|
||||
# TODO validate format of event coordinate properly
|
||||
tag.length == 1 && tag.first.is_a?(String)
|
||||
end
|
||||
|
||||
def valid_amount_tag?(tag)
|
||||
return true unless tag.present?
|
||||
amount = tag.first
|
||||
amount.is_a?(String) && amount.to_i == @amount
|
||||
end
|
||||
|
||||
def valid_lnurl_tag?(tag)
|
||||
return true unless tag.present?
|
||||
# TODO validate lnurl matching recipient's lnurlp
|
||||
tag.first.is_a?(String)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,4 +1,22 @@
|
||||
require "nostr"
|
||||
|
||||
class NostrManagerService < ApplicationService
|
||||
def parse_tags(tags)
|
||||
out = {}
|
||||
tags.each do |tag|
|
||||
out[tag[0].to_sym] = tag[1, tag.length]
|
||||
end
|
||||
out
|
||||
end
|
||||
|
||||
def site_keypair
|
||||
Nostr::KeyPair.new(
|
||||
private_key: Nostr::PrivateKey.new(Setting.nostr_private_key),
|
||||
public_key: Nostr::PublicKey.new(Setting.nostr_public_key)
|
||||
)
|
||||
end
|
||||
|
||||
def site_user
|
||||
Nostr::User.new(keypair: site_keypair)
|
||||
end
|
||||
end
|
||||
|
@ -7,4 +7,27 @@
|
||||
title: "Enable Nostr integration (experimental)",
|
||||
description: "Allow adding nostr pubkeys and resolve user addresses via NIP-05"
|
||||
) %>
|
||||
<% if Setting.nostr_enabled? %>
|
||||
<%= render FormElements::FieldsetResettableSettingComponent.new(
|
||||
key: :nostr_private_key,
|
||||
type: :password,
|
||||
title: "Private key",
|
||||
description: "The private key of the accounts service, used when publishing events (e.g. zap receipts)"
|
||||
) %>
|
||||
<%= render FormElements::FieldsetResettableSettingComponent.new(
|
||||
key: :nostr_public_key,
|
||||
title: "Public key",
|
||||
description: "The corresponding public key of the accounts service"
|
||||
) %>
|
||||
</ul>
|
||||
</section>
|
||||
<section>
|
||||
<h3>Zaps</h3>
|
||||
<ul role="list">
|
||||
<%= render FormElements::FieldsetResettableSettingComponent.new(
|
||||
key: :nostr_zaps_relay_limit,
|
||||
title: "Relay limit",
|
||||
description: "The maximum number of relays to publish zap receipts to"
|
||||
) %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
@ -3,3 +3,4 @@
|
||||
- default
|
||||
- mailers
|
||||
- remotestorage
|
||||
- nostr
|
||||
|
13
db/migrate/20240422171653_create_zaps.rb
Normal file
13
db/migrate/20240422171653_create_zaps.rb
Normal file
@ -0,0 +1,13 @@
|
||||
class CreateZaps < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :zaps do |t|
|
||||
t.references :user, null: false, foreign_key: true
|
||||
t.json :request
|
||||
t.json :receipt, default: nil
|
||||
t.text :payment_request
|
||||
t.bigint :amount
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
14
db/schema.rb
14
db/schema.rb
@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_03_16_153558) do
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_04_22_171653) do
|
||||
create_table "active_storage_attachments", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@ -136,8 +136,20 @@ ActiveRecord::Schema[7.1].define(version: 2024_03_16_153558) do
|
||||
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
||||
end
|
||||
|
||||
create_table "zaps", force: :cascade do |t|
|
||||
t.integer "user_id", null: false
|
||||
t.json "request"
|
||||
t.json "receipt"
|
||||
t.text "payment_request"
|
||||
t.bigint "amount"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["user_id"], name: "index_zaps_on_user_id"
|
||||
end
|
||||
|
||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "remote_storage_authorizations", "app_catalog_web_apps", column: "web_app_id"
|
||||
add_foreign_key "remote_storage_authorizations", "users"
|
||||
add_foreign_key "zaps", "users"
|
||||
end
|
||||
|
@ -107,6 +107,17 @@ services:
|
||||
- minio
|
||||
- redis
|
||||
|
||||
nostr-relay:
|
||||
image: pluja/strfry:latest
|
||||
volumes:
|
||||
- ./docker/strfry/strfry.conf:/etc/strfry.conf
|
||||
- strfry-data:/app/strfry-db
|
||||
networks:
|
||||
- external_network
|
||||
- internal_network
|
||||
ports:
|
||||
- "4777:7777"
|
||||
|
||||
# phpldapadmin:
|
||||
# image: osixia/phpldapadmin:0.9.0
|
||||
# ports:
|
||||
@ -128,3 +139,5 @@ volumes:
|
||||
driver: local
|
||||
redis-data:
|
||||
driver: local
|
||||
strfry-data:
|
||||
driver: local
|
||||
|
138
docker/strfry/strfry.conf
Normal file
138
docker/strfry/strfry.conf
Normal file
@ -0,0 +1,138 @@
|
||||
##
|
||||
## Default strfry config
|
||||
##
|
||||
|
||||
# Directory that contains the strfry LMDB database (restart required)
|
||||
db = "./strfry-db/"
|
||||
|
||||
dbParams {
|
||||
# Maximum number of threads/processes that can simultaneously have LMDB transactions open (restart required)
|
||||
maxreaders = 256
|
||||
|
||||
# Size of mmap() to use when loading LMDB (default is 10TB, does *not* correspond to disk-space used) (restart required)
|
||||
mapsize = 10995116277760
|
||||
|
||||
# Disables read-ahead when accessing the LMDB mapping. Reduces IO activity when DB size is larger than RAM. (restart required)
|
||||
noReadAhead = false
|
||||
}
|
||||
|
||||
events {
|
||||
# Maximum size of normalised JSON, in bytes
|
||||
maxEventSize = 65536
|
||||
|
||||
# Events newer than this will be rejected
|
||||
rejectEventsNewerThanSeconds = 900
|
||||
|
||||
# Events older than this will be rejected
|
||||
rejectEventsOlderThanSeconds = 94608000
|
||||
|
||||
# Ephemeral events older than this will be rejected
|
||||
rejectEphemeralEventsOlderThanSeconds = 60
|
||||
|
||||
# Ephemeral events will be deleted from the DB when older than this
|
||||
ephemeralEventsLifetimeSeconds = 300
|
||||
|
||||
# Maximum number of tags allowed
|
||||
maxNumTags = 2000
|
||||
|
||||
# Maximum size for tag values, in bytes
|
||||
maxTagValSize = 1024
|
||||
}
|
||||
|
||||
relay {
|
||||
# Interface to listen on. Use 0.0.0.0 to listen on all interfaces (restart required)
|
||||
bind = "0.0.0.0"
|
||||
|
||||
# Port to open for the nostr websocket protocol (restart required)
|
||||
port = 7777
|
||||
|
||||
# Set OS-limit on maximum number of open files/sockets (if 0, don't attempt to set) (restart required)
|
||||
nofiles = 200000
|
||||
|
||||
# HTTP header that contains the client's real IP, before reverse proxying (ie x-real-ip) (MUST be all lower-case)
|
||||
realIpHeader = ""
|
||||
|
||||
info {
|
||||
# NIP-11: Name of this server. Short/descriptive (< 30 characters)
|
||||
name = "akkounts-nostr-relay"
|
||||
|
||||
# NIP-11: Detailed information about relay, free-form
|
||||
description = "Local strfry instance for akkounts development"
|
||||
|
||||
# NIP-11: Administrative nostr pubkey, for contact purposes
|
||||
pubkey = ""
|
||||
|
||||
# NIP-11: Alternative administrative contact (email, website, etc)
|
||||
contact = ""
|
||||
}
|
||||
|
||||
# Maximum accepted incoming websocket frame size (should be larger than max event) (restart required)
|
||||
maxWebsocketPayloadSize = 131072
|
||||
|
||||
# Websocket-level PING message frequency (should be less than any reverse proxy idle timeouts) (restart required)
|
||||
autoPingSeconds = 55
|
||||
|
||||
# If TCP keep-alive should be enabled (detect dropped connections to upstream reverse proxy)
|
||||
enableTcpKeepalive = false
|
||||
|
||||
# How much uninterrupted CPU time a REQ query should get during its DB scan
|
||||
queryTimesliceBudgetMicroseconds = 10000
|
||||
|
||||
# Maximum records that can be returned per filter
|
||||
maxFilterLimit = 500
|
||||
|
||||
# Maximum number of subscriptions (concurrent REQs) a connection can have open at any time
|
||||
maxSubsPerConnection = 20
|
||||
|
||||
writePolicy {
|
||||
# If non-empty, path to an executable script that implements the writePolicy plugin logic
|
||||
plugin = ""
|
||||
}
|
||||
|
||||
compression {
|
||||
# Use permessage-deflate compression if supported by client. Reduces bandwidth, but slight increase in CPU (restart required)
|
||||
enabled = true
|
||||
|
||||
# Maintain a sliding window buffer for each connection. Improves compression, but uses more memory (restart required)
|
||||
slidingWindow = true
|
||||
}
|
||||
|
||||
logging {
|
||||
# Dump all incoming messages
|
||||
dumpInAll = true
|
||||
|
||||
# Dump all incoming EVENT messages
|
||||
dumpInEvents = false
|
||||
|
||||
# Dump all incoming REQ/CLOSE messages
|
||||
dumpInReqs = false
|
||||
|
||||
# Log performance metrics for initial REQ database scans
|
||||
dbScanPerf = true
|
||||
|
||||
# Log reason for invalid event rejection? Can be disabled to silence excessive logging
|
||||
invalidEvents = true
|
||||
}
|
||||
|
||||
numThreads {
|
||||
# Ingester threads: route incoming requests, validate events/sigs (restart required)
|
||||
ingester = 3
|
||||
|
||||
# reqWorker threads: Handle initial DB scan for events (restart required)
|
||||
reqWorker = 3
|
||||
|
||||
# reqMonitor threads: Handle filtering of new events (restart required)
|
||||
reqMonitor = 3
|
||||
|
||||
# negentropy threads: Handle negentropy protocol messages (restart required)
|
||||
negentropy = 2
|
||||
}
|
||||
|
||||
negentropy {
|
||||
# Support negentropy protocol messages
|
||||
enabled = true
|
||||
|
||||
# Maximum records that sync will process before returning an error
|
||||
maxSyncEvents = 1000000
|
||||
}
|
||||
}
|
55
lib/nostr/event_kind.rb
Normal file
55
lib/nostr/event_kind.rb
Normal file
@ -0,0 +1,55 @@
|
||||
# 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
|
||||
|
||||
# A special event with kind 3, meaning "contact list" is defined as having a list of p tags, one for each of
|
||||
# the followed/known profiles one is following.
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
CONTACT_LIST = 3
|
||||
|
||||
# A special event with kind 4, meaning "encrypted direct message". An event of this kind has its +content+
|
||||
# equal to the base64-encoded, aes-256-cbc encrypted string of anything a user wants to write, encrypted using a
|
||||
# shared cipher generated by combining the recipient's public-key with the sender's private-key.
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
ENCRYPTED_DIRECT_MESSAGE = 4
|
||||
|
||||
# NIP-57 Zap request
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
ZAP_REQUEST = 9734
|
||||
|
||||
# NIP-57 Zap receipt
|
||||
#
|
||||
# @return [Integer]
|
||||
#
|
||||
ZAP_RECEIPT = 9735
|
||||
end
|
||||
end
|
@ -1,3 +1,6 @@
|
||||
require "digest"
|
||||
require "pp"
|
||||
|
||||
namespace :lndhub do
|
||||
desc "Generate wallets for all users"
|
||||
task :generate_wallets => :environment do |t, args|
|
||||
@ -22,6 +25,21 @@ namespace :lndhub do
|
||||
puts "--\nSum of user balances: #{sum} sats"
|
||||
end
|
||||
|
||||
desc "Create test invoice"
|
||||
task :create_test_invoice, [:username, :amount, :description] => :environment do |t, args|
|
||||
user = User.find_by cn: args[:username]
|
||||
desc = args[:description]
|
||||
hash = Digest::SHA256.hexdigest(desc)
|
||||
|
||||
invoice = LndhubManager::CreateUserInvoice.call(user: user, payload: {
|
||||
"amount": args[:amount].to_i,
|
||||
"description": desc,
|
||||
"description_hash": hash
|
||||
})
|
||||
|
||||
pp invoice
|
||||
end
|
||||
|
||||
desc "Migrate existing accounts to lndhub.go"
|
||||
task :migrate => :environment do |t, args|
|
||||
# user = User.find_by cn: "jimmy"
|
||||
|
20
spec/factories/zaps.rb
Normal file
20
spec/factories/zaps.rb
Normal file
@ -0,0 +1,20 @@
|
||||
FactoryBot.define do
|
||||
factory :zap do
|
||||
user { nil }
|
||||
request {
|
||||
Nostr::Event.new(
|
||||
id: "3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712487443,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org", "wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"]],
|
||||
content: "",
|
||||
sig: "e9e9bb2bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2"
|
||||
).to_h
|
||||
}
|
||||
payment_request { "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu" }
|
||||
receipt { nil }
|
||||
end
|
||||
end
|
19
spec/fixtures/lndhub/incoming-zap.json
vendored
Normal file
19
spec/fixtures/lndhub/incoming-zap.json
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"id": 58,
|
||||
"type": "incoming",
|
||||
"user_login": "123456abcdef",
|
||||
"amount": 21000,
|
||||
"fee": 0,
|
||||
"memo": "Zap for satoshi@kosmos.org",
|
||||
"description_hash": "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a",
|
||||
"payment_request": "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu",
|
||||
"destination_pubkey_hex": "024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946",
|
||||
"r_hash": "03aa6aeb2ed5a569f1a40925041f7d7163458e96b705e5eafe0f10188575dc08",
|
||||
"preimage": "3539663535656537343331663432653165396430623966633664656664646563",
|
||||
"keysend": false,
|
||||
"state": "settled",
|
||||
"created_at": "2023-01-11T09:22:57.546364Z",
|
||||
"expires_at": "2023-01-12T09:22:57.547209Z",
|
||||
"updated_at": "2023-01-11T09:22:58.046236131Z",
|
||||
"settled_at": "2023-01-11T09:22:58.046232174Z"
|
||||
}
|
22
spec/fixtures/nostr/zap_request_event.json
vendored
Normal file
22
spec/fixtures/nostr/zap_request_event.json
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"kind": 9734,
|
||||
"created_at": 1712066899,
|
||||
"content": "",
|
||||
"tags": [
|
||||
["p", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"],
|
||||
["amount", "21000"],
|
||||
[
|
||||
"relays",
|
||||
"wss://relay.primal.net",
|
||||
"wss://relay.snort.social",
|
||||
"wss://nostr.wine",
|
||||
"wss://relay.damus.io",
|
||||
"wss://eden.nostr.land",
|
||||
"wss://atlas.nostr.land",
|
||||
"wss://nostr.bitcoiner.social",
|
||||
"wss://relay.mostr.pub",
|
||||
"wss://relay.mostr.pub/",
|
||||
"wss://nostr-01.bolt.observer"
|
||||
]
|
||||
]
|
||||
}
|
12
spec/models/zap_spec.rb
Normal file
12
spec/models/zap_spec.rb
Normal file
@ -0,0 +1,12 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Zap, type: :model do
|
||||
describe "#request_event" do
|
||||
let(:user) { create :user, cn: 'satoshi', ou: 'kosmos.org', ln_account: 'abcdefg123456' }
|
||||
let(:zap) { create :zap, user: user }
|
||||
|
||||
it "returns the stored request as a Nostr::Event" do
|
||||
expect(zap.request_event).to be_a(Nostr::Event)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,9 +1,8 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "/lnurlpay", type: :request do
|
||||
|
||||
context "Non-existent user" do
|
||||
describe "GET /.well-known/lnurlpay/:username" do
|
||||
describe "GET /.well-known/lnurlp/:username" do
|
||||
it "returns a 404" do
|
||||
get lightning_address_path(username: "csw")
|
||||
expect(response).to have_http_status(:not_found)
|
||||
@ -30,9 +29,10 @@ RSpec.describe "/lnurlpay", type: :request do
|
||||
|
||||
before do
|
||||
login_as user, :scope => :user
|
||||
Setting.nostr_enabled = false
|
||||
end
|
||||
|
||||
describe "GET /.well-known/lnurlpay/:username" do
|
||||
describe "GET /.well-known/lnurlp/:username" do
|
||||
it "returns a formatted Lightning Address response" do
|
||||
get lightning_address_path(username: "satoshi")
|
||||
|
||||
@ -45,17 +45,41 @@ RSpec.describe "/lnurlpay", type: :request do
|
||||
expect(res["minSendable"]).to be_a(Integer)
|
||||
expect(res["maxSendable"]).to be_a(Integer)
|
||||
expect(res["commentAllowed"]).to be_a(Integer)
|
||||
expect(res["allowsNostr"]).to be_nil
|
||||
end
|
||||
|
||||
context "with support for nostr zaps" do
|
||||
before do
|
||||
Setting.nostr_enabled = true
|
||||
end
|
||||
|
||||
it "returns NIP-57 properties" do
|
||||
get lightning_address_path(username: "satoshi")
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["allowsNostr"]).to be(true)
|
||||
expect(res["nostrPubkey"]).to eq(Setting.nostr_public_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /lnurlpay/:username/invoice" do
|
||||
let(:invoice) {
|
||||
{
|
||||
"payment_hash" => "35778ccdb8319b5104e41d2043d18446bf91bcd51450b5f1cf1b6082a7cc6203",
|
||||
"payment_request" => "lnbc210n1pnzzr6rpp5x4mcendcxxd4zp8yr5sy85vyg6ler0x4z3gttuw0rdsg9f7vvgpshp52y6nf64apaqta2kjuwp2apglewqa9fva2mada6x2mmdj20t57jdscqzzsxqyz5vqsp5a3h88efdc436wunupz293gdtvm5843yfcfc8hxm2rpdunaetl39q9qyyssq07ec02dqr4epa73ssy0lzwglw49aa9rfywlp0c7jpnf448uapsgqch79d4222xqlh8674lzddvcyptpnwqqq8vpppf8djrn8yjf53dqpzwx5kh",
|
||||
"expires_at" => "2024-04-19T12:17:07.725314947Z"
|
||||
}
|
||||
}
|
||||
|
||||
before do
|
||||
allow_any_instance_of(User).to receive(:ln_create_invoice).and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an")
|
||||
allow(LndhubManager::CreateUserInvoice).to receive(:call)
|
||||
.and_return(invoice)
|
||||
end
|
||||
|
||||
it "returns a formatted lnurlpay response" do
|
||||
get lnurlpay_invoice_path(username: "satoshi", params: {
|
||||
amount: 50000, comment: "Coffee time!"
|
||||
amount: 21000, comment: "Coffee time!"
|
||||
})
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
@ -64,32 +88,108 @@ RSpec.describe "/lnurlpay", type: :request do
|
||||
expect(res["status"]).to eq('OK')
|
||||
expect(res["successAction"]["tag"]).to eq('message')
|
||||
expect(res["successAction"]["message"]).to match('Thank you')
|
||||
expect(res["pr"]).to eq("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an")
|
||||
expect(res["pr"]).to eq("lnbc210n1pnzzr6rpp5x4mcendcxxd4zp8yr5sy85vyg6ler0x4z3gttuw0rdsg9f7vvgpshp52y6nf64apaqta2kjuwp2apglewqa9fva2mada6x2mmdj20t57jdscqzzsxqyz5vqsp5a3h88efdc436wunupz293gdtvm5843yfcfc8hxm2rpdunaetl39q9qyyssq07ec02dqr4epa73ssy0lzwglw49aa9rfywlp0c7jpnf448uapsgqch79d4222xqlh8674lzddvcyptpnwqqq8vpppf8djrn8yjf53dqpzwx5kh")
|
||||
end
|
||||
|
||||
context "amount too low" do
|
||||
describe "amount too low" do
|
||||
it "returns an error" do
|
||||
get lnurlpay_invoice_path(username: "satoshi", params: {
|
||||
amount: 5000, comment: "Coffee time!"
|
||||
})
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["status"]).to eq('ERROR')
|
||||
expect(res["reason"]).to eq('Invalid amount')
|
||||
end
|
||||
end
|
||||
|
||||
context "comment too long" do
|
||||
describe "comment too long" do
|
||||
it "returns an error" do
|
||||
get lnurlpay_invoice_path(username: "satoshi", params: {
|
||||
amount: 5000000, comment: "Coffee time is the best time, so here's some money for you to get some. May I suggest to sample some Pacamara beans from El Salvador?"
|
||||
})
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["status"]).to eq('ERROR')
|
||||
expect(res["reason"]).to eq('Comment too long')
|
||||
end
|
||||
end
|
||||
|
||||
context "zap request" do
|
||||
before do
|
||||
Setting.nostr_enabled = true
|
||||
end
|
||||
|
||||
describe "with invalid request event" do
|
||||
it "returns an error" do
|
||||
get lnurlpay_invoice_path(username: "satoshi", params: {
|
||||
amount: 2100000, nostr: { foo: "bar" }.to_json
|
||||
})
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["status"]).to eq('ERROR')
|
||||
expect(res["reason"]).to eq('Invalid zap request')
|
||||
end
|
||||
end
|
||||
|
||||
describe "with valid request event" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712487443,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org", "wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"]],
|
||||
content: "",
|
||||
sig: "e9e9bb2bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2"
|
||||
)
|
||||
}
|
||||
|
||||
let(:invoice) {
|
||||
{
|
||||
"payment_hash" => "d6fa1d4b66587ab247d7b1f7608f2aed780a0cca021d09a30279c3dd9ff80f85",
|
||||
"payment_request" => "lnbc210n1pnzzyvjpp56map6jmxtpaty37hk8mkpre2a4uq5rx2qgwsngcz08pam8lcp7zshp5xs7v3qlx0j0gyu9grrzx9xgews3t9vq64v30579le9z9wqr6fc5scqzzsxqyz5vqsp5kmltj5eayh47c6trwj8wdrz5nxymqp0eqwtk7k5nk6ytyz522nvs9qyyssqvkluufkp34gtzxdg0uyqcsdum2n34xz94tqr4jfwwx53czteutvj7eptz4lm5vcu0m8jqzxck484ycxzcqgqlqmpj2r3jxjlj4x6nygp8fvnag",
|
||||
"expires_at" => "2024-04-19T12:26:58.432434748Z"
|
||||
}
|
||||
}
|
||||
|
||||
it "returns an invoice" do
|
||||
expect(LndhubManager::CreateUserInvoice).to receive(:call)
|
||||
.with(user: user, payload: {
|
||||
amount: 21,
|
||||
description: "Zap for satoshi@kosmos.org",
|
||||
description_hash: "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a"
|
||||
})
|
||||
.and_return(invoice)
|
||||
|
||||
get lnurlpay_invoice_path(username: "satoshi", params: {
|
||||
amount: 21000, nostr: event.to_json
|
||||
})
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["status"]).to eq('OK')
|
||||
expect(res["pr"]).to eq(invoice["payment_request"])
|
||||
end
|
||||
|
||||
it "creates a zap record" do
|
||||
get lnurlpay_invoice_path(username: "satoshi", params: {
|
||||
amount: 21000, nostr: event.to_json
|
||||
})
|
||||
|
||||
zap = user.zaps.find_by payment_request: invoice["payment_request"]
|
||||
expect(zap.request_event.id).to eq(event.id)
|
||||
expect(zap.receipt).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /.well-known/keysend/:username/" do
|
||||
|
@ -53,9 +53,7 @@ RSpec.describe "Webhooks", type: :request do
|
||||
let(:user) { create :user, ln_account: "123456abcdef" }
|
||||
let(:payload) { JSON.parse(File.read(File.expand_path("../fixtures/lndhub/incoming.json", File.dirname(__FILE__)))) }
|
||||
|
||||
before do
|
||||
user.save! #FIXME this should not be necessary
|
||||
end
|
||||
before { user.save! } #FIXME this should not be necessary
|
||||
|
||||
it "returns a 200 status" do
|
||||
post "/webhooks/lndhub", params: payload.to_json
|
||||
@ -63,9 +61,15 @@ RSpec.describe "Webhooks", type: :request do
|
||||
end
|
||||
|
||||
it "does not send notifications by default" do
|
||||
post "/webhooks/lndhub", params: payload.to_json
|
||||
expect(enqueued_jobs.size).to eq(0)
|
||||
end
|
||||
|
||||
it "does not send a zap receipt" do
|
||||
expect(NostrManager::PublishZapReceipt).not_to receive(:call)
|
||||
post "/webhooks/lndhub", params: payload.to_json
|
||||
end
|
||||
|
||||
context "notification preference set to 'xmpp'" do
|
||||
before do
|
||||
Setting.xmpp_notifications_from_address = "botka@kosmos.org"
|
||||
@ -103,5 +107,53 @@ RSpec.describe "Webhooks", type: :request do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "Valid payload for zap transaction" do
|
||||
let(:user) { create :user, ln_account: "123456abcdef" }
|
||||
let(:zap) { create :zap, user: user }
|
||||
let(:payload) { JSON.parse(File.read(File.expand_path("../fixtures/lndhub/incoming-zap.json", File.dirname(__FILE__)))) }
|
||||
let(:zap_receipt) {
|
||||
Nostr::Event.new(
|
||||
id: "cb66278a9add37a2f1e018826327ff15304e8055ff7b910100225baf83a9d691",
|
||||
sig: "a808d6792e21824bfddc98742b6831b1070e8b21e12aa424d2bb168a09f3a95a217d4513e803f2acb6e38404f763eb09fa07a341ee9c8c4c7d18bbe3d381eb6f",
|
||||
pubkey: "bdd76ce2934b2f591f9fad2ebe9da18f20d2921de527494ba00eeaa0a0efadcf",
|
||||
created_at: 1673428978,
|
||||
kind: 9735,
|
||||
tags: [
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"],
|
||||
["bolt11", "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu"],
|
||||
["preimage", "3539663535656537343331663432653165396430623966633664656664646563"],
|
||||
["description", "{\"id\":\"3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff\",\"sig\":\"e9e9bb2bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2\",\"pubkey\":\"730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c\",\"created_at\":1712487443,\"kind\":9734,\"tags\":[[\"relays\",\"wss://nostr.kosmos.org\",\"wss://relay.example.com\"],[\"p\",\"07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3\"]],\"content\":\"\"}"]
|
||||
],
|
||||
content: ""
|
||||
)
|
||||
}
|
||||
|
||||
before do
|
||||
user.save!
|
||||
zap.save!
|
||||
|
||||
allow(NostrManager::CreateZapReceipt).to receive(:call)
|
||||
.and_return(zap_receipt)
|
||||
allow(NostrManager::PublishZapReceipt).to receive(:call)
|
||||
.and_return(true)
|
||||
end
|
||||
|
||||
it "returns a 200 status" do
|
||||
post "/webhooks/lndhub", params: payload.to_json
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
|
||||
it "creates and adds a zap receipt to the zap record" do
|
||||
post "/webhooks/lndhub", params: payload.to_json
|
||||
zap = user.zaps.first
|
||||
expect(zap.receipt).not_to be_nil
|
||||
end
|
||||
|
||||
it "publishes the zap receipt" do
|
||||
expect(NostrManager::PublishZapReceipt).to receive(:call).with(zap: zap)
|
||||
post "/webhooks/lndhub", params: payload.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
45
spec/services/nostr_manager/create_zap_receipt_spec.rb
Normal file
45
spec/services/nostr_manager/create_zap_receipt_spec.rb
Normal file
@ -0,0 +1,45 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe NostrManager::CreateZapReceipt, type: :model do
|
||||
let(:user) { create :user, ln_account: "123456abcdef" }
|
||||
let(:zap) { create :zap, user: user }
|
||||
|
||||
# before do
|
||||
# user.save!
|
||||
# zap.save!
|
||||
# end
|
||||
|
||||
subject {
|
||||
described_class.call(
|
||||
zap: zap, paid_at: 1673428978,
|
||||
preimage: "3539663535656537343331663432653165396430623966633664656664646563"
|
||||
)
|
||||
}
|
||||
|
||||
describe "Zap receipt" do
|
||||
it "is a kind:9735 note" do
|
||||
expect(subject).to be_a(Nostr::Event)
|
||||
expect(subject.kind).to eq(9735)
|
||||
end
|
||||
|
||||
it "sets created_at to when the invoice was paid" do
|
||||
expect(subject.created_at).to eq(1673428978)
|
||||
end
|
||||
|
||||
it "contains the zap recipient" do
|
||||
expect(subject.tags.find{|t| t[0] == "p"}[1]).to eq("07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3")
|
||||
end
|
||||
|
||||
it "contains the bolt11 invoice" do
|
||||
expect(subject.tags.find{|t| t[0] == "bolt11"}[1]).to eq(zap.payment_request)
|
||||
end
|
||||
|
||||
it "contains the invoice preimage" do
|
||||
expect(subject.tags.find{|t| t[0] == "preimage"}[1]).to eq("3539663535656537343331663432653165396430623966633664656664646563")
|
||||
end
|
||||
|
||||
it "contains the serialized zap request event as description" do
|
||||
expect(subject.tags.find{|t| t[0] == "description"}[1]).to eq(zap.request_event.to_json)
|
||||
end
|
||||
end
|
||||
end
|
40
spec/services/nostr_manager/publish_zap_receipt_spec.rb
Normal file
40
spec/services/nostr_manager/publish_zap_receipt_spec.rb
Normal file
@ -0,0 +1,40 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe NostrManager::PublishZapReceipt, type: :model do
|
||||
let(:user) { create :user, ln_account: "123456abcdef" }
|
||||
let(:zap) { create :zap, user: user }
|
||||
|
||||
describe "Default/delayed execution" do
|
||||
it "publishes zap receipts to all requested relays" do
|
||||
expect(NostrPublishEventJob).to receive(:perform_later)
|
||||
.exactly(2).times.and_return(true)
|
||||
|
||||
described_class.call(zap: zap)
|
||||
end
|
||||
|
||||
context "with a long relay list" do
|
||||
before do
|
||||
relays = zap.request["tags"].find { |t| t.first == "relays" }
|
||||
[
|
||||
"wss://aegonstargaryen.example.com", "wss://visenya.example.com",
|
||||
"wss://rhaenys.example.com", "wss://housevelaryon.example.com",
|
||||
"wss://aemond.example.com", "wss://jaehaerys.example.com",
|
||||
"wss://daenerys.example.com", "wss://corlys.example.com",
|
||||
"wss://laenor.example.com", "wss://alysanne.example.com",
|
||||
"wss://balerion.example.com", "wss://meraxes.example.com",
|
||||
"wss://vhaegar.example.com", "wss://vermax.example.com",
|
||||
"wss://caraxes.example.com"
|
||||
].each do |url|
|
||||
relays << url
|
||||
end
|
||||
end
|
||||
|
||||
it "limits publishing attempts to the first 12 relays" do
|
||||
expect(NostrPublishEventJob).to receive(:perform_later)
|
||||
.exactly(12).times.and_return(true)
|
||||
|
||||
described_class.call(zap: zap)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
206
spec/services/nostr_manager/verify_zap_request_spec.rb
Normal file
206
spec/services/nostr_manager/verify_zap_request_spec.rb
Normal file
@ -0,0 +1,206 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe NostrManager::VerifyZapRequest, type: :model do
|
||||
describe "Signature invalid" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712487443,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org", "wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"]],
|
||||
content: "",
|
||||
sig: "1234562bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 100000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns false" do
|
||||
expect(subject).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe "p tag missing" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "eb53c5e728b625831e318c7ab09373502dd4a41ed11c17f80f32fd1c3fe1252b",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712580669,
|
||||
kind: 9734,
|
||||
tags: [["relays", "wss://nostr.kosmos.org","wss://relay.example.com"]],
|
||||
content: "",
|
||||
sig: "8e22dcb3e91d080c9549ea0808b018194cc352dde3056a4911796d6f0239de7e1955f287c7e6769909e59ab0ffa09e307178c32128dc451823fb00102ed7e80a"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 100000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns false" do
|
||||
expect(subject).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe "p tag not a valid pubkey" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "f2c23f038cf4d0307147d40ddceb87e34ee94acf632f5a67217a55a0b41c5d95",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712581702,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org","wss://relay.example.com"],
|
||||
["p","123456abcdef"]
|
||||
],
|
||||
content: "",
|
||||
sig: "b33e6b231273d38629f5efb86325ce3b4e02a7d84dfa1f37167ca2de8392cea654ae34000fc365ddfc6e21dc8ce2365fcb000ccb9064fbbcd54c7ea4a943955b"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 100000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns false" do
|
||||
expect(subject).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Minimum valid request" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712487443,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org", "wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"]],
|
||||
content: "",
|
||||
sig: "e9e9bb2bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 100000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns true" do
|
||||
expect(subject).to be(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Optional amount property" do
|
||||
describe "does not match given amount" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "be3d3ba15a257546f6fbba849d4717641fd4ea9f21ae6e9278a045f31d212c5e",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712579812,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org","wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"],
|
||||
["amount", "21000"]
|
||||
],
|
||||
content: "",
|
||||
sig: "2ca9cfd0fdaf43dd8a50ab586e83da4bd9d592def8ed198536f5e3e7aad3537818687e42d98eb61d60e33dbd848c1eecf72b68fd98376bbabdab7e029e810869"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 100000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns false" do
|
||||
expect(subject).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe "matches given amount" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "be3d3ba15a257546f6fbba849d4717641fd4ea9f21ae6e9278a045f31d212c5e",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712579812,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org","wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"],
|
||||
["amount", "21000"]
|
||||
],
|
||||
content: "",
|
||||
sig: "2ca9cfd0fdaf43dd8a50ab586e83da4bd9d592def8ed198536f5e3e7aad3537818687e42d98eb61d60e33dbd848c1eecf72b68fd98376bbabdab7e029e810869"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 21000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns true" do
|
||||
expect(subject).to be(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "Optional amount property" do
|
||||
describe "does not match given amount" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "be3d3ba15a257546f6fbba849d4717641fd4ea9f21ae6e9278a045f31d212c5e",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712579812,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org","wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"],
|
||||
["amount", "21000"]
|
||||
],
|
||||
content: "",
|
||||
sig: "2ca9cfd0fdaf43dd8a50ab586e83da4bd9d592def8ed198536f5e3e7aad3537818687e42d98eb61d60e33dbd848c1eecf72b68fd98376bbabdab7e029e810869"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 100000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns false" do
|
||||
expect(subject).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe "matches given amount" do
|
||||
let(:event) {
|
||||
Nostr::Event.new(
|
||||
id: "be3d3ba15a257546f6fbba849d4717641fd4ea9f21ae6e9278a045f31d212c5e",
|
||||
pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c",
|
||||
created_at: 1712579812,
|
||||
kind: 9734,
|
||||
tags: [
|
||||
["relays", "wss://nostr.kosmos.org","wss://relay.example.com"],
|
||||
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"],
|
||||
["amount", "21000"]
|
||||
],
|
||||
content: "",
|
||||
sig: "2ca9cfd0fdaf43dd8a50ab586e83da4bd9d592def8ed198536f5e3e7aad3537818687e42d98eb61d60e33dbd848c1eecf72b68fd98376bbabdab7e029e810869"
|
||||
)
|
||||
}
|
||||
|
||||
subject {
|
||||
NostrManager::VerifyZapRequest.call(amount: 21000, event: event, lnurl: nil)
|
||||
}
|
||||
|
||||
it "returns true" do
|
||||
expect(subject).to be(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user