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

Commit

Permalink
Mint: some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ybensacq committed Nov 1, 2024
1 parent c4056e4 commit 4fa4b22
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 14 deletions.
3 changes: 3 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ config :cashubrew, dev_routes: true
# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

# Use mock
config :cashubrew, :lightning_service, Cashubrew.LightningMock

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20
Expand Down
10 changes: 7 additions & 3 deletions lib/cashubrew/NUTs/NUT-04/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ defmodule Cashubrew.Nuts.Nut04.Impl do

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

{payment_request, _payment_hash} = LightningNetworkService.create_invoice!(amount, unit)
# Mock for dev
lightning_service = Application.get_env(:cashubrew, :lightning_service, LightningNetworkService)
{:ok, payment_request} = lightning_service.create_invoice!(amount, unit)
amount = if is_integer(amount), do: amount, else: String.to_integer(amount)

# 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.
quote_id = Ecto.UUID.bingenerate()
quote_id = :os.system_time(:millisecond)

# 1 hour expiry
expiry = :os.system_time(:second) + 3600

Schema.MintQuote.create!(repo, %{
id: quote_id,
payment_request: payment_request,
amount: amount,
unit: unit,
expiry: expiry,
# Unpaid
state: <<0>>
Expand Down
1 change: 1 addition & 0 deletions lib/cashubrew/NUTs/NUT-04/serde.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ defmodule Cashubrew.Nuts.Nut04.Serde.PostMintBolt11Response do
The body of the post mint response
"""
@enforce_keys [:signatures]
@derive [Jason.Encoder]
defstruct [:signatures]
end
2 changes: 1 addition & 1 deletion lib/cashubrew/lightning/lightning_network_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Cashubrew.Lightning.LightningNetworkService do
{payment_request, payment_hash}

{:error, reason} ->
raise reason
raise RuntimeError, "Failed to create invoice: #{inspect(reason)}"
end
end

Expand Down
5 changes: 5 additions & 0 deletions lib/cashubrew/mocks/lightning_mock.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Cashubrew.LightningMock do
def create_invoice!(amount, unit) do
{:ok, "mock_payment_request_#{amount}_#{unit}"}
end
end
4 changes: 2 additions & 2 deletions lib/cashubrew/schema/mint_quote.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Cashubrew.Schema.MintQuote do
use Ecto.Schema
import Ecto.Changeset

@primary_key {:id, :binary_id, autogenerate: false}
@primary_key {:id, :id, autogenerate: false}
schema "mint_quotes" do
field(:payment_request, :string)
field(:amount, :integer)
Expand All @@ -23,7 +23,7 @@ defmodule Cashubrew.Schema.MintQuote do
def changeset(quote, attrs) do
quote
|> cast(attrs, [:id, :payment_request, :amount, :unit, :expiry, :state])
|> validate_required([:id, :payment_request, :amout, :unit, :expiry, :state])
|> validate_required([:id, :payment_request, :amount, :unit, :expiry, :state])
|> validate_inclusion(:state, [<<0>>, <<1>>, <<2>>, <<128>>, <<129>>, <<130>>])
end

Expand Down
23 changes: 16 additions & 7 deletions lib/cashubrew/web/controllers/mint_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,25 @@ defmodule Cashubrew.Web.MintController do
raise "UnsuportedMethod"
end

%Nut04.Serde.PostMintQuoteBolt11Request{
amount: amount,
unit: unit,
description: _description
} = params["body"]
unit = params["unit"]

if !unit do
raise "NoUnit"
end

amount = params["amount"]

if !amount do
raise "NoUnit"
end

res = Nut04.Impl.create_mint_quote!(amount, unit)
json(conn, struct(Nut04.Serde.PostMintBolt11Response, Map.put(res, :state, "UNPAID")))
json(conn, struct(Nut04.Serde.PostMintBolt11Response, Map.merge(res, %{signatures: [], state: "UNPAID"})))
rescue
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
e in RuntimeError ->
conn
|> put_status(:bad_request)
|> json(%{error: e.message})
end

def get_mint_quote(conn, %{"quote_id" => quote_id, "method" => method}) do
Expand Down
3 changes: 2 additions & 1 deletion priv/repo/migrations/20240918113122_create_mint_quote.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ defmodule Cashubrew.Repo.Migrations.CreateMintQuote do
create table(:mint_quotes) do
add :amount, :integer, null: false
add :payment_request, :text, null: false
add :state, :string, default: "UNPAID", null: false
add :state, :binary, null: false, default: fragment("decode('00', 'hex')")
add :expiry, :integer, null: false
add :description, :string
add :payment_hash, :string
add :unit, :string

timestamps()
end
Expand Down

0 comments on commit 4fa4b22

Please sign in to comment.