From 723c7c885df9c2d44a3fc50ca340f5106897d30c Mon Sep 17 00:00:00 2001 From: victor felder Date: Thu, 10 Oct 2024 14:41:28 +0200 Subject: [PATCH] fix(rpc): add error handling --- lib/rpc.ex | 33 ++++++++++++++++++++++----------- test/rpc_test.exs | 2 +- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/rpc.ex b/lib/rpc.ex index 08ff9fc..0acd258 100644 --- a/lib/rpc.ex +++ b/lib/rpc.ex @@ -200,18 +200,24 @@ defmodule Tezex.Rpc do @doc """ Sign the forged operation and returns the forged operation+signature payload to be injected. """ - @spec forge_and_sign_operation(operation(), encoded_private_key()) :: nonempty_binary() + @spec forge_and_sign_operation(operation(), encoded_private_key()) :: + {:ok, nonempty_binary()} | {:error, nonempty_binary()} def forge_and_sign_operation(operation, encoded_private_key) do - {:ok, forged_operation} = ForgeOperation.operation_group(operation) + case ForgeOperation.operation_group(operation) do + {:ok, forged_operation} -> + signature = Crypto.sign_operation(encoded_private_key, forged_operation) - signature = Crypto.sign_operation(encoded_private_key, forged_operation) + payload_signature = + signature + |> Crypto.decode_signature!() + |> Base.encode16(case: :lower) - payload_signature = - signature - |> Crypto.decode_signature!() - |> Base.encode16(case: :lower) + signed_payload = forged_operation <> payload_signature + {:ok, signed_payload} - forged_operation <> payload_signature + err -> + err + end end @doc """ @@ -223,10 +229,15 @@ defmodule Tezex.Rpc do | {:error, Jason.DecodeError.t()} | {:error, term()} def preapply_operation(%Rpc{} = rpc, operation, encoded_private_key, protocol) do - {:ok, forged_operation} = ForgeOperation.operation_group(operation) - signature = Crypto.sign_operation(encoded_private_key, forged_operation) - payload = [Map.merge(operation, %{"signature" => signature, "protocol" => protocol})] + with {:ok, forged_operation} <- ForgeOperation.operation_group(operation), + signature = Crypto.sign_operation(encoded_private_key, forged_operation), + payload = [Map.merge(operation, %{"signature" => signature, "protocol" => protocol})], + {:ok, preapplied_operations} <- do_preapply_operation(rpc, payload) do + {:ok, preapplied_operations} + end + end + defp do_preapply_operation(%Rpc{} = rpc, payload) do case post(rpc, "/blocks/head/helpers/preapply/operations", payload) do {:ok, [%{"contents" => preapplied_operations}]} -> applied? = diff --git a/test/rpc_test.exs b/test/rpc_test.exs index 4794356..b911ae9 100644 --- a/test/rpc_test.exs +++ b/test/rpc_test.exs @@ -75,7 +75,7 @@ defmodule Tezex.RpcTest do result = "6940b2318870c4b84862aef187c1bbcd4138170459e85092fe34be5d179f40ac6c00980d7cebb50d4b83a4e3307b3ca1b40ebe8f71ab00e308ab0b8102640000b75f0c30536bee108b068d90b151ce846aca11b1009bc207291f08c7117ea3a341328a036ce7bd5da996d9148cc491f96c3097748d7adcdb60599504ae5227aea4a6e69d536baaf96ae2bbb6c261a69d000b323f0a" - assert result == Rpc.forge_and_sign_operation(operation, @ghostnet_1_pkey) + assert {:ok, result} == Rpc.forge_and_sign_operation(operation, @ghostnet_1_pkey) end describe "fill_operation_fee/3" do