From d8aa9ad3598de328ddd486a84c40113717abcb63 Mon Sep 17 00:00:00 2001 From: Santiago Botero <98826652+santiagoboterokommit@users.noreply.github.com> Date: Thu, 13 Oct 2022 11:33:13 -0500 Subject: [PATCH] Wallet types (#81) * SigningCap type * Update README.md * Requested changes solved --- README.md | 4 +- lib/types/signing_cap.ex | 49 ++++++++++++++++++++++ test/types/signing_cap_test.exs | 74 +++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 lib/types/signing_cap.ex create mode 100644 test/types/signing_cap_test.exs diff --git a/README.md b/README.md index 6f7cd4c..30a58c4 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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) diff --git a/lib/types/signing_cap.ex b/lib/types/signing_cap.ex new file mode 100644 index 0000000..0451f5a --- /dev/null +++ b/lib/types/signing_cap.ex @@ -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 +end diff --git a/test/types/signing_cap_test.exs b/test/types/signing_cap_test.exs new file mode 100644 index 0000000..50c3538 --- /dev/null +++ b/test/types/signing_cap_test.exs @@ -0,0 +1,74 @@ +defmodule Kadena.Types.SigningCapTest do + @moduledoc """ + `SigningCap` struct definition tests. + """ + + use ExUnit.Case + + alias Kadena.Types.{Cap, SigningCap} + + describe "new/1" do + setup do + cap_list = [name: "gas", args: ["COIN.gas", 1.0e-2]] + + %{ + role: "", + description: "", + cap: cap_list, + cap_struct: Cap.new(cap_list) + } + 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 + + test "with a no list cap", %{ + role: role, + description: description + } do + {:error, [cap: :invalid, args: :invalid]} = + SigningCap.new(role: role, description: description, cap: :invalid_value) + end + end +end