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

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Nov 7, 2024
1 parent a795f8a commit 7bae3f0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
4 changes: 0 additions & 4 deletions integration-tests/nut05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ pub async fn melt_quote_ok() {
assert_matches!(
melt_quote,
MeltQuote {
<<<<<<< HEAD
ref id,
=======
id,
>>>>>>> b96d0cf (test: add integration test for 05 mint quote)
unit: _,
amount,
request: _,
Expand Down
11 changes: 8 additions & 3 deletions lib/cashubrew/NUTs/NUT-04/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ defmodule Cashubrew.Nuts.Nut04.Impl do
alias Cashubrew.Schema

def create_mint_quote!(amount, unit, description) do
# Todo: sanitize earlier?
# amount = if is_integer(amount), do: amount, else: String.to_integer(amount)

repo = Application.get_env(:cashubrew, :repo)
lnd_client = Application.get_env(:cashubrew, :lnd_client)

{payment_request, _payment_hash} =
Cashubrew.LightingNetwork.Lnd.create_invoice!(amount, unit, description)
{:ok, payment_request} = lnd_client.create_invoice!(amount, unit, description)

# 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
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
29 changes: 18 additions & 11 deletions lib/cashubrew/web/controllers/mint_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,30 @@ defmodule Cashubrew.Web.MintController do
e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e))
end

def create_mint_quote(conn, params) do
method = params["method"]

def create_mint_quote(conn, %{
"method" => method,
"amount" => amount,
"unit" => unit,
"description" => description
}) do
if method != "bolt11" do
raise "UnsuportedMethod"
end

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

res = Nut04.Impl.create_mint_quote!(amount, unit, description)
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 7bae3f0

Please sign in to comment.