From 1b28df5c0957c2b2790c290aa28b73b32fae741a Mon Sep 17 00:00:00 2001 From: victor felder Date: Mon, 6 May 2024 10:52:38 +0200 Subject: [PATCH] fix: replace :binary.encode_hex/2 because it requires OTP26 --- lib/crypto/hmacdrbg.ex | 2 +- lib/forge.ex | 4 ++-- lib/forge_operation.ex | 3 ++- lib/micheline.ex | 4 ++-- lib/transaction.ex | 1 - test/crypto/hmacdrbg_test.exs | 6 +++--- test/forge_operation_test.exs | 27 +++++++++------------------ 7 files changed, 19 insertions(+), 28 deletions(-) diff --git a/lib/crypto/hmacdrbg.ex b/lib/crypto/hmacdrbg.ex index c6a870f..8586e70 100644 --- a/lib/crypto/hmacdrbg.ex +++ b/lib/crypto/hmacdrbg.ex @@ -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" + String.downcase(:binary.encode_hex(result)) == "a00cb0982eec3917b4b48abccfd460366d98b887943ff402bb7147cda174a46f" ``` Implementation inspired by both: diff --git a/lib/forge.ex b/lib/forge.ex index 2fc4fe2..da743c4 100644 --- a/lib/forge.ex +++ b/lib/forge.ex @@ -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" => String.downcase(:binary.encode_hex(val))}, ptr} _ -> raise "Unknown tag: #{tag} at position #{ptr}" @@ -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: String.downcase(:binary.encode_hex(data)) end diff --git a/lib/forge_operation.ex b/lib/forge_operation.ex index b2bd2e5..c930ea0 100644 --- a/lib/forge_operation.ex +++ b/lib/forge_operation.ex @@ -90,7 +90,8 @@ defmodule Tezex.ForgeOperation do Enum.join(Enum.map(operation_group["contents"], &operation/1)) ] |> IO.iodata_to_binary() - |> :binary.encode_hex(:lowercase) + |> :binary.encode_hex() + |> String.downcase() end @spec activate_account(map()) :: nonempty_binary() diff --git a/lib/micheline.ex b/lib/micheline.ex index 4e93a1d..20afb01 100644 --- a/lib/micheline.ex +++ b/lib/micheline.ex @@ -27,11 +27,11 @@ defmodule Tezex.Micheline do "0500" <> Zarith.encode(value) :string -> - hex_bytes = :binary.encode_hex(value, :lowercase) + hex_bytes = String.downcase(:binary.encode_hex(value)) "0501" <> encode_byte_size(hex_bytes) <> hex_bytes :bytes -> - hex_bytes = :binary.encode_hex(value, :lowercase) + hex_bytes = String.downcase(:binary.encode_hex(value)) "050a#{encode_byte_size(hex_bytes)}#{hex_bytes}" :key_hash -> diff --git a/lib/transaction.ex b/lib/transaction.ex index 8303f42..2decf08 100644 --- a/lib/transaction.ex +++ b/lib/transaction.ex @@ -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) diff --git a/test/crypto/hmacdrbg_test.exs b/test/crypto/hmacdrbg_test.exs index 3aedecb..0ac042e 100644 --- a/test/crypto/hmacdrbg_test.exs +++ b/test/crypto/hmacdrbg_test.exs @@ -10,7 +10,7 @@ defmodule Tezex.Crypto.HMACDRBGTest do {result, _} = HMACDRBG.generate(drbg, 32) - assert :binary.encode_hex(result, :lowercase) == + assert String.downcase(:binary.encode_hex(result)) == "018ec5f8e08c41e5ac974eb129ac297c5388ee1864324fa13d9b15cf98d9a157" end @@ -21,7 +21,7 @@ defmodule Tezex.Crypto.HMACDRBGTest do {result, _} = HMACDRBG.generate(drbg, 32) - assert :binary.encode_hex(result, :lowercase) == + assert String.downcase(:binary.encode_hex(result)) == "ed5d61ecf0ef38258e62f03bbb49f19f2cd07ba5145a840d83b134d5963b3633" end @@ -64,7 +64,7 @@ defmodule Tezex.Crypto.HMACDRBGTest do HMACDRBG.generate(drbg, 256, add) end) - assert :binary.encode_hex(result, :lowercase) == expected + assert String.downcase(:binary.encode_hex(result)) == expected end end end diff --git a/test/forge_operation_test.exs b/test/forge_operation_test.exs index b005830..5955900 100644 --- a/test/forge_operation_test.exs +++ b/test/forge_operation_test.exs @@ -17,8 +17,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.transaction(transaction) - |> :binary.encode_hex(:lowercase) + ForgeOperation.transaction(transaction) |> :binary.encode_hex() |> String.downcase() assert result == "6c0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e80ade204000154f5d8f71ce18f9f05bb885a4120e64c667bc1b400" @@ -36,8 +35,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.transaction(transaction) - |> :binary.encode_hex(:lowercase) + ForgeOperation.transaction(transaction) |> :binary.encode_hex() |> String.downcase() assert result == "6c00a8d45bdc966ddaaac83188a1e1c1fde2a3e05e5ca08d06c4e901b15b00904e01f61128c6abd2426d0c49b1fee1fa8c98dcc4ce0a0000" @@ -72,8 +70,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.transaction(transaction) - |> :binary.encode_hex(:lowercase) + ForgeOperation.transaction(transaction) |> :binary.encode_hex() |> String.downcase() assert result == "6c003ff84abc64319bda01968fd5269981d7615a6f75a08d06c4e901b15b0080c2d72f01fae98b912bb3644d56b8409cb98f40c779a9befe00ffff0a78747a546f546f6b656e0000005507070100000024747a3152686e47783968437862724e387a4b454b4c627755317a4b4c595a5471527336330707008b858b81c289bfcefc2a0100000018323032312d30312d32315431383a30393a31342e3531395a" @@ -91,8 +88,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.reveal(reveal) - |> :binary.encode_hex(:lowercase) + ForgeOperation.reveal(reveal) |> :binary.encode_hex() |> String.downcase() assert result == "6b0069ef8fb5d47d8a4321c94576a2316a632be8ce890094fe19904e00004c7b0501f6ea08f472b7e88791d3b8da49d64ac1e2c90f93c27e6531473305c6" @@ -134,8 +130,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.operation(origination) - |> :binary.encode_hex(:lowercase) + ForgeOperation.operation(origination) |> :binary.encode_hex() |> String.downcase() assert result == "6d0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e934e000000003702000000320500035b0501035b0502020000002303160743035b00010312074303690a000000080123456789abcdef0320053d036d034200000002001e" @@ -178,8 +173,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.operation(origination) - |> :binary.encode_hex(:lowercase) + ForgeOperation.operation(origination) |> :binary.encode_hex() |> String.downcase() assert result == "6d0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e934eff001392b07a567de5cb3a4301fbef2030696b4dfd8b0000003702000000320500035b0501035b0502020000002303160743035b00010312074303690a000000080123456789abcdef0320053d036d034200000002001e" @@ -197,8 +191,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.delegation(delegation) - |> :binary.encode_hex(:lowercase) + ForgeOperation.delegation(delegation) |> :binary.encode_hex() |> String.downcase() assert result == "6e0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914eff026fde46af0356a0476dae4e4600172dc9309b3aa4" @@ -206,8 +199,7 @@ defmodule Tezex.ForgeOperationTest do delegation = %{delegation | "delegate" => nil} result = - ForgeOperation.delegation(delegation) - |> :binary.encode_hex(:lowercase) + ForgeOperation.delegation(delegation) |> :binary.encode_hex() |> String.downcase() assert result == "6e0069ef8fb5d47d8a4321c94576a2316a632be8ce89904e09924e914e00" end @@ -220,8 +212,7 @@ defmodule Tezex.ForgeOperationTest do } result = - ForgeOperation.activate_account(activation) - |> :binary.encode_hex(:lowercase) + ForgeOperation.activate_account(activation) |> :binary.encode_hex() |> String.downcase() assert result == "040cb9f9da085607c05cac1ca4c62a3f3cfb8146aa9b7f631e52f877a1d363474404da8130b0b940ee"