Need to add unique constraints and chengeset for hash fields

account
Никита Поздняков 2 years ago committed by Viktor Baranov
parent 4e9e7acd0d
commit 053b2db32e
  1. 33
      apps/explorer/lib/encrypt.ex
  2. 16
      apps/explorer/lib/explorer/account/custom_abi.ex
  3. 36
      apps/explorer/lib/explorer/account/identity.ex
  4. 14
      apps/explorer/lib/explorer/account/tag_address.ex
  5. 14
      apps/explorer/lib/explorer/account/tag_transaction.ex
  6. 14
      apps/explorer/lib/explorer/account/watchlist_address.ex
  7. 38
      apps/explorer/lib/explorer/account/watchlist_notification.ex
  8. 9
      apps/explorer/priv/account/migrations/20220706114430_encrypt_account_data.exs
  9. 37
      apps/explorer/priv/account/migrations/20220706211444_set_new_indexes.exs
  10. 6
      apps/explorer/priv/repo/migrations/20220706211444_set_new_indexes.exs

@ -8,14 +8,15 @@ defmodule Mix.Tasks.Encrypt do
Explorer.Account.Identity Explorer.Account.Identity
|> Explorer.Repo.Account.all() |> Explorer.Repo.Account.all()
|> Enum.map(fn identity -> |> Enum.map(fn element ->
identity element
|> Ecto.Changeset.change(%{ |> Ecto.Changeset.change(%{
encrypted_uid: identity.uid, encrypted_uid: element.uid,
encrypted_email: identity.email, encrypted_email: element.email,
encrypted_name: identity.name, encrypted_name: element.name,
encrypted_nickname: identity.nickname, encrypted_nickname: element.nickname,
encrypted_avatar: identity.avatar encrypted_avatar: element.avatar,
uid_hash: Cloak.Ecto.SHA256.hash(element.uid)
}) })
|> Explorer.Repo.Account.update!() |> Explorer.Repo.Account.update!()
end) end)
@ -26,7 +27,8 @@ defmodule Mix.Tasks.Encrypt do
element element
|> Ecto.Changeset.change(%{ |> Ecto.Changeset.change(%{
encrypted_name: element.name, encrypted_name: element.name,
encrypted_address_hash: element.address_hash encrypted_address_hash: element.address_hash,
address_hash_hash: Cloak.Ecto.SHA256.hash(to_string(element.address_hash))
}) })
|> Explorer.Repo.Account.update!() |> Explorer.Repo.Account.update!()
end) end)
@ -37,7 +39,8 @@ defmodule Mix.Tasks.Encrypt do
element element
|> Ecto.Changeset.change(%{ |> Ecto.Changeset.change(%{
encrypted_name: element.name, encrypted_name: element.name,
encrypted_tx_hash: element.tx_hash encrypted_tx_hash: element.tx_hash,
tx_hash_hash: Cloak.Ecto.SHA256.hash(to_string(element.tx_hash))
}) })
|> Explorer.Repo.Account.update!() |> Explorer.Repo.Account.update!()
end) end)
@ -48,7 +51,8 @@ defmodule Mix.Tasks.Encrypt do
element element
|> Ecto.Changeset.change(%{ |> Ecto.Changeset.change(%{
encrypted_name: element.name, encrypted_name: element.name,
encrypted_address_hash: element.address_hash encrypted_address_hash: element.address_hash,
address_hash_hash: Cloak.Ecto.SHA256.hash(to_string(element.address_hash))
}) })
|> Explorer.Repo.Account.update!() |> Explorer.Repo.Account.update!()
end) end)
@ -59,7 +63,8 @@ defmodule Mix.Tasks.Encrypt do
element element
|> Ecto.Changeset.change(%{ |> Ecto.Changeset.change(%{
encrypted_name: element.name, encrypted_name: element.name,
encrypted_address_hash: element.address_hash encrypted_address_hash: element.address_hash,
address_hash_hash: Cloak.Ecto.SHA256.hash(to_string(element.address_hash))
}) })
|> Explorer.Repo.Account.update!() |> Explorer.Repo.Account.update!()
end) end)
@ -73,7 +78,11 @@ defmodule Mix.Tasks.Encrypt do
encrypted_from_address_hash: element.from_address_hash, encrypted_from_address_hash: element.from_address_hash,
encrypted_to_address_hash: element.to_address_hash, encrypted_to_address_hash: element.to_address_hash,
encrypted_transaction_hash: element.transaction_hash, encrypted_transaction_hash: element.transaction_hash,
encrypted_subject: element.subject encrypted_subject: element.subject,
from_address_hash_hash: Cloak.Ecto.SHA256.hash(to_string(element.from_address_hash)),
to_address_hash_hash: Cloak.Ecto.SHA256.hash(to_string(element.to_address_hash)),
transaction_hash_hash: Cloak.Ecto.SHA256.hash(to_string(element.transaction_hash)),
subject_hash: Cloak.Ecto.SHA256.hash(element.subject)
}) })
|> Explorer.Repo.Account.update!() |> Explorer.Repo.Account.update!()
end) end)

@ -18,12 +18,16 @@ defmodule Explorer.Account.CustomABI do
field(:abi, {:array, :map}) field(:abi, {:array, :map})
field(:given_abi, :string, virtual: true) field(:given_abi, :string, virtual: true)
field(:abi_validating_error, :string, virtual: true) field(:abi_validating_error, :string, virtual: true)
# field(:name, :string)
# field(:address_hash, Hash.Address, null: false) field(:name, :string)
# field(:encrypted_address_hash, Explorer.Encrypted.AddressHash, null: false) field(:address_hash, Hash.Address, null: false)
# field(:encrypted_name, Explorer.Encrypted.Binary)
field(:address_hash, Explorer.Encrypted.AddressHash, null: false) field(:encrypted_address_hash, Explorer.Encrypted.AddressHash, null: false)
field(:name, Explorer.Encrypted.Binary) field(:encrypted_name, Explorer.Encrypted.Binary)
field(:address_hash_hash, Cloak.Ecto.SHA256)
# field(:address_hash, Explorer.Encrypted.AddressHash, null: false)
# field(:name, Explorer.Encrypted.Binary)
belongs_to(:identity, Identity) belongs_to(:identity, Identity)

@ -11,23 +11,25 @@ defmodule Explorer.Account.Identity do
alias Explorer.Account.{TagAddress, Watchlist} alias Explorer.Account.{TagAddress, Watchlist}
schema "account_identities" do schema "account_identities" do
# field(:uid, :string) field(:uid, :string)
# field(:email, :string) field(:email, :string)
# field(:name, :string) field(:name, :string)
# field(:nickname, :string) field(:nickname, :string)
# field(:avatar, :string) field(:avatar, :string)
# field(:encrypted_uid, Explorer.Encrypted.Binary) field(:encrypted_uid, Explorer.Encrypted.Binary)
# field(:encrypted_email, Explorer.Encrypted.Binary) field(:encrypted_email, Explorer.Encrypted.Binary)
# field(:encrypted_name, Explorer.Encrypted.Binary) field(:encrypted_name, Explorer.Encrypted.Binary)
# field(:encrypted_nickname, Explorer.Encrypted.Binary) field(:encrypted_nickname, Explorer.Encrypted.Binary)
# field(:encrypted_avatar, Explorer.Encrypted.Binary) field(:encrypted_avatar, Explorer.Encrypted.Binary)
field(:uid, Explorer.Encrypted.Binary) field(:uid_hash, Cloak.Ecto.SHA256)
field(:email, Explorer.Encrypted.Binary)
field(:name, Explorer.Encrypted.Binary) # field(:uid, Explorer.Encrypted.Binary)
field(:nickname, Explorer.Encrypted.Binary) # field(:email, Explorer.Encrypted.Binary)
field(:avatar, Explorer.Encrypted.Binary) # field(:name, Explorer.Encrypted.Binary)
# field(:nickname, Explorer.Encrypted.Binary)
# field(:avatar, Explorer.Encrypted.Binary)
has_many(:tag_addresses, TagAddress) has_many(:tag_addresses, TagAddress)
has_many(:watchlists, Watchlist) has_many(:watchlists, Watchlist)

@ -15,13 +15,15 @@ defmodule Explorer.Account.TagAddress do
@max_tag_address_per_account 15 @max_tag_address_per_account 15
schema "account_tag_addresses" do schema "account_tag_addresses" do
# field(:name, :string) field(:name, :string)
# field(:address_hash, Hash.Address, null: false) field(:address_hash, Hash.Address, null: false)
# field(:encrypted_name, Explorer.Encrypted.Binary) field(:encrypted_name, Explorer.Encrypted.Binary)
# field(:encrypted_address_hash, Explorer.Encrypted.AddressHash, null: false) field(:encrypted_address_hash, Explorer.Encrypted.AddressHash, null: false)
field(:name, Explorer.Encrypted.Binary) field(:address_hash_hash, Cloak.Ecto.SHA256)
field(:address_hash, Explorer.Encrypted.AddressHash, null: false)
# field(:name, Explorer.Encrypted.Binary)
# field(:address_hash, Explorer.Encrypted.AddressHash, null: false)
belongs_to(:identity, Identity) belongs_to(:identity, Identity)

@ -15,13 +15,15 @@ defmodule Explorer.Account.TagTransaction do
@max_tag_transaction_per_account 15 @max_tag_transaction_per_account 15
schema "account_tag_transactions" do schema "account_tag_transactions" do
# field(:name, :string) field(:name, :string)
# field(:tx_hash, Hash.Full, null: false) field(:tx_hash, Hash.Full, null: false)
# field(:encrypted_name, Explorer.Encrypted.Binary)
# field(:encrypted_tx_hash, Explorer.Encrypted.TransactionHash, null: false)
field(:name, Explorer.Encrypted.Binary) field(:encrypted_name, Explorer.Encrypted.Binary)
field(:tx_hash, Explorer.Encrypted.TransactionHash, null: false) field(:encrypted_tx_hash, Explorer.Encrypted.TransactionHash, null: false)
field(:tx_hash_hash, Cloak.Ecto.SHA256)
# field(:name, Explorer.Encrypted.Binary)
# field(:tx_hash, Explorer.Encrypted.TransactionHash, null: false)
belongs_to(:identity, Identity) belongs_to(:identity, Identity)

@ -16,13 +16,15 @@ defmodule Explorer.Account.WatchlistAddress do
@max_watchlist_addresses_per_account 10 @max_watchlist_addresses_per_account 10
schema "account_watchlist_addresses" do schema "account_watchlist_addresses" do
# field(:name, :string) field(:name, :string)
# field(:address_hash, Hash.Address, null: false) field(:address_hash, Hash.Address, null: false)
# field(:encrypted_name, Explorer.Encrypted.Binary) field(:encrypted_name, Explorer.Encrypted.Binary)
# field(:encrypted_address_hash, Explorer.Encrypted.AddressHash, null: false) field(:encrypted_address_hash, Explorer.Encrypted.AddressHash, null: false)
field(:name, Explorer.Encrypted.Binary) field(:address_hash_hash, Cloak.Ecto.SHA256)
field(:address_hash, Explorer.Encrypted.AddressHash, null: false)
# field(:name, Explorer.Encrypted.Binary)
# field(:address_hash, Explorer.Encrypted.AddressHash, null: false)
belongs_to(:watchlist, Watchlist) belongs_to(:watchlist, Watchlist)

@ -16,30 +16,38 @@ defmodule Explorer.Account.WatchlistNotification do
field(:block_number, :integer) field(:block_number, :integer)
field(:direction, :string) field(:direction, :string)
field(:method, :string) field(:method, :string)
# field(:name, :string)
# field(:subject, :string)
field(:tx_fee, :decimal) field(:tx_fee, :decimal)
field(:type, :string) field(:type, :string)
field(:viewed_at, :integer) field(:viewed_at, :integer)
# field(:encrypted_name, Explorer.Encrypted.Binary)
# field(:encrypted_subject, Explorer.Encrypted.Binary)
field(:name, Explorer.Encrypted.Binary) field(:name, :string)
field(:subject, Explorer.Encrypted.Binary) field(:subject, :string)
field(:encrypted_name, Explorer.Encrypted.Binary)
field(:encrypted_subject, Explorer.Encrypted.Binary)
# field(:name, Explorer.Encrypted.Binary)
# field(:subject, Explorer.Encrypted.Binary)
field(:subject_hash, Cloak.Ecto.SHA256)
belongs_to(:watchlist_address, WatchlistAddress) belongs_to(:watchlist_address, WatchlistAddress)
# field(:encrypted_from_address_hash, Explorer.Encrypted.AddressHash) field(:encrypted_from_address_hash, Explorer.Encrypted.AddressHash)
# field(:encrypted_to_address_hash, Explorer.Encrypted.AddressHash) field(:encrypted_to_address_hash, Explorer.Encrypted.AddressHash)
# field(:encrypted_transaction_hash, Explorer.Encrypted.TransactionHash) field(:encrypted_transaction_hash, Explorer.Encrypted.TransactionHash)
field(:from_address_hash, Hash.Address)
field(:to_address_hash, Hash.Address)
field(:transaction_hash, Hash.Full)
# field(:from_address_hash, Hash.Address) # field(:from_address_hash, Explorer.Encrypted.AddressHash)
# field(:to_address_hash, Hash.Address) # field(:to_address_hash, Explorer.Encrypted.AddressHash)
# field(:transaction_hash, Hash.Full) # field(:transaction_hash, Explorer.Encrypted.TransactionHash)
field(:from_address_hash, Explorer.Encrypted.AddressHash) field(:from_address_hash_hash, Cloak.Ecto.SHA256)
field(:to_address_hash, Explorer.Encrypted.AddressHash) field(:to_address_hash_hash, Cloak.Ecto.SHA256)
field(:transaction_hash, Explorer.Encrypted.TransactionHash) field(:transaction_hash_hash, Cloak.Ecto.SHA256)
timestamps() timestamps()
end end

@ -4,6 +4,7 @@ defmodule Explorer.Repo.Account.Migrations.EncryptAccountData do
def change do def change do
alter table(:account_identities) do alter table(:account_identities) do
add(:encrypted_uid, :binary) add(:encrypted_uid, :binary)
add(:uid_hash, :binary)
add(:encrypted_email, :binary) add(:encrypted_email, :binary)
add(:encrypted_name, :binary) add(:encrypted_name, :binary)
add(:encrypted_nickname, :binary, null: true) add(:encrypted_nickname, :binary, null: true)
@ -15,21 +16,25 @@ defmodule Explorer.Repo.Account.Migrations.EncryptAccountData do
# end # end
alter table(:account_custom_abis) do alter table(:account_custom_abis) do
add(:address_hash_hash, :binary)
add(:encrypted_address_hash, :binary) add(:encrypted_address_hash, :binary)
add(:encrypted_name, :binary) add(:encrypted_name, :binary)
end end
alter table(:account_tag_addresses) do alter table(:account_tag_addresses) do
add(:address_hash_hash, :binary)
add(:encrypted_name, :binary) add(:encrypted_name, :binary)
add(:encrypted_address_hash, :binary) add(:encrypted_address_hash, :binary)
end end
alter table(:account_tag_transactions) do alter table(:account_tag_transactions) do
add(:tx_hash_hash, :binary)
add(:encrypted_name, :binary) add(:encrypted_name, :binary)
add(:encrypted_tx_hash, :binary) add(:encrypted_tx_hash, :binary)
end end
alter table(:account_watchlist_addresses) do alter table(:account_watchlist_addresses) do
add(:address_hash_hash, :binary)
add(:encrypted_name, :binary) add(:encrypted_name, :binary)
add(:encrypted_address_hash, :binary) add(:encrypted_address_hash, :binary)
end end
@ -40,6 +45,10 @@ defmodule Explorer.Repo.Account.Migrations.EncryptAccountData do
add(:encrypted_from_address_hash, :binary) add(:encrypted_from_address_hash, :binary)
add(:encrypted_to_address_hash, :binary) add(:encrypted_to_address_hash, :binary)
add(:encrypted_transaction_hash, :binary) add(:encrypted_transaction_hash, :binary)
add(:subject_hash, :binary, null: true)
add(:from_address_hash_hash, :binary, null: true)
add(:to_address_hash_hash, :binary, null: true)
add(:transaction_hash_hash, :binary, null: true)
end end
end end
end end

@ -0,0 +1,37 @@
defmodule Explorer.Repo.Account.Migrations.SetNewIndexes do
use Ecto.Migration
def change do
drop_if_exists(unique_index(:account_tag_addresses, [:identity_id, :address_hash]))
drop_if_exists(unique_index(:account_tag_transactions, [:identity_id, :tx_hash]))
drop_if_exists(unique_index(:account_watchlist_addresses, [:watchlist_id, :address_hash]))
drop_if_exists(unique_index(:account_custom_abis, [:identity_id, :address_hash]))
drop_if_exists(index(:account_watchlist_notifications, [:transaction_hash]))
drop_if_exists(index(:account_watchlist_notifications, [:from_address_hash]))
drop_if_exists(index(:account_watchlist_notifications, [:to_address_hash]))
drop_if_exists(unique_index(:account_identities, [:uid]))
drop_if_exists(index(:account_tag_addresses, [:address_hash]))
drop_if_exists(index(:account_tag_transactions, [:tx_hash]))
drop_if_exists(index(:account_watchlist_addresses, [:address_hash]))
create(unique_index(:account_tag_addresses, [:identity_id, :address_hash_hash]))
create(unique_index(:account_tag_transactions, [:identity_id, :tx_hash_hash]))
create(unique_index(:account_watchlist_addresses, [:watchlist_id, :address_hash_hash]))
create(unique_index(:account_custom_abis, [:identity_id, :address_hash_hash]))
create(index(:account_watchlist_notifications, [:transaction_hash_hash]))
create(index(:account_watchlist_notifications, [:from_address_hash_hash]))
create(index(:account_watchlist_notifications, [:to_address_hash_hash]))
create(unique_index(:account_identities, [:uid_hash]))
create(index(:account_tag_addresses, [:address_hash_hash]))
create(index(:account_tag_transactions, [:tx_hash_hash]))
create(index(:account_watchlist_addresses, [:address_hash_hash]))
end
end

@ -0,0 +1,6 @@
defmodule Explorer.Repo.Migrations.SetNewIndexes do
use Ecto.Migration
def change do
end
end
Loading…
Cancel
Save