4
0
Fork 0

Added keccak256 hash

contract-refactor
hswick 7 years ago
parent a6037e0850
commit 7abfae5b4a
  1. 7
      lib/exw3.ex
  2. 12
      test/exw3_test.exs

@ -1,4 +1,11 @@
defmodule ExW3 do defmodule ExW3 do
@spec keccak256(binary()) :: binary()
@doc "Returns a 0x prepended 32 byte hash of the input string"
def keccak256(string) do
Enum.join(["0x", ExthCrypto.Hash.Keccak.kec(string) |> Base.encode16(case: :lower)], "")
end
@spec bytes_to_string(binary()) :: binary() @spec bytes_to_string(binary()) :: binary()
@doc "converts Ethereum style bytes to string" @doc "converts Ethereum style bytes to string"
def bytes_to_string(bytes) do def bytes_to_string(bytes) do

@ -41,6 +41,18 @@ defmodule EXW3Test do
assert ExW3.block_number() == block_number + 5 assert ExW3.block_number() == block_number + 5
end end
test "keccak256 hash some data" do
hash = ExW3.keccak256("foo")
assert String.slice(hash, 0..1) == "0x"
num_bytes =
hash |>
String.slice(2..-1) |>
byte_size
assert trunc(num_bytes / 2) == 32
end
test "starts a Contract GenServer for simple storage contract", context do test "starts a Contract GenServer for simple storage contract", context do
ExW3.Contract.start_link(SimpleStorage, abi: context[:simple_storage_abi]) ExW3.Contract.start_link(SimpleStorage, abi: context[:simple_storage_abi])

Loading…
Cancel
Save