Skip to content

Commit

Permalink
fix(rpc): add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Oct 10, 2024
1 parent 4153730 commit 723c7c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
33 changes: 22 additions & 11 deletions lib/rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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? =
Expand Down
2 changes: 1 addition & 1 deletion test/rpc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 723c7c8

Please sign in to comment.