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

Add tests for NUT-02 and refactor a bit #33

Merged
merged 3 commits into from
Oct 23, 2024
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: 2 additions & 0 deletions lib/cashubrew/NUTs/NUT-01/serde.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Cashubrew.Nuts.Nut01.Serde.GetKeysResponse do
"""
@derive Jason.Encoder
@enforce_keys [:keysets]
@derive [Jason.Encoder]
defstruct [:keysets]

def from_keysets(keysets) do
Expand Down Expand Up @@ -60,6 +61,7 @@ defmodule Cashubrew.Nuts.Nut01.Serde.Keyset do
"""
@derive Jason.Encoder
@enforce_keys [:id, :unit, :keys]
@derive [Jason.Encoder]
defstruct [:id, :unit, :keys]

def from_keyset(id, unit, keys) do
Expand Down
7 changes: 4 additions & 3 deletions lib/cashubrew/NUTs/NUT-02/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ defmodule Cashubrew.Nuts.Nut02.Impl do
The mint logic for handling Nut02
"""
alias Cashubrew.Mint
alias Cashubrew.Repo
alias Cashubrew.Schema.Keyset

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

def keys_for_keyset_id!(id) do
Expand Down
2 changes: 1 addition & 1 deletion lib/cashubrew/NUTs/NUT-02/keysets.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Cashubrew.Nuts.Nut02.Keyset do
defmodule Cashubrew.Nuts.Nut02.Keysets do
@moduledoc """
Contains the logic required for the Mint to generate keys and keysets
"""
Expand Down
3 changes: 1 addition & 2 deletions lib/cashubrew/NUTs/NUT-02/routes.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defmodule Cashubrew.Nuts.Nut02.Routes do
alias Cashubrew.Nuts.Nut01

@moduledoc """
List the rest routes defined in the NUT-02
"""
alias Cashubrew.Nuts.Nut01

@doc """
The route to query keys part of a specific keyset from the Mint
Expand Down
5 changes: 3 additions & 2 deletions lib/cashubrew/NUTs/NUT-02/serde.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Cashubrew.Nuts.Nut02.Serde.Keyset do
A keyset
"""
@enforce_keys [:id, :unit, :active]
@derive [Jason.Encoder]
defstruct [:id, :unit, :active, :input_fee_ppk]

def from_keyset(keyset) do
Expand All @@ -20,11 +21,11 @@ defmodule Cashubrew.Nuts.Nut02.Serde.GetKeysetsResponse do
The body of the get keysets rest response
"""
@enforce_keys [:keysets]
@derive [Jason.Encoder]
defstruct [:keysets]

def from_keysets(keysets) do
keysets_responses =
Enum.map(keysets, fn keyset -> Cashubrew.Nuts.Nut02.Serde.Keyset.from_keyset(keyset) end)
keysets_responses = Enum.map(keysets, &Cashubrew.Nuts.Nut02.Serde.Keyset.from_keyset(&1))

%__MODULE__{
keysets: keysets_responses
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 @@ -85,7 +85,7 @@ defmodule Cashubrew.Mint do
defp load_or_create_keysets(repo, seed) do
case repo.all(Schema.Keyset) do
[] ->
keys = Nut02.Keyset.generate_keys(seed, keyset_generation_derivation_path())
keys = Nut02.Keysets.generate_keys(seed, keyset_generation_derivation_path())

keyset =
Schema.Keyset.register_keyset(
Expand Down
6 changes: 4 additions & 2 deletions lib/cashubrew/schema/keyset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ defmodule Cashubrew.Schema.Keyset do
Keyset schema for the Cashubrew mint.
"""
use Ecto.Schema

import Ecto.Changeset
alias Cashubrew.Nuts.Nut02

alias Cashubrew.Schema
alias Cashubrew.Nuts.Nut02

@primary_key {:id, :string, autogenerate: false}
schema "keysets" do
Expand All @@ -22,7 +24,7 @@ defmodule Cashubrew.Schema.Keyset do
end

def register_keyset(keys, unit, input_fee_ppk) do
id = Nut02.Keyset.derive_keyset_id(keys)
id = Nut02.Keysets.derive_keyset_id(keys)

keyset = %__MODULE__{
id: id,
Expand Down
68 changes: 68 additions & 0 deletions test/nut02_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule Cashubrew.Nuts.Nut02Test do
use Cashubrew.Test.ConnCase
alias Cashubrew.Schema.Key
alias Cashubrew.Schema.Keyset
alias Cashubrew.Repo

test "get all keysets", %{conn: conn} do
data = conn |> get(~p"/api/v1/keysets") |> json_response(200)

[keyset] = data["keysets"]
assert Map.has_key?(keyset, "id")
assert keyset["id"] |> String.starts_with?("00")
assert keyset["unit"] == "sat"
assert keyset["active"] == true
end

test "get 2 keysets", %{conn: conn} do
pub_keys = [
%{amount: 1, public_key: "pub_01", private_key: "priv_01"},
%{amount: 2, public_key: "pub_02", private_key: "priv_02"},
%{amount: 3, public_key: "pub_03", private_key: "priv_03"},
%{amount: 4, public_key: "pub_04", private_key: "priv_04"}
]

id = Cashubrew.Nuts.Nut02.Keysets.derive_keyset_id(pub_keys)

now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

pub_keys =
Enum.map(pub_keys, fn key ->
key
|> Map.put(:keyset_id, id)
|> Map.put(:inserted_at, now)
|> Map.put(:updated_at, now)
end)

Repo.insert!(%Keyset{id: id, unit: "sat", active: false})
Repo.insert_all(Key, pub_keys)

data = conn |> get(~p"/api/v1/keysets") |> json_response(200)

[keyset1, keyset2] = data["keysets"]

assert Map.has_key?(keyset1, "id")
assert keyset1["id"] |> String.starts_with?("00")
assert keyset1["unit"] == "sat"
assert keyset1["active"] == true

assert Map.has_key?(keyset2, "id")
assert keyset2["id"] |> String.starts_with?("00")
tdelabro marked this conversation as resolved.
Show resolved Hide resolved
assert keyset2["id"] == id
assert keyset2["unit"] == "sat"
assert keyset2["active"] == false
end

test "get one keyset", %{conn: conn} do
data = conn |> get(~p"/api/v1/keysets") |> json_response(200)

[keyset] = data["keysets"]
id = keyset["id"]

data = conn |> get(~p"/api/v1/keys/#{id}") |> json_response(200)
[keyset] = data["keysets"]

assert keyset["id"] == id
assert map_size(keyset["keys"]) > 0
end
end
Loading