Skip to content
Snippets Groups Projects
Commit 0e9c71fc authored by ABDELGHANI Nassim's avatar ABDELGHANI Nassim Committed by ABDELGHANI Nassim
Browse files

add transaction hash computation

parent 23139a3d
Branches
Tags
1 merge request!46Resolve "Validation locale"
......@@ -62,6 +62,35 @@ defmodule Doc.Transaction do
def verif(_), do: false
@doc """
Compute the hash of a transaction
## Example
iex> Doc.Transaction.hash(%{
...> "blockstamp" => "506140-00000006174C9FD4F4088D1CD830A1B372865977DBA7ED834AE94C6767EA42C6",
...> "comment" => "",
...> "currency" => "g1",
...> "inputs" => ["7500:0:T:05B64E84A7F6DECB91B8FAAE287676B121D9C3D3CCFD7EF57FD6F91B4B3E548C:1"],
...> "issuers" => ["7HzhXCA9Cp3JQE2GpY6JJwv89fJi6FQSBtDsq657jpj3"],
...> "locktime" => 0,
...> "outputs" => [
...> "2500:0:SIG(6SMqmUyJ7zCzht5twiQg6ePLiQ6HfLd3shhihMbAq5av)",
...> "5000:0:SIG(7HzhXCA9Cp3JQE2GpY6JJwv89fJi6FQSBtDsq657jpj3)"
...> ],
...> "signatures" => [
...> "/FC7D8TMkDuUn+0MNKSbFM/4x2dIWo/cQkGIj4KKTHjcm2F900P/nC9YyWvNUQLrhwD5lIZ6v8iSGyFAVFiVAw=="
...> ],
...> "unlocks" => ["0:SIG(0)"],
...> "version" => 10
...> })
"BB62BC1515CC1BE35993CC05EA8702A358763C3315DCE321C4E9D5B60E2A1D6B"
"""
def hash(transaction) do
:crypto.hash(:sha256, raw_format(transaction, true))
|> Base.encode16()
|> String.upcase()
end
defp regex_exact_match(regex, string) do
Regex.compile!("^#{regex}$")
|> Regex.match?(string)
......@@ -114,7 +143,7 @@ defmodule Doc.Transaction do
raw <> "#{line}\n"
end
defp raw_format(transaction) do
defp raw_format(transaction, signature \\ false) do
# Convert to the raw format of DUBP
"""
Version: #{transaction["version"]}
......@@ -132,5 +161,10 @@ defmodule Doc.Transaction do
|> add_line("Outputs:")
|> add_list_entries(transaction["outputs"])
|> add_line("Comment: #{transaction["comment"]}")
|> then(fn raw_without_signatures ->
if signature,
do: raw_without_signatures |> add_list_entries(transaction["signatures"]),
else: raw_without_signatures
end)
end
end
......@@ -36,6 +36,26 @@ defmodule Doc.TransactionTest do
]
}
@transaction_hash %{
"blockstamp" => "506140-00000006174C9FD4F4088D1CD830A1B372865977DBA7ED834AE94C6767EA42C6",
"blockstampTime" => 1_646_604_540,
"comment" => "",
"currency" => "g1",
"hash" => "BB62BC1515CC1BE35993CC05EA8702A358763C3315DCE321C4E9D5B60E2A1D6B",
"inputs" => ["7500:0:T:05B64E84A7F6DECB91B8FAAE287676B121D9C3D3CCFD7EF57FD6F91B4B3E548C:1"],
"issuers" => ["7HzhXCA9Cp3JQE2GpY6JJwv89fJi6FQSBtDsq657jpj3"],
"locktime" => 0,
"outputs" => [
"2500:0:SIG(6SMqmUyJ7zCzht5twiQg6ePLiQ6HfLd3shhihMbAq5av)",
"5000:0:SIG(7HzhXCA9Cp3JQE2GpY6JJwv89fJi6FQSBtDsq657jpj3)"
],
"signatures" => [
"/FC7D8TMkDuUn+0MNKSbFM/4x2dIWo/cQkGIj4KKTHjcm2F900P/nC9YyWvNUQLrhwD5lIZ6v8iSGyFAVFiVAw=="
],
"unlocks" => ["0:SIG(0)"],
"version" => 10
}
def replaceKey(map, old_name, new_name) do
{value, updated_map} = Map.pop(map, old_name)
Map.put(updated_map, new_name, value)
......@@ -96,4 +116,8 @@ defmodule Doc.TransactionTest do
)
)
end
test "transaction hash" do
assert Doc.Transaction.hash(@transaction_hash) == @transaction_hash["hash"]
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment