class LnurlpayController < ApplicationController before_action :find_user_by_address MIN_SATS = 100 MAX_SATS = 1_000_000 MAX_COMMENT_CHARS = 100 def index render json: { status: "OK", callback: "https://accounts.kosmos.org/lnurlpay/#{@user.address}/invoice", tag: "payRequest", maxSendable: MAX_SATS * 1000, # msat minSendable: MIN_SATS * 1000, # msat metadata: metadata(@user.address), commentAllowed: MAX_COMMENT_CHARS } end def invoice amount = params[:amount].to_i / 1000 # msats address = params[:address] comment = params[:comment] || "" if !valid_amount?(amount) render json: { status: "ERROR", reason: "Invalid amount" } return end if !valid_comment?(comment) render json: { status: "ERROR", reason: "Comment too long" } return end memo = "Sats for #{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 find_user_by_address address = params[:address].split("@") @user = User.where(cn: address.first, ou: address.last).first http_status :not_found if @user.nil? 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_comment?(comment) comment.length <= MAX_COMMENT_CHARS end end