Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

feat: apply the try/rescue design for all routes in the controler #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
run: mix format --check-formatted

- name: Run Credo
run: mix credo --ignore todo
run: mix credo --ignore todo,aliasusage

- name: Check for security vulnerabilities
run: mix sobelow --config
12 changes: 0 additions & 12 deletions lib/cashubrew/NUTs/NUT-01/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,4 @@ defmodule Cashubrew.Nuts.Nut01.Impl do

keysets_with_keys
end

def keys_for_keyset_id(id) do
repo = Application.get_env(:cashubrew, :repo)
keyset = Mint.get_keyset(repo, id)

if keyset do
keys = Mint.get_keys_for_keyset(repo, id)
%{id: keyset.id, unit: keyset.unit, keys: keys}
else
raise "KeysetDoesNotExist"
end
end
end
23 changes: 23 additions & 0 deletions lib/cashubrew/NUTs/NUT-02/impl.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Cashubrew.Nuts.Nut02.Impl do
@moduledoc """
The mint logic for handling Nut02
"""
alias Cashubrew.Mint

def keysets do
repo = Application.get_env(:cashubrew, :repo)
Mint.get_all_keysets(repo)
end

def keys_for_keyset_id!(id) do
repo = Application.get_env(:cashubrew, :repo)
keyset = Mint.get_keyset(repo, id)

if keyset do
keys = Mint.get_keys_for_keyset(repo, id)
%{id: keyset.id, unit: keyset.unit, keys: keys}
else
raise "KeysetDoesNotExist"
end
end
end
4 changes: 2 additions & 2 deletions lib/cashubrew/NUTs/NUT-04/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ defmodule Cashubrew.Nuts.Nut04.Impl do
alias Cashubrew.Lightning.LightningNetworkService
alias Cashubrew.Schema

def create_mint_quote(amount, unit) do
def create_mint_quote!(amount, unit) do
repo = Application.get_env(:cashubrew, :repo)

{payment_request, _payment_hash} = LightningNetworkService.create_invoice!(amount, unit)

# Note: quote is a unique and random id generated by the mint to internally look up the payment state.
# quote MUST remain a secret between user and mint and MUST NOT be derivable from the payment request.
# A third party who knows the quote ID can front-run and steal the tokens that this operation mints.
#
#
# For more simplicity it is used as the database ID for both the items in PendingInvoice and the MintQuote tables.
quote_id = Ecto.UUID.bingenerate()

Expand Down
2 changes: 1 addition & 1 deletion lib/cashubrew/NUTs/NUT-06/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ defmodule Cashubrew.Nuts.Nut06.Info do
@moduledoc """
Implementation and structs of the NUT-06
"""
alias Cashubrew.Mint
alias Cashubrew.Nuts.Nut00
alias Cashubrew.Nuts.Nut01
alias Cashubrew.Nuts.Nut02
alias Cashubrew.Nuts.Nut03
alias Cashubrew.Nuts.Nut04
alias Cashubrew.Mint

defstruct [
:name,
Expand Down
2 changes: 1 addition & 1 deletion lib/cashubrew/core/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ defmodule Cashubrew.Mint do
repo.all(Schema.Keyset)
end

def get_keysets(repo, keyset_id) do
def get_keyset(repo, keyset_id) do
repo.get(Schema.Keyset, keyset_id)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cashubrew/schema/keyset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ defmodule Cashubrew.Schema.Keyset do
end
end

# Todo: create rotate function
# Todo: create rotate function
end
79 changes: 32 additions & 47 deletions lib/cashubrew/web/controllers/mint_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,37 @@ defmodule Cashubrew.Web.MintController do
def info(conn, _params) do
info = Nut06.Info.info()
json(conn, info)
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

def keysets(conn, _params) do
repo = Application.get_env(:cashubrew, :repo)
keysets = Mint.get_all_keysets(repo)

response = Nut02.Serde.GetKeysetsResponse.from_keysets(keysets)

json(conn, response)
keysets = Nut02.Impl.keysets()
json(conn, Nut02.Serde.GetKeysetsResponse.from_keysets(keysets))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

def keys(conn, _params) do
keysets_with_keys = Nut01.Impl.active_keysets_with_keys()

response = Nut01.Serde.GetKeysResponse.from_keysets(keysets_with_keys)

json(conn, response)
json(conn, Nut01.Serde.GetKeysResponse.from_keysets(keysets_with_keys))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

def keys_for_keyset(conn, %{"keyset_id" => keyset_id}) do
keyset_with_keys = Nut01.Impl.keys_for_keyset_id(keyset_id)

if keyset_with_keys do
response =
Nut01.Serde.GetKeysResponse.from_keysets([keyset_with_keys])

json(conn, response)
else
# TODO: use proper error
# https://cashubtc.github.io/nuts/00/#errors
conn
|> put_status(:not_found)
|> json(%{error: "Keyset not found"})
end
keyset_with_keys = Nut02.Impl.keys_for_keyset_id!(keyset_id)
json(conn, Nut01.Serde.GetKeysResponse.from_keysets([keyset_with_keys]))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

def swap(conn, params) do
%Nut03.Serde.PostSwapRequest{inputs: proofs, outputs: blinded_messages} = params["body"]

signatures = Nut03.Impl.swap!(proofs, blinded_messages)

json(conn, %Nut03.Serde.PostSwapResponse{signatures: signatures})
end

defp validate_blinded_messages(outputs) do
blinded_messages =
Enum.map(outputs, fn output ->
Nut00.BlindedMessage.from_map(output)
end)

{:ok, blinded_messages}
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

def create_mint_quote(conn, params) do
Expand All @@ -71,21 +51,26 @@ defmodule Cashubrew.Web.MintController do
description: _description
} = params["body"]

try do
res = Nut04.Impl.create_mint_quote(amount, unit)
json(conn, struct(Nut04.Serde.PostMintBolt11Response, %{res | state: "UNPAID"}))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end
res = Nut04.Impl.create_mint_quote!(amount, unit)
json(conn, struct(Nut04.Serde.PostMintBolt11Response, %{res | state: "UNPAID"}))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

def get_mint_quote(conn, %{"quote_id" => quote_id}) do
try do
res = Nut04.Impl.get_mint_quote(quote_id)
json(conn, struct(Nut04.Serde.PostMintBolt11Response, res))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end
res = Nut04.Impl.get_mint_quote(quote_id)
json(conn, struct(Nut04.Serde.PostMintBolt11Response, res))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

defp validate_blinded_messages(outputs) do
blinded_messages =
Enum.map(outputs, fn output ->
Nut00.BlindedMessage.from_map(output)
end)

{:ok, blinded_messages}
end

def mint_tokens(conn, %{"quote" => quote_id, "outputs" => outputs}) do
Expand Down
Loading