WIP Persist zaps, create and send zap receipts
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Râu Cao 2024-05-09 14:31:37 +02:00
parent c0f4e7925e
commit c6c5d80fb4
Signed by: raucao
GPG Key ID: 37036C356E56CC51
17 changed files with 242 additions and 37 deletions

View File

@ -18,6 +18,9 @@ LNDHUB_API_URL='http://localhost:3026'
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org' LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
LNDHUB_PUBLIC_KEY='024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946' LNDHUB_PUBLIC_KEY='024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946'
NOSTR_PRIVATE_KEY='7c3ef7e448505f0615137af38569d01807d3b05b5005d5ecf8aaafcd40323cea'
NOSTR_PUBLIC_KEY='bdd76ce2934b2f591f9fad2ebe9da18f20d2921de527494ba00eeaa0a0efadcf'
RS_STORAGE_URL='https://storage.kosmos.org' RS_STORAGE_URL='https://storage.kosmos.org'
RS_REDIS_URL='redis://localhost:6379/1' RS_REDIS_URL='redis://localhost:6379/1'

View File

@ -52,7 +52,7 @@ class LnurlpayController < ApplicationController
return return
end end
if params[:nostr].present? if params[:nostr].present?# TODO && Setting.nostr_enabled?
handle_zap_request amount, params[:nostr], params[:lnurl] handle_zap_request amount, params[:nostr], params[:lnurl]
else else
handle_pay_request address, amount, comment handle_pay_request address, amount, comment
@ -131,6 +131,9 @@ class LnurlpayController < ApplicationController
return return
end 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 = "Zap for #{@user.address}"
desc = "#{desc}: \"#{event.content}\"" if event.content.present? desc = "#{desc}: \"#{event.content}\"" if event.content.present?
@ -142,6 +145,10 @@ class LnurlpayController < ApplicationController
} }
) )
@user.zaps.create! request: event,
payment_request: invoice["payment_request"],
amount: amount
render json: { status: "OK", pr: invoice["payment_request"] } render json: { status: "OK", pr: invoice["payment_request"] }
end end
end end

View File

@ -7,10 +7,14 @@ class WebhooksController < ApplicationController
def lndhub def lndhub
@user = User.find_by!(ln_account: @payload[:user_login]) @user = User.find_by!(ln_account: @payload[:user_login])
if @zap_request = fetch_nostr_event_from_description if zap = @user.zaps.find_by(payment_request: @payload[:payment_request])
NostrManager::PublishZapReceipt.call( zap_receipt = NostrManager::CreateZapReceipt.call(
user: @user, zap_request: @zap_request 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 end
send_notifications send_notifications
@ -28,21 +32,16 @@ class WebhooksController < ApplicationController
def process_payload def process_payload
@payload = JSON.parse(request.body.read, symbolize_names: true) @payload = JSON.parse(request.body.read, symbolize_names: true)
head :no_content and return unless @payload[:type] == "incoming" unless @payload[:type] == "incoming" &&
@payload[:state] == "settled"
head :no_content and return
end
rescue rescue
head :unprocessable_entity and return head :unprocessable_entity and return
end end
def fetch_nostr_event_from_description
memo_json = JSON.parse(@payload[:memo])
Nostr::Event.new(**memo_json.to_h.symbolize_keys)
rescue
nil
end
def send_notifications def send_notifications
notify = @user.preferences[:lightning_notify_sats_received] case @user.preferences[:lightning_notify_sats_received]
case notify
when "xmpp" when "xmpp"
notify_xmpp notify_xmpp
when "email" when "email"

View File

@ -0,0 +1,7 @@
class NostrPublishEventJob < ApplicationJob
queue_as :nostr
def perform(event:, relay:)
NostrManager::PublishEvent.call(event: event, relay: relay)
end
end

View File

@ -1,3 +1,17 @@
class Zap < ApplicationRecord class Zap < ApplicationRecord
belongs_to :user 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 end

View 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

View File

@ -1,17 +1,17 @@
module NostrManager module NostrManager
class PublishEvent < NostrManagerService class PublishEvent < NostrManagerService
def initialize(event: nil, relay: nil) def initialize(event: nil, relay_url: nil)
# @relay = relay relay_name = relay_url.gsub(/^ws(s):\/\//, "")
@relay = Nostr::Relay.new(url: 'ws://nostr-relay:7777', name: 'strfry') @relay = Nostr::Relay.new(url: relay_url, name: relay_name)
keypair = Nostr::KeyPair.new( @event = case event.class
private_key: Nostr::PrivateKey.new(Setting.nostr_private_key), when Nostr::Event
public_key: Nostr::PublicKey.new(Setting.nostr_public_key) event
) when Hash
@user = Nostr::User.new(keypair: keypair) Nostr::Event.new(**event.symbolize_keys)
@event = @user.create_event( # else
kind: Nostr::EventKind::TEXT_NOTE, # TODO
content: "The time is #{Time.now.strftime('%H:%M:%S')}" # raise NotImplementedError
) end
@client = Nostr::Client.new @client = Nostr::Client.new
end end
@ -25,6 +25,7 @@ module NostrManager
end end
client.on :error do |e| client.on :error do |e|
# TODO log relay URL/name
puts "Error: #{e}" puts "Error: #{e}"
puts "Closing thread..." puts "Closing thread..."
Thread.exit Thread.exit
@ -36,6 +37,7 @@ module NostrManager
if msg[0] == "OK" && msg[1] == event.id if msg[0] == "OK" && msg[1] == event.id
puts "Event published. Closing thread..." puts "Event published. Closing thread..."
else else
# TODO log relay URL/name
puts "Unexpected message from relay. Closing thread..." puts "Unexpected message from relay. Closing thread..."
end end
Thread.exit Thread.exit

View File

@ -1,10 +1,20 @@
module NostrManager module NostrManager
class PublishZapReceipt < NostrManagerService class PublishZapReceipt < NostrManagerService
def initialize(user:, zap_request:) def initialize(zap:, delayed: true)
@user, @zap_request = user, zap_request @zap, @delayed = zap, delayed
end end
def call def call
tags = parse_tags(@zap.request_event.tags)
# TODO limit to 15 or so relays
tags[:relays].each do |relay_url|
if @delayed
NostrPublishEventJob.perform_later(event: @zap.receipt, relay: relay_url)
else
NostrManager::PublishEvent.call(event: @zap.receipt_event, relay: relay_url)
end
end
end end
end end
end end

View File

@ -8,4 +8,15 @@ class NostrManagerService < ApplicationService
end end
out out
end 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 end

View File

@ -3,7 +3,7 @@ class CreateZaps < ActiveRecord::Migration[7.1]
create_table :zaps do |t| create_table :zaps do |t|
t.references :user, null: false, foreign_key: true t.references :user, null: false, foreign_key: true
t.json :request t.json :request
t.json :receipt t.json :receipt, default: nil
t.text :payment_request t.text :payment_request
t.bigint :amount t.bigint :amount

View File

@ -1,7 +1,20 @@
FactoryBot.define do FactoryBot.define do
factory :zap do factory :zap do
user { nil } user { nil }
request { "" } request {
receipt { "" } 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
end end

View File

@ -4,7 +4,7 @@
"user_login": "123456abcdef", "user_login": "123456abcdef",
"amount": 21000, "amount": 21000,
"fee": 0, "fee": 0,
"memo": "{\"id\":\"3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff\",\"sig\":\"e9e9bb2bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2\",\"pubkey\":\"730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c\",\"created_at\":1712487443,\"kind\":9734,\"tags\":[[\"relays\",\"wss://nostr.kosmos.org\",\"wss://relay.example.com\"],[\"p\",\"07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3\"]],\"content\":\"\"}", "memo": "Zap for satoshi@kosmos.org",
"description_hash": "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a", "description_hash": "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a",
"payment_request": "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu", "payment_request": "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu",
"destination_pubkey_hex": "024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946", "destination_pubkey_hex": "024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946",

12
spec/models/zap_spec.rb Normal file
View 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

View File

@ -143,7 +143,7 @@ RSpec.describe "/lnurlpay", type: :request do
expect(LndhubManager::CreateUserInvoice).to receive(:call) expect(LndhubManager::CreateUserInvoice).to receive(:call)
.with(user: user, payload: { .with(user: user, payload: {
amount: 21, amount: 21,
description: event.to_json, description: "Zap for satoshi@kosmos.org",
description_hash: "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a" description_hash: "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a"
}) })
.and_return(invoice) .and_return(invoice)
@ -156,7 +156,17 @@ RSpec.describe "/lnurlpay", type: :request do
res = JSON.parse(response.body) res = JSON.parse(response.body)
expect(res["status"]).to eq('OK') expect(res["status"]).to eq('OK')
expect(res["pr"]).to eq("lnbc210n1pnzzyvjpp56map6jmxtpaty37hk8mkpre2a4uq5rx2qgwsngcz08pam8lcp7zshp5xs7v3qlx0j0gyu9grrzx9xgews3t9vq64v30579le9z9wqr6fc5scqzzsxqyz5vqsp5kmltj5eayh47c6trwj8wdrz5nxymqp0eqwtk7k5nk6ytyz522nvs9qyyssqvkluufkp34gtzxdg0uyqcsdum2n34xz94tqr4jfwwx53czteutvj7eptz4lm5vcu0m8jqzxck484ycxzcqgqlqmpj2r3jxjlj4x6nygp8fvnag") 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
end end

View File

@ -110,17 +110,48 @@ RSpec.describe "Webhooks", type: :request do
describe "Valid payload for zap transaction" do describe "Valid payload for zap transaction" do
let(:user) { create :user, ln_account: "123456abcdef" } 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(: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 { user.save! } #FIXME this should not be necessary 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 it "returns a 200 status" do
post "/webhooks/lndhub", params: payload.to_json post "/webhooks/lndhub", params: payload.to_json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it "sends a zap receipt" do it "creates and adds a zap receipt to the zap record" do
expect(NostrManager::PublishZapReceipt).to receive(:call) 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 post "/webhooks/lndhub", params: payload.to_json
end end
end end

View 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

View File

@ -0,0 +1,16 @@
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
2.times do
expect(NostrPublishEventJob).to receive(:perform_later).and_return(true)
end
described_class.call(zap: zap)
end
end
end