├── lib
├── hello_blockchain_web
│ ├── templates
│ │ ├── block
│ │ │ ├── show.html.eex
│ │ │ ├── new.html.eex
│ │ │ ├── edit.html.eex
│ │ │ ├── form.html.eex
│ │ │ └── index.html.eex
│ │ ├── header
│ │ │ └── show.html.eex
│ │ ├── layout
│ │ │ └── app.html.eex
│ │ └── page
│ │ │ └── index.html.eex
│ ├── views
│ │ ├── page_view.ex
│ │ ├── layout_view.ex
│ │ ├── error_view.ex
│ │ ├── block_view.ex
│ │ ├── header_view.ex
│ │ └── error_helpers.ex
│ ├── controllers
│ │ ├── page_controller.ex
│ │ ├── block_controller.ex
│ │ └── header_controller.ex
│ ├── router.ex
│ ├── gettext.ex
│ ├── channels
│ │ └── user_socket.ex
│ └── endpoint.ex
├── hello_blockchain.ex
├── hello_blockchain
│ ├── repo.ex
│ ├── blockchain
│ │ └── blockchain.ex
│ └── application.ex
└── hello_blockchain_web.ex
├── assets
├── static
│ ├── favicon.ico
│ ├── images
│ │ └── phoenix.png
│ └── robots.txt
├── package.json
├── css
│ └── app.css
├── js
│ ├── app.js
│ └── socket.js
└── brunch-config.js
├── test
├── test_helper.exs
├── hello_blockchain_web
│ ├── views
│ │ ├── page_view_test.exs
│ │ ├── layout_view_test.exs
│ │ └── error_view_test.exs
│ └── controllers
│ │ ├── page_controller_test.exs
│ │ └── block_controller_test.exs
├── support
│ ├── channel_case.ex
│ ├── conn_case.ex
│ └── data_case.ex
└── hello_blockchain
│ └── blockchain
│ └── blockchain_test.exs
├── priv
├── repo
│ └── seeds.exs
└── gettext
│ ├── en
│ └── LC_MESSAGES
│ │ └── errors.po
│ └── errors.pot
├── config
├── test.exs
├── config.exs
├── dev.exs
└── prod.exs
├── .gitignore
├── README.md
├── mix.exs
└── mix.lock
/lib/hello_blockchain_web/templates/block/show.html.eex:
--------------------------------------------------------------------------------
1 | <%= raw(mark_up_block(@block)) %>
2 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/templates/header/show.html.eex:
--------------------------------------------------------------------------------
1 | <%= raw(mark_up_block(@block)) %>
2 |
--------------------------------------------------------------------------------
/assets/static/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pcorey/hello_blockchain/HEAD/assets/static/favicon.ico
--------------------------------------------------------------------------------
/test/test_helper.exs:
--------------------------------------------------------------------------------
1 | ExUnit.start()
2 |
3 | Ecto.Adapters.SQL.Sandbox.mode(HelloBlockchain.Repo, :manual)
4 |
5 |
--------------------------------------------------------------------------------
/assets/static/images/phoenix.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pcorey/hello_blockchain/HEAD/assets/static/images/phoenix.png
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/views/page_view.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.PageView do
2 | use HelloBlockchainWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/views/layout_view.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.LayoutView do
2 | use HelloBlockchainWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/test/hello_blockchain_web/views/page_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.PageViewTest do
2 | use HelloBlockchainWeb.ConnCase, async: true
3 | end
4 |
--------------------------------------------------------------------------------
/test/hello_blockchain_web/views/layout_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.LayoutViewTest do
2 | use HelloBlockchainWeb.ConnCase, async: true
3 | end
4 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/templates/block/new.html.eex:
--------------------------------------------------------------------------------
1 |
New Block
2 |
3 | <%= render "form.html", Map.put(assigns, :action, block_path(@conn, :create)) %>
4 |
5 | <%= link "Back", to: block_path(@conn, :index) %>
6 |
--------------------------------------------------------------------------------
/assets/static/robots.txt:
--------------------------------------------------------------------------------
1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
2 | #
3 | # To ban all spiders from the entire site uncomment the next two lines:
4 | # User-agent: *
5 | # Disallow: /
6 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/templates/block/edit.html.eex:
--------------------------------------------------------------------------------
1 | Edit Block
2 |
3 | <%= render "form.html", Map.put(assigns, :action, block_path(@conn, :update, @block)) %>
4 |
5 | <%= link "Back", to: block_path(@conn, :index) %>
6 |
--------------------------------------------------------------------------------
/test/hello_blockchain_web/controllers/page_controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.PageControllerTest do
2 | use HelloBlockchainWeb.ConnCase
3 |
4 | test "GET /", %{conn: conn} do
5 | conn = get conn, "/"
6 | assert html_response(conn, 200) =~ "Welcome to Phoenix!"
7 | end
8 | end
9 |
--------------------------------------------------------------------------------
/lib/hello_blockchain.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchain do
2 | @moduledoc """
3 | HelloBlockchain keeps the contexts that define your domain
4 | and business logic.
5 |
6 | Contexts are also responsible for managing your data, regardless
7 | if it comes from the database, an external API or others.
8 | """
9 | end
10 |
--------------------------------------------------------------------------------
/lib/hello_blockchain/repo.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchain.Repo do
2 | use Ecto.Repo, otp_app: :hello_blockchain
3 |
4 | @doc """
5 | Dynamically loads the repository url from the
6 | DATABASE_URL environment variable.
7 | """
8 | def init(_, opts) do
9 | {:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
10 | end
11 | end
12 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/controllers/page_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.PageController do
2 | use HelloBlockchainWeb, :controller
3 |
4 | alias HelloBlockchain.Blockchain
5 |
6 | def index(conn, _params) do
7 | with {:ok, hash} <- Blockchain.getbestblockhash() do
8 | redirect(conn, to: header_path(conn, :show, hash))
9 | end
10 | end
11 |
12 | end
13 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/templates/block/form.html.eex:
--------------------------------------------------------------------------------
1 | <%= form_for @changeset, @action, fn f -> %>
2 | <%= if @changeset.action do %>
3 |
4 |
Oops, something went wrong! Please check the errors below.
5 |
6 | <% end %>
7 |
8 |
9 | <%= submit "Submit", class: "btn btn-primary" %>
10 |
11 | <% end %>
12 |
--------------------------------------------------------------------------------
/priv/repo/seeds.exs:
--------------------------------------------------------------------------------
1 | # Script for populating the database. You can run it as:
2 | #
3 | # mix run priv/repo/seeds.exs
4 | #
5 | # Inside the script, you can read and write to any of your
6 | # repositories directly:
7 | #
8 | # HelloBlockchain.Repo.insert!(%HelloBlockchain.SomeSchema{})
9 | #
10 | # We recommend using the bang functions (`insert!`, `update!`
11 | # and so on) as they will fail if something goes wrong.
12 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/views/error_view.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.ErrorView do
2 | use HelloBlockchainWeb, :view
3 |
4 | def render("404.html", _assigns) do
5 | "Page not found"
6 | end
7 |
8 | def render("500.html", _assigns) do
9 | "Internal server error"
10 | end
11 |
12 | # In case no render clause matches or no
13 | # template is found, let's render it as 500
14 | def template_not_found(_template, assigns) do
15 | render "500.html", assigns
16 | end
17 | end
18 |
--------------------------------------------------------------------------------
/assets/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "repository": {},
3 | "license": "MIT",
4 | "scripts": {
5 | "deploy": "brunch build --production",
6 | "watch": "brunch watch --stdin"
7 | },
8 | "dependencies": {
9 | "babel-preset-latest": "^6.24.1",
10 | "phoenix": "file:../deps/phoenix",
11 | "phoenix_html": "file:../deps/phoenix_html"
12 | },
13 | "devDependencies": {
14 | "babel-brunch": "6.1.1",
15 | "brunch": "2.10.9",
16 | "clean-css-brunch": "2.10.0",
17 | "uglify-js-brunch": "2.10.0"
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/controllers/block_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.BlockController do
2 | use HelloBlockchainWeb, :controller
3 |
4 | alias HelloBlockchain.Blockchain
5 |
6 | def index(conn, _params) do
7 | with {:ok, hash} <- Blockchain.getbestblockhash() do
8 | redirect(conn, to: block_path(conn, :show, hash))
9 | end
10 | end
11 |
12 | def show(conn, %{"id" => hash}) do
13 | with {:ok, block} <- Blockchain.getblock(hash) do
14 | render(conn, "show.html", block: block)
15 | end
16 | end
17 |
18 | end
19 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/controllers/header_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.HeaderController do
2 | use HelloBlockchainWeb, :controller
3 |
4 | alias HelloBlockchain.Blockchain
5 |
6 | def index(conn, _params) do
7 | with {:ok, hash} <- Blockchain.getbestblockhash() do
8 | redirect(conn, to: header_path(conn, :show, hash))
9 | end
10 | end
11 |
12 | def show(conn, %{"id" => hash}) do
13 | with {:ok, block} <- Blockchain.getblockheader(hash) do
14 | render(conn, "show.html", block: block)
15 | end
16 | end
17 |
18 | end
19 |
--------------------------------------------------------------------------------
/config/test.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # We don't run a server during test. If one is required,
4 | # you can enable the server option below.
5 | config :hello_blockchain, HelloBlockchainWeb.Endpoint,
6 | http: [port: 4001],
7 | server: false
8 |
9 | # Print only warnings and errors during test
10 | config :logger, level: :warn
11 |
12 | # Configure your database
13 | config :hello_blockchain, HelloBlockchain.Repo,
14 | adapter: Ecto.Adapters.Postgres,
15 | username: "postgres",
16 | password: "postgres",
17 | database: "hello_blockchain_test",
18 | hostname: "localhost",
19 | pool: Ecto.Adapters.SQL.Sandbox
20 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # App artifacts
2 | /_build
3 | /db
4 | /deps
5 | /*.ez
6 |
7 | # Generated on crash by the VM
8 | erl_crash.dump
9 |
10 | # Generated on crash by NPM
11 | npm-debug.log
12 |
13 | # Static artifacts
14 | /assets/node_modules
15 |
16 | # Since we are building assets from assets/,
17 | # we ignore priv/static. You may want to comment
18 | # this depending on your deployment strategy.
19 | /priv/static/
20 |
21 | # Files matching config/*.secret.exs pattern contain sensitive
22 | # data and you should not commit them into version control.
23 | #
24 | # Alternatively, you may comment the line below and commit the
25 | # secrets files as long as you replace their contents by environment
26 | # variables.
27 | /config/*.secret.exs
--------------------------------------------------------------------------------
/test/hello_blockchain_web/views/error_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.ErrorViewTest do
2 | use HelloBlockchainWeb.ConnCase, async: true
3 |
4 | # Bring render/3 and render_to_string/3 for testing custom views
5 | import Phoenix.View
6 |
7 | test "renders 404.html" do
8 | assert render_to_string(HelloBlockchainWeb.ErrorView, "404.html", []) ==
9 | "Page not found"
10 | end
11 |
12 | test "render 500.html" do
13 | assert render_to_string(HelloBlockchainWeb.ErrorView, "500.html", []) ==
14 | "Internal server error"
15 | end
16 |
17 | test "render any other" do
18 | assert render_to_string(HelloBlockchainWeb.ErrorView, "505.html", []) ==
19 | "Internal server error"
20 | end
21 | end
22 |
--------------------------------------------------------------------------------
/assets/css/app.css:
--------------------------------------------------------------------------------
1 | html {
2 | box-sizing: border-box;
3 | }
4 |
5 | *, *:before, *:after {
6 | box-sizing: inherit;
7 | }
8 |
9 |
10 | html {
11 | min-height: 100%;
12 | height: 100%;
13 | font-size: 14px;
14 | }
15 |
16 | body {
17 | margin: 0;
18 | min-height: 100%;
19 | height: 100%;
20 | }
21 |
22 | .container {
23 | min-height: 100%;
24 | width: fit-content;
25 | margin: 0 auto;
26 | padding: 4em 2em;
27 | }
28 |
29 | code {
30 | display: block !important;
31 | color: #666;
32 | font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
33 | white-space: pre !important;
34 | background: transparent !important;
35 | }
36 |
37 | a {
38 | color: #37f;
39 | font-weight: bold;
40 | }
--------------------------------------------------------------------------------
/assets/js/app.js:
--------------------------------------------------------------------------------
1 | // Brunch automatically concatenates all files in your
2 | // watched paths. Those paths can be configured at
3 | // config.paths.watched in "brunch-config.js".
4 | //
5 | // However, those files will only be executed if
6 | // explicitly imported. The only exception are files
7 | // in vendor, which are never wrapped in imports and
8 | // therefore are always executed.
9 |
10 | // Import dependencies
11 | //
12 | // If you no longer want to use a dependency, remember
13 | // to also remove its path from "config.paths.watched".
14 | import "phoenix_html"
15 |
16 | // Import local files
17 | //
18 | // Local files can be imported directly using relative
19 | // paths "./socket" or full ones "web/static/js/socket".
20 |
21 | // import socket from "./socket"
22 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/router.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.Router do
2 | use HelloBlockchainWeb, :router
3 |
4 | pipeline :browser do
5 | plug :accepts, ["html"]
6 | plug :fetch_session
7 | plug :fetch_flash
8 | plug :protect_from_forgery
9 | plug :put_secure_browser_headers
10 | end
11 |
12 | pipeline :api do
13 | plug :accepts, ["json"]
14 | end
15 |
16 | scope "/", HelloBlockchainWeb do
17 | pipe_through :browser # Use the default browser stack
18 | resources "/", PageController, only: [:index]
19 | resources "/blocks", BlockController, only: [:index, :show]
20 | resources "/headers", HeaderController, only: [:index, :show]
21 | end
22 |
23 | # Other scopes may use custom stacks.
24 | # scope "/api", HelloBlockchainWeb do
25 | # pipe_through :api
26 | # end
27 | end
28 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/views/block_view.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.BlockView do
2 | use HelloBlockchainWeb, :view
3 |
4 | # alias HelloBlockchainWeb.Endpoint
5 | # alias HelloBlockchainWeb.Router
6 | # alias Phoenix.HTML
7 | # alias Phoenix.HTML.Link
8 |
9 | defp hash_link(hash), do: "#{hash} "
10 |
11 | # defp hash_link(hash) do
12 | # Link.link(hash, to: Router.Helpers.block_path(Endpoint, :show, hash || ""))
13 | # |> HTML.safe_to_string
14 | # |> String.replace("\"", "", global: true)
15 | # end
16 |
17 | def mark_up_block(block) do
18 | block
19 | |> Map.replace("previousblockhash", hash_link(block["previousblockhash"]))
20 | |> Map.replace("nextblockhash", hash_link(block["nextblockhash"]))
21 | |> Poison.encode!(pretty: true)
22 | end
23 |
24 | end
25 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/views/header_view.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.HeaderView do
2 | use HelloBlockchainWeb, :view
3 |
4 | # alias HelloBlockchainWeb.Endpoint
5 | # alias HelloBlockchainWeb.Router
6 | # alias Phoenix.HTML
7 | # alias Phoenix.HTML.Link
8 |
9 | defp hash_link(hash), do: "#{hash} "
10 |
11 | # defp hash_link(hash) do
12 | # Link.link(hash, to: Router.Helpers.block_path(Endpoint, :show, hash || ""))
13 | # |> HTML.safe_to_string
14 | # |> String.replace("\"", "", global: true)
15 | # end
16 |
17 | def mark_up_block(block) do
18 | block
19 | |> Map.replace("previousblockhash", hash_link(block["previousblockhash"]))
20 | |> Map.replace("nextblockhash", hash_link(block["nextblockhash"]))
21 | |> Poison.encode!(pretty: true)
22 | end
23 |
24 | end
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HelloBlockchain
2 |
3 | To start your Phoenix server:
4 |
5 | * Install dependencies with `mix deps.get`
6 | * Create and migrate your database with `mix ecto.create && mix ecto.migrate`
7 | * Install Node.js dependencies with `cd assets && npm install`
8 | * Start Phoenix endpoint with `mix phx.server`
9 |
10 | Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
11 |
12 | Ready to run in production? Please [check our deployment guides](http://www.phoenixframework.org/docs/deployment).
13 |
14 | ## Learn more
15 |
16 | * Official website: http://www.phoenixframework.org/
17 | * Guides: http://phoenixframework.org/docs/overview
18 | * Docs: https://hexdocs.pm/phoenix
19 | * Mailing list: http://groups.google.com/group/phoenix-talk
20 | * Source: https://github.com/phoenixframework/phoenix
21 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/gettext.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.Gettext do
2 | @moduledoc """
3 | A module providing Internationalization with a gettext-based API.
4 |
5 | By using [Gettext](https://hexdocs.pm/gettext),
6 | your module gains a set of macros for translations, for example:
7 |
8 | import HelloBlockchainWeb.Gettext
9 |
10 | # Simple translation
11 | gettext "Here is the string to translate"
12 |
13 | # Plural translation
14 | ngettext "Here is the string to translate",
15 | "Here are the strings to translate",
16 | 3
17 |
18 | # Domain-based translation
19 | dgettext "errors", "Here is the error message to translate"
20 |
21 | See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
22 | """
23 | use Gettext, otp_app: :hello_blockchain
24 | end
25 |
--------------------------------------------------------------------------------
/lib/hello_blockchain/blockchain/blockchain.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchain.Blockchain do
2 |
3 | def bitcoin_rpc(method, params \\ []) do
4 | with url <- Application.get_env(:hello_blockchain, :bitcoin_url),
5 | command <- %{jsonrpc: "1.0", method: method, params: params},
6 | {:ok, body} <- Poison.encode(command),
7 | {:ok, response} <- HTTPoison.post(url, body),
8 | {:ok, metadata} <- Poison.decode(response.body),
9 | %{"error" => nil, "result" => result} <- metadata do
10 | {:ok, result}
11 | else
12 | %{"error" => reason} -> {:error, reason}
13 | error -> error
14 | end
15 | end
16 |
17 | def getbestblockhash, do: bitcoin_rpc("getbestblockhash")
18 |
19 | def getblockhash(height), do: bitcoin_rpc("getblockhash", [height])
20 |
21 | def getblock(hash), do: bitcoin_rpc("getblock", [hash])
22 |
23 | def getblockheader(hash), do: bitcoin_rpc("getblockheader", [hash])
24 |
25 | end
26 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/templates/layout/app.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | Hello HelloBlockchain!
11 | ">
12 |
13 |
14 |
15 |
16 |
17 | Hello blockchain
18 | [blocks] [headers]
19 |
20 |
21 | <%= render @view_module, @view_template, assigns %>
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/test/support/channel_case.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.ChannelCase do
2 | @moduledoc """
3 | This module defines the test case to be used by
4 | channel tests.
5 |
6 | Such tests rely on `Phoenix.ChannelTest` and also
7 | import other functionality to make it easier
8 | to build common datastructures and query the data layer.
9 |
10 | Finally, if the test case interacts with the database,
11 | it cannot be async. For this reason, every test runs
12 | inside a transaction which is reset at the beginning
13 | of the test unless the test case is marked as async.
14 | """
15 |
16 | use ExUnit.CaseTemplate
17 |
18 | using do
19 | quote do
20 | # Import conveniences for testing with channels
21 | use Phoenix.ChannelTest
22 |
23 | # The default endpoint for testing
24 | @endpoint HelloBlockchainWeb.Endpoint
25 | end
26 | end
27 |
28 |
29 | setup tags do
30 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(HelloBlockchain.Repo)
31 | unless tags[:async] do
32 | Ecto.Adapters.SQL.Sandbox.mode(HelloBlockchain.Repo, {:shared, self()})
33 | end
34 | :ok
35 | end
36 |
37 | end
38 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/templates/page/index.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
<%= gettext "Welcome to %{name}!", name: "Phoenix" %>
3 |
A productive web framework that does not compromise speed and maintainability.
4 |
5 |
6 |
37 |
--------------------------------------------------------------------------------
/config/config.exs:
--------------------------------------------------------------------------------
1 | # This file is responsible for configuring your application
2 | # and its dependencies with the aid of the Mix.Config module.
3 | #
4 | # This configuration file is loaded before any dependency and
5 | # is restricted to this project.
6 | use Mix.Config
7 |
8 | # General application configuration
9 | config :hello_blockchain,
10 | ecto_repos: [HelloBlockchain.Repo]
11 |
12 | config :hello_blockchain,
13 | bitcoin_url: "http://lolbitcoin:lolpassword@localhost:8332"
14 |
15 | # Configures the endpoint
16 | config :hello_blockchain, HelloBlockchainWeb.Endpoint,
17 | url: [host: "localhost"],
18 | secret_key_base: "iAk3XzhkACUWGx6517EPLuswkibOEagnemvzsz5czCR5h6sf52urW9w4NP+H2CV5",
19 | render_errors: [view: HelloBlockchainWeb.ErrorView, accepts: ~w(html json)],
20 | pubsub: [name: HelloBlockchain.PubSub,
21 | adapter: Phoenix.PubSub.PG2]
22 |
23 | # Configures Elixir's Logger
24 | config :logger, :console,
25 | format: "$time $metadata[$level] $message\n",
26 | metadata: [:request_id]
27 |
28 | # Import environment specific config. This must remain at the bottom
29 | # of this file so it overrides the configuration defined above.
30 | import_config "#{Mix.env}.exs"
31 |
--------------------------------------------------------------------------------
/lib/hello_blockchain/application.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchain.Application do
2 | use Application
3 |
4 | # See https://hexdocs.pm/elixir/Application.html
5 | # for more information on OTP Applications
6 | def start(_type, _args) do
7 | import Supervisor.Spec
8 |
9 | # Define workers and child supervisors to be supervised
10 | children = [
11 | # Start the Ecto repository
12 | supervisor(HelloBlockchain.Repo, []),
13 | # Start the endpoint when the application starts
14 | supervisor(HelloBlockchainWeb.Endpoint, []),
15 | # Start your own worker by calling: HelloBlockchain.Worker.start_link(arg1, arg2, arg3)
16 | # worker(HelloBlockchain.Worker, [arg1, arg2, arg3]),
17 | ]
18 |
19 | # See https://hexdocs.pm/elixir/Supervisor.html
20 | # for other strategies and supported options
21 | opts = [strategy: :one_for_one, name: HelloBlockchain.Supervisor]
22 | Supervisor.start_link(children, opts)
23 | end
24 |
25 | # Tell Phoenix to update the endpoint configuration
26 | # whenever the application is updated.
27 | def config_change(changed, _new, removed) do
28 | HelloBlockchainWeb.Endpoint.config_change(changed, removed)
29 | :ok
30 | end
31 | end
32 |
--------------------------------------------------------------------------------
/test/support/conn_case.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.ConnCase do
2 | @moduledoc """
3 | This module defines the test case to be used by
4 | tests that require setting up a connection.
5 |
6 | Such tests rely on `Phoenix.ConnTest` and also
7 | import other functionality to make it easier
8 | to build common datastructures and query the data layer.
9 |
10 | Finally, if the test case interacts with the database,
11 | it cannot be async. For this reason, every test runs
12 | inside a transaction which is reset at the beginning
13 | of the test unless the test case is marked as async.
14 | """
15 |
16 | use ExUnit.CaseTemplate
17 |
18 | using do
19 | quote do
20 | # Import conveniences for testing with connections
21 | use Phoenix.ConnTest
22 | import HelloBlockchainWeb.Router.Helpers
23 |
24 | # The default endpoint for testing
25 | @endpoint HelloBlockchainWeb.Endpoint
26 | end
27 | end
28 |
29 |
30 | setup tags do
31 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(HelloBlockchain.Repo)
32 | unless tags[:async] do
33 | Ecto.Adapters.SQL.Sandbox.mode(HelloBlockchain.Repo, {:shared, self()})
34 | end
35 | {:ok, conn: Phoenix.ConnTest.build_conn()}
36 | end
37 |
38 | end
39 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/channels/user_socket.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.UserSocket do
2 | use Phoenix.Socket
3 |
4 | ## Channels
5 | # channel "room:*", HelloBlockchainWeb.RoomChannel
6 |
7 | ## Transports
8 | transport :websocket, Phoenix.Transports.WebSocket
9 | # transport :longpoll, Phoenix.Transports.LongPoll
10 |
11 | # Socket params are passed from the client and can
12 | # be used to verify and authenticate a user. After
13 | # verification, you can put default assigns into
14 | # the socket that will be set for all channels, ie
15 | #
16 | # {:ok, assign(socket, :user_id, verified_user_id)}
17 | #
18 | # To deny connection, return `:error`.
19 | #
20 | # See `Phoenix.Token` documentation for examples in
21 | # performing token verification on connect.
22 | def connect(_params, socket) do
23 | {:ok, socket}
24 | end
25 |
26 | # Socket id's are topics that allow you to identify all sockets for a given user:
27 | #
28 | # def id(socket), do: "user_socket:#{socket.assigns.user_id}"
29 | #
30 | # Would allow you to broadcast a "disconnect" event and terminate
31 | # all active sockets and channels for a given user:
32 | #
33 | # HelloBlockchainWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
34 | #
35 | # Returning `nil` makes this socket anonymous.
36 | def id(_socket), do: nil
37 | end
38 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/views/error_helpers.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.ErrorHelpers do
2 | @moduledoc """
3 | Conveniences for translating and building error messages.
4 | """
5 |
6 | use Phoenix.HTML
7 |
8 | @doc """
9 | Generates tag for inlined form input errors.
10 | """
11 | def error_tag(form, field) do
12 | Enum.map(Keyword.get_values(form.errors, field), fn (error) ->
13 | content_tag :span, translate_error(error), class: "help-block"
14 | end)
15 | end
16 |
17 | @doc """
18 | Translates an error message using gettext.
19 | """
20 | def translate_error({msg, opts}) do
21 | # Because error messages were defined within Ecto, we must
22 | # call the Gettext module passing our Gettext backend. We
23 | # also use the "errors" domain as translations are placed
24 | # in the errors.po file.
25 | # Ecto will pass the :count keyword if the error message is
26 | # meant to be pluralized.
27 | # On your own code and templates, depending on whether you
28 | # need the message to be pluralized or not, this could be
29 | # written simply as:
30 | #
31 | # dngettext "errors", "1 file", "%{count} files", count
32 | # dgettext "errors", "is invalid"
33 | #
34 | if count = opts[:count] do
35 | Gettext.dngettext(HelloBlockchainWeb.Gettext, "errors", msg, msg, count, opts)
36 | else
37 | Gettext.dgettext(HelloBlockchainWeb.Gettext, "errors", msg, opts)
38 | end
39 | end
40 | end
41 |
--------------------------------------------------------------------------------
/test/support/data_case.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchain.DataCase do
2 | @moduledoc """
3 | This module defines the setup for tests requiring
4 | access to the application's data layer.
5 |
6 | You may define functions here to be used as helpers in
7 | your tests.
8 |
9 | Finally, if the test case interacts with the database,
10 | it cannot be async. For this reason, every test runs
11 | inside a transaction which is reset at the beginning
12 | of the test unless the test case is marked as async.
13 | """
14 |
15 | use ExUnit.CaseTemplate
16 |
17 | using do
18 | quote do
19 | alias HelloBlockchain.Repo
20 |
21 | import Ecto
22 | import Ecto.Changeset
23 | import Ecto.Query
24 | import HelloBlockchain.DataCase
25 | end
26 | end
27 |
28 | setup tags do
29 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(HelloBlockchain.Repo)
30 |
31 | unless tags[:async] do
32 | Ecto.Adapters.SQL.Sandbox.mode(HelloBlockchain.Repo, {:shared, self()})
33 | end
34 |
35 | :ok
36 | end
37 |
38 | @doc """
39 | A helper that transform changeset errors to a map of messages.
40 |
41 | assert {:error, changeset} = Accounts.create_user(%{password: "short"})
42 | assert "password is too short" in errors_on(changeset).password
43 | assert %{password: ["password is too short"]} = errors_on(changeset)
44 |
45 | """
46 | def errors_on(changeset) do
47 | Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
48 | Enum.reduce(opts, message, fn {key, value}, acc ->
49 | String.replace(acc, "%{#{key}}", to_string(value))
50 | end)
51 | end)
52 | end
53 | end
54 |
--------------------------------------------------------------------------------
/assets/brunch-config.js:
--------------------------------------------------------------------------------
1 | exports.config = {
2 | // See http://brunch.io/#documentation for docs.
3 | files: {
4 | javascripts: {
5 | joinTo: "js/app.js"
6 |
7 | // To use a separate vendor.js bundle, specify two files path
8 | // http://brunch.io/docs/config#-files-
9 | // joinTo: {
10 | // "js/app.js": /^js/,
11 | // "js/vendor.js": /^(?!js)/
12 | // }
13 | //
14 | // To change the order of concatenation of files, explicitly mention here
15 | // order: {
16 | // before: [
17 | // "vendor/js/jquery-2.1.1.js",
18 | // "vendor/js/bootstrap.min.js"
19 | // ]
20 | // }
21 | },
22 | stylesheets: {
23 | joinTo: "css/app.css"
24 | },
25 | templates: {
26 | joinTo: "js/app.js"
27 | }
28 | },
29 |
30 | conventions: {
31 | // This option sets where we should place non-css and non-js assets in.
32 | // By default, we set this to "/assets/static". Files in this directory
33 | // will be copied to `paths.public`, which is "priv/static" by default.
34 | assets: /^(static)/
35 | },
36 |
37 | // Phoenix paths configuration
38 | paths: {
39 | // Dependencies and current project directories to watch
40 | watched: ["static", "css", "js", "vendor"],
41 | // Where to compile files to
42 | public: "../priv/static"
43 | },
44 |
45 | // Configure your plugins
46 | plugins: {
47 | babel: {
48 | // Do not use ES6 compiler in vendor code
49 | ignore: [/vendor/]
50 | }
51 | },
52 |
53 | modules: {
54 | autoRequire: {
55 | "js/app.js": ["js/app"]
56 | }
57 | },
58 |
59 | npm: {
60 | enabled: true
61 | }
62 | };
63 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/templates/block/index.html.eex:
--------------------------------------------------------------------------------
1 | Listing Blocks
2 |
3 |
4 |
5 |
6 |
7 |
Blocks:
8 |
<%= @blocks["blocks"] %>
9 |
10 |
Connections:
11 |
<%= @blocks["connections"] %>
12 |
13 |
Difficulty:
14 |
<%= @blocks["difficulty"] %>
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
Protocol Version:
26 |
<%= @blocks["protocolversion"] %>
27 |
28 |
29 |
30 |
31 |
Testnet:
32 |
<%= @blocks["testnet"] %>
33 |
34 |
35 |
36 |
37 |
Version:
38 |
<%= @blocks["version"] %>
39 |
40 |
See first block
41 |
42 |
43 |
--------------------------------------------------------------------------------
/mix.exs:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchain.Mixfile do
2 | use Mix.Project
3 |
4 | def project do
5 | [
6 | app: :hello_blockchain,
7 | version: "0.0.1",
8 | elixir: "~> 1.4",
9 | elixirc_paths: elixirc_paths(Mix.env),
10 | compilers: [:phoenix, :gettext] ++ Mix.compilers,
11 | start_permanent: Mix.env == :prod,
12 | aliases: aliases(),
13 | deps: deps()
14 | ]
15 | end
16 |
17 | # Configuration for the OTP application.
18 | #
19 | # Type `mix help compile.app` for more information.
20 | def application do
21 | [
22 | mod: {HelloBlockchain.Application, []},
23 | extra_applications: [:logger, :runtime_tools]
24 | ]
25 | end
26 |
27 | # Specifies which paths to compile per environment.
28 | defp elixirc_paths(:test), do: ["lib", "test/support"]
29 | defp elixirc_paths(_), do: ["lib"]
30 |
31 | # Specifies your project dependencies.
32 | #
33 | # Type `mix help deps` for examples and options.
34 | defp deps do
35 | [
36 | {:phoenix, "~> 1.3.0"},
37 | {:phoenix_pubsub, "~> 1.0"},
38 | {:phoenix_ecto, "~> 3.2"},
39 | {:postgrex, ">= 0.0.0"},
40 | {:phoenix_html, "~> 2.10"},
41 | {:phoenix_live_reload, "~> 1.0", only: :dev},
42 | {:gettext, "~> 0.11"},
43 | {:cowboy, "~> 1.0"},
44 | {:httpoison, "~> 0.13"}
45 | ]
46 | end
47 |
48 | # Aliases are shortcuts or tasks specific to the current project.
49 | # For example, to create, migrate and run the seeds file at once:
50 | #
51 | # $ mix ecto.setup
52 | #
53 | # See the documentation for `Mix` for more info on aliases.
54 | defp aliases do
55 | [
56 | "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
57 | "ecto.reset": ["ecto.drop", "ecto.setup"],
58 | "test": ["ecto.create --quiet", "ecto.migrate", "test"]
59 | ]
60 | end
61 | end
62 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web/endpoint.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.Endpoint do
2 | use Phoenix.Endpoint, otp_app: :hello_blockchain
3 |
4 | socket "/socket", HelloBlockchainWeb.UserSocket
5 |
6 | # Serve at "/" the static files from "priv/static" directory.
7 | #
8 | # You should set gzip to true if you are running phoenix.digest
9 | # when deploying your static files in production.
10 | plug Plug.Static,
11 | at: "/", from: :hello_blockchain, gzip: false,
12 | only: ~w(css fonts images js favicon.ico robots.txt)
13 |
14 | # Code reloading can be explicitly enabled under the
15 | # :code_reloader configuration of your endpoint.
16 | if code_reloading? do
17 | socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
18 | plug Phoenix.LiveReloader
19 | plug Phoenix.CodeReloader
20 | end
21 |
22 | plug Plug.RequestId
23 | plug Plug.Logger
24 |
25 | plug Plug.Parsers,
26 | parsers: [:urlencoded, :multipart, :json],
27 | pass: ["*/*"],
28 | json_decoder: Poison
29 |
30 | plug Plug.MethodOverride
31 | plug Plug.Head
32 |
33 | # The session will be stored in the cookie and signed,
34 | # this means its contents can be read but not tampered with.
35 | # Set :encryption_salt if you would also like to encrypt it.
36 | plug Plug.Session,
37 | store: :cookie,
38 | key: "_hello_blockchain_key",
39 | signing_salt: "JW8J3Tx5"
40 |
41 | plug HelloBlockchainWeb.Router
42 |
43 | @doc """
44 | Callback invoked for dynamically configuring the endpoint.
45 |
46 | It receives the endpoint configuration and checks if
47 | configuration should be loaded from the system environment.
48 | """
49 | def init(_key, config) do
50 | if config[:load_from_system_env] do
51 | port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
52 | {:ok, Keyword.put(config, :http, [:inet6, port: port])}
53 | else
54 | {:ok, config}
55 | end
56 | end
57 | end
58 |
--------------------------------------------------------------------------------
/lib/hello_blockchain_web.ex:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb do
2 | @moduledoc """
3 | The entrypoint for defining your web interface, such
4 | as controllers, views, channels and so on.
5 |
6 | This can be used in your application as:
7 |
8 | use HelloBlockchainWeb, :controller
9 | use HelloBlockchainWeb, :view
10 |
11 | The definitions below will be executed for every view,
12 | controller, etc, so keep them short and clean, focused
13 | on imports, uses and aliases.
14 |
15 | Do NOT define functions inside the quoted expressions
16 | below. Instead, define any helper function in modules
17 | and import those modules here.
18 | """
19 |
20 | def controller do
21 | quote do
22 | use Phoenix.Controller, namespace: HelloBlockchainWeb
23 | import Plug.Conn
24 | import HelloBlockchainWeb.Router.Helpers
25 | import HelloBlockchainWeb.Gettext
26 | end
27 | end
28 |
29 | def view do
30 | quote do
31 | use Phoenix.View, root: "lib/hello_blockchain_web/templates",
32 | namespace: HelloBlockchainWeb
33 |
34 | # Import convenience functions from controllers
35 | import Phoenix.Controller, only: [get_flash: 2, view_module: 1]
36 |
37 | # Use all HTML functionality (forms, tags, etc)
38 | use Phoenix.HTML
39 |
40 | import HelloBlockchainWeb.Router.Helpers
41 | import HelloBlockchainWeb.ErrorHelpers
42 | import HelloBlockchainWeb.Gettext
43 | end
44 | end
45 |
46 | def router do
47 | quote do
48 | use Phoenix.Router
49 | import Plug.Conn
50 | import Phoenix.Controller
51 | end
52 | end
53 |
54 | def channel do
55 | quote do
56 | use Phoenix.Channel
57 | import HelloBlockchainWeb.Gettext
58 | end
59 | end
60 |
61 | @doc """
62 | When used, dispatch to the appropriate controller/view/etc.
63 | """
64 | defmacro __using__(which) when is_atom(which) do
65 | apply(__MODULE__, which, [])
66 | end
67 | end
68 |
--------------------------------------------------------------------------------
/test/hello_blockchain/blockchain/blockchain_test.exs:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchain.BlockchainTest do
2 | use HelloBlockchain.DataCase
3 |
4 | alias HelloBlockchain.Blockchain
5 |
6 | describe "blocks" do
7 | alias HelloBlockchain.Blockchain.Block
8 |
9 | @valid_attrs %{}
10 | @update_attrs %{}
11 | @invalid_attrs %{}
12 |
13 | def block_fixture(attrs \\ %{}) do
14 | {:ok, block} =
15 | attrs
16 | |> Enum.into(@valid_attrs)
17 | |> Blockchain.create_block()
18 |
19 | block
20 | end
21 |
22 | test "list_blocks/0 returns all blocks" do
23 | block = block_fixture()
24 | assert Blockchain.list_blocks() == [block]
25 | end
26 |
27 | test "get_block!/1 returns the block with given id" do
28 | block = block_fixture()
29 | assert Blockchain.get_block!(block.id) == block
30 | end
31 |
32 | test "create_block/1 with valid data creates a block" do
33 | assert {:ok, %Block{} = block} = Blockchain.create_block(@valid_attrs)
34 | end
35 |
36 | test "create_block/1 with invalid data returns error changeset" do
37 | assert {:error, %Ecto.Changeset{}} = Blockchain.create_block(@invalid_attrs)
38 | end
39 |
40 | test "update_block/2 with valid data updates the block" do
41 | block = block_fixture()
42 | assert {:ok, block} = Blockchain.update_block(block, @update_attrs)
43 | assert %Block{} = block
44 | end
45 |
46 | test "update_block/2 with invalid data returns error changeset" do
47 | block = block_fixture()
48 | assert {:error, %Ecto.Changeset{}} = Blockchain.update_block(block, @invalid_attrs)
49 | assert block == Blockchain.get_block!(block.id)
50 | end
51 |
52 | test "delete_block/1 deletes the block" do
53 | block = block_fixture()
54 | assert {:ok, %Block{}} = Blockchain.delete_block(block)
55 | assert_raise Ecto.NoResultsError, fn -> Blockchain.get_block!(block.id) end
56 | end
57 |
58 | test "change_block/1 returns a block changeset" do
59 | block = block_fixture()
60 | assert %Ecto.Changeset{} = Blockchain.change_block(block)
61 | end
62 | end
63 | end
64 |
--------------------------------------------------------------------------------
/config/dev.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # For development, we disable any cache and enable
4 | # debugging and code reloading.
5 | #
6 | # The watchers configuration can be used to run external
7 | # watchers to your application. For example, we use it
8 | # with brunch.io to recompile .js and .css sources.
9 | config :hello_blockchain, HelloBlockchainWeb.Endpoint,
10 | http: [port: 4000],
11 | debug_errors: true,
12 | code_reloader: true,
13 | check_origin: false,
14 | watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
15 | cd: Path.expand("../assets", __DIR__)]]
16 |
17 | # ## SSL Support
18 | #
19 | # In order to use HTTPS in development, a self-signed
20 | # certificate can be generated by running the following
21 | # command from your terminal:
22 | #
23 | # openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem
24 | #
25 | # The `http:` config above can be replaced with:
26 | #
27 | # https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"],
28 | #
29 | # If desired, both `http:` and `https:` keys can be
30 | # configured to run both http and https servers on
31 | # different ports.
32 |
33 | # Watch static and templates for browser reloading.
34 | config :hello_blockchain, HelloBlockchainWeb.Endpoint,
35 | live_reload: [
36 | patterns: [
37 | ~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
38 | ~r{priv/gettext/.*(po)$},
39 | ~r{lib/hello_blockchain_web/views/.*(ex)$},
40 | ~r{lib/hello_blockchain_web/templates/.*(eex)$}
41 | ]
42 | ]
43 |
44 | # Do not include metadata nor timestamps in development logs
45 | config :logger, :console, format: "[$level] $message\n"
46 |
47 | # Set a higher stacktrace during development. Avoid configuring such
48 | # in production as building large stacktraces may be expensive.
49 | config :phoenix, :stacktrace_depth, 20
50 |
51 | # Configure your database
52 | config :hello_blockchain, HelloBlockchain.Repo,
53 | adapter: Ecto.Adapters.Postgres,
54 | username: "postgres",
55 | password: "postgres",
56 | database: "hello_blockchain_dev",
57 | hostname: "localhost",
58 | pool_size: 10
59 |
--------------------------------------------------------------------------------
/assets/js/socket.js:
--------------------------------------------------------------------------------
1 | // NOTE: The contents of this file will only be executed if
2 | // you uncomment its entry in "assets/js/app.js".
3 |
4 | // To use Phoenix channels, the first step is to import Socket
5 | // and connect at the socket path in "lib/web/endpoint.ex":
6 | import {Socket} from "phoenix"
7 |
8 | let socket = new Socket("/socket", {params: {token: window.userToken}})
9 |
10 | // When you connect, you'll often need to authenticate the client.
11 | // For example, imagine you have an authentication plug, `MyAuth`,
12 | // which authenticates the session and assigns a `:current_user`.
13 | // If the current user exists you can assign the user's token in
14 | // the connection for use in the layout.
15 | //
16 | // In your "lib/web/router.ex":
17 | //
18 | // pipeline :browser do
19 | // ...
20 | // plug MyAuth
21 | // plug :put_user_token
22 | // end
23 | //
24 | // defp put_user_token(conn, _) do
25 | // if current_user = conn.assigns[:current_user] do
26 | // token = Phoenix.Token.sign(conn, "user socket", current_user.id)
27 | // assign(conn, :user_token, token)
28 | // else
29 | // conn
30 | // end
31 | // end
32 | //
33 | // Now you need to pass this token to JavaScript. You can do so
34 | // inside a script tag in "lib/web/templates/layout/app.html.eex":
35 | //
36 | //
37 | //
38 | // You will need to verify the user token in the "connect/2" function
39 | // in "lib/web/channels/user_socket.ex":
40 | //
41 | // def connect(%{"token" => token}, socket) do
42 | // # max_age: 1209600 is equivalent to two weeks in seconds
43 | // case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do
44 | // {:ok, user_id} ->
45 | // {:ok, assign(socket, :user, user_id)}
46 | // {:error, reason} ->
47 | // :error
48 | // end
49 | // end
50 | //
51 | // Finally, pass the token on connect as below. Or remove it
52 | // from connect if you don't care about authentication.
53 |
54 | socket.connect()
55 |
56 | // Now that you are connected, you can join channels with a topic:
57 | let channel = socket.channel("topic:subtopic", {})
58 | channel.join()
59 | .receive("ok", resp => { console.log("Joined successfully", resp) })
60 | .receive("error", resp => { console.log("Unable to join", resp) })
61 |
62 | export default socket
63 |
--------------------------------------------------------------------------------
/config/prod.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # For production, we often load configuration from external
4 | # sources, such as your system environment. For this reason,
5 | # you won't find the :http configuration below, but set inside
6 | # HelloBlockchainWeb.Endpoint.init/2 when load_from_system_env is
7 | # true. Any dynamic configuration should be done there.
8 | #
9 | # Don't forget to configure the url host to something meaningful,
10 | # Phoenix uses this information when generating URLs.
11 | #
12 | # Finally, we also include the path to a cache manifest
13 | # containing the digested version of static files. This
14 | # manifest is generated by the mix phx.digest task
15 | # which you typically run after static files are built.
16 | config :hello_blockchain, HelloBlockchainWeb.Endpoint,
17 | load_from_system_env: true,
18 | url: [host: "example.com", port: 80],
19 | cache_static_manifest: "priv/static/cache_manifest.json"
20 |
21 | # Do not print debug messages in production
22 | config :logger, level: :info
23 |
24 | # ## SSL Support
25 | #
26 | # To get SSL working, you will need to add the `https` key
27 | # to the previous section and set your `:url` port to 443:
28 | #
29 | # config :hello_blockchain, HelloBlockchainWeb.Endpoint,
30 | # ...
31 | # url: [host: "example.com", port: 443],
32 | # https: [:inet6,
33 | # port: 443,
34 | # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
35 | # certfile: System.get_env("SOME_APP_SSL_CERT_PATH")]
36 | #
37 | # Where those two env variables return an absolute path to
38 | # the key and cert in disk or a relative path inside priv,
39 | # for example "priv/ssl/server.key".
40 | #
41 | # We also recommend setting `force_ssl`, ensuring no data is
42 | # ever sent via http, always redirecting to https:
43 | #
44 | # config :hello_blockchain, HelloBlockchainWeb.Endpoint,
45 | # force_ssl: [hsts: true]
46 | #
47 | # Check `Plug.SSL` for all available options in `force_ssl`.
48 |
49 | # ## Using releases
50 | #
51 | # If you are doing OTP releases, you need to instruct Phoenix
52 | # to start the server for all endpoints:
53 | #
54 | # config :phoenix, :serve_endpoints, true
55 | #
56 | # Alternatively, you can configure exactly which server to
57 | # start per endpoint:
58 | #
59 | # config :hello_blockchain, HelloBlockchainWeb.Endpoint, server: true
60 | #
61 |
62 | # Finally import the config/prod.secret.exs
63 | # which should be versioned separately.
64 | import_config "prod.secret.exs"
65 |
--------------------------------------------------------------------------------
/priv/gettext/en/LC_MESSAGES/errors.po:
--------------------------------------------------------------------------------
1 | ## `msgid`s in this file come from POT (.pot) files.
2 | ##
3 | ## Do not add, change, or remove `msgid`s manually here as
4 | ## they're tied to the ones in the corresponding POT file
5 | ## (with the same domain).
6 | ##
7 | ## Use `mix gettext.extract --merge` or `mix gettext.merge`
8 | ## to merge POT files into PO files.
9 | msgid ""
10 | msgstr ""
11 | "Language: en\n"
12 |
13 | ## From Ecto.Changeset.cast/4
14 | msgid "can't be blank"
15 | msgstr ""
16 |
17 | ## From Ecto.Changeset.unique_constraint/3
18 | msgid "has already been taken"
19 | msgstr ""
20 |
21 | ## From Ecto.Changeset.put_change/3
22 | msgid "is invalid"
23 | msgstr ""
24 |
25 | ## From Ecto.Changeset.validate_acceptance/3
26 | msgid "must be accepted"
27 | msgstr ""
28 |
29 | ## From Ecto.Changeset.validate_format/3
30 | msgid "has invalid format"
31 | msgstr ""
32 |
33 | ## From Ecto.Changeset.validate_subset/3
34 | msgid "has an invalid entry"
35 | msgstr ""
36 |
37 | ## From Ecto.Changeset.validate_exclusion/3
38 | msgid "is reserved"
39 | msgstr ""
40 |
41 | ## From Ecto.Changeset.validate_confirmation/3
42 | msgid "does not match confirmation"
43 | msgstr ""
44 |
45 | ## From Ecto.Changeset.no_assoc_constraint/3
46 | msgid "is still associated with this entry"
47 | msgstr ""
48 |
49 | msgid "are still associated with this entry"
50 | msgstr ""
51 |
52 | ## From Ecto.Changeset.validate_length/3
53 | msgid "should be %{count} character(s)"
54 | msgid_plural "should be %{count} character(s)"
55 | msgstr[0] ""
56 | msgstr[1] ""
57 |
58 | msgid "should have %{count} item(s)"
59 | msgid_plural "should have %{count} item(s)"
60 | msgstr[0] ""
61 | msgstr[1] ""
62 |
63 | msgid "should be at least %{count} character(s)"
64 | msgid_plural "should be at least %{count} character(s)"
65 | msgstr[0] ""
66 | msgstr[1] ""
67 |
68 | msgid "should have at least %{count} item(s)"
69 | msgid_plural "should have at least %{count} item(s)"
70 | msgstr[0] ""
71 | msgstr[1] ""
72 |
73 | msgid "should be at most %{count} character(s)"
74 | msgid_plural "should be at most %{count} character(s)"
75 | msgstr[0] ""
76 | msgstr[1] ""
77 |
78 | msgid "should have at most %{count} item(s)"
79 | msgid_plural "should have at most %{count} item(s)"
80 | msgstr[0] ""
81 | msgstr[1] ""
82 |
83 | ## From Ecto.Changeset.validate_number/3
84 | msgid "must be less than %{number}"
85 | msgstr ""
86 |
87 | msgid "must be greater than %{number}"
88 | msgstr ""
89 |
90 | msgid "must be less than or equal to %{number}"
91 | msgstr ""
92 |
93 | msgid "must be greater than or equal to %{number}"
94 | msgstr ""
95 |
96 | msgid "must be equal to %{number}"
97 | msgstr ""
98 |
--------------------------------------------------------------------------------
/priv/gettext/errors.pot:
--------------------------------------------------------------------------------
1 | ## This file is a PO Template file.
2 | ##
3 | ## `msgid`s here are often extracted from source code.
4 | ## Add new translations manually only if they're dynamic
5 | ## translations that can't be statically extracted.
6 | ##
7 | ## Run `mix gettext.extract` to bring this file up to
8 | ## date. Leave `msgstr`s empty as changing them here as no
9 | ## effect: edit them in PO (`.po`) files instead.
10 |
11 | ## From Ecto.Changeset.cast/4
12 | msgid "can't be blank"
13 | msgstr ""
14 |
15 | ## From Ecto.Changeset.unique_constraint/3
16 | msgid "has already been taken"
17 | msgstr ""
18 |
19 | ## From Ecto.Changeset.put_change/3
20 | msgid "is invalid"
21 | msgstr ""
22 |
23 | ## From Ecto.Changeset.validate_acceptance/3
24 | msgid "must be accepted"
25 | msgstr ""
26 |
27 | ## From Ecto.Changeset.validate_format/3
28 | msgid "has invalid format"
29 | msgstr ""
30 |
31 | ## From Ecto.Changeset.validate_subset/3
32 | msgid "has an invalid entry"
33 | msgstr ""
34 |
35 | ## From Ecto.Changeset.validate_exclusion/3
36 | msgid "is reserved"
37 | msgstr ""
38 |
39 | ## From Ecto.Changeset.validate_confirmation/3
40 | msgid "does not match confirmation"
41 | msgstr ""
42 |
43 | ## From Ecto.Changeset.no_assoc_constraint/3
44 | msgid "is still associated with this entry"
45 | msgstr ""
46 |
47 | msgid "are still associated with this entry"
48 | msgstr ""
49 |
50 | ## From Ecto.Changeset.validate_length/3
51 | msgid "should be %{count} character(s)"
52 | msgid_plural "should be %{count} character(s)"
53 | msgstr[0] ""
54 | msgstr[1] ""
55 |
56 | msgid "should have %{count} item(s)"
57 | msgid_plural "should have %{count} item(s)"
58 | msgstr[0] ""
59 | msgstr[1] ""
60 |
61 | msgid "should be at least %{count} character(s)"
62 | msgid_plural "should be at least %{count} character(s)"
63 | msgstr[0] ""
64 | msgstr[1] ""
65 |
66 | msgid "should have at least %{count} item(s)"
67 | msgid_plural "should have at least %{count} item(s)"
68 | msgstr[0] ""
69 | msgstr[1] ""
70 |
71 | msgid "should be at most %{count} character(s)"
72 | msgid_plural "should be at most %{count} character(s)"
73 | msgstr[0] ""
74 | msgstr[1] ""
75 |
76 | msgid "should have at most %{count} item(s)"
77 | msgid_plural "should have at most %{count} item(s)"
78 | msgstr[0] ""
79 | msgstr[1] ""
80 |
81 | ## From Ecto.Changeset.validate_number/3
82 | msgid "must be less than %{number}"
83 | msgstr ""
84 |
85 | msgid "must be greater than %{number}"
86 | msgstr ""
87 |
88 | msgid "must be less than or equal to %{number}"
89 | msgstr ""
90 |
91 | msgid "must be greater than or equal to %{number}"
92 | msgstr ""
93 |
94 | msgid "must be equal to %{number}"
95 | msgstr ""
96 |
--------------------------------------------------------------------------------
/test/hello_blockchain_web/controllers/block_controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule HelloBlockchainWeb.BlockControllerTest do
2 | use HelloBlockchainWeb.ConnCase
3 |
4 | alias HelloBlockchain.Blockchain
5 |
6 | @create_attrs %{}
7 | @update_attrs %{}
8 | @invalid_attrs %{}
9 |
10 | def fixture(:block) do
11 | {:ok, block} = Blockchain.create_block(@create_attrs)
12 | block
13 | end
14 |
15 | describe "index" do
16 | test "lists all blocks", %{conn: conn} do
17 | conn = get conn, block_path(conn, :index)
18 | assert html_response(conn, 200) =~ "Listing Blocks"
19 | end
20 | end
21 |
22 | describe "new block" do
23 | test "renders form", %{conn: conn} do
24 | conn = get conn, block_path(conn, :new)
25 | assert html_response(conn, 200) =~ "New Block"
26 | end
27 | end
28 |
29 | describe "create block" do
30 | test "redirects to show when data is valid", %{conn: conn} do
31 | conn = post conn, block_path(conn, :create), block: @create_attrs
32 |
33 | assert %{id: id} = redirected_params(conn)
34 | assert redirected_to(conn) == block_path(conn, :show, id)
35 |
36 | conn = get conn, block_path(conn, :show, id)
37 | assert html_response(conn, 200) =~ "Show Block"
38 | end
39 |
40 | test "renders errors when data is invalid", %{conn: conn} do
41 | conn = post conn, block_path(conn, :create), block: @invalid_attrs
42 | assert html_response(conn, 200) =~ "New Block"
43 | end
44 | end
45 |
46 | describe "edit block" do
47 | setup [:create_block]
48 |
49 | test "renders form for editing chosen block", %{conn: conn, block: block} do
50 | conn = get conn, block_path(conn, :edit, block)
51 | assert html_response(conn, 200) =~ "Edit Block"
52 | end
53 | end
54 |
55 | describe "update block" do
56 | setup [:create_block]
57 |
58 | test "redirects when data is valid", %{conn: conn, block: block} do
59 | conn = put conn, block_path(conn, :update, block), block: @update_attrs
60 | assert redirected_to(conn) == block_path(conn, :show, block)
61 |
62 | conn = get conn, block_path(conn, :show, block)
63 | assert html_response(conn, 200)
64 | end
65 |
66 | test "renders errors when data is invalid", %{conn: conn, block: block} do
67 | conn = put conn, block_path(conn, :update, block), block: @invalid_attrs
68 | assert html_response(conn, 200) =~ "Edit Block"
69 | end
70 | end
71 |
72 | describe "delete block" do
73 | setup [:create_block]
74 |
75 | test "deletes chosen block", %{conn: conn, block: block} do
76 | conn = delete conn, block_path(conn, :delete, block)
77 | assert redirected_to(conn) == block_path(conn, :index)
78 | assert_error_sent 404, fn ->
79 | get conn, block_path(conn, :show, block)
80 | end
81 | end
82 | end
83 |
84 | defp create_block(_) do
85 | block = fixture(:block)
86 | {:ok, block: block}
87 | end
88 | end
89 |
--------------------------------------------------------------------------------
/mix.lock:
--------------------------------------------------------------------------------
1 | %{"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [:rebar3], [], "hexpm"},
2 | "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [], [], "hexpm"},
3 | "cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
4 | "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [], [], "hexpm"},
5 | "db_connection": {:hex, :db_connection, "1.1.2", "2865c2a4bae0714e2213a0ce60a1b12d76a6efba0c51fbda59c9ab8d1accc7a8", [], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
6 | "decimal": {:hex, :decimal, "1.4.0", "fac965ce71a46aab53d3a6ce45662806bdd708a4a95a65cde8a12eb0124a1333", [], [], "hexpm"},
7 | "ecto": {:hex, :ecto, "2.2.1", "ccc6fd304f9bb785f2c3cfd0ee8da6bad6544ab12ca5f7162b20a743d938417c", [], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
8 | "file_system": {:hex, :file_system, "0.2.0", "1b899a8708dd121648b4f4f632022cf8fd989f3d7250621f34eca4831b217ee9", [], [], "hexpm"},
9 | "gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [], [], "hexpm"},
10 | "hackney": {:hex, :hackney, "1.9.0", "51c506afc0a365868469dcfc79a9d0b94d896ec741cfd5bd338f49a5ec515bfe", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
11 | "httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
12 | "idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
13 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
14 | "mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [], [], "hexpm"},
15 | "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
16 | "phoenix": {:hex, :phoenix, "1.3.0", "1c01124caa1b4a7af46f2050ff11b267baa3edb441b45dbf243e979cd4c5891b", [], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
17 | "phoenix_ecto": {:hex, :phoenix_ecto, "3.2.3", "450c749876ff1de4a78fdb305a142a76817c77a1cd79aeca29e5fc9a6c630b26", [], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
18 | "phoenix_html": {:hex, :phoenix_html, "2.10.4", "d4f99c32d5dc4918b531fdf163e1fd7cf20acdd7703f16f5d02d4db36de803b7", [], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
19 | "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.1.1", "8c9f0fb95d5592f3769f83fda852d3aab806309c946831f7e50fe447c2859e13", [], [{:file_system, "~> 0.2", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.0", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"},
20 | "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.2", "bfa7fd52788b5eaa09cb51ff9fcad1d9edfeb68251add458523f839392f034c1", [], [], "hexpm"},
21 | "plug": {:hex, :plug, "1.4.3", "236d77ce7bf3e3a2668dc0d32a9b6f1f9b1f05361019946aae49874904be4aed", [], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
22 | "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [], [], "hexpm"},
23 | "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [], [], "hexpm"},
24 | "postgrex": {:hex, :postgrex, "0.13.3", "c277cfb2a9c5034d445a722494c13359e361d344ef6f25d604c2353185682bfc", [], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
25 | "ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [], [], "hexpm"},
26 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
27 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"}}
28 |
--------------------------------------------------------------------------------