diff --git a/lib/crypto/nacl.ex b/lib/crypto/nacl.ex index 1e720e1..ee704ce 100644 --- a/lib/crypto/nacl.ex +++ b/lib/crypto/nacl.ex @@ -21,7 +21,8 @@ defmodule Tezex.Crypto.NaCl do - `{:error, reason}` - Decryption failed """ @spec crypto_secretbox_open(binary(), binary(), binary()) :: - {:ok, binary()} | {:error, :decryption_failed} + {:ok, binary()} + | {:error, :decryption_failed | :invalid_nonce_length | :invalid_key_length} def crypto_secretbox_open(_ciphertext, nonce, _key) when byte_size(nonce) != 24 do {:error, :invalid_nonce_length} end diff --git a/lib/forge.ex b/lib/forge.ex index 82e9edd..8b66c91 100644 --- a/lib/forge.ex +++ b/lib/forge.ex @@ -360,7 +360,7 @@ defmodule Tezex.Forge do address_bytes = forge_address(address) - if entrypoint != nil && entrypoint != "default" do + if entrypoint != nil and entrypoint != "default" do address_bytes <> entrypoint else address_bytes diff --git a/lib/forge_operation.ex b/lib/forge_operation.ex index 764cbce..ac5b382 100644 --- a/lib/forge_operation.ex +++ b/lib/forge_operation.ex @@ -53,8 +53,8 @@ defmodule Tezex.ForgeOperation do end end - defp forge_tag(tag) do - <> + defp forge_tag(kind) do + <> end @spec operation(map()) :: {:ok, nonempty_binary()} | {:error, nonempty_binary()} @@ -100,7 +100,7 @@ defmodule Tezex.ForgeOperation do with :ok <- validate_required_keys(content, ~w(kind pkh secret)) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), binary_slice(Forge.forge_address(content["pkh"]), 2..-1//1), Base.decode16!(content["secret"], case: :mixed) ] @@ -119,7 +119,7 @@ defmodule Tezex.ForgeOperation do ) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), @@ -155,7 +155,7 @@ defmodule Tezex.ForgeOperation do )) || :ok do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), @@ -190,7 +190,7 @@ defmodule Tezex.ForgeOperation do ) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), @@ -221,7 +221,7 @@ defmodule Tezex.ForgeOperation do ) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), @@ -245,7 +245,10 @@ defmodule Tezex.ForgeOperation do def endorsement(content) do with :ok <- validate_required_keys(content, ~w(kind level)) do content = - [forge_tag(content["kind"]), Forge.forge_int32(String.to_integer(content["level"]))] + [ + forge_tag(content["kind"]), + Forge.forge_int32(String.to_integer(content["level"])) + ] |> IO.iodata_to_binary() {:ok, content} @@ -292,7 +295,10 @@ defmodule Tezex.ForgeOperation do def failing_noop(content) do with :ok <- validate_required_keys(content, ~w(kind arbitrary)) do content = - [forge_tag(content["kind"]), Forge.forge_array(content["arbitrary"])] + [ + forge_tag(content["kind"]), + Forge.forge_array(content["arbitrary"]) + ] |> IO.iodata_to_binary() {:ok, content} @@ -308,7 +314,7 @@ defmodule Tezex.ForgeOperation do ) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), @@ -331,7 +337,7 @@ defmodule Tezex.ForgeOperation do ) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), @@ -359,7 +365,7 @@ defmodule Tezex.ForgeOperation do ) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), @@ -388,7 +394,7 @@ defmodule Tezex.ForgeOperation do ) do content = [ - forge_tag(@operation_tags[content["kind"]]), + forge_tag(content["kind"]), Forge.forge_address(content["source"], :bytes, true), Forge.forge_nat(String.to_integer(content["fee"])), Forge.forge_nat(String.to_integer(content["counter"])), diff --git a/test/forge_operation_test.exs b/test/forge_operation_test.exs index 79a3f29..f3dd151 100644 --- a/test/forge_operation_test.exs +++ b/test/forge_operation_test.exs @@ -233,6 +233,41 @@ defmodule Tezex.ForgeOperationTest do end end + describe "operations using the @operation_tags lookup" do + test "endorsement encodes the kind tag, not the kind string" do + assert {:ok, result} = + ForgeOperation.endorsement(%{"kind" => "endorsement", "level" => "100"}) + + # tag for "endorsement" is 0; level is forge_int32(100) = <<0,0,0,100>> + assert result == <<0, 0, 0, 0, 100>> + end + + test "failing_noop encodes the kind tag, not the kind string" do + assert {:ok, result} = + ForgeOperation.failing_noop(%{"kind" => "failing_noop", "arbitrary" => "deadbeef"}) + + # tag for "failing_noop" is 17; "arbitrary" is wrapped in a forge_array + assert result == <<17, 0, 0, 0, 8, "deadbeef">> + end + + test "endorsement_with_slot encodes the kind tag, not the kind string" do + assert {:ok, result} = + ForgeOperation.endorsement_with_slot(%{ + "kind" => "endorsement_with_slot", + "endorsement" => %{ + "branch" => "BLWdshvgEYbtUaABnmqkMuyMezpfsu36DEJPDJN63CW3TFuk7bP", + "operations" => %{"kind" => "endorsement", "level" => "100"}, + "signature" => + "edsigtqqVg7CM2ynDXRHUfVEdL4LxKAJ1xrM1poMPXZdwVQ3SQ6YBkqPcAuaGYbeqUTrg374dDNFvJKCVfMA7T1Vrotg91Hsuam" + }, + "slot" => "1" + }) + + # tag for "endorsement_with_slot" is 10 + assert <<10, _rest::binary>> = result + end + end + test "validate_required_keys/2" do assert ForgeOperation.validate_required_keys(%{"a" => 1, "b" => 2}, ~w(a b)) == :ok