Skip to content

Commit

Permalink
Use standard hash algorithm for SC library hash function (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Oct 12, 2022
1 parent df6fd91 commit 7ec19d4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
21 changes: 13 additions & 8 deletions lib/archethic/contracts/interpreter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1157,12 +1157,18 @@ defmodule Archethic.Contracts.Interpreter do
...> {:scope, [line: 3], nil},
...> ["next_transaction"],
...> {:&, [line: 3], [
...> {{:., [line: 3], [{:__aliases__, [alias: Archethic.Contracts.Interpreter.TransactionStatements], [:TransactionStatements]}, :add_uco_transfer]},
...> [line: 3], [{:&, [line: 3], [1]},
...> [{"to", {{:., [line: 3], [{:__aliases__, [alias: Archethic.Contracts.Interpreter.Library], [:Library]}, :hash]}, [line: 3], ["@Alice2"]}}, {"amount", 10.04}]]}
...> ]}
...> {
...> {:., [line: 3], [{:__aliases__, [alias: Archethic.Contracts.Interpreter.TransactionStatements], [:TransactionStatements]}, :add_uco_transfer]},
...> [line: 3], [
...> {:&, [line: 3], [1]},
...> [
...> {"to", "005220865F2237E3B62FFAA2AB72260A9FA711FBADF7F1DA391AB02B93D9E0D4A3"},
...> {"amount", 10.04}
...> ]
...> ]
...> }
...> ]}
...> ]
...> ]}]
...> }
...> ]},
...> }]}, :transaction)
Expand All @@ -1173,8 +1179,7 @@ defmodule Archethic.Contracts.Interpreter do
uco: %UCOLedger{
transfers: [
%UCOTransfer{
to: <<0, 252, 103, 8, 52, 151, 127, 195, 65, 104, 171, 247, 238, 227, 111, 140, 89,
49, 204, 58, 141, 215, 66, 253, 40, 183, 165, 117, 120, 80, 100, 232, 95>>,
to: <<0, 82, 32, 134, 95, 34, 55, 227, 182, 47, 250, 162, 171, 114, 38, 10, 159, 167, 17, 251, 173, 247, 241, 218, 57, 26, 176, 43, 147, 217, 224, 212, 163>>,
amount: 10.04
}
]
Expand Down Expand Up @@ -1225,7 +1230,7 @@ defmodule Archethic.Contracts.Interpreter do
...> :hash
...> ]}, [line: 2],
...> [{:get_in, [line: 2], [{:scope, [line: 2], nil}, ["content"]]}]}, %{ "content" => "abc" })
<<0, 186, 120, 22, 191, 143, 1, 207, 234, 65, 65, 64, 222, 93, 174, 34, 35, 176, 3, 97, 163, 150, 23, 122, 156, 180, 16, 255, 97, 242, 0, 21, 173>>
<<186, 120, 22, 191, 143, 1, 207, 234, 65, 65, 64, 222, 93, 174, 34, 35, 176, 3, 97, 163, 150, 23, 122, 156, 180, 16, 255, 97, 242, 0, 21, 173>>
"""
def execute(quoted_code, constants = %{}) do
{res, _} = Code.eval_quoted(quoted_code, scope: constants)
Expand Down
6 changes: 2 additions & 4 deletions lib/archethic/contracts/interpreter/library.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Library do
@moduledoc false

alias Archethic.Crypto

alias Archethic.Election

alias Archethic.P2P
Expand Down Expand Up @@ -111,12 +109,12 @@ defmodule Archethic.Contracts.Interpreter.Library do
## Examples
iex> Library.hash("hello")
<<0, 44, 242, 77, 186, 95, 176, 163, 14, 38, 232, 59, 42, 197, 185, 226, 158,
<<44, 242, 77, 186, 95, 176, 163, 14, 38, 232, 59, 42, 197, 185, 226, 158,
27, 22, 30, 92, 31, 167, 66, 94, 115, 4, 51, 98, 147, 139, 152, 36>>
"""
@spec hash(binary()) :: binary()
def hash(content) when is_binary(content) do
Crypto.hash(content)
:crypto.hash(:sha256, content)
end

@doc """
Expand Down
3 changes: 1 addition & 2 deletions test/archethic/contracts/interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Archethic.Contracts.InterpreterTest do

alias Archethic.Contracts.Interpreter

alias Archethic.Crypto
alias Archethic.P2P
alias Archethic.P2P.Node
alias Archethic.P2P.Message.FirstAddress
Expand Down Expand Up @@ -519,7 +518,7 @@ defmodule Archethic.Contracts.InterpreterTest do

assert true ==
Interpreter.valid_conditions?(transaction_conditions, %{
"transaction" => %{"content" => Crypto.hash(code)},
"transaction" => %{"content" => :crypto.hash(:sha256, code)},
"contract" => %{"code" => code}
})
end
Expand Down

0 comments on commit 7ec19d4

Please sign in to comment.