From 3ba333e80223e885ec5dae233f30812879790e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 31 Mar 2024 12:08:51 +0300 Subject: [PATCH 01/24] Indentation --- app/controllers/lnurlpay_controller.rb | 34 +++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 457fbec..5fd32fb 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -69,26 +69,26 @@ class LnurlpayController < ApplicationController private - def find_user - @user = User.where(cn: params[:username], ou: Setting.primary_domain).first - http_status :not_found if @user.nil? - 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.\"]]" - end + def metadata(address) + "[[\"text/identifier\", \"#{address}\"], [\"text/plain\", \"Send sats, receive thanks.\"]]" + end - def valid_amount?(amount_in_sats) - amount_in_sats <= MAX_SATS && amount_in_sats >= MIN_SATS - end + def valid_amount?(amount_in_sats) + amount_in_sats <= MAX_SATS && amount_in_sats >= MIN_SATS + end - def valid_comment?(comment) - comment.length <= MAX_COMMENT_CHARS - end + def valid_comment?(comment) + comment.length <= MAX_COMMENT_CHARS + end - private + private - def check_service_available - http_status :not_found unless Setting.lndhub_enabled? - end + def check_service_available + http_status :not_found unless Setting.lndhub_enabled? + end end From 3f90a011c4617cb15cede77f285ee445937afc6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 31 Mar 2024 12:09:18 +0300 Subject: [PATCH 02/24] Document URLs --- app/controllers/lnurlpay_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 5fd32fb..0ca11be 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -6,6 +6,7 @@ class LnurlpayController < ApplicationController MAX_SATS = 1_000_000 MAX_COMMENT_CHARS = 100 + # GET /.well-known/lnurlp/:username def index render json: { status: "OK", @@ -18,6 +19,7 @@ class LnurlpayController < ApplicationController } end + # GET /.well-known/keysend/:username def keysend http_status :not_found and return unless Setting.lndhub_keysend_enabled? @@ -32,6 +34,7 @@ class LnurlpayController < ApplicationController } end + # GET /lnurlpay/:username/invoice def invoice amount = params[:amount].to_i / 1000 # msats comment = params[:comment] || "" From 46b4723999feb7bb89f908cf916abfc194463082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 31 Mar 2024 12:10:04 +0300 Subject: [PATCH 03/24] Add global settings for account service's Nostr keys --- app/models/setting.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/models/setting.rb b/app/models/setting.rb index 1d895b2..1ffddc1 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -162,6 +162,12 @@ class Setting < RailsSettings::Base field :nostr_enabled, type: :boolean, default: false + field :nostr_private_key, type: :string, + default: ENV["NOSTR_PRIVATE_KEY"].presence + + field :nostr_public_key, type: :string, + default: ENV["NOSTR_PUBLIC_KEY"].presence + # # OpenCollective # From f2507409a30aaad990ab3d0a3a6b04afe7c01b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 31 Mar 2024 12:11:06 +0300 Subject: [PATCH 04/24] Announce nostr pubkey on lnurlp endpoint --- app/controllers/lnurlpay_controller.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 0ca11be..bc595bc 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -8,7 +8,7 @@ class LnurlpayController < ApplicationController # GET /.well-known/lnurlp/:username def index - render json: { + res = { status: "OK", callback: "https://#{Setting.accounts_domain}/lnurlpay/#{@user.cn}/invoice", tag: "payRequest", @@ -17,6 +17,13 @@ class LnurlpayController < ApplicationController metadata: metadata(@user.address), commentAllowed: MAX_COMMENT_CHARS } + + if Setting.nostr_enabled? && Setting.nostr_private_key.present? + res[:allows_nostr] = true + res[:nostrPubkey] = Setting.nostr_public_key + end + + render json: res end # GET /.well-known/keysend/:username From b09225543ba2a905d8d261e2513b43f15f07396e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 4 Apr 2024 12:16:45 +0300 Subject: [PATCH 05/24] Add Nostr relay service to Docker Compose config --- docker-compose.yml | 13 ++++ docker/strfry/strfry.conf | 138 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 docker/strfry/strfry.conf diff --git a/docker-compose.yml b/docker-compose.yml index 2b268d5..ec7069f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/docker/strfry/strfry.conf b/docker/strfry/strfry.conf new file mode 100644 index 0000000..9342387 --- /dev/null +++ b/docker/strfry/strfry.conf @@ -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 + } +} From bc4308283949ce2d7470f1ce9545282042e7754a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 4 Apr 2024 19:16:26 +0300 Subject: [PATCH 06/24] Add admin settings for nostr keys --- app/views/admin/settings/services/_nostr.html.erb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/views/admin/settings/services/_nostr.html.erb b/app/views/admin/settings/services/_nostr.html.erb index 664d089..c4f2182 100644 --- a/app/views/admin/settings/services/_nostr.html.erb +++ b/app/views/admin/settings/services/_nostr.html.erb @@ -7,4 +7,17 @@ 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" + ) %> +<% end %> From 77e2fe579296b9a085b68ca57166ab9a856ef324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 16 Apr 2024 19:10:48 +0200 Subject: [PATCH 07/24] Add helper method for parsing nostr event tags --- app/services/nostr_manager/verify_auth.rb | 5 +++-- app/services/nostr_manager_service.rb | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/services/nostr_manager/verify_auth.rb b/app/services/nostr_manager/verify_auth.rb index fdcef2d..481b1ce 100644 --- a/app/services/nostr_manager/verify_auth.rb +++ b/app/services/nostr_manager/verify_auth.rb @@ -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 diff --git a/app/services/nostr_manager_service.rb b/app/services/nostr_manager_service.rb index 3376226..d838e72 100644 --- a/app/services/nostr_manager_service.rb +++ b/app/services/nostr_manager_service.rb @@ -1,4 +1,11 @@ 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 end From c3b82fc2a9e6e3002298993cdf87b098e4e3089f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 16 Apr 2024 19:13:10 +0200 Subject: [PATCH 08/24] WIP Verify and respond to zap requests --- app/controllers/lnurlpay_controller.rb | 96 +++++--- .../nostr_manager/verify_zap_request.rb | 51 +++++ spec/fixtures/nostr/zap_request_event.json | 22 ++ spec/requests/lnurlpay_spec.rb | 63 +++++- .../nostr_manager/verify_zap_request_spec.rb | 206 ++++++++++++++++++ 5 files changed, 407 insertions(+), 31 deletions(-) create mode 100644 app/services/nostr_manager/verify_zap_request.rb create mode 100644 spec/fixtures/nostr/zap_request_event.json create mode 100644 spec/services/nostr_manager/verify_zap_request_spec.rb diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index bc595bc..1708903 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -43,7 +43,7 @@ class LnurlpayController < ApplicationController # 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 @@ -52,33 +52,19 @@ class LnurlpayController < ApplicationController return end - if !valid_comment?(comment) - render json: { status: "ERROR", reason: "Comment too long" } - return + if params[:nostr].present? + 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 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? @@ -88,6 +74,10 @@ class LnurlpayController < ApplicationController "[[\"text/identifier\", \"#{address}\"], [\"text/plain\", \"Send sats, receive thanks.\"]]" end + def zap_metadata(event) + "[[\"application/json\", #{event.to_json}]]" + end + def valid_amount?(amount_in_sats) amount_in_sats <= MAX_SATS && amount_in_sats >= MIN_SATS end @@ -96,9 +86,63 @@ 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? + memo = "To #{address}" + memo = "#{memo}: \"#{comment}\"" if comment.present? + + payment_request = @user.ln_create_invoice({ + amount: amount, # 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 + + 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 + # raise zap_metadata(event).inspect + + payment_request = @user.ln_create_invoice({ + amount: amount, # sats + # TODO should be npub instead of address? + memo: "Zapped #{@user.address} on Nostr", # TODO include event ID if given + description_hash: Digest::SHA2.hexdigest(zap_metadata(event)), + }) + + render json: { status: "OK", pr: payment_request } end end diff --git a/app/services/nostr_manager/verify_zap_request.rb b/app/services/nostr_manager/verify_zap_request.rb new file mode 100644 index 0000000..ad05024 --- /dev/null +++ b/app/services/nostr_manager/verify_zap_request.rb @@ -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 diff --git a/spec/fixtures/nostr/zap_request_event.json b/spec/fixtures/nostr/zap_request_event.json new file mode 100644 index 0000000..79854e6 --- /dev/null +++ b/spec/fixtures/nostr/zap_request_event.json @@ -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" + ] + ] +} diff --git a/spec/requests/lnurlpay_spec.rb b/spec/requests/lnurlpay_spec.rb index b3f9a17..707b711 100644 --- a/spec/requests/lnurlpay_spec.rb +++ b/spec/requests/lnurlpay_spec.rb @@ -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) @@ -32,7 +31,7 @@ RSpec.describe "/lnurlpay", type: :request do login_as user, :scope => :user 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") @@ -67,29 +66,83 @@ RSpec.describe "/lnurlpay", type: :request do expect(res["pr"]).to eq("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") 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 + 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) { + { + id: "3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff", + pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c", + created_at: 1712487443, + kind: 9734, + tags: [ + ["relays", "wss://nostr.kosmos.org", "wss://relay.example.com"], + ["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"]], + content: "", + sig: "e9e9bb2bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2" + } + } + + it "returns an invoice" do + expect_any_instance_of(User).to receive(:ln_create_invoice) + .with( + amount: 2100, + memo: "Zapped satoshi@kosmos.org on Nostr", + description_hash: "540279cd9da15279c8299d6d9ff1ab2a79eb259ee218adf3de393e1abe723077" + ) + .and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") + + get lnurlpay_invoice_path(username: "satoshi", params: { + amount: 2100000, 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 match(/^lnbc/) + end + end + end end describe "GET /.well-known/keysend/:username/" do diff --git a/spec/services/nostr_manager/verify_zap_request_spec.rb b/spec/services/nostr_manager/verify_zap_request_spec.rb new file mode 100644 index 0000000..1981132 --- /dev/null +++ b/spec/services/nostr_manager/verify_zap_request_spec.rb @@ -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 From 5685e1b7bcab9d8e879ab59d5c8c34b29ec4b18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 16 Apr 2024 20:19:15 +0200 Subject: [PATCH 09/24] Move lndhub invoice creation to service --- app/controllers/lnurlpay_controller.rb | 29 ++++++++++--------- app/models/user.rb | 6 ---- .../lndhub_manager/create_user_invoice.rb | 13 +++++++++ spec/requests/lnurlpay_spec.rb | 9 +++--- 4 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 app/services/lndhub_manager/create_user_invoice.rb diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 1708903..35739ea 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -95,11 +95,13 @@ class LnurlpayController < ApplicationController memo = "To #{address}" memo = "#{memo}: \"#{comment}\"" if comment.present? - payment_request = @user.ln_create_invoice({ - amount: amount, # sats - memo: memo, - description_hash: Digest::SHA2.hexdigest(metadata(address)), - }) + payment_request = LndhubManager::CreateUserInvoice.call( + user: @user, payload: { + amount: amount, # sats + memo: memo, + description_hash: Digest::SHA2.hexdigest(metadata(address)), + } + ) render json: { status: "OK", @@ -133,15 +135,14 @@ class LnurlpayController < ApplicationController return end - # TODO - # raise zap_metadata(event).inspect - - payment_request = @user.ln_create_invoice({ - amount: amount, # sats - # TODO should be npub instead of address? - memo: "Zapped #{@user.address} on Nostr", # TODO include event ID if given - description_hash: Digest::SHA2.hexdigest(zap_metadata(event)), - }) + payment_request = LndhubManager::CreateUserInvoice.call( + user: @user, payload: { + amount: amount, # sats + # TODO should be npub instead of address? + memo: "Zapped #{@user.address} on Nostr", # TODO include event ID if given + description_hash: Digest::SHA2.hexdigest(zap_metadata(event)), + } + ) render json: { status: "OK", pr: payment_request } end diff --git a/app/models/user.rb b/app/models/user.rb index 735d7e0..1a181dd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -143,12 +143,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) diff --git a/app/services/lndhub_manager/create_user_invoice.rb b/app/services/lndhub_manager/create_user_invoice.rb new file mode 100644 index 0000000..d0728a0 --- /dev/null +++ b/app/services/lndhub_manager/create_user_invoice.rb @@ -0,0 +1,13 @@ +module LndhubManager + class CreateUserInvoice < Lndhub + def initialize(user:, payload:) + @user = user + @payload = payload + end + + def call + authenticate @user + addinvoice @payload + end + end +end diff --git a/spec/requests/lnurlpay_spec.rb b/spec/requests/lnurlpay_spec.rb index 707b711..1727513 100644 --- a/spec/requests/lnurlpay_spec.rb +++ b/spec/requests/lnurlpay_spec.rb @@ -49,7 +49,8 @@ RSpec.describe "/lnurlpay", type: :request do describe "GET /lnurlpay/:username/invoice" do before do - allow_any_instance_of(User).to receive(:ln_create_invoice).and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") + allow(LndhubManager::CreateUserInvoice).to receive(:call) + .and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") end it "returns a formatted lnurlpay response" do @@ -123,12 +124,12 @@ RSpec.describe "/lnurlpay", type: :request do } it "returns an invoice" do - expect_any_instance_of(User).to receive(:ln_create_invoice) - .with( + expect(LndhubManager::CreateUserInvoice).to receive(:call) + .with(user: user, payload: { amount: 2100, memo: "Zapped satoshi@kosmos.org on Nostr", description_hash: "540279cd9da15279c8299d6d9ff1ab2a79eb259ee218adf3de393e1abe723077" - ) + }) .and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") get lnurlpay_invoice_path(username: "satoshi", params: { From 596ed7fccc7ce262228f16fcfa2775d03365e29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 21 Apr 2024 10:01:18 +0200 Subject: [PATCH 10/24] Use lndhub.go v2 endpoint for invoice creation --- app/controllers/lnurlpay_controller.rb | 27 ++++++-------- .../lndhub_manager/create_user_invoice.rb | 4 +-- spec/requests/lnurlpay_spec.rb | 36 +++++++++++++------ 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 35739ea..40681a5 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -71,11 +71,7 @@ class LnurlpayController < ApplicationController end def metadata(address) - "[[\"text/identifier\", \"#{address}\"], [\"text/plain\", \"Send sats, receive thanks.\"]]" - end - - def zap_metadata(event) - "[[\"application/json\", #{event.to_json}]]" + "[[\"text/identifier\",\"#{address}\"],[\"text/plain\",\"Sats for #{address}\"]]" end def valid_amount?(amount_in_sats) @@ -92,14 +88,14 @@ class LnurlpayController < ApplicationController return end - memo = "To #{address}" - memo = "#{memo}: \"#{comment}\"" if comment.present? + desc = "To #{address}" + desc = "#{desc}: \"#{comment}\"" if comment.present? - payment_request = LndhubManager::CreateUserInvoice.call( + invoice = LndhubManager::CreateUserInvoice.call( user: @user, payload: { amount: amount, # sats - memo: memo, - description_hash: Digest::SHA2.hexdigest(metadata(address)), + description: desc, + description_hash: Digest::SHA256.hexdigest(metadata(address)), } ) @@ -110,7 +106,7 @@ class LnurlpayController < ApplicationController message: "Sats received. Thank you!" }, routes: [], - pr: payment_request + pr: invoice["payment_request"] } end @@ -135,15 +131,14 @@ class LnurlpayController < ApplicationController return end - payment_request = LndhubManager::CreateUserInvoice.call( + invoice = LndhubManager::CreateUserInvoice.call( user: @user, payload: { amount: amount, # sats - # TODO should be npub instead of address? - memo: "Zapped #{@user.address} on Nostr", # TODO include event ID if given - description_hash: Digest::SHA2.hexdigest(zap_metadata(event)), + description: event.to_json, + description_hash: Digest::SHA256.hexdigest(event.to_json), } ) - render json: { status: "OK", pr: payment_request } + render json: { status: "OK", pr: invoice["payment_request"] } end end diff --git a/app/services/lndhub_manager/create_user_invoice.rb b/app/services/lndhub_manager/create_user_invoice.rb index d0728a0..70e40f9 100644 --- a/app/services/lndhub_manager/create_user_invoice.rb +++ b/app/services/lndhub_manager/create_user_invoice.rb @@ -1,5 +1,5 @@ module LndhubManager - class CreateUserInvoice < Lndhub + class CreateUserInvoice < LndhubV2 def initialize(user:, payload:) @user = user @payload = payload @@ -7,7 +7,7 @@ module LndhubManager def call authenticate @user - addinvoice @payload + create_invoice @payload end end end diff --git a/spec/requests/lnurlpay_spec.rb b/spec/requests/lnurlpay_spec.rb index 1727513..7479e65 100644 --- a/spec/requests/lnurlpay_spec.rb +++ b/spec/requests/lnurlpay_spec.rb @@ -48,14 +48,22 @@ RSpec.describe "/lnurlpay", type: :request do 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(LndhubManager::CreateUserInvoice).to receive(:call) - .and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") + .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,7 +72,7 @@ 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 describe "amount too low" do @@ -110,7 +118,7 @@ RSpec.describe "/lnurlpay", type: :request do describe "with valid request event" do let(:event) { - { + Nostr::Event.new( id: "3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff", pubkey: "730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c", created_at: 1712487443, @@ -120,27 +128,35 @@ RSpec.describe "/lnurlpay", type: :request do ["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: 2100, - memo: "Zapped satoshi@kosmos.org on Nostr", - description_hash: "540279cd9da15279c8299d6d9ff1ab2a79eb259ee218adf3de393e1abe723077" + amount: 21, + description: event.to_json, + description_hash: "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a" }) - .and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") + .and_return(invoice) get lnurlpay_invoice_path(username: "satoshi", params: { - amount: 2100000, nostr: event.to_json + 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 match(/^lnbc/) + expect(res["pr"]).to eq("lnbc210n1pnzzyvjpp56map6jmxtpaty37hk8mkpre2a4uq5rx2qgwsngcz08pam8lcp7zshp5xs7v3qlx0j0gyu9grrzx9xgews3t9vq64v30579le9z9wqr6fc5scqzzsxqyz5vqsp5kmltj5eayh47c6trwj8wdrz5nxymqp0eqwtk7k5nk6ytyz522nvs9qyyssqvkluufkp34gtzxdg0uyqcsdum2n34xz94tqr4jfwwx53czteutvj7eptz4lm5vcu0m8jqzxck484ycxzcqgqlqmpj2r3jxjlj4x6nygp8fvnag") end end end From adedaa5f7b584c6285e7b7a16ff912bbf063b4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 21 Apr 2024 10:01:54 +0200 Subject: [PATCH 11/24] Add task for easily creating test invoices --- lib/tasks/lndhub.rake | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/tasks/lndhub.rake b/lib/tasks/lndhub.rake index 04e7aac..bc6b13f 100644 --- a/lib/tasks/lndhub.rake +++ b/lib/tasks/lndhub.rake @@ -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" From b36baf26eb965414424d0095d6f28d526b575b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 21 Apr 2024 10:02:17 +0200 Subject: [PATCH 12/24] Refactor WebhooksController --- app/controllers/webhooks_controller.rb | 65 +++++++++++++++----------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index e3657c1..446c39e 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -2,45 +2,56 @@ 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 - end + @user = User.find_by!(ln_account: @payload[:user_login]) - 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 + 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 + end + + def send_notifications + notify = @user.preferences[:lightning_notify_sats_received] + case notify + 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 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 13/24] 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 From 619bd954b70fb8e75477688c31786a9782fc9fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 21 Apr 2024 10:51:41 +0200 Subject: [PATCH 14/24] WIP --- app/services/nostr_manager/publish_event.rb | 48 ++++++++++++++++++ config/sidekiq.yml | 1 + lib/nostr/event_kind.rb | 55 +++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 app/services/nostr_manager/publish_event.rb create mode 100644 lib/nostr/event_kind.rb diff --git a/app/services/nostr_manager/publish_event.rb b/app/services/nostr_manager/publish_event.rb new file mode 100644 index 0000000..35ab5be --- /dev/null +++ b/app/services/nostr_manager/publish_event.rb @@ -0,0 +1,48 @@ +module NostrManager + class PublishEvent < NostrManagerService + def initialize(event: nil, relay: nil) + # @relay = relay + @relay = Nostr::Relay.new(url: 'ws://nostr-relay:7777', name: 'strfry') + keypair = Nostr::KeyPair.new( + private_key: Nostr::PrivateKey.new(Setting.nostr_private_key), + public_key: Nostr::PublicKey.new(Setting.nostr_public_key) + ) + @user = Nostr::User.new(keypair: keypair) + @event = @user.create_event( + kind: Nostr::EventKind::TEXT_NOTE, + content: "The time is #{Time.now.strftime('%H:%M:%S')}" + ) + @client = Nostr::Client.new + end + + def call + client, relay, event = @client, @relay, @event + + client.on :connect do + puts "Connected to #{relay.url}" + puts "Publishing #{event.id}..." + client.publish event + end + + client.on :error do |e| + puts "Error: #{e}" + puts "Closing thread..." + Thread.exit + end + + client.on :message do |m| + puts "Message: #{m}" + msg = JSON.parse(m) rescue [] + if msg[0] == "OK" && msg[1] == event.id + puts "Event published. Closing thread..." + else + puts "Unexpected message from relay. Closing thread..." + end + Thread.exit + end + + puts "Connecting to #{relay.url}..." + client.connect relay + end + end +end diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 481ec8e..eb26f1e 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -3,3 +3,4 @@ - default - mailers - remotestorage + - nostr diff --git a/lib/nostr/event_kind.rb b/lib/nostr/event_kind.rb new file mode 100644 index 0000000..7445a1c --- /dev/null +++ b/lib/nostr/event_kind.rb @@ -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: , about: , + # picture: }+ 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 From 49d24990b48b002da34ec27e8d36a59e7578cc97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sat, 4 May 2024 17:05:34 +0200 Subject: [PATCH 15/24] Add zap model, user relation --- app/models/user.rb | 5 ++--- app/models/zap.rb | 3 +++ db/migrate/20240422171653_create_zaps.rb | 13 +++++++++++++ db/schema.rb | 14 +++++++++++++- spec/factories/zaps.rb | 7 +++++++ 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 app/models/zap.rb create mode 100644 db/migrate/20240422171653_create_zaps.rb create mode 100644 spec/factories/zaps.rb diff --git a/app/models/user.rb b/app/models/user.rb index 1a181dd..f387817 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 # diff --git a/app/models/zap.rb b/app/models/zap.rb new file mode 100644 index 0000000..5e2c9a8 --- /dev/null +++ b/app/models/zap.rb @@ -0,0 +1,3 @@ +class Zap < ApplicationRecord + belongs_to :user +end diff --git a/db/migrate/20240422171653_create_zaps.rb b/db/migrate/20240422171653_create_zaps.rb new file mode 100644 index 0000000..64735a1 --- /dev/null +++ b/db/migrate/20240422171653_create_zaps.rb @@ -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 + t.text :payment_request + t.bigint :amount + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ccd332f..8400be3 100644 --- a/db/schema.rb +++ b/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 diff --git a/spec/factories/zaps.rb b/spec/factories/zaps.rb new file mode 100644 index 0000000..2d695d0 --- /dev/null +++ b/spec/factories/zaps.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :zap do + user { nil } + request { "" } + receipt { "" } + end +end From c0f4e7925e08cd5ff61945a19a73d7b4b627d1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sat, 4 May 2024 17:07:23 +0200 Subject: [PATCH 16/24] Use zap comment for description/memo But use the hashed zap request event for the description hash. --- app/controllers/lnurlpay_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 40681a5..47265ae 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -131,10 +131,13 @@ class LnurlpayController < ApplicationController return end + desc = "Zap for #{@user.address}" + desc = "#{desc}: \"#{event.content}\"" if event.content.present? + invoice = LndhubManager::CreateUserInvoice.call( user: @user, payload: { amount: amount, # sats - description: event.to_json, + description: desc, description_hash: Digest::SHA256.hexdigest(event.to_json), } ) From c6c5d80fb4cea799513589afd04970a595138c49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 9 May 2024 14:31:37 +0200 Subject: [PATCH 17/24] WIP Persist zaps, create and send zap receipts --- .env.test | 3 ++ app/controllers/lnurlpay_controller.rb | 9 +++- app/controllers/webhooks_controller.rb | 25 +++++------ app/jobs/nostr_publish_event_job.rb | 7 +++ app/models/zap.rb | 14 ++++++ .../nostr_manager/create_zap_receipt.rb | 25 +++++++++++ app/services/nostr_manager/publish_event.rb | 26 ++++++----- .../nostr_manager/publish_zap_receipt.rb | 14 +++++- app/services/nostr_manager_service.rb | 11 +++++ db/migrate/20240422171653_create_zaps.rb | 2 +- spec/factories/zaps.rb | 17 ++++++- spec/fixtures/lndhub/incoming-zap.json | 2 +- spec/models/zap_spec.rb | 12 +++++ spec/requests/lnurlpay_spec.rb | 14 +++++- spec/requests/webhooks_spec.rb | 37 +++++++++++++-- .../nostr_manager/create_zap_receipt_spec.rb | 45 +++++++++++++++++++ .../nostr_manager/publish_zap_receipt_spec.rb | 16 +++++++ 17 files changed, 242 insertions(+), 37 deletions(-) create mode 100644 app/jobs/nostr_publish_event_job.rb create mode 100644 app/services/nostr_manager/create_zap_receipt.rb create mode 100644 spec/models/zap_spec.rb create mode 100644 spec/services/nostr_manager/create_zap_receipt_spec.rb create mode 100644 spec/services/nostr_manager/publish_zap_receipt_spec.rb diff --git a/.env.test b/.env.test index 4b683ca..cc153db 100644 --- a/.env.test +++ b/.env.test @@ -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' diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 47265ae..d600287 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -52,7 +52,7 @@ class LnurlpayController < ApplicationController return end - if params[:nostr].present? + if params[:nostr].present?# TODO && Setting.nostr_enabled? handle_zap_request amount, params[:nostr], params[:lnurl] else handle_pay_request address, amount, comment @@ -131,6 +131,9 @@ class LnurlpayController < ApplicationController 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? @@ -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"] } end end diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index d58041f..eb1c383 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -7,10 +7,14 @@ 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 + 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 send_notifications @@ -28,21 +32,16 @@ class WebhooksController < ApplicationController def process_payload @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 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 - notify = @user.preferences[:lightning_notify_sats_received] - case notify + case @user.preferences[:lightning_notify_sats_received] when "xmpp" notify_xmpp when "email" diff --git a/app/jobs/nostr_publish_event_job.rb b/app/jobs/nostr_publish_event_job.rb new file mode 100644 index 0000000..3e98d7d --- /dev/null +++ b/app/jobs/nostr_publish_event_job.rb @@ -0,0 +1,7 @@ +class NostrPublishEventJob < ApplicationJob + queue_as :nostr + + def perform(event:, relay:) + NostrManager::PublishEvent.call(event: event, relay: relay) + end +end diff --git a/app/models/zap.rb b/app/models/zap.rb index 5e2c9a8..ef839a2 100644 --- a/app/models/zap.rb +++ b/app/models/zap.rb @@ -1,3 +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 diff --git a/app/services/nostr_manager/create_zap_receipt.rb b/app/services/nostr_manager/create_zap_receipt.rb new file mode 100644 index 0000000..52fae9b --- /dev/null +++ b/app/services/nostr_manager/create_zap_receipt.rb @@ -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 diff --git a/app/services/nostr_manager/publish_event.rb b/app/services/nostr_manager/publish_event.rb index 35ab5be..a5afd83 100644 --- a/app/services/nostr_manager/publish_event.rb +++ b/app/services/nostr_manager/publish_event.rb @@ -1,17 +1,17 @@ module NostrManager class PublishEvent < NostrManagerService - def initialize(event: nil, relay: nil) - # @relay = relay - @relay = Nostr::Relay.new(url: 'ws://nostr-relay:7777', name: 'strfry') - keypair = Nostr::KeyPair.new( - private_key: Nostr::PrivateKey.new(Setting.nostr_private_key), - public_key: Nostr::PublicKey.new(Setting.nostr_public_key) - ) - @user = Nostr::User.new(keypair: keypair) - @event = @user.create_event( - kind: Nostr::EventKind::TEXT_NOTE, - content: "The time is #{Time.now.strftime('%H:%M:%S')}" - ) + def initialize(event: nil, relay_url: nil) + relay_name = relay_url.gsub(/^ws(s):\/\//, "") + @relay = Nostr::Relay.new(url: relay_url, name: relay_name) + @event = case event.class + when Nostr::Event + event + when Hash + Nostr::Event.new(**event.symbolize_keys) + # else + # TODO + # raise NotImplementedError + end @client = Nostr::Client.new end @@ -25,6 +25,7 @@ module NostrManager end client.on :error do |e| + # TODO log relay URL/name puts "Error: #{e}" puts "Closing thread..." Thread.exit @@ -36,6 +37,7 @@ module NostrManager if msg[0] == "OK" && msg[1] == event.id puts "Event published. Closing thread..." else + # TODO log relay URL/name puts "Unexpected message from relay. Closing thread..." end Thread.exit diff --git a/app/services/nostr_manager/publish_zap_receipt.rb b/app/services/nostr_manager/publish_zap_receipt.rb index a3e8102..a88e8e4 100644 --- a/app/services/nostr_manager/publish_zap_receipt.rb +++ b/app/services/nostr_manager/publish_zap_receipt.rb @@ -1,10 +1,20 @@ module NostrManager class PublishZapReceipt < NostrManagerService - def initialize(user:, zap_request:) - @user, @zap_request = user, zap_request + def initialize(zap:, delayed: true) + @zap, @delayed = zap, delayed end 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 diff --git a/app/services/nostr_manager_service.rb b/app/services/nostr_manager_service.rb index d838e72..581ab10 100644 --- a/app/services/nostr_manager_service.rb +++ b/app/services/nostr_manager_service.rb @@ -8,4 +8,15 @@ class NostrManagerService < ApplicationService 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 diff --git a/db/migrate/20240422171653_create_zaps.rb b/db/migrate/20240422171653_create_zaps.rb index 64735a1..3f33e95 100644 --- a/db/migrate/20240422171653_create_zaps.rb +++ b/db/migrate/20240422171653_create_zaps.rb @@ -3,7 +3,7 @@ class CreateZaps < ActiveRecord::Migration[7.1] create_table :zaps do |t| t.references :user, null: false, foreign_key: true t.json :request - t.json :receipt + t.json :receipt, default: nil t.text :payment_request t.bigint :amount diff --git a/spec/factories/zaps.rb b/spec/factories/zaps.rb index 2d695d0..b25f318 100644 --- a/spec/factories/zaps.rb +++ b/spec/factories/zaps.rb @@ -1,7 +1,20 @@ FactoryBot.define do factory :zap do user { nil } - request { "" } - receipt { "" } + 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 diff --git a/spec/fixtures/lndhub/incoming-zap.json b/spec/fixtures/lndhub/incoming-zap.json index c7a9f31..5b26746 100644 --- a/spec/fixtures/lndhub/incoming-zap.json +++ b/spec/fixtures/lndhub/incoming-zap.json @@ -4,7 +4,7 @@ "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\":\"\"}", + "memo": "Zap for satoshi@kosmos.org", "description_hash": "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a", "payment_request": "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu", "destination_pubkey_hex": "024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946", diff --git a/spec/models/zap_spec.rb b/spec/models/zap_spec.rb new file mode 100644 index 0000000..25fe0ef --- /dev/null +++ b/spec/models/zap_spec.rb @@ -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 diff --git a/spec/requests/lnurlpay_spec.rb b/spec/requests/lnurlpay_spec.rb index 7479e65..33ef90c 100644 --- a/spec/requests/lnurlpay_spec.rb +++ b/spec/requests/lnurlpay_spec.rb @@ -143,7 +143,7 @@ RSpec.describe "/lnurlpay", type: :request do expect(LndhubManager::CreateUserInvoice).to receive(:call) .with(user: user, payload: { amount: 21, - description: event.to_json, + description: "Zap for satoshi@kosmos.org", description_hash: "b1a9910724bc9c1b03b4eba2e2d78ac69a8ac7b244e6ff6d4e7391bf6893f26a" }) .and_return(invoice) @@ -156,7 +156,17 @@ RSpec.describe "/lnurlpay", type: :request do res = JSON.parse(response.body) 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 diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index e34bfab..65e0f5b 100644 --- a/spec/requests/webhooks_spec.rb +++ b/spec/requests/webhooks_spec.rb @@ -110,17 +110,48 @@ RSpec.describe "Webhooks", type: :request do 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 { 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 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) + 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 diff --git a/spec/services/nostr_manager/create_zap_receipt_spec.rb b/spec/services/nostr_manager/create_zap_receipt_spec.rb new file mode 100644 index 0000000..1c59730 --- /dev/null +++ b/spec/services/nostr_manager/create_zap_receipt_spec.rb @@ -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 diff --git a/spec/services/nostr_manager/publish_zap_receipt_spec.rb b/spec/services/nostr_manager/publish_zap_receipt_spec.rb new file mode 100644 index 0000000..e3a31fa --- /dev/null +++ b/spec/services/nostr_manager/publish_zap_receipt_spec.rb @@ -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 From 2d1ff29eca656872fceaf8f04c7b484fd6b8f380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 10 May 2024 13:19:09 +0200 Subject: [PATCH 18/24] Improve nostr settings, fix allowsNostr property name --- app/controllers/lnurlpay_controller.rb | 6 +++--- app/models/setting.rb | 3 ++- spec/requests/lnurlpay_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index d600287..5763262 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -18,8 +18,8 @@ class LnurlpayController < ApplicationController commentAllowed: MAX_COMMENT_CHARS } - if Setting.nostr_enabled? && Setting.nostr_private_key.present? - res[:allows_nostr] = true + if Setting.nostr_enabled? + res[:allowsNostr] = true res[:nostrPubkey] = Setting.nostr_public_key end @@ -52,7 +52,7 @@ class LnurlpayController < ApplicationController return end - if params[:nostr].present?# TODO && Setting.nostr_enabled? + if params[:nostr].present? && Setting.nostr_enabled? handle_zap_request amount, params[:nostr], params[:lnurl] else handle_pay_request address, amount, comment diff --git a/app/models/setting.rb b/app/models/setting.rb index 1ffddc1..c0aa001 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -160,7 +160,8 @@ 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 diff --git a/spec/requests/lnurlpay_spec.rb b/spec/requests/lnurlpay_spec.rb index 33ef90c..5e3b900 100644 --- a/spec/requests/lnurlpay_spec.rb +++ b/spec/requests/lnurlpay_spec.rb @@ -29,6 +29,7 @@ RSpec.describe "/lnurlpay", type: :request do before do login_as user, :scope => :user + Setting.nostr_enabled = false end describe "GET /.well-known/lnurlp/:username" do @@ -44,6 +45,21 @@ 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 @@ -104,6 +120,10 @@ RSpec.describe "/lnurlpay", type: :request do 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: { From 48041630ca8f7cba505131829c84772daeb4bb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 10 May 2024 13:57:25 +0200 Subject: [PATCH 19/24] Limit number of relays to publish zap receipts to --- app/models/setting.rb | 3 ++ .../nostr_manager/publish_zap_receipt.rb | 3 +- .../admin/settings/services/_nostr.html.erb | 12 +++++++- .../nostr_manager/publish_zap_receipt_spec.rb | 30 +++++++++++++++++-- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/models/setting.rb b/app/models/setting.rb index c0aa001..15c8755 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -169,6 +169,9 @@ class Setting < RailsSettings::Base field :nostr_public_key, type: :string, default: ENV["NOSTR_PUBLIC_KEY"].presence + field :nostr_zaps_relay_limit, type: :integer, + default: 12 + # # OpenCollective # diff --git a/app/services/nostr_manager/publish_zap_receipt.rb b/app/services/nostr_manager/publish_zap_receipt.rb index a88e8e4..3d3c4d0 100644 --- a/app/services/nostr_manager/publish_zap_receipt.rb +++ b/app/services/nostr_manager/publish_zap_receipt.rb @@ -7,8 +7,7 @@ module NostrManager def call tags = parse_tags(@zap.request_event.tags) - # TODO limit to 15 or so relays - tags[:relays].each do |relay_url| + tags[:relays].take(Setting.nostr_zaps_relay_limit).each do |relay_url| if @delayed NostrPublishEventJob.perform_later(event: @zap.receipt, relay: relay_url) else diff --git a/app/views/admin/settings/services/_nostr.html.erb b/app/views/admin/settings/services/_nostr.html.erb index c4f2182..302c52a 100644 --- a/app/views/admin/settings/services/_nostr.html.erb +++ b/app/views/admin/settings/services/_nostr.html.erb @@ -19,5 +19,15 @@ title: "Public key", description: "The corresponding public key of the accounts service" ) %> -<% end %> + +
+

Zaps

+
    + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :nostr_zaps_relay_limit, + title: "Relay limit", + description: "The maximum number of relays to publish zap receipts to" + ) %> +
+<% end %> diff --git a/spec/services/nostr_manager/publish_zap_receipt_spec.rb b/spec/services/nostr_manager/publish_zap_receipt_spec.rb index e3a31fa..452e34e 100644 --- a/spec/services/nostr_manager/publish_zap_receipt_spec.rb +++ b/spec/services/nostr_manager/publish_zap_receipt_spec.rb @@ -6,11 +6,35 @@ RSpec.describe NostrManager::PublishZapReceipt, type: :model do 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 + 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 From bc34e9c5e0772d458953763534a8a7d309b7f01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 19 May 2024 16:48:09 +0200 Subject: [PATCH 20/24] Allow CORS requests for lnurlp invoice --- app/controllers/lnurlpay_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/lnurlpay_controller.rb b/app/controllers/lnurlpay_controller.rb index 5763262..79661d9 100644 --- a/app/controllers/lnurlpay_controller.rb +++ b/app/controllers/lnurlpay_controller.rb @@ -1,6 +1,7 @@ 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 @@ -61,6 +62,12 @@ class LnurlpayController < ApplicationController 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 From fa5dc8ca4680e3b7fc4ddad446495fcd0d7784da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 19 May 2024 16:54:51 +0200 Subject: [PATCH 21/24] Fix argument name --- app/jobs/nostr_publish_event_job.rb | 4 ++-- app/services/nostr_manager/publish_zap_receipt.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/jobs/nostr_publish_event_job.rb b/app/jobs/nostr_publish_event_job.rb index 3e98d7d..f31e7b6 100644 --- a/app/jobs/nostr_publish_event_job.rb +++ b/app/jobs/nostr_publish_event_job.rb @@ -1,7 +1,7 @@ class NostrPublishEventJob < ApplicationJob queue_as :nostr - def perform(event:, relay:) - NostrManager::PublishEvent.call(event: event, relay: relay) + def perform(event:, relay_url:) + NostrManager::PublishEvent.call(event: event, relay_url: relay_url) end end diff --git a/app/services/nostr_manager/publish_zap_receipt.rb b/app/services/nostr_manager/publish_zap_receipt.rb index 3d3c4d0..22a7714 100644 --- a/app/services/nostr_manager/publish_zap_receipt.rb +++ b/app/services/nostr_manager/publish_zap_receipt.rb @@ -9,9 +9,9 @@ module NostrManager tags[:relays].take(Setting.nostr_zaps_relay_limit).each do |relay_url| if @delayed - NostrPublishEventJob.perform_later(event: @zap.receipt, relay: relay_url) + NostrPublishEventJob.perform_later(event: @zap.receipt, relay_url: relay_url) else - NostrManager::PublishEvent.call(event: @zap.receipt_event, relay: relay_url) + NostrManager::PublishEvent.call(event: @zap.receipt_event, relay_url: relay_url) end end end From 08e783d185ba28f7120e33bd2ce180f7151053de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 19 May 2024 17:07:27 +0200 Subject: [PATCH 22/24] Remove default nil values --- app/services/nostr_manager/publish_event.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/nostr_manager/publish_event.rb b/app/services/nostr_manager/publish_event.rb index a5afd83..0ad4c1e 100644 --- a/app/services/nostr_manager/publish_event.rb +++ b/app/services/nostr_manager/publish_event.rb @@ -1,6 +1,6 @@ module NostrManager class PublishEvent < NostrManagerService - def initialize(event: nil, relay_url: nil) + def initialize(event:, relay_url:) relay_name = relay_url.gsub(/^ws(s):\/\//, "") @relay = Nostr::Relay.new(url: relay_url, name: relay_name) @event = case event.class From eeb9b0a3311df5a7105d99d63a66fe0c937abe33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 21 May 2024 18:06:24 +0200 Subject: [PATCH 23/24] Improve NostrManager::PublishEvent * Use URI hostname as relay name * Log relay name/URL for every websocket event * Fix variable assignment for nostr event * Fix Sidekiq job finishing too early, by creating a new thread waiting for it to be closed from a callback --- app/services/nostr_manager/publish_event.rb | 68 ++++++++++----------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/app/services/nostr_manager/publish_event.rb b/app/services/nostr_manager/publish_event.rb index 0ad4c1e..fb6b9a5 100644 --- a/app/services/nostr_manager/publish_event.rb +++ b/app/services/nostr_manager/publish_event.rb @@ -1,50 +1,50 @@ module NostrManager class PublishEvent < NostrManagerService def initialize(event:, relay_url:) - relay_name = relay_url.gsub(/^ws(s):\/\//, "") + relay_name = URI.parse(relay_url).host @relay = Nostr::Relay.new(url: relay_url, name: relay_name) - @event = case event.class - when Nostr::Event - event - when Hash - Nostr::Event.new(**event.symbolize_keys) - # else - # TODO - # raise NotImplementedError - end + + 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}]" - client.on :connect do - puts "Connected to #{relay.url}" - puts "Publishing #{event.id}..." - client.publish event - end - - client.on :error do |e| - # TODO log relay URL/name - puts "Error: #{e}" - puts "Closing thread..." - Thread.exit - end - - client.on :message do |m| - puts "Message: #{m}" - msg = JSON.parse(m) rescue [] - if msg[0] == "OK" && msg[1] == event.id - puts "Event published. Closing thread..." - else - # TODO log relay URL/name - puts "Unexpected message from relay. Closing thread..." + thread = Thread.new do + client.on :connect do + puts "#{log_prefix} Publishing #{event.id}..." + client.publish event end - Thread.exit + + 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 + 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 - puts "Connecting to #{relay.url}..." - client.connect relay + thread.join end end end From 231dfc8404011234ca281ae4b40e7126f544ef7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 21 May 2024 18:28:46 +0200 Subject: [PATCH 24/24] Log correct publish status --- app/services/nostr_manager/publish_event.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/nostr_manager/publish_event.rb b/app/services/nostr_manager/publish_event.rb index fb6b9a5..42c995d 100644 --- a/app/services/nostr_manager/publish_event.rb +++ b/app/services/nostr_manager/publish_event.rb @@ -32,7 +32,7 @@ module NostrManager client.on :message do |m| puts "#{log_prefix} Message: #{m}" msg = JSON.parse(m) rescue [] - if msg[0] == "OK" && msg[1] == event.id + 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..."