From aedaabc7ba86a25f173c995f41389c573548bbf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Mon, 12 Jun 2023 18:18:06 +0200 Subject: [PATCH 1/4] Offer lnurl-pay QR codes for download on the Lightning page --- Gemfile | 3 + Gemfile.lock | 3 + .../services/lightning_controller.rb | 57 ++++++++++++++++--- app/views/services/lightning/index.html.erb | 20 ++++++- config/routes.rb | 1 + 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index dad66b1..2985570 100644 --- a/Gemfile +++ b/Gemfile @@ -58,6 +58,9 @@ gem 'discourse_api' gem "sentry-ruby" gem "sentry-rails" +# Lightning +gem "lnurl" + group :development, :test do # Use sqlite3 as the database for Active Record gem 'sqlite3', '~> 1.4' diff --git a/Gemfile.lock b/Gemfile.lock index 6b33fa8..211008a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -180,6 +180,8 @@ GEM listen (3.7.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) + lnurl (1.0.1) + bech32 (~> 1.1) lockbox (1.1.0) loofah (2.19.0) crass (~> 1.0.2) @@ -409,6 +411,7 @@ DEPENDENCIES letter_opener letter_opener_web listen (~> 3.2) + lnurl lockbox net-ldap pagy (~> 6.0, >= 6.0.2) diff --git a/app/controllers/services/lightning_controller.rb b/app/controllers/services/lightning_controller.rb index 44a4589..e38445b 100644 --- a/app/controllers/services/lightning_controller.rb +++ b/app/controllers/services/lightning_controller.rb @@ -1,4 +1,5 @@ require "rqrcode" +require "lnurl" class Services::LightningController < ApplicationController before_action :authenticate_user! @@ -8,9 +9,55 @@ class Services::LightningController < ApplicationController def index @wallet_url = "lndhub://#{current_user.ln_account}:#{current_user.ln_password}@#{ENV['LNDHUB_PUBLIC_URL']}" + initialize_lndhub_qr_code + end - qrcode = RQRCode::QRCode.new(@wallet_url) - @svg = qrcode.as_svg( + def transactions + @transactions = fetch_transactions + end + + def qr_lnurlp + lnurlp_url = "https://kosmos.org/.well-known/lnurlp/#{current_user.cn}" + lnurlp_bech32 = Lnurl.new(lnurlp_url).to_bech32 + qr_code = RQRCode::QRCode.new("lightning:" + lnurlp_bech32) + + if params[:format] == "svg" + qr_svg = qr_code.as_svg( + color: "000", + shape_rendering: "crispEdges", + module_size: 6, + standalone: true, + use_path: true, + svg_attributes: { + class: 'inline-block' + } + ) + send_data( + qr_svg, + filename: "bitcoin-lightning-#{current_user.address}.svg", + type: "image/svg+xml" + ) + elsif params[:format] == "png" + qr_png = qr_code.as_png( + fill: "white", + color: "black", + size: 1024, + ) + send_data( + qr_png, + filename: "bitcoin-lightning-#{current_user.address}.png", + type: "image/png" + ) + else + http_status :not_found + end + end + + private + + def initialize_lndhub_qr_code + qr_code = RQRCode::QRCode.new(@wallet_url) + @lndhub_qr_svg = qr_code.as_svg( color: "000", shape_rendering: "crispEdges", module_size: 6, @@ -22,12 +69,6 @@ class Services::LightningController < ApplicationController ) end - def transactions - @transactions = fetch_transactions - end - - private - def authenticate_with_lndhub(options={}) if session[:ln_auth_token].present? && !options[:force_reauth] @ln_auth_token = session[:ln_auth_token] diff --git a/app/views/services/lightning/index.html.erb b/app/views/services/lightning/index.html.erb index fda9558..9eff3ce 100644 --- a/app/views/services/lightning/index.html.erb +++ b/app/views/services/lightning/index.html.erb @@ -39,7 +39,7 @@

@@ -88,6 +88,24 @@

+ +
+

QR Code for Donations/Tips

+

+ You can print out or publish a QR code for people to scan with their + wallet apps, so they can send you sats without a direct personal + interaction (for example at a concert, or on your website). +

+

+ <%= link_to "Download SVG file", + qr_lnurlp_services_lightning_index_path(format: "svg"), + class: "btn-md btn-blue w-full sm:w-auto"%> + or + <%= link_to "Download PNG file", + qr_lnurlp_services_lightning_index_path(format: "png"), + class: "btn-md btn-blue w-full sm:w-auto"%> +

+
<% end %>