This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
246 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use assert_matches::assert_matches; | ||
use cdk::{ | ||
amount::Amount, | ||
nuts::{CurrencyUnit, MeltQuoteState}, | ||
wallet::MeltQuote, | ||
}; | ||
use integration_tests::init_wallet; | ||
use uuid::Uuid; | ||
|
||
#[tokio::test] | ||
pub async fn melt_quote_ok() { | ||
const URL: &str = "http://localhost:4000"; | ||
let wallet = init_wallet(URL, CurrencyUnit::Sat).unwrap(); | ||
|
||
let bolt11_invoice = "lnbc100n1pnvpufspp5djn8hrq49r8cghwye9kqw752qjncwyfnrprhprpqk43mwcy4yfsqdq5g9kxy7fqd9h8vmmfvdjscqzzsxqyz5vqsp5uhpjt36rj75pl7jq2sshaukzfkt7uulj456s4mh7uy7l6vx7lvxs9qxpqysgqedwz08acmqwtk8g4vkwm2w78suwt2qyzz6jkkwcgrjm3r3hs6fskyhvud4fan3keru7emjm8ygqpcrwtlmhfjfmer3afs5hhwamgr4cqtactdq".to_string(); | ||
let melt_quote = wallet.melt_quote(bolt11_invoice, None).await.unwrap(); | ||
|
||
let now = std::time::SystemTime::now() | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs(); | ||
|
||
assert_matches!( | ||
melt_quote, | ||
MeltQuote { | ||
id, | ||
unit: _, | ||
amount, | ||
request: _, | ||
fee_reserve, | ||
state, | ||
expiry, | ||
payment_preimage | ||
} if Uuid::try_parse(&id).is_ok() | ||
&& amount == Amount::from(10) | ||
&& fee_reserve == Amount::from(1) | ||
&& state == MeltQuoteState::Unpaid | ||
&& expiry >= now | ||
&& payment_preimage.is_none() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Cashubrew.Nuts.Nut05.Impl do | ||
@moduledoc """ | ||
Implementation and structs of the NUT-05 | ||
""" | ||
alias Cashubrew.Schema | ||
|
||
defp percent_fee_reserve, do: 1 | ||
defp min_fee_reserve, do: 1 | ||
# 15min | ||
defp melt_quote_validity_duration_in_sec, do: 900 | ||
|
||
def create_melt_quote!(request, unit) do | ||
{:ok, ln_invoice} = Bitcoinex.LightningNetwork.decode_invoice(request) | ||
|
||
repo = Application.get_env(:cashubrew, :repo) | ||
|
||
amount = | ||
case unit do | ||
"sat" -> div(ln_invoice.amount_msat, 1000) | ||
_ -> raise "UnsupportedUnit" | ||
end | ||
|
||
# TODO: impl max_amout min_amount config checks | ||
|
||
relative_fee_reserve = amount * percent_fee_reserve() / 100 | ||
fee = max(relative_fee_reserve, min_fee_reserve()) | ||
|
||
expiry = System.os_time(:second) + melt_quote_validity_duration_in_sec() | ||
quote_id = Ecto.UUID.bingenerate() | ||
quote_id_as_string = Ecto.UUID.cast!(quote_id) | ||
|
||
Schema.MeltQuote.create!(repo, %{ | ||
id: quote_id, | ||
request: request, | ||
unit: unit, | ||
amount: amount, | ||
fee_reserve: fee, | ||
expiry: expiry, | ||
request_lookup_id: ln_invoice.payment_hash | ||
}) | ||
|
||
%{ | ||
quote: quote_id_as_string, | ||
amount: amount, | ||
fee_reserve: fee, | ||
state: "UNPAID", | ||
expiry: expiry | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Cashubrew.Nuts.Nut05.Routes do | ||
@moduledoc """ | ||
List the rest routes defined in the NUT-05 | ||
""" | ||
|
||
@doc """ | ||
The route to ask the melt for a quote | ||
""" | ||
def v1_melt_quote do | ||
"/v1/melt/quote/:method" | ||
end | ||
|
||
@doc """ | ||
The route to check a melt quote state | ||
""" | ||
def v1_melt_quote_for_quote_id do | ||
v1_melt_quote() <> "/:quote_id" | ||
end | ||
|
||
@doc """ | ||
The route proceed to the melt | ||
""" | ||
def v1_melt do | ||
"/v1/melt/:method" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Cashubrew.Nuts.Nut05.Serde.PostMeltQuoteBolt11Request do | ||
@moduledoc """ | ||
The body of the post melt quote request | ||
""" | ||
@enforce_keys [:request, :unit] | ||
@derive [Jason.Encoder] | ||
defstruct [:request, :unit] | ||
end | ||
|
||
defmodule Cashubrew.Nuts.Nut05.Serde.PostMeltBolt11Request do | ||
@moduledoc """ | ||
The body of the post melt request | ||
""" | ||
alias Cashubrew.Nuts.Nut00.Proof | ||
|
||
@enforce_keys [:quote, :inputs] | ||
@derive [Jason.Encoder] | ||
defstruct [:quote, :inputs] | ||
|
||
def from_map(map) do | ||
%__MODULE__{ | ||
quote: Map.fetch!(map, "quote"), | ||
inputs: Proof.from_list(Map.fetch!(map, "inputs")) | ||
} | ||
end | ||
end | ||
|
||
defmodule Cashubrew.Nuts.Nut05.Serde.PostMeltQuoteBolt11Response do | ||
@moduledoc """ | ||
The body of the post melt quote response | ||
""" | ||
@enforce_keys [:quote, :amount, :fee_reserve, :state, :expiry] | ||
@derive [Jason.Encoder] | ||
defstruct [:quote, :amount, :fee_reserve, :state, :expiry, :payment_preimage] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Cashubrew.Nuts.Nut05.MeltMethodSetting do | ||
@moduledoc """ | ||
The infos retlated to this Nut support | ||
""" | ||
|
||
@enforce_keys [:method, :unit] | ||
@derive [Jason.Encoder] | ||
defstruct [:method, :unit, :min_amount, :max_amount, :description] | ||
|
||
@doc """ | ||
Return the map to be used in Nut06 info "nuts" field | ||
""" | ||
def bolt11 do | ||
%__MODULE__{ | ||
method: "bolt11", | ||
unit: "sat" | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.