Skip to content

Commit

Permalink
fix: replace :binary.encode_hex/2 because it requires OTP26
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed May 6, 2024
1 parent a1b5005 commit 89603ff
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/crypto/hmacdrbg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Tezex.Crypto.HMACDRBG do
{_result, drbg} = HMACDRBG.generate(drbg, 32)
{result, drbg} = HMACDRBG.generate(drbg, 32)
:binary.encode_hex(result, :lowercase) == "a00cb0982eec3917b4b48abccfd460366d98b887943ff402bb7147cda174a46f"
Base16.encode(result, case: :lower) == "a00cb0982eec3917b4b48abccfd460366d98b887943ff402bb7147cda174a46f"
```
Implementation inspired by both:
Expand Down
4 changes: 2 additions & 2 deletions lib/forge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ defmodule Tezex.Forge do
10 ->
{val, offset} = unforge_array(binary_slice(data, ptr..-1//1))
ptr = ptr + offset
{%{"bytes" => :binary.encode_hex(val, :lowercase)}, ptr}
{%{"bytes" => Base.encode16(val, case: :lower)}, ptr}

_ ->
raise "Unknown tag: #{tag} at position #{ptr}"
Expand Down Expand Up @@ -630,5 +630,5 @@ defmodule Tezex.Forge do

@spec encode_output(binary(), io_encoding()) :: binary()
defp encode_output(data, :bytes), do: data
defp encode_output(data, :hex), do: :binary.encode_hex(data, :lowercase)
defp encode_output(data, :hex), do: Base.encode16(data, case: :lower)
end
2 changes: 1 addition & 1 deletion lib/forge_operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule Tezex.ForgeOperation do
Enum.join(Enum.map(operation_group["contents"], &operation/1))
]
|> IO.iodata_to_binary()
|> :binary.encode_hex(:lowercase)
|> Base.encode16(case: :lower)
end

@spec activate_account(map()) :: nonempty_binary()
Expand Down
4 changes: 2 additions & 2 deletions lib/micheline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ defmodule Tezex.Micheline do
"0500" <> Zarith.encode(value)

:string ->
hex_bytes = :binary.encode_hex(value, :lowercase)
hex_bytes = Base.encode16(value, case: :lower)
"0501" <> encode_byte_size(hex_bytes) <> hex_bytes

:bytes ->
hex_bytes = :binary.encode_hex(value, :lowercase)
hex_bytes = Base.encode16(value, case: :lower)
"050a#{encode_byte_size(hex_bytes)}#{hex_bytes}"

:key_hash ->
Expand Down
1 change: 0 additions & 1 deletion lib/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ defmodule Tezex.Transaction do

def encode(value, opts) do
value
|> Map.put(:kind, "transaction")
|> Map.update!(:fee, &to_string/1)
|> Map.update!(:gas_limit, &to_string/1)
|> Map.update!(:amount, &to_string/1)
Expand Down
6 changes: 3 additions & 3 deletions test/crypto/hmacdrbg_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Tezex.Crypto.HMACDRBGTest do

{result, _} = HMACDRBG.generate(drbg, 32)

assert :binary.encode_hex(result, :lowercase) ==
assert Base.encode16(result, case: :lower) ==
"018ec5f8e08c41e5ac974eb129ac297c5388ee1864324fa13d9b15cf98d9a157"
end

Expand All @@ -21,7 +21,7 @@ defmodule Tezex.Crypto.HMACDRBGTest do

{result, _} = HMACDRBG.generate(drbg, 32)

assert :binary.encode_hex(result, :lowercase) ==
assert Base.encode16(result, case: :lower) ==
"ed5d61ecf0ef38258e62f03bbb49f19f2cd07ba5145a840d83b134d5963b3633"
end

Expand Down Expand Up @@ -64,7 +64,7 @@ defmodule Tezex.Crypto.HMACDRBGTest do
HMACDRBG.generate(drbg, 256, add)
end)

assert :binary.encode_hex(result, :lowercase) == expected
assert Base.encode16(result, case: :lower) == expected
end
end
end
Expand Down
27 changes: 9 additions & 18 deletions test/forge_operation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.transaction(transaction)
|> :binary.encode_hex(:lowercase)
ForgeOperation.transaction(transaction) |> Base.encode16(case: :lower)

assert result ==
"6c0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e80ade204000154f5d8f71ce18f9f05bb885a4120e64c667bc1b400"
Expand All @@ -36,8 +35,7 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.transaction(transaction)
|> :binary.encode_hex(:lowercase)
ForgeOperation.transaction(transaction) |> Base.encode16(case: :lower)

assert result ==
"6c00a8d45bdc966ddaaac83188a1e1c1fde2a3e05e5ca08d06c4e901b15b00904e01f61128c6abd2426d0c49b1fee1fa8c98dcc4ce0a0000"
Expand Down Expand Up @@ -72,8 +70,7 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.transaction(transaction)
|> :binary.encode_hex(:lowercase)
ForgeOperation.transaction(transaction) |> Base.encode16(case: :lower)

assert result ==
"6c003ff84abc64319bda01968fd5269981d7615a6f75a08d06c4e901b15b0080c2d72f01fae98b912bb3644d56b8409cb98f40c779a9befe00ffff0a78747a546f546f6b656e0000005507070100000024747a3152686e47783968437862724e387a4b454b4c627755317a4b4c595a5471527336330707008b858b81c289bfcefc2a0100000018323032312d30312d32315431383a30393a31342e3531395a"
Expand All @@ -91,8 +88,7 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.reveal(reveal)
|> :binary.encode_hex(:lowercase)
ForgeOperation.reveal(reveal) |> Base.encode16(case: :lower)

assert result ==
"6b0069ef8fb5d47d8a4321c94576a2316a632be8ce890094fe19904e00004c7b0501f6ea08f472b7e88791d3b8da49d64ac1e2c90f93c27e6531473305c6"
Expand Down Expand Up @@ -134,8 +130,7 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.operation(origination)
|> :binary.encode_hex(:lowercase)
ForgeOperation.operation(origination) |> Base.encode16(case: :lower)

assert result ==
"6d0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e934e000000003702000000320500035b0501035b0502020000002303160743035b00010312074303690a000000080123456789abcdef0320053d036d034200000002001e"
Expand Down Expand Up @@ -178,8 +173,7 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.operation(origination)
|> :binary.encode_hex(:lowercase)
ForgeOperation.operation(origination) |> Base.encode16(case: :lower)

assert result ==
"6d0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e934eff001392b07a567de5cb3a4301fbef2030696b4dfd8b0000003702000000320500035b0501035b0502020000002303160743035b00010312074303690a000000080123456789abcdef0320053d036d034200000002001e"
Expand All @@ -197,17 +191,15 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.delegation(delegation)
|> :binary.encode_hex(:lowercase)
ForgeOperation.delegation(delegation) |> Base.encode16(case: :lower)

assert result ==
"6e0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914eff026fde46af0356a0476dae4e4600172dc9309b3aa4"

delegation = %{delegation | "delegate" => nil}

result =
ForgeOperation.delegation(delegation)
|> :binary.encode_hex(:lowercase)
ForgeOperation.delegation(delegation) |> Base.encode16(case: :lower)

assert result == "6e0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e00"
end
Expand All @@ -220,8 +212,7 @@ defmodule Tezex.ForgeOperationTest do
}

result =
ForgeOperation.activate_account(activation)
|> :binary.encode_hex(:lowercase)
ForgeOperation.activate_account(activation) |> Base.encode16(case: :lower)

assert result ==
"040cb9f9da085607c05cac1ca4c62a3f3cfb8146aa9b7f631e52f877a1d363474404da8130b0b940ee"
Expand Down

0 comments on commit 89603ff

Please sign in to comment.