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

Json RPC endpoint does not require content to be base16 #1238

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ config :ex_cldr,
default_backend: Archethic.Cldr,
json_library: Jason

config :ex_json_schema, :remote_schema_resolver, {Archethic.Utils, :local_schema_resolver!}

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config("#{Mix.env()}.exs")
7 changes: 5 additions & 2 deletions lib/archethic/transaction_chain/transaction/data/recipient.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ defmodule Archethic.TransactionChain.TransactionData.Recipient do
@spec cast(recipient :: binary() | map()) :: t()
def cast(recipient) when is_binary(recipient), do: %__MODULE__{address: recipient}

def cast(%{address: address, action: action, args: args}),
do: %__MODULE__{address: address, action: action, args: args}
def cast(recipient = %{address: address}) do
action = Map.get(recipient, :action)
args = Map.get(recipient, :args)
%__MODULE__{address: address, action: action, args: args}
end

@doc false
@spec to_map(recipient :: t()) :: map()
Expand Down
63 changes: 48 additions & 15 deletions lib/archethic/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ defmodule Archethic.Utils do
# }
# }

# iex> %{ "a" => "hello", "b.c" => "hi" } |> Utils.atomize_keys(true)
# iex> %{ "a" => "hello", "b.c" => "hi" } |> Utils.atomize_keys(nest_dot?: true)
# %{
# a: "hello",
# b: %{
Expand Down Expand Up @@ -225,7 +225,7 @@ defmodule Archethic.Utils do
...> 221, 48, 165, 205, 196, 124, 240, 132, 54, 75, 237, 2, 33, 0, 141, 28, 71,
...> 218, 224, 201>>
...> }
...> |> Utils.atomize_keys(true)
...> |> Utils.atomize_keys(nest_dot?: true)
%{
address: <<0, 177, 211, 117, 14, 219, 147, 129, 201, 107, 26, 151, 90, 85, 181, 180, 228, 251,
55, 191, 171, 16, 76, 16, 176, 182, 201, 160, 4, 51, 236, 70, 70>>,
Expand All @@ -248,44 +248,65 @@ defmodule Archethic.Utils do
}
}

# iex> %{ "a.b.c" => "hello" } |> Utils.atomize_keys(false)
# iex> %{ "a.b.c" => "hello" } |> Utils.atomize_keys()
# %{ "a.b.c": "hello"}

# iex> %{ "argumentNames" => %{"firstName" => "toto"} } |> Utils.atomize_keys(to_snake_case?: true)
# %{ :argument_names: %{firs_name: "toto"}}
"""
@spec atomize_keys(map(), nest_dot? :: boolean()) :: map()
def atomize_keys(map, nest_dot? \\ false)
@spec atomize_keys(map :: map(), opts :: Keyword.t()) :: map()
def atomize_keys(map, opts \\ []) do
nest_dot? = Keyword.get(opts, :nest_dot?, false)
to_snake_case? = Keyword.get(opts, :to_snake_case?, false)

atomize_keys(map, nest_dot?, to_snake_case?)
end

def atomize_keys(struct = %{__struct__: _}, _nest_dot?) do
def atomize_keys(struct = %{__struct__: _}, _, _) do
struct
end

def atomize_keys(map = %{}, nest_dot?) do
def atomize_keys(map = %{}, nest_dot?, to_snake_case?) do
map
|> Enum.reduce(%{}, fn
{k, v}, acc when is_binary(k) ->
if String.valid?(k) do
if nest_dot? and String.contains?(k, ".") do
put_in(acc, nested_path(String.split(k, ".")), atomize_keys(v, nest_dot?))
path =
String.split(k, ".")
|> Enum.map(fn k ->
if to_snake_case?, do: Macro.underscore(k), else: k
end)

put_in(
acc,
nested_path(path),
atomize_keys(v, nest_dot?, to_snake_case?)
)
else
Map.put(acc, String.to_existing_atom(k), atomize_keys(v, nest_dot?))
k = if to_snake_case?, do: Macro.underscore(k), else: k
Map.put(acc, String.to_existing_atom(k), atomize_keys(v, nest_dot?, to_snake_case?))
end
else
Map.put(acc, k, atomize_keys(v, nest_dot?))
Map.put(acc, k, atomize_keys(v, nest_dot?, to_snake_case?))
end

{k, v}, acc ->
Map.put(acc, k, atomize_keys(v, nest_dot?))
Map.put(acc, k, atomize_keys(v, nest_dot?, to_snake_case?))
end)
end

# Walk the list and atomize the keys of
# of any map members
def atomize_keys([head | []], _), do: [atomize_keys(head)]
def atomize_keys([head | []], nest_dot?, to_snake_case?),
do: [atomize_keys(head, nest_dot?, to_snake_case?)]

def atomize_keys([head | rest], _) do
[atomize_keys(head)] ++ atomize_keys(rest)
def atomize_keys([head | rest], nest_dot?, to_snake_case?) do
[atomize_keys(head, nest_dot?, to_snake_case?)] ++
atomize_keys(rest, nest_dot?, to_snake_case?)
end

def atomize_keys(not_a_map, _) do
def atomize_keys(not_a_map, _, _) do
not_a_map
end

Expand Down Expand Up @@ -1157,4 +1178,16 @@ defmodule Archethic.Utils do
:not_found
end
end

@doc """
Resolver function used by ExJsonSchema to resolve local path
"""
@spec local_schema_resolver!(path :: binary()) :: map()
def local_schema_resolver!("file://" <> path) do
Application.app_dir(:archethic, "priv/json-schemas/#{path}")
|> File.read!()
|> Jason.decode!()
end

def local_schema_resolver!(_), do: raise("Invalid URI for $ref")
end
22 changes: 9 additions & 13 deletions lib/archethic_web/api/jsonrpc/methods/estimate_transaction_fee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ defmodule ArchethicWeb.API.JsonRPC.Method.EstimateTransactionFee do
alias Archethic.TransactionChain.Transaction

alias ArchethicWeb.API.JsonRPC.Method
alias ArchethicWeb.API.TransactionPayload

alias ArchethicWeb.WebUtils
alias ArchethicWeb.API.JsonRPC.TransactionSchema

@behaviour Method

Expand All @@ -20,21 +18,19 @@ defmodule ArchethicWeb.API.JsonRPC.Method.EstimateTransactionFee do
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: map()}
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
{:ok, changeset = %{valid?: true}} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}

{:ok, changeset} ->
reasons = Ecto.Changeset.traverse_errors(changeset, &WebUtils.translate_error/1)
{:error, reasons}
case TransactionSchema.validate(transaction_params) do
:ok ->
{:ok, TransactionSchema.to_transaction(transaction_params)}

:error ->
{:error, %{transaction: ["must be an object"]}}
{:error, %{"transaction" => "Must be an object"}}

{:error, reasons} ->
{:error, reasons}
end
end

def validate_params(_), do: {:error, %{transaction: ["is required"]}}
def validate_params(_), do: {:error, %{"transaction" => "Is required"}}

@doc """
Execute the function to send a new tranaction in the network
Expand Down
22 changes: 9 additions & 13 deletions lib/archethic_web/api/jsonrpc/methods/send_transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SendTransaction do
alias Archethic.TransactionChain.Transaction

alias ArchethicWeb.API.JsonRPC.Method
alias ArchethicWeb.API.TransactionPayload
alias ArchethicWeb.API.JsonRPC.TransactionSchema

alias ArchethicWeb.TransactionSubscriber

alias ArchethicWeb.WebUtils

@behaviour Method

@doc """
Expand All @@ -20,21 +18,19 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SendTransaction do
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: map()}
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
{:ok, changeset = %{valid?: true}} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}

{:ok, changeset} ->
reasons = Ecto.Changeset.traverse_errors(changeset, &WebUtils.translate_error/1)
{:error, reasons}
case TransactionSchema.validate(transaction_params) do
:ok ->
{:ok, TransactionSchema.to_transaction(transaction_params)}

:error ->
{:error, %{transaction: ["must be an object"]}}
{:error, %{"transaction" => "Must be an object"}}

{:error, reasons} ->
{:error, reasons}
end
end

def validate_params(_), do: {:error, %{transaction: ["is required"]}}
def validate_params(_), do: {:error, %{"transaction" => "Is required"}}

@doc """
Execute the function to send a new tranaction in the network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SimulateContractExecution do

alias ArchethicWeb.API.JsonRPC.Method
alias ArchethicWeb.API.JsonRPC.Error
alias ArchethicWeb.API.TransactionPayload

alias ArchethicWeb.WebUtils
alias ArchethicWeb.API.JsonRPC.TransactionSchema

@behaviour Method

Expand All @@ -26,21 +24,19 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SimulateContractExecution do
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: map()}
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
{:ok, changeset = %{valid?: true}} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}

{:ok, changeset} ->
reasons = Ecto.Changeset.traverse_errors(changeset, &WebUtils.translate_error/1)
{:error, reasons}
case TransactionSchema.validate(transaction_params) do
:ok ->
{:ok, TransactionSchema.to_transaction(transaction_params)}

:error ->
{:error, %{transaction: ["must be an object"]}}
{:error, %{"transaction" => "Must be an object"}}

{:error, reasons} ->
{:error, reasons}
end
end

def validate_params(_), do: {:error, %{transaction: ["is required"]}}
def validate_params(_), do: {:error, %{"transaction" => "Is required"}}

@doc """
Execute the function to send a new tranaction in the network
Expand Down
Loading
Loading