Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet types #81

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ You can see a big picture of the roadmap here: [**ROADMAP**][roadmap]

### What we're working on now 🎉

- [Wallet types](https://github.com/kommitters/kadena.ex/issues/18)
- [Kadena Crypto](https://github.com/kommitters/kadena.ex/issues/51)

### What we're working on next! 🍰

- [Kadena Crypto](https://github.com/kommitters/kadena.ex/issues/51)
- [Kadena PACT](https://github.com/kommitters/kadena.ex/issues/55)
- [Kadena API](https://github.com/kommitters/kadena.ex/issues/56)
- [Kadena Chainweb](https://github.com/kommitters/kadena.ex/issues/57)
Expand All @@ -63,6 +62,7 @@ You can see a big picture of the roadmap here: [**ROADMAP**][roadmap]
- [CommandResult types](https://github.com/kommitters/kadena.ex/issues/43)
- [PactCommand types](https://github.com/kommitters/kadena.ex/issues/13)
- [PactAPI types](https://github.com/kommitters/kadena.ex/issues/17)
- [Wallet types](https://github.com/kommitters/kadena.ex/issues/18)

</details>

Expand Down
49 changes: 49 additions & 0 deletions lib/types/signing_cap.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule Kadena.Types.SigningCap do
@moduledoc """
`SigningCap` struct definition.
"""

alias Kadena.Types.Cap

@behaviour Kadena.Types.Spec

@type str :: String.t()
@type role :: str()
@type description :: str()
@type cap :: Cap.t()
@type value :: str() | cap()
@type validation :: {:ok, value()} | {:error, Keyword.t()}

@type t :: %__MODULE__{role: role(), description: description(), cap: cap()}

defstruct [:role, :description, :cap]

@impl true
def new(args) when is_list(args) do
role = Keyword.get(args, :role)
description = Keyword.get(args, :description)
cap = Keyword.get(args, :cap)

with {:ok, role} <- validate_str(:role, role),
{:ok, description} <- validate_str(:description, description),
{:ok, cap} <- validate_cap(cap) do
%__MODULE__{role: role, description: description, cap: cap}
end
end

def new(_args), do: {:error, [signing_cap: :not_a_list]}

@spec validate_str(field :: atom(), value :: str()) :: validation()
defp validate_str(_field, value) when is_binary(value), do: {:ok, value}
defp validate_str(field, _value), do: {:error, [{field, :invalid}]}

@spec validate_cap(cap :: cap()) :: validation()
defp validate_cap(%Cap{} = cap), do: {:ok, cap}

defp validate_cap(cap) do
case Cap.new(cap) do
%Cap{} = cap -> {:ok, cap}
{:error, reasons} -> {:error, [cap: :invalid] ++ reasons}
end
end
boterop marked this conversation as resolved.
Show resolved Hide resolved
end
64 changes: 64 additions & 0 deletions test/types/signing_cap_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
defmodule Kadena.Types.SigningCapTest do
@moduledoc """
`SigningCap` struct definition tests.
"""

use ExUnit.Case

alias Kadena.Types.{Cap, SigningCap}

describe "new/1" do
setup do
%{
role: "",
description: "",
cap: [name: "gas", args: ["COIN.gas", 1.0e-2]],
cap_struct: Cap.new(name: "gas", args: ["COIN.gas", 1.0e-2])
boterop marked this conversation as resolved.
Show resolved Hide resolved
}
end

test "with a valid param list", %{
role: role,
description: description,
cap: cap,
cap_struct: cap_struct
} do
%SigningCap{role: ^role, description: ^description, cap: ^cap_struct} =
SigningCap.new(role: role, description: description, cap: cap)
end

test "with a valid cap struct", %{
role: role,
description: description,
cap_struct: cap_struct
} do
%SigningCap{role: ^role, description: ^description, cap: ^cap_struct} =
SigningCap.new(role: role, description: description, cap: cap_struct)
end

test "with an invalid params list" do
{:error, [signing_cap: :not_a_list]} = SigningCap.new("invalid_param")
end

test "with an invalid role", %{
description: description,
cap_struct: cap_struct
} do
{:error, [role: :invalid]} =
SigningCap.new(role: 123, description: description, cap: cap_struct)
end

test "with an invalid description", %{role: role, cap_struct: cap_struct} do
{:error, [description: :invalid]} =
SigningCap.new(role: role, description: 123, cap: cap_struct)
end

test "with an invalid cap list", %{
role: role,
description: description
} do
{:error, [cap: :invalid, name: :invalid]} =
SigningCap.new(role: role, description: description, cap: [invalid_key: :invalid_value])
end
end
end