Skip to content

Commit

Permalink
fix and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
polvorin committed Nov 20, 2023
1 parent d4c784a commit 51ca98c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,10 @@ defmodule Ockam.SecureChannel.Channel do
end

defp attach_metadata(msg, additional, %Established{peer_identity: i, peer_identity_id: id}) do
Message.with_local_metadata(msg, Map.merge(additional, %{identity: i, identity_id: id}))
Message.with_local_metadata(
msg,
Map.merge(additional, %{identity: i, identity_id: id, channel: :secure_channel})
)
end

defp handle_outer_message_impl(message, %Channel{channel_state: %Established{} = e} = state) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ defmodule Ockam.SecureChannel.Messages do
end
end

def encode(%Ockam.SecureChannel.Messages.Payload{payload: data} = msg) do
bare_encoded_payload = :bare.encode(data, :data)

TypedCBOR.encode(@enum_schema, %Ockam.SecureChannel.Messages.Payload{
msg
| payload: bare_encoded_payload
})
end

def encode(msg) do
TypedCBOR.encode(@enum_schema, msg)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ defmodule Ockam.SecureChannel.Tests do
alias Ockam.SecureChannel.Crypto
alias Ockam.Tests.Helpers.Echoer

@identity_impl Ockam.Identity.Stub

setup do
Node.register_address("test")
{:ok, alice} = Identity.create()
Expand Down
11 changes: 8 additions & 3 deletions implementations/elixir/ockam/ockam_typed_cbor/lib/typed_cbor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ defmodule Ockam.TypedCBOR do
val
end
end

def from_cbor_term({:enum, vals}, [n, list]) when is_integer(n) do
case {List.keyfind(vals, n, 1), list} do
{nil, _} ->
raise Exception, message: "invalid enum encoding: #{n}, allowed: #{inspect(vals)}"

{{val, ^n}, []} ->
val

{{schema, ^n}, [val]} ->
from_cbor_term(schema, val)
end
Expand Down Expand Up @@ -175,7 +177,10 @@ defmodule Ockam.TypedCBOR do
val
else
r ->
Logger.error("type mismatch, expected schema #{inspect(schema)}, value: #{inspect(data)} err: #{inspect(r)}")
Logger.error(
"type mismatch, expected schema #{inspect(schema)}, value: #{inspect(data)} err: #{inspect(r)}"
)

raise(Exception, "type mismatch, expected schema #{inspect(schema)}")
end
end
Expand Down Expand Up @@ -229,7 +234,6 @@ defmodule Ockam.TypedCBOR do
else
raise Exception, message: "invalid string #{inspect(val)}"
end

end

def to_cbor_term(:binary, val) when is_binary(val), do: %CBOR.Tag{tag: :bytes, value: val}
Expand All @@ -244,8 +248,10 @@ defmodule Ockam.TypedCBOR do
n
end
end

def to_cbor_term({:enum, vals}, val) when is_struct(val) do
schema = val.__struct__

case vals[schema] do
nil ->
raise Exception,
Expand All @@ -256,7 +262,6 @@ defmodule Ockam.TypedCBOR do
end
end


def to_cbor_term({:list, element_schema}, values) when is_list(values),
do: Enum.map(values, fn val -> to_cbor_term(element_schema, val) end)

Expand Down

0 comments on commit 51ca98c

Please sign in to comment.