From 9f6113b5f77eee05d7dda662d53c7b91e20e3021 Mon Sep 17 00:00:00 2001 From: Neylix Date: Tue, 12 Sep 2023 12:41:21 +0200 Subject: [PATCH] Change Contract.add_ownership api --- .../interpreter/legacy/action_interpreter.ex | 18 +- .../legacy/transaction_statements.ex | 75 +-- .../legacy/action_interpreter_test.exs | 467 ++++++++++++------ .../legacy/transaction_statements_test.exs | 2 + .../library/common/contract_test.exs | 81 ++- test/support/template.ex | 8 + 6 files changed, 407 insertions(+), 244 deletions(-) diff --git a/lib/archethic/contracts/interpreter/legacy/action_interpreter.ex b/lib/archethic/contracts/interpreter/legacy/action_interpreter.ex index b93016b5d0..c087871c6e 100644 --- a/lib/archethic/contracts/interpreter/legacy/action_interpreter.ex +++ b/lib/archethic/contracts/interpreter/legacy/action_interpreter.ex @@ -213,29 +213,13 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreter do end defp prewalk( - node = {{:atom, "secret_key"}, secret_key}, - _acc = {:ok, %{scope: {:function, "add_ownership", {:actions, _}}}} - ) - when not is_tuple(secret_key) and not is_binary(secret_key) do - throw({:error, "invalid add_ownership arguments", node}) - end - - defp prewalk( - node = {{:atom, "authorized_public_keys"}, authorized_public_keys}, + node = {{:atom, "authorized_keys"}, authorized_public_keys}, _acc = {:ok, %{scope: {:function, "add_ownership", {:actions, _}}}} ) when not is_tuple(authorized_public_keys) and not is_list(authorized_public_keys) do throw({:error, "invalid add_ownership arguments", node}) end - defp prewalk( - node = {{:atom, atom}, _}, - _acc = {:ok, %{scope: {:function, "add_ownership", {:actions, _}}}} - ) - when atom != "secret" and atom != "secret_key" and atom != "authorized_public_keys" do - throw({:error, "invalid add_ownership arguments", node}) - end - # Whitelist the keywords defp prewalk( node = {{:atom, _}, _}, diff --git a/lib/archethic/contracts/interpreter/legacy/transaction_statements.ex b/lib/archethic/contracts/interpreter/legacy/transaction_statements.ex index 68572ab136..176832e51f 100644 --- a/lib/archethic/contracts/interpreter/legacy/transaction_statements.ex +++ b/lib/archethic/contracts/interpreter/legacy/transaction_statements.ex @@ -9,6 +9,8 @@ defmodule Archethic.Contracts.Interpreter.Legacy.TransactionStatements do alias Archethic.Contracts.Interpreter.Legacy.UtilsInterpreter + alias Archethic.Contracts.Interpreter.Library + @doc """ Set the transaction type @@ -168,39 +170,48 @@ defmodule Archethic.Contracts.Interpreter.Legacy.TransactionStatements do ## Examples iex> %Transaction{data: %TransactionData{ownerships: [%Ownership{authorized_keys: authorized_keys}]}} = TransactionStatements.add_ownership(%Transaction{data: %TransactionData{}}, [ - ...> {"secret", "mysecret"}, - ...> {"secret_key", "62FE599BB217FC608D29E28C3FC4D825EA7989471261E43326FAB1A20A3C71B0"}, - ...> {"authorized_public_keys", [ - ...> "01000416A31DADE19AB4D9E7F22A4FA934694F265D0F20CB9D86B0B0B8FD28505CB6F9EF4D803AB5D2C49944DB0C24A12373F90A4406DBEF4577A9A59669DCAD10EBB6" - ...> ]} + ...> {"secret", random_secret()}, + ...> {"authorized_keys", %{ + ...> "000178321F76C48F2885A2EE209B2FB28A9FD2C8F1EBABBB6209F47D24BA10B73ED5" => random_encrypted_key() + ...> }} ...> ]) iex> Map.keys(authorized_keys) [ - <<1, 0, 4, 22, 163, 29, 173, 225, 154, 180, 217, 231, 242, 42, 79, 169, 52, 105, - 79, 38, 93, 15, 32, 203, 157, 134, 176, 176, 184, 253, 40, 80, 92, 182, 249, - 239, 77, 128, 58, 181, 210, 196, 153, 68, 219, 12, 36, 161, 35, 115, 249, 10, - 68, 6, 219, 239, 69, 119, 169, 165, 150, 105, 220, 173, 16, 235, 182>> + <<0, 1, 120, 50, 31, 118, 196, 143, 40, 133, 162, 238, 32, 155, 47, 178, 138, + 159, 210, 200, 241, 235, 171, 187, 98, 9, 244, 125, 36, 186, 16, 183, 62, + 213>> ] """ @spec add_ownership(Transaction.t(), list()) :: Transaction.t() def add_ownership(tx = %Transaction{}, args) when is_list(args) do - %{ - "secret" => secret, - "secret_key" => secret_key, - "authorized_public_keys" => authorized_public_keys - } = Enum.into(args, %{}) - - ownership = - Ownership.new( - UtilsInterpreter.maybe_decode_hex(secret), - UtilsInterpreter.maybe_decode_hex(secret_key), - Enum.map(authorized_public_keys, &UtilsInterpreter.get_public_key(&1, :add_ownership)) - ) + %{"secret" => secret, "authorized_keys" => authorized_keys} = Enum.into(args, %{}) + + authorized_keys = + Enum.map(authorized_keys, fn {pub, key} -> + decoded_pub = UtilsInterpreter.get_public_key(pub, :add_ownership) + decoded_key = UtilsInterpreter.maybe_decode_hex(key) + + if byte_size(decoded_key) != 80, + do: raise(Library.Error, message: "Encrypted key is not valid") + + {decoded_pub, UtilsInterpreter.maybe_decode_hex(key)} + end) + |> Enum.into(%{}) + + secret = UtilsInterpreter.maybe_decode_hex(secret) + + if byte_size(secret) != 32, + do: raise(Library.Error, message: "Secret is not valid") + + ownership = %Ownership{ + secret: secret, + authorized_keys: authorized_keys + } update_in( tx, [Access.key(:data, %{}), Access.key(:ownerships, [])], - &[ownership | &1] + &(&1 ++ [ownership]) ) end @@ -262,32 +273,32 @@ defmodule Archethic.Contracts.Interpreter.Legacy.TransactionStatements do iex> {pub_key1, _} = Archethic.Crypto.generate_deterministic_keypair("seed") iex> {pub_key2, _} = Archethic.Crypto.generate_deterministic_keypair("seed2") + iex> secret1 = random_secret() + iex> secret2 = random_secret() iex> %Transaction{ ...> data: %TransactionData{ ...> ownerships: [ ...> %Ownership{ ...> authorized_keys: %{ - ...> ^pub_key2 => _ + ...> ^pub_key1 => _ ...> }, - ...> secret: "ENCODED_SECRET2" + ...> secret: ^secret1 ...> }, ...> %Ownership{ ...> authorized_keys: %{ - ...> ^pub_key1 => _ + ...> ^pub_key2 => _ ...> }, - ...> secret: "ENCODED_SECRET1" + ...> secret: ^secret2 ...> } ...> ] ...> } ...> } = TransactionStatements.add_ownerships(%Transaction{data: %TransactionData{}}, [[ - ...> {"secret", "ENCODED_SECRET1"}, - ...> {"secret_key", :crypto.strong_rand_bytes(32)}, - ...> {"authorized_public_keys", [pub_key1]} + ...> {"secret", secret1}, + ...> {"authorized_keys", %{pub_key1 => random_encrypted_key()}} ...> ], ...> [ - ...> {"secret", "ENCODED_SECRET2"}, - ...> {"secret_key", :crypto.strong_rand_bytes(32)}, - ...> {"authorized_public_keys", [pub_key2]} + ...> {"secret", secret2}, + ...> {"authorized_keys", %{pub_key2 => random_encrypted_key()}} ...> ] ...> ]) """ diff --git a/test/archethic/contracts/interpreter/legacy/action_interpreter_test.exs b/test/archethic/contracts/interpreter/legacy/action_interpreter_test.exs index f5d1cc2dae..efe6c0e02c 100644 --- a/test/archethic/contracts/interpreter/legacy/action_interpreter_test.exs +++ b/test/archethic/contracts/interpreter/legacy/action_interpreter_test.exs @@ -35,27 +35,53 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do [line: 2], [ {:scope, [line: 2], nil}, - {:update_in, [line: 2], - [ - {:scope, [line: 2], nil}, - ["next_transaction"], - {:&, [line: 2], - [ - {{:., [line: 2], - [ - {:__aliases__, - [ - alias: - Archethic.Contracts.Interpreter.Legacy.TransactionStatements - ], [:TransactionStatements]}, - :set_type - ]}, [line: 2], [{:&, [line: 2], [1]}, "transfer"]} - ]} - ]} + { + :update_in, + [line: 2], + [ + {:scope, [line: 2], nil}, + ["next_transaction"], + { + :&, + [line: 2], + [ + { + { + :., + [line: 2], + [ + { + :__aliases__, + [ + alias: + Archethic.Contracts.Interpreter.Legacy.TransactionStatements + ], + [:TransactionStatements] + }, + :set_type + ] + }, + [line: 2], + [{:&, [line: 2], [1]}, "transfer"] + } + ] + } + ] + } ] }, - {{:., [], [{:__aliases__, [alias: false], [:Function]}, :identity]}, [], - [{:scope, [], nil}]} + { + { + :., + [], + [ + {:__aliases__, [alias: false], [:Function]}, + :identity + ] + }, + [], + [{:scope, [], nil}] + } ] }, { @@ -67,35 +93,62 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do [line: 3], [ {:scope, [line: 3], nil}, - {:update_in, [line: 3], - [ - {:scope, [line: 3], nil}, - ["next_transaction"], - {:&, [line: 3], - [ - {{:., [line: 3], - [ - {:__aliases__, - [ - alias: - Archethic.Contracts.Interpreter.Legacy.TransactionStatements - ], [:TransactionStatements]}, - :add_uco_transfer - ]}, [line: 3], - [ - {:&, [line: 3], [1]}, - [ - {"to", - "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3"}, - {"amount", 1_040_000_000} - ] - ]} - ]} - ]} + { + :update_in, + [line: 3], + [ + {:scope, [line: 3], nil}, + ["next_transaction"], + { + :&, + [line: 3], + [ + { + { + :., + [line: 3], + [ + { + :__aliases__, + [ + alias: + Archethic.Contracts.Interpreter.Legacy.TransactionStatements + ], + [:TransactionStatements] + }, + :add_uco_transfer + ] + }, + [line: 3], + [ + {:&, [line: 3], [1]}, + [ + { + "to", + "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3" + }, + {"amount", 1_040_000_000} + ] + ] + } + ] + } + ] + } ] }, - {{:., [], [{:__aliases__, [alias: false], [:Function]}, :identity]}, [], - [{:scope, [], nil}]} + { + { + :., + [], + [ + {:__aliases__, [alias: false], [:Function]}, + :identity + ] + }, + [], + [{:scope, [], nil}] + } ] }, { @@ -107,38 +160,67 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do [line: 4], [ {:scope, [line: 4], nil}, - {:update_in, [line: 4], - [ - {:scope, [line: 4], nil}, - ["next_transaction"], - {:&, [line: 4], - [ - {{:., [line: 4], - [ - {:__aliases__, - [ - alias: - Archethic.Contracts.Interpreter.Legacy.TransactionStatements - ], [:TransactionStatements]}, - :add_token_transfer - ]}, [line: 4], - [ - {:&, [line: 4], [1]}, - [ - {"to", - "30670455713E2CBECF94591226A903651ED8625635181DDA236FECC221D1E7E4"}, - {"amount", 20_000_000_000}, - {"token_address", - "AEB4A6F5AB6D82BE223C5867EBA5FE616F52F410DCF83B45AFF158DD40AE8AC3"}, - {"token_id", 0} - ] - ]} - ]} - ]} + { + :update_in, + [line: 4], + [ + {:scope, [line: 4], nil}, + ["next_transaction"], + { + :&, + [line: 4], + [ + { + { + :., + [line: 4], + [ + { + :__aliases__, + [ + alias: + Archethic.Contracts.Interpreter.Legacy.TransactionStatements + ], + [:TransactionStatements] + }, + :add_token_transfer + ] + }, + [line: 4], + [ + {:&, [line: 4], [1]}, + [ + { + "to", + "30670455713E2CBECF94591226A903651ED8625635181DDA236FECC221D1E7E4" + }, + {"amount", 20_000_000_000}, + { + "token_address", + "AEB4A6F5AB6D82BE223C5867EBA5FE616F52F410DCF83B45AFF158DD40AE8AC3" + }, + {"token_id", 0} + ] + ] + } + ] + } + ] + } ] }, - {{:., [], [{:__aliases__, [alias: false], [:Function]}, :identity]}, [], - [{:scope, [], nil}]} + { + { + :., + [], + [ + {:__aliases__, [alias: false], [:Function]}, + :identity + ] + }, + [], + [{:scope, [], nil}] + } ] }, { @@ -150,27 +232,53 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do [line: 5], [ {:scope, [line: 5], nil}, - {:update_in, [line: 5], - [ - {:scope, [line: 5], nil}, - ["next_transaction"], - {:&, [line: 5], - [ - {{:., [line: 5], - [ - {:__aliases__, - [ - alias: - Archethic.Contracts.Interpreter.Legacy.TransactionStatements - ], [:TransactionStatements]}, - :set_content - ]}, [line: 5], [{:&, [line: 5], [1]}, "Receipt"]} - ]} - ]} + { + :update_in, + [line: 5], + [ + {:scope, [line: 5], nil}, + ["next_transaction"], + { + :&, + [line: 5], + [ + { + { + :., + [line: 5], + [ + { + :__aliases__, + [ + alias: + Archethic.Contracts.Interpreter.Legacy.TransactionStatements + ], + [:TransactionStatements] + }, + :set_content + ] + }, + [line: 5], + [{:&, [line: 5], [1]}, "Receipt"] + } + ] + } + ] + } ] }, - {{:., [], [{:__aliases__, [alias: false], [:Function]}, :identity]}, [], - [{:scope, [], nil}]} + { + { + :., + [], + [ + {:__aliases__, [alias: false], [:Function]}, + :identity + ] + }, + [], + [{:scope, [], nil}] + } ] }, { @@ -182,38 +290,65 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do [line: 6], [ {:scope, [line: 6], nil}, - {:update_in, [line: 6], - [ - {:scope, [line: 6], nil}, - ["next_transaction"], - {:&, [line: 6], - [ - {{:., [line: 6], - [ - {:__aliases__, - [ - alias: - Archethic.Contracts.Interpreter.Legacy.TransactionStatements - ], [:TransactionStatements]}, - :add_ownership - ]}, [line: 6], - [ - {:&, [line: 6], [1]}, - [ - {"secret", "MyEncryptedSecret"}, - {"secret_key", "MySecretKey"}, - {"authorized_public_keys", + { + :update_in, + [line: 6], + [ + {:scope, [line: 6], nil}, + ["next_transaction"], + { + :&, + [line: 6], + [ + { + { + :., + [line: 6], + [ + { + :__aliases__, + [ + alias: + Archethic.Contracts.Interpreter.Legacy.TransactionStatements + ], + [:TransactionStatements] + }, + :add_ownership + ] + }, + [line: 6], + [ + {:&, [line: 6], [1]}, [ - "70C245E5D970B59DF65638BDD5D963EE22E6D892EA224D8809D0FB75D0B1907A" - ]} - ] - ]} - ]} - ]} + {"secret", "MyEncryptedSecret"}, + { + "authorized_keys", + [ + {"70C245E5D970B59DF65638BDD5D963EE22E6D892EA224D8809D0FB75D0B1907A", + "MySecretKey"} + ] + } + ] + ] + } + ] + } + ] + } ] }, - {{:., [], [{:__aliases__, [alias: false], [:Function]}, :identity]}, [], - [{:scope, [], nil}]} + { + { + :., + [], + [ + {:__aliases__, [alias: false], [:Function]}, + :identity + ] + }, + [], + [{:scope, [], nil}] + } ] }, { @@ -222,34 +357,59 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do [ { :=, - [line: 7], + [line: 10], [ - {:scope, [line: 7], nil}, - {:update_in, [line: 7], - [ - {:scope, [line: 7], nil}, - ["next_transaction"], - {:&, [line: 7], - [ - {{:., [line: 7], - [ - {:__aliases__, - [ - alias: - Archethic.Contracts.Interpreter.Legacy.TransactionStatements - ], [:TransactionStatements]}, - :add_recipient - ]}, [line: 7], - [ - {:&, [line: 7], [1]}, - "78273C5CBCEB8617F54380CC2F173DF2404DB676C9F10D546B6F395E6F3BDDEE" - ]} - ]} - ]} + {:scope, [line: 10], nil}, + { + :update_in, + [line: 10], + [ + {:scope, [line: 10], nil}, + ["next_transaction"], + { + :&, + [line: 10], + [ + { + { + :., + [line: 10], + [ + { + :__aliases__, + [ + alias: + Archethic.Contracts.Interpreter.Legacy.TransactionStatements + ], + [:TransactionStatements] + }, + :add_recipient + ] + }, + [line: 10], + [ + {:&, [line: 10], [1]}, + "78273C5CBCEB8617F54380CC2F173DF2404DB676C9F10D546B6F395E6F3BDDEE" + ] + } + ] + } + ] + } ] }, - {{:., [], [{:__aliases__, [alias: false], [:Function]}, :identity]}, [], - [{:scope, [], nil}]} + { + { + :., + [], + [ + {:__aliases__, [alias: false], [:Function]}, + :identity + ] + }, + [], + [{:scope, [], nil}] + } ] } ] @@ -261,7 +421,10 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do add_uco_transfer to: \"7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3\", amount: 1040000000 add_token_transfer to: \"30670455713E2CBECF94591226A903651ED8625635181DDA236FECC221D1E7E4\", amount: 20000000000, token_address: \"AEB4A6F5AB6D82BE223C5867EBA5FE616F52F410DCF83B45AFF158DD40AE8AC3\", token_id: 0 set_content \"Receipt\" - add_ownership secret: \"MyEncryptedSecret\", secret_key: \"MySecretKey\", authorized_public_keys: ["70C245E5D970B59DF65638BDD5D963EE22E6D892EA224D8809D0FB75D0B1907A"] + add_ownership( + secret: \"MyEncryptedSecret\", + authorized_keys: ["70C245E5D970B59DF65638BDD5D963EE22E6D892EA224D8809D0FB75D0B1907A": \"MySecretKey\"] + ) add_recipient \"78273C5CBCEB8617F54380CC2F173DF2404DB676C9F10D546B6F395E6F3BDDEE\" end """ @@ -578,10 +741,10 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ActionInterpreterTest do |> elem(1) |> ActionInterpreter.parse() - assert {:error, "invalid add_ownership arguments - authorized_public_keys"} = + assert {:error, "invalid add_ownership arguments - authorized_keys"} = ~S""" actions triggered_by: transaction do - add_ownership secret: "ABC123", secret_key: "s3cr3t", authorized_public_keys: 42 + add_ownership secret: "ABC123", authorized_keys: 42 end """ |> Interpreter.sanitize_code() diff --git a/test/archethic/contracts/interpreter/legacy/transaction_statements_test.exs b/test/archethic/contracts/interpreter/legacy/transaction_statements_test.exs index fb46cf36d0..8ad9f5bc11 100644 --- a/test/archethic/contracts/interpreter/legacy/transaction_statements_test.exs +++ b/test/archethic/contracts/interpreter/legacy/transaction_statements_test.exs @@ -14,5 +14,7 @@ defmodule Archethic.Contracts.Interpreter.Legacy.TransactionStatementsTest do alias Archethic.TransactionChain.TransactionData.UCOLedger alias Archethic.TransactionChain.TransactionData.UCOLedger.Transfer, as: UCOTransfer + import ArchethicCase + doctest TransactionStatements end diff --git a/test/archethic/contracts/interpreter/library/common/contract_test.exs b/test/archethic/contracts/interpreter/library/common/contract_test.exs index f3df7d8665..e32ad0e1d4 100644 --- a/test/archethic/contracts/interpreter/library/common/contract_test.exs +++ b/test/archethic/contracts/interpreter/library/common/contract_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Library.ContractTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.ContractTest do @moduledoc """ Here we test the contract module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -418,21 +418,20 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do test "should work with keyword" do {pub_key1, _} = Archethic.Crypto.generate_deterministic_keypair("seed") + secret = random_secret() + encrypted_key = random_encrypted_key() + code = ~s""" actions triggered_by: transaction do - Contract.add_ownership(secret: "ENCODED_SECRET1", authorized_public_keys: ["#{Base.encode16(pub_key1)}"], secret_key: "___") + authorized_key = Map.set(Map.new(), 0x#{Base.encode16(pub_key1)}, 0x#{Base.encode16(encrypted_key)}) + Contract.add_ownership(secret: 0x#{Base.encode16(secret)}, authorized_keys: authorized_key) end """ assert %Transaction{ data: %TransactionData{ ownerships: [ - %Ownership{ - authorized_keys: %{ - ^pub_key1 => _ - }, - secret: "ENCODED_SECRET1" - } + %Ownership{authorized_keys: %{^pub_key1 => ^encrypted_key}, secret: ^secret} ] } } = sanitize_parse_execute(code) @@ -442,28 +441,26 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do {pub_key1, _} = Archethic.Crypto.generate_deterministic_keypair("seed") {pub_key2, _} = Archethic.Crypto.generate_deterministic_keypair("seed2") + secret1 = random_secret() + secret2 = random_secret() + + encrypted_key1 = random_encrypted_key() + encrypted_key2 = random_encrypted_key() + code = ~s""" actions triggered_by: transaction do - Contract.add_ownership(secret: "ENCODED_SECRET1", authorized_public_keys: ["#{Base.encode16(pub_key1)}"], secret_key: "___") - Contract.add_ownership(secret: "ENCODED_SECRET2", authorized_public_keys: ["#{Base.encode16(pub_key2)}"], secret_key: "___") + authorized_key1 = Map.set(Map.new(), 0x#{Base.encode16(pub_key1)}, 0x#{Base.encode16(encrypted_key1)}) + authorized_key2 = Map.set(Map.new(), 0x#{Base.encode16(pub_key2)}, 0x#{Base.encode16(encrypted_key2)}) + Contract.add_ownership(secret: 0x#{Base.encode16(secret1)}, authorized_keys: authorized_key1) + Contract.add_ownership(secret: 0x#{Base.encode16(secret2)}, authorized_keys: authorized_key2) end """ assert %Transaction{ data: %TransactionData{ ownerships: [ - %Ownership{ - authorized_keys: %{ - ^pub_key2 => _ - }, - secret: "ENCODED_SECRET2" - }, - %Ownership{ - authorized_keys: %{ - ^pub_key1 => _ - }, - secret: "ENCODED_SECRET1" - } + %Ownership{authorized_keys: %{^pub_key1 => ^encrypted_key1}, secret: ^secret1}, + %Ownership{authorized_keys: %{^pub_key2 => ^encrypted_key2}, secret: ^secret2} ] } } = sanitize_parse_execute(code) @@ -472,9 +469,13 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do test "should work with variable" do {pub_key1, _} = Archethic.Crypto.generate_deterministic_keypair("seed") + secret = random_secret() + encrypted_key = random_encrypted_key() + code = ~s""" actions triggered_by: transaction do - ownership = [secret: "ENCODED_SECRET1", authorized_public_keys: ["#{Base.encode16(pub_key1)}"], secret_key: "___"] + authorized_key = Map.set(Map.new(), 0x#{Base.encode16(pub_key1)}, 0x#{Base.encode16(encrypted_key)}) + ownership = [secret: 0x#{Base.encode16(secret)}, authorized_keys: authorized_key] Contract.add_ownership(ownership) end """ @@ -482,12 +483,7 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do assert %Transaction{ data: %TransactionData{ ownerships: [ - %Ownership{ - authorized_keys: %{ - ^pub_key1 => _ - }, - secret: "ENCODED_SECRET1" - } + %Ownership{authorized_keys: %{^pub_key1 => ^encrypted_key}, secret: ^secret} ] } } = sanitize_parse_execute(code) @@ -631,11 +627,20 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do {pub_key1, _} = Archethic.Crypto.generate_deterministic_keypair("seed") {pub_key2, _} = Archethic.Crypto.generate_deterministic_keypair("seed2") + secret1 = random_secret() + secret2 = random_secret() + + encrypted_key1 = random_encrypted_key() + encrypted_key2 = random_encrypted_key() + code = ~s""" actions triggered_by: transaction do + authorized_key1 = Map.set(Map.new(), 0x#{Base.encode16(pub_key1)}, 0x#{Base.encode16(encrypted_key1)}) + authorized_key2 = Map.set(Map.new(), 0x#{Base.encode16(pub_key2)}, 0x#{Base.encode16(encrypted_key2)}) + ownerships = [ - [secret: "ENCODED_SECRET1", authorized_public_keys: ["#{Base.encode16(pub_key1)}"], secret_key: "___"], - [secret: "ENCODED_SECRET2", authorized_public_keys: ["#{Base.encode16(pub_key2)}"], secret_key: "___"] + [secret: 0x#{Base.encode16(secret1)}, authorized_keys: authorized_key1], + [secret: 0x#{Base.encode16(secret2)}, authorized_keys: authorized_key2] ] Contract.add_ownerships(ownerships) end @@ -644,18 +649,8 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do assert %Transaction{ data: %TransactionData{ ownerships: [ - %Ownership{ - authorized_keys: %{ - ^pub_key2 => _ - }, - secret: "ENCODED_SECRET2" - }, - %Ownership{ - authorized_keys: %{ - ^pub_key1 => _ - }, - secret: "ENCODED_SECRET1" - } + %Ownership{authorized_keys: %{^pub_key1 => ^encrypted_key1}, secret: ^secret1}, + %Ownership{authorized_keys: %{^pub_key2 => ^encrypted_key2}, secret: ^secret2} ] } } = sanitize_parse_execute(code) diff --git a/test/support/template.ex b/test/support/template.ex index bb2a8f6f6e..6b2220d410 100644 --- a/test/support/template.ex +++ b/test/support/template.ex @@ -255,6 +255,14 @@ defmodule ArchethicCase do <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>> end + def random_secret() do + :crypto.strong_rand_bytes(32) + end + + def random_encrypted_key() do + :crypto.strong_rand_bytes(80) + end + # sugar for readability def expect_not(mock, function_name, function) do expect(mock, function_name, 0, function)