From 5f1a3427e6c409c6e5417e5176ad62584901d872 Mon Sep 17 00:00:00 2001 From: MSGhais Date: Sat, 5 Oct 2024 14:22:21 +0200 Subject: [PATCH 1/3] fix mock invoice + lnbits invoice --- .env.example | 3 +- README.md | 10 ++++ lib/cashubrew/core/mint.ex | 46 ++++++++++++------- .../lightning/lightning_network_service.ex | 21 ++++++--- lib/cashubrew/lightning/ln_bits.ex | 4 -- lib/cashubrew/lightning/ln_lib.ex | 21 --------- .../web/controllers/mint_controller.ex | 4 +- 7 files changed, 59 insertions(+), 50 deletions(-) delete mode 100644 lib/cashubrew/lightning/ln_lib.ex diff --git a/.env.example b/.env.example index e0e0b6f..243aa0f 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ LN_BITS_API_ENDPOINT=https://your-lnbits-node -LN_BITS_API_KEY=ADMIN_API_KEY \ No newline at end of file +LN_BITS_API_KEY=ADMIN_API_KEY +use_mock_env_ln=true \ No newline at end of file diff --git a/README.md b/README.md index 000ef8c..69dcfe5 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,16 @@ Launch your Cashubrew server with a single command: mix phx.server ``` +## Environnement vars for the backend + +Create an .env to setup your LN_BITS or Mock LN mode +``` +LN_BITS_API_ENDPOINT=https://your-lnbits-node +LN_BITS_API_KEY=ADMIN_API_KEY +use_mock_env_ln=true + +``` + 🌈 Voilà! Your mint is now live at `http://localhost:4000`. ## 🧙‍♂️ Wallet CLI Magic diff --git a/lib/cashubrew/core/mint.ex b/lib/cashubrew/core/mint.ex index 7eb8a91..1fbe60a 100644 --- a/lib/cashubrew/core/mint.ex +++ b/lib/cashubrew/core/mint.ex @@ -125,9 +125,17 @@ defmodule Cashubrew.Mint do def handle_call({:create_mint_quote, amount, description}, _from, state) do repo = Application.get_env(:cashubrew, :repo) + use_mock_env_ln = System.get_env("use_mock_env_ln") - case LightningNetworkService.create_invoice(amount, description) do - {:ok, payment_request, _payment_hash} -> + result = + if use_mock_env_ln == "true" do + MockLightningNetworkService.create_invoice(amount, description) + else + LightningNetworkService.create_invoice(amount, description) + end + + case result do + {:ok, payment_request, payment_hash} -> # 1 hour expiry expiry = :os.system_time(:second) + 3600 @@ -135,21 +143,27 @@ defmodule Cashubrew.Mint do amount: amount, payment_request: payment_request, expiry: expiry, - description: description - # payment_hash: _payment_hash, + description: description, + payment_hash: payment_hash } - case repo.insert(MintQuote.changeset(%MintQuote{}, attrs)) do - {:ok, quote} -> - {:reply, {:ok, quote}, state} - - {:error, changeset} -> - {:reply, {:error, changeset}, state} - end + insert_quote(repo, attrs, state) {:error, reason} -> {:reply, {:error, reason}, state} end + + end + + # Helper function to insert the quote into the database + defp insert_quote(repo, attrs, state) do + case repo.insert(MintQuote.changeset(%MintQuote{}, attrs)) do + {:ok, quote} -> + {:reply, {:ok, quote}, state} + + {:error, changeset} -> + {:reply, {:error, changeset}, state} + end end def handle_call({:get_mint_quote, quote_id}, _from, state) do @@ -258,10 +272,11 @@ defmodule Cashubrew.Mint do end end + # TODO def handle_call({:create_melt_tokens, quote_id, inputs}, _from, state) do repo = Application.get_env(:cashubrew, :repo) + IO.puts("inputs: #{inputs}") - # TODO # Verify quote_id {:ok, melt_find} = Cashubrew.Query.MeltTokens.get_melt_by_quote_id!(quote_id) @@ -277,19 +292,18 @@ defmodule Cashubrew.Mint do fee_reserve = 0 # Create and Saved melt quote + expiry = :os.system_time(:second) + 3600 attrs = %{ # quote_id request: quote_id, unit: quote_id, amount: 0, - fee_reserve: 0, - expiry: 0, + fee_reserve: fee_reserve, + expiry: expiry, request_lookup_id: quote_id } - expiry = :os.system_time(:second) + 3600 - case repo.insert(MeltTokens.changeset(%MeltTokens{}, attrs)) do {:ok, melt_quote} -> {:reply, {:ok, melt_quote}, state} diff --git a/lib/cashubrew/lightning/lightning_network_service.ex b/lib/cashubrew/lightning/lightning_network_service.ex index b844aa4..e790c86 100644 --- a/lib/cashubrew/lightning/lightning_network_service.ex +++ b/lib/cashubrew/lightning/lightning_network_service.ex @@ -21,16 +21,25 @@ defmodule Cashubrew.Lightning.LightningNetworkService do GenServer.call(__MODULE__, {:check_payment, payment_hash}) end - def handle_call({:create_invoice, amount, _description}, _from, state) do - unit_input = "sat" + def handle_call({:create_invoice, amount, description}, _from, state) do + + amount = + case Integer.parse(amount) do + {int, _} -> int + :error -> amount # If parsing fails, assume it's already an integer + end attributes = %{ - out: "false", - amount: amount, - unit_input: unit_input + out: false, # out: false means it is an incoming payment request + amount: amount, # amount in satoshis + memo: description, # description/memo for the invoice + # unit_input: unit_input, + # expiry: 0, + # internal: false, + # webhook: "", } - case LNBitsApi.post_data("api/v1/payments", attributes) do + case LNBitsApi.post_data("/api/v1/payments", attributes) do {:ok, response_body} -> IO.puts("Success create in: #{response_body}") diff --git a/lib/cashubrew/lightning/ln_bits.ex b/lib/cashubrew/lightning/ln_bits.ex index f5a4b0c..aca9b5a 100644 --- a/lib/cashubrew/lightning/ln_bits.ex +++ b/lib/cashubrew/lightning/ln_bits.ex @@ -4,15 +4,11 @@ defmodule Cashubrew.LNBitsApi do """ Dotenv.load() - @api_endpoint System.get_env("LN_BITS_API_ENDPOINT") - @api_key System.get_env("LN_BITS_API_KEY") - def fetch_data(path, attributes) do api_base_url = System.get_env("LN_BITS_API_ENDPOINT") api_key = System.get_env("LN_BITS_API_KEY") headers = [{"X-Api-Key", "#{api_key}"}] full_url = "#{api_base_url}#{path}" - case HTTPoison.get(full_url, headers) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, body} diff --git a/lib/cashubrew/lightning/ln_lib.ex b/lib/cashubrew/lightning/ln_lib.ex deleted file mode 100644 index 848fbb1..0000000 --- a/lib/cashubrew/lightning/ln_lib.ex +++ /dev/null @@ -1,21 +0,0 @@ -defmodule Cashubrew.LightningLib do - @moduledoc """ - Mock Lightning Network Service for testing purposes. - """ - use GenServer - - def start_link(_) do - GenServer.start_link(__MODULE__, %{}, name: __MODULE__) - end - - def init(_) do - {:ok, %{invoices: %{}}} - end - - def decode_invoice(payment_hash) do - GenServer.call(__MODULE__, {:decode_invoice, payment_hash}) - end - - def handle_call({:decode_invoice, invoice}, _from, state) do - end -end diff --git a/lib/cashubrew/web/controllers/mint_controller.ex b/lib/cashubrew/web/controllers/mint_controller.ex index e7e0446..0df265a 100644 --- a/lib/cashubrew/web/controllers/mint_controller.ex +++ b/lib/cashubrew/web/controllers/mint_controller.ex @@ -165,9 +165,9 @@ defmodule Cashubrew.Web.MintController do end def create_mint_quote(conn, %{"amount" => amount, "unit" => "sat"} = params) do - description = Map.get(params, "description") + unit = Map.get(params, "unit") - case Mint.create_mint_quote(amount, description) do + case Mint.create_mint_quote(amount, unit) do {:ok, quote} -> conn |> put_status(:created) From 92adda8f4fcb84d186ceaa72e22441613c0be831 Mon Sep 17 00:00:00 2001 From: MSGhais Date: Sat, 5 Oct 2024 14:31:05 +0200 Subject: [PATCH 2/3] format --- lib/cashubrew/core/mint.ex | 2 +- .../lightning/lightning_network_service.ex | 13 ++++++++----- lib/cashubrew/lightning/ln_bits.ex | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/cashubrew/core/mint.ex b/lib/cashubrew/core/mint.ex index 1fbe60a..02f7f92 100644 --- a/lib/cashubrew/core/mint.ex +++ b/lib/cashubrew/core/mint.ex @@ -126,6 +126,7 @@ defmodule Cashubrew.Mint do def handle_call({:create_mint_quote, amount, description}, _from, state) do repo = Application.get_env(:cashubrew, :repo) use_mock_env_ln = System.get_env("use_mock_env_ln") + IO.puts("use_mock_env_ln #{use_mock_env_ln}") result = if use_mock_env_ln == "true" do @@ -152,7 +153,6 @@ defmodule Cashubrew.Mint do {:error, reason} -> {:reply, {:error, reason}, state} end - end # Helper function to insert the quote into the database diff --git a/lib/cashubrew/lightning/lightning_network_service.ex b/lib/cashubrew/lightning/lightning_network_service.ex index e790c86..4d77ec3 100644 --- a/lib/cashubrew/lightning/lightning_network_service.ex +++ b/lib/cashubrew/lightning/lightning_network_service.ex @@ -22,17 +22,20 @@ defmodule Cashubrew.Lightning.LightningNetworkService do end def handle_call({:create_invoice, amount, description}, _from, state) do - amount = case Integer.parse(amount) do {int, _} -> int - :error -> amount # If parsing fails, assume it's already an integer + # If parsing fails, assume it's already an integer + :error -> amount end attributes = %{ - out: false, # out: false means it is an incoming payment request - amount: amount, # amount in satoshis - memo: description, # description/memo for the invoice + # out: false means it is an incoming payment request + out: false, + # amount in satoshis + amount: amount, + # description/memo for the invoice + memo: description # unit_input: unit_input, # expiry: 0, # internal: false, diff --git a/lib/cashubrew/lightning/ln_bits.ex b/lib/cashubrew/lightning/ln_bits.ex index aca9b5a..683a210 100644 --- a/lib/cashubrew/lightning/ln_bits.ex +++ b/lib/cashubrew/lightning/ln_bits.ex @@ -9,6 +9,7 @@ defmodule Cashubrew.LNBitsApi do api_key = System.get_env("LN_BITS_API_KEY") headers = [{"X-Api-Key", "#{api_key}"}] full_url = "#{api_base_url}#{path}" + case HTTPoison.get(full_url, headers) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, body} From 1f3eec9a83bbf076dfc6e72f9b96a27fa3e401fe Mon Sep 17 00:00:00 2001 From: MSGhais Date: Mon, 7 Oct 2024 12:46:27 +0200 Subject: [PATCH 3/3] add ln config with MOCK_LN --- .env.example | 2 +- README.md | 2 +- config/dev.exs | 7 +++++++ config/prod.exs | 7 +++++++ config/test.exs | 7 +++++++ lib/cashubrew/core/mint.ex | 17 +++++------------ 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index 243aa0f..1184d2a 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ LN_BITS_API_ENDPOINT=https://your-lnbits-node LN_BITS_API_KEY=ADMIN_API_KEY -use_mock_env_ln=true \ No newline at end of file +MOCK_LN=true \ No newline at end of file diff --git a/README.md b/README.md index 69dcfe5..0e08254 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ Create an .env to setup your LN_BITS or Mock LN mode ``` LN_BITS_API_ENDPOINT=https://your-lnbits-node LN_BITS_API_KEY=ADMIN_API_KEY -use_mock_env_ln=true +MOCK_LN=true ``` diff --git a/config/dev.exs b/config/dev.exs index 76ee36b..1b6f241 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -42,3 +42,10 @@ config :cashubrew, Cashubrew.Repo, config :cashubrew, :repo, Cashubrew.Repo config :cashubrew, ecto_repos: [Cashubrew.Repo] + +# Use mock repo if MOCK_LN environment variable is set to "true" +if System.get_env("MOCK_LN") == "true" do + config :cashubrew, :ln, Cashubrew.Lightning.MockLightningNetworkService +else + config :cashubrew, :ln, Cashubrew.Lightning.LightningNetworkService +end diff --git a/config/prod.exs b/config/prod.exs index db37ff8..e198307 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -21,3 +21,10 @@ config :cashubrew, Cashubrew.Web.Endpoint, secret_key_base: System.get_env("SECR # Do not print debug messages in production config :logger, level: :info + +# Use mock ln if MOCK_LN environment variable is set to "true" +if System.get_env("MOCK_LN") == "true" do + config :cashubrew, :ln, Cashubrew.Lightning.MockLightningNetworkService +else + config :cashubrew, :ln, Cashubrew.Lightning.LightningNetworkService +end diff --git a/config/test.exs b/config/test.exs index 2e61dfd..54ae9a6 100644 --- a/config/test.exs +++ b/config/test.exs @@ -27,3 +27,10 @@ else config :cashubrew, :repo, Cashubrew.Repo end + +# Use mock LN if MOCK_LN environment variable is set to "true" +if System.get_env("MOCK_LN") == "true" do + config :cashubrew, :ln, Cashubrew.Lightning.MockLightningNetworkService +else + config :cashubrew, :ln, Cashubrew.Lightning.LightningNetworkService +end diff --git a/lib/cashubrew/core/mint.ex b/lib/cashubrew/core/mint.ex index 02f7f92..7aa4533 100644 --- a/lib/cashubrew/core/mint.ex +++ b/lib/cashubrew/core/mint.ex @@ -6,9 +6,6 @@ defmodule Cashubrew.Mint do use GenServer alias Cashubrew.Cashu.BlindSignature alias Cashubrew.Crypto.BDHKE - alias Cashubrew.Lightning.LightningNetworkService - alias Cashubrew.Lightning.MockLightningNetworkService - alias Cashubrew.LNBitsApi alias Cashubrew.Query.MeltTokens alias Cashubrew.Schema.{Key, Keyset, MeltQuote, MeltTokens, MintConfiguration, MintQuote} @@ -125,15 +122,11 @@ defmodule Cashubrew.Mint do def handle_call({:create_mint_quote, amount, description}, _from, state) do repo = Application.get_env(:cashubrew, :repo) - use_mock_env_ln = System.get_env("use_mock_env_ln") - IO.puts("use_mock_env_ln #{use_mock_env_ln}") - - result = - if use_mock_env_ln == "true" do - MockLightningNetworkService.create_invoice(amount, description) - else - LightningNetworkService.create_invoice(amount, description) - end + ln = Application.get_env(:cashubrew, :ln) + + IO.inspect(System.get_env("MOCK_LN"), label: "MOCK_LN") + IO.inspect(Application.get_env(:cashubrew, :ln), label: "Configured LN Module") + result = ln.create_invoice(amount, description) case result do {:ok, payment_request, payment_hash} ->