From e27c64b5f18fc21b7ab19b73b56d7a478e7f0a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 21 Apr 2024 10:35:30 +0200 Subject: [PATCH] WIP Check for zaps, send zap receipt on incoming zap tx --- app/controllers/webhooks_controller.rb | 23 +++++++++++----- .../nostr_manager/publish_zap_receipt.rb | 10 +++++++ spec/fixtures/lndhub/incoming-zap.json | 19 +++++++++++++ spec/requests/webhooks_spec.rb | 27 ++++++++++++++++--- 4 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 app/services/nostr_manager/publish_zap_receipt.rb create mode 100644 spec/fixtures/lndhub/incoming-zap.json diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 446c39e..d58041f 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -7,6 +7,12 @@ class WebhooksController < ApplicationController def lndhub @user = User.find_by!(ln_account: @payload[:user_login]) + if @zap_request = fetch_nostr_event_from_description + NostrManager::PublishZapReceipt.call( + user: @user, zap_request: @zap_request + ) + end + send_notifications head :ok @@ -21,12 +27,17 @@ class WebhooksController < ApplicationController end def process_payload - 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 - end + @payload = JSON.parse(request.body.read, symbolize_names: true) + head :no_content and return unless @payload[:type] == "incoming" + rescue + head :unprocessable_entity and return + 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 diff --git a/app/services/nostr_manager/publish_zap_receipt.rb b/app/services/nostr_manager/publish_zap_receipt.rb new file mode 100644 index 0000000..a3e8102 --- /dev/null +++ b/app/services/nostr_manager/publish_zap_receipt.rb @@ -0,0 +1,10 @@ +module NostrManager + class PublishZapReceipt < NostrManagerService + def initialize(user:, zap_request:) + @user, @zap_request = user, zap_request + end + + def call + end + end +end diff --git a/spec/fixtures/lndhub/incoming-zap.json b/spec/fixtures/lndhub/incoming-zap.json new file mode 100644 index 0000000..c7a9f31 --- /dev/null +++ b/spec/fixtures/lndhub/incoming-zap.json @@ -0,0 +1,19 @@ +{ + "id": 58, + "type": "incoming", + "user_login": "123456abcdef", + "amount": 21000, + "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\":\"\"}", + "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" +} diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index c7d826d..e34bfab 100644 --- a/spec/requests/webhooks_spec.rb +++ b/spec/requests/webhooks_spec.rb @@ -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,22 @@ RSpec.describe "Webhooks", type: :request do end end end + + describe "Valid payload for zap transaction" do + let(:user) { create :user, ln_account: "123456abcdef" } + let(:payload) { JSON.parse(File.read(File.expand_path("../fixtures/lndhub/incoming-zap.json", File.dirname(__FILE__)))) } + + before { user.save! } #FIXME this should not be necessary + + it "returns a 200 status" do + post "/webhooks/lndhub", params: payload.to_json + expect(response).to have_http_status(:ok) + end + + it "sends a zap receipt" do + expect(NostrManager::PublishZapReceipt).to receive(:call) + post "/webhooks/lndhub", params: payload.to_json + end + end end end