From 0021d4034475d69507003a405795278fd5345da3 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 5 Feb 2019 11:49:27 +0300 Subject: [PATCH 1/6] add external libraries to smart contract verifier --- .../lib/explorer/smart_contract/verifier.ex | 12 ++++++++ .../explorer/smart_contract/verifier_test.exs | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/apps/explorer/lib/explorer/smart_contract/verifier.ex b/apps/explorer/lib/explorer/smart_contract/verifier.ex index ca1384b67b..3291c87a68 100644 --- a/apps/explorer/lib/explorer/smart_contract/verifier.ex +++ b/apps/explorer/lib/explorer/smart_contract/verifier.ex @@ -15,6 +15,18 @@ defmodule Explorer.SmartContract.Verifier do def evaluate_authenticity(_, %{"contract_source_code" => ""}), do: {:error, :contract_source_code} + def evaluate_authenticity(address_hash, %{ + "name" => name, + "contract_source_code" => contract_source_code, + "optimization" => optimization, + "compiler_version" => compiler_version, + "external_libraries" => external_libraries + }) do + solc_output = CodeCompiler.run(name, compiler_version, contract_source_code, optimization, external_libraries) + + compare_bytecodes(solc_output, address_hash) + end + def evaluate_authenticity(address_hash, %{ "name" => name, "contract_source_code" => contract_source_code, diff --git a/apps/explorer/test/explorer/smart_contract/verifier_test.exs b/apps/explorer/test/explorer/smart_contract/verifier_test.exs index bb5d2ba5c5..7d5a14be0e 100644 --- a/apps/explorer/test/explorer/smart_contract/verifier_test.exs +++ b/apps/explorer/test/explorer/smart_contract/verifier_test.exs @@ -28,6 +28,34 @@ defmodule Explorer.SmartContract.VerifierTest do assert abi != nil end + test "verifies the generated bytecode with external libraries" do + contract_data = + "#{System.cwd!()}/test/support/fixture/smart_contract/compiler_tests.json" + |> File.read!() + |> Jason.decode!() + |> List.first() + + compiler_version = contract_data["compiler_version"] + external_libraries = contract_data["external_libraries"] + name = contract_data["name"] + optimize = contract_data["optimize"] + contract = contract_data["contract"] + expected_bytecode = contract_data["expected_bytecode"] + + contract_address = insert(:contract_address, contract_code: "0x" <> expected_bytecode) + + params = %{ + "contract_source_code" => contract, + "compiler_version" => compiler_version, + "name" => name, + "optimization" => optimize, + "external_libraries" => external_libraries + } + + assert {:ok, %{abi: abi}} = Verifier.evaluate_authenticity(contract_address.hash, params) + assert abi != nil + end + test "returns error when bytecode doesn't match", %{contract_code_info: contract_code_info} do contract_address = insert(:contract_address, contract_code: contract_code_info.bytecode) From 15a8406dffc8cfeb3a8f97c8483de3f3330008d4 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 5 Feb 2019 15:05:43 +0300 Subject: [PATCH 2/6] add view field for external libraries --- .../new.html.eex | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex index 2808178963..fa255c4e78 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex @@ -49,6 +49,58 @@ <%= error_tag f, :contract_source_code, id: "contract-source-code-help-block", class: "text-danger", "data-test": "contract-source-code-error" %> +

<%= gettext "Contract Libraries" %>

+ +
+ <%= label :external_libraries, :library1, gettext("1 Library Name") %> + <%= text_input :external_libraries, :library1_name, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library1, gettext("1 Library Address") %> + <%= text_input :external_libraries, :library1_address, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library2, gettext("2 Library Name") %> + <%= text_input :external_libraries, :library2_name, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library2, gettext("2 Library Address") %> + <%= text_input :external_libraries, :library2_address, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library3, gettext("3 Library Name") %> + <%= text_input :external_libraries, :library3_name, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library3, gettext("3 Library Address") %> + <%= text_input :external_libraries, :library3_address, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library4, gettext("4 Library Name") %> + <%= text_input :external_libraries, :library4_name, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library4, gettext("4 Library Address") %> + <%= text_input :external_libraries, :library4_address, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label :external_libraries, :library5, gettext("5 Library Name") %> + <%= text_input :external_libraries, :library5_name, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+ +
+ <%= label f, :library5, gettext("5 Library Address") %> + <%= text_input f, :library5_address, class: "form-control", "aria-describedby": "contract-name-help-block", "data-test": "contract_name" %> +
+