Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

ExternalLedgerDB.Wallet schema #806

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion .circleci/config.yml
Expand Up @@ -200,7 +200,10 @@ jobs:
make test-ewallet
environment:
DATABASE_URL: postgresql://postgres:@localhost:5432/ewallet
LOCAL_LEDGER_DATABASE_URL: postgresql://postgres:@localhost:5432/ledger
LOCAL_LEDGER_DATABASE_URL: postgresql://postgres:@localhost:5432/local_ledger
EXTERNAL_LEDGER_DATABASE_URL: postgresql://postgres:@localhost:5432/external_ledger
USE_JUNIT: 1
MIX_ENV: test
- run:
name: Running eWallet assets tests
command: |
Expand Down
4 changes: 2 additions & 2 deletions apps/activity_logger/config/test.exs
Expand Up @@ -5,5 +5,5 @@ config :activity_logger, ActivityLogger.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
url: {:system, "DATABASE_URL", "postgres://localhost/ewallet_test"},
migration_timestamps: [type: :naive_datetime_usec],
queue_target: 1_000,
queue_interval: 5_000
queue_target: 5_000,
queue_interval: 10_000
4 changes: 2 additions & 2 deletions apps/ewallet_config/config/test.exs
Expand Up @@ -5,5 +5,5 @@ config :ewallet_config, EWalletConfig.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
url: {:system, "DATABASE_URL", "postgres://localhost/ewallet_test"},
migration_timestamps: [type: :naive_datetime_usec],
queue_target: 1_000,
queue_interval: 5_000
queue_target: 5_000,
queue_interval: 10_000
4 changes: 2 additions & 2 deletions apps/ewallet_db/config/test.exs
Expand Up @@ -5,5 +5,5 @@ config :ewallet_db, EWalletDB.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
url: {:system, "DATABASE_URL", "postgres://localhost/ewallet_test"},
migration_timestamps: [type: :naive_datetime_usec],
queue_target: 1_000,
queue_interval: 5_000
queue_target: 5_000,
queue_interval: 10_000
16 changes: 1 addition & 15 deletions apps/ewallet_db/lib/ewallet_db.ex
@@ -1,18 +1,4 @@
# Copyright 2018 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Copyright 2017-2018 OmiseGO
# Copyright 2017-2019 OmiseGO
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion apps/ewallet_db/lib/ewallet_db/application.ex
Expand Up @@ -16,7 +16,7 @@ defmodule EWalletDB.Application do
@moduledoc """
The EWalletDB Data Store

Kebura's data store lives in this application.
The eWallet's data store lives in this application.
"""
use Application
alias Appsignal.Ecto
Expand Down
2 changes: 1 addition & 1 deletion apps/ewallet_db/lib/ewallet_db/user_query.ex
Expand Up @@ -54,7 +54,7 @@ defmodule EWalletDB.UserQuery do
:inner,
[u],
m in Membership,
u.uuid == m.user_uuid and m.account_uuid in ^account_uuids
on: u.uuid == m.user_uuid and m.account_uuid in ^account_uuids
)
|> distinct(true)
|> select([c], c)
Expand Down
7 changes: 7 additions & 0 deletions apps/external_ledger_db/README.md
@@ -0,0 +1,7 @@
# ExternalLedgerDB

`ExternalLedgerDB` is the eWallet's sub-app that stores data from the external ledgers,
such as the Ethereum blockchain, so that data can be provided to other sub-apps efficiently.

This is a part of the eWallet umbrella app. You can start this sub-application
by starting the global umbrella application rather that this one specifically.
7 changes: 7 additions & 0 deletions apps/external_ledger_db/config/config.exs
@@ -0,0 +1,7 @@
use Mix.Config

config :external_ledger_db,
ecto_repos: [ExternalLedgerDB.Repo],
settings: []

import_config "#{Mix.env()}.exs"
6 changes: 6 additions & 0 deletions apps/external_ledger_db/config/dev.exs
@@ -0,0 +1,6 @@
use Mix.Config

config :external_ledger_db, ExternalLedgerDB.Repo,
adapter: Ecto.Adapters.Postgres,
url: {:system, "EXTERNAL_LEDGER_DATABASE_URL", "postgres://localhost/external_ledger_dev"},
migration_timestamps: [type: :naive_datetime_usec]
6 changes: 6 additions & 0 deletions apps/external_ledger_db/config/prod.exs
@@ -0,0 +1,6 @@
use Mix.Config

config :external_ledger_db, ExternalLedgerDB.Repo,
adapter: Ecto.Adapters.Postgres,
url: {:system, "EXTERNAL_LEDGER_DATABASE_URL", "postgres://localhost/external_ledger"},
migration_timestamps: [type: :naive_datetime_usec]
9 changes: 9 additions & 0 deletions apps/external_ledger_db/config/test.exs
@@ -0,0 +1,9 @@
use Mix.Config

config :external_ledger_db, ExternalLedgerDB.Repo,
adapter: Ecto.Adapters.Postgres,
pool: Ecto.Adapters.SQL.Sandbox,
url: {:system, "EXTERNAL_LEDGER_DATABASE_URL", "postgres://localhost/external_ledger_test"},
migration_timestamps: [type: :naive_datetime_usec],
queue_target: 5_000,
queue_interval: 10_000
17 changes: 17 additions & 0 deletions apps/external_ledger_db/lib/external_ledger_db.ex
@@ -0,0 +1,17 @@
# Copyright 2017-2019 OmiseGO
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule ExternalLedgerDB do
@moduledoc false
end
49 changes: 49 additions & 0 deletions apps/external_ledger_db/lib/external_ledger_db/application.ex
@@ -0,0 +1,49 @@
# Copyright 2017-2019 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule ExternalLedgerDB.Application do
@moduledoc """
The ExternalLedgerDB data store.
"""
use Application
alias Appsignal.Ecto
alias EWalletConfig.Config

def start(_type, _args) do
import Supervisor.Spec
DeferredConfig.populate(:external_ledger_db)

settings = Application.get_env(:external_ledger_db, :settings)
Config.register_and_load(:external_ledger_db, settings)

ActivityLogger.configure(%{
ExternalLedgerDB.Wallet => %{type: "external_ledger_wallet", identifier: :id}
})

:telemetry.attach(
"appsignal-ecto",
[:external_ledger_db, :repo, :query],
&Ecto.handle_event/4,
nil
)

children = [
supervisor(ExternalLedgerDB.Repo, []),
supervisor(ExternalLedgerDB.Vault, [])
]

opts = [strategy: :one_for_one, name: ExternalLedgerDB.Supervisor]
Supervisor.start_link(children, opts)
end
end
33 changes: 33 additions & 0 deletions apps/external_ledger_db/lib/external_ledger_db/repo.ex
@@ -0,0 +1,33 @@
# Copyright 2017-2019 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule ExternalLedgerDB.Repo do
use Ecto.Repo,
otp_app: :external_ledger_db,
adapter: Ecto.Adapters.Postgres

# Workaround an issue where ecto.migrate task won't start the app
# thus DeferredConfig.populate is not getting called.
#
# Ecto itself only supports {:system, ENV_VAR} tuple, but not
# DeferredConfig's {:system, ENV_VAR, DEFAULT} tuple nor the
# {:apply, MFA} tuple.
#
# See also: https://github.com/mrluc/deferred_config/issues/2
def init(_, config) do
config
|> DeferredConfig.transform_cfg()
|> (fn updated -> {:ok, updated} end).()
end
end
19 changes: 19 additions & 0 deletions apps/external_ledger_db/lib/external_ledger_db/types/map.ex
@@ -0,0 +1,19 @@
# Copyright 2017-2019 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule ExternalLedgerDB.Encrypted.Map do
@moduledoc false

use Cloak.Fields.Map, vault: ExternalLedgerDB.Vault
end
52 changes: 52 additions & 0 deletions apps/external_ledger_db/lib/external_ledger_db/vault.ex
@@ -0,0 +1,52 @@
# Copyright 2017-2019 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule ExternalLedgerDB.Vault do
@moduledoc false
use Cloak.Vault, otp_app: :external_ledger_db

@impl GenServer
def init(config) do
env = Application.get_env(:ewallet, :env)

config =
Keyword.put(
config,
:ciphers,
default: {
Cloak.Ciphers.AES.GCM,
tag: "AES.GCM.V1", key: secret_key(env)
}
)

{:ok, config}
end

defp secret_key(t) when is_binary(t) do
t
|> String.to_atom()
|> secret_key()
end

defp secret_key(:prod) do
"EXTERNAL_LEDGER_SECRET_KEY"
|> System.get_env()
|> Base.decode64!()
end

defp secret_key(_) do
<<81, 98, 218, 231, 73, 11, 210, 156, 118, 252, 177, 144, 224, 97, 197, 156, 196, 13, 183, 9,
154, 170, 231, 61, 6, 26, 166, 46, 16, 246, 150, 61>>
end
end