From 884070a3cbfe96ecaabde6a3f722fe14c09847f7 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Sun, 21 Nov 2021 16:47:55 -0600 Subject: [PATCH] Show available balance on wallet page --- .env.example | 1 + app/controllers/wallet_controller.rb | 34 +++++++++++++++++++++++++++- app/views/wallet/index.html.erb | 20 ++++++++++------ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index dc710a1..77244a3 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ EJABBERD_API_URL='https://xmpp.kosmos.org/api' LNDHUB_API_URL='http://10.1.1.163:3023' +LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org' diff --git a/app/controllers/wallet_controller.rb b/app/controllers/wallet_controller.rb index 231ecae..c74a4f3 100644 --- a/app/controllers/wallet_controller.rb +++ b/app/controllers/wallet_controller.rb @@ -2,11 +2,12 @@ require "rqrcode" class WalletController < ApplicationController before_action :require_user_signed_in + before_action :authenticate_with_lndhub def index @current_section = :wallet - @wallet_url = "lndhub://#{current_user.ln_login}:#{current_user.ln_password}@https://lndhub.kosmos.org" + @wallet_url = "lndhub://#{current_user.ln_login}:#{current_user.ln_password}@#{ENV['LNDHUB_PUBLIC_URL']}" qrcode = RQRCode::QRCode.new(@wallet_url) @svg = qrcode.as_svg( @@ -16,6 +17,37 @@ class WalletController < ApplicationController standalone: true, use_path: true ) + + @balance = fetch_balance rescue nil end + private + + def authenticate_with_lndhub + if session["ln_auth_token"].present? + @ln_auth_token = session["ln_auth_token"] + else + res = Faraday.post("#{ENV["LNDHUB_API_URL"]}/auth?type=auth", + { login: current_user.ln_login, password: current_user.ln_password }.to_json, + "Content-Type" => "application/json", + "Accept" => "application/json") + + credentials = JSON.parse(res.body) + session["ln_auth_token"] = credentials["access_token"] + @ln_auth_token = credentials["access_token"] + end + rescue + # TODO add exception tracking + end + + def fetch_balance + res = Faraday.get("#{ENV["LNDHUB_API_URL"]}/balance", {}, { + "Content-Type" => "application/json", + "Accept" => "application/json", + "Authorization" => "Bearer #{@ln_auth_token}" + }) + + data = JSON.parse(res.body) + data["BTC"]["AvailableBalance"] + end end diff --git a/app/views/wallet/index.html.erb b/app/views/wallet/index.html.erb index 7b03eb2..fda7bd6 100644 --- a/app/views/wallet/index.html.erb +++ b/app/views/wallet/index.html.erb @@ -1,14 +1,20 @@ -
-

Wallet

-

- Send and receive BTC via the Lightning Network. +

+
+

Wallet

+

+ Send and receive BTC via the Lightning Network. +

+
+

+ <% if @balance %> + <%= @balance %> sats
+ Available balance + <% end %>

-

- Blue Wallet -

+

Blue Wallet

You can connect <%= link_to "Blue Wallet", "https://bluewallet.io",