aboutsummaryrefslogtreecommitdiff
path: root/shard/lib
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-09-26 15:26:06 +0200
committerAlex Auvolat <alex@adnab.me>2018-09-26 15:26:06 +0200
commit7d0009d83c9b5c747d4adb535817924762429f30 (patch)
tree59b368cf189fc10976337e8caf4e6b623d69a04b /shard/lib
parentf16973d3a492ae6d4890c40d77b0a93d3293bf3a (diff)
downloadshard-7d0009d83c9b5c747d4adb535817924762429f30.tar.gz
shard-7d0009d83c9b5c747d4adb535817924762429f30.zip
Make it work, including Web interface
Diffstat (limited to 'shard/lib')
-rw-r--r--shard/lib/app/chat.ex53
-rw-r--r--shard/lib/app/identity.ex22
-rw-r--r--shard/lib/cli/cli.ex28
-rw-r--r--shard/lib/data/merklesearchtree.ex7
-rw-r--r--shard/lib/keys.ex28
5 files changed, 85 insertions, 53 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex
index e61d648..c7df62e 100644
--- a/shard/lib/app/chat.ex
+++ b/shard/lib/app/chat.ex
@@ -201,33 +201,37 @@ defmodule SApp.Chat do
end
defp init_merge(state, new_root, source_peer) do
- # TODO: make the merge asynchronous
-
- Logger.info("Starting merge for #{inspect state.manifest}, merging root: #{new_root|>Base.encode16}")
+ if new_root == nil do
+ state
+ else
+ # TODO: make the merge asynchronous
+
+ Logger.info("Starting merge for #{inspect state.manifest}, merging root: #{new_root|>Base.encode16}")
- prev_last = for {x, true} <- MST.last(state.mst, nil, 100), into: MapSet.new, do: x
+ prev_last = for {x, true} <- MST.last(state.mst, nil, 100), into: MapSet.new, do: x
- mgmst = %{state.mst | root: new_root}
- mgmst = put_in(mgmst.store.prefer_ask, [source_peer])
- mst = MST.merge(state.mst, mgmst)
+ mgmst = %{state.mst | root: new_root}
+ mgmst = put_in(mgmst.store.prefer_ask, [source_peer])
+ mst = MST.merge(state.mst, mgmst)
- correct = for {x, true} <- MST.last(mst, nil, 100) do
- if not MapSet.member? prev_last, x do
- msg_callback(state, x)
- {pk, bin, sign} = x
- Shard.Keys.verify(pk, bin, sign)
- else
- true
+ correct = for {x, true} <- MST.last(mst, nil, 100) do
+ if not MapSet.member? prev_last, x do
+ msg_callback(state, x)
+ {pk, bin, sign} = x
+ Shard.Keys.verify(pk, bin, sign)
+ else
+ true
+ end
end
- end
- if Enum.all? correct do
- GenServer.cast(state.page_store, {:set_roots, [mst.root]})
- Shard.Manager.save_state(state.id, mst.root)
- %{state | mst: mst}
- else
- Logger.warn("Incorrect signatures somewhere while merging, dropping merged data")
- state
+ if Enum.all? correct do
+ GenServer.cast(state.page_store, {:set_roots, [mst.root]})
+ Shard.Manager.save_state(state.id, mst.root)
+ %{state | mst: mst}
+ else
+ Logger.warn("Incorrect signatures somewhere while merging, dropping merged data")
+ state
+ end
end
end
@@ -236,11 +240,10 @@ defmodule SApp.Chat do
{:noreply, %{ state | subs: new_subs }}
end
- defp msg_callback(state, {pk, msgbin, _sign}) do
- {ts, msg} = SData.term_unbin msgbin
+ defp msg_callback(state, {pk, msgbin, sign}) do
for pid <- state.subs do
if Process.alive?(pid) do
- send(pid, {:chat_recv, state.channel, {ts, pk, msg}})
+ send(pid, {:chat_recv, state.channel, {pk, msgbin, sign}})
end
end
end
diff --git a/shard/lib/app/identity.ex b/shard/lib/app/identity.ex
index 7e97897..204dfb1 100644
--- a/shard/lib/app/identity.ex
+++ b/shard/lib/app/identity.ex
@@ -43,10 +43,7 @@ defmodule SApp.Identity do
end
def default_nick(pk) do
- nick_suffix = pk
- |> binary_part(0, 4)
- |> Base.encode16
- |> String.downcase
+ nick_suffix = Shard.Keys.pk_display pk
"Anon" <> nick_suffix
end
@@ -56,6 +53,19 @@ defmodule SApp.Identity do
Shard.Manager.find_proc id
end
+ def get_nick(pk) do
+ case find_proc pk do
+ nil ->
+ if Shard.Keys.valid_identity_pk? pk do
+ Shard.Manifest.start %Manifest{pk: pk}
+ end
+ default_nick pk
+ pid ->
+ info = GenServer.call(pid, :get_info)
+ info.nick
+ end
+ end
+
def handle_call(:manifest, _from, state) do
{:replyl, state.manifest, state}
end
@@ -85,11 +95,12 @@ defmodule SApp.Identity do
def handle_cast({:interested, peer_id}, state) do
Shard.Manager.send(peer_id, {state.id, nil, {:update, SData.SignRev.signed(state.state)}})
+ {:noreply, state}
end
def handle_cast({:msg, peer_id, _shard_id, nil, msg}, state) do
state = case msg do
- {:update, signed} ->
+ {:update, signed} when signed != nil ->
case SData.SignRev.merge(state.state, signed, state.pk) do
{true, st2} ->
Shard.Manager.save_state(state.id, st2)
@@ -99,6 +110,7 @@ defmodule SApp.Identity do
{false, _} ->
state
end
+ _ -> state
end
{:noreply, state}
end
diff --git a/shard/lib/cli/cli.ex b/shard/lib/cli/cli.ex
index 3778b2d..2e35e45 100644
--- a/shard/lib/cli/cli.ex
+++ b/shard/lib/cli/cli.ex
@@ -12,13 +12,7 @@ defmodule SCLI do
GenServer.cast(chpid, {:subscribe, self()})
end
- pk = case Shard.Keys.list_identities do
- [pk1|_] -> pk1
- _ ->
- IO.puts "Generating a new identity..."
- Shard.Keys.new_identity
- end
-
+ pk = Shard.Keys.get_any_identity
run(%State{room_pid: nil, id_pid: nil, pk: pk})
end
@@ -63,8 +57,10 @@ defmodule SCLI do
defp handle_messages() do
receive do
- {:chat_recv, chan, {ts, nick, msg}} ->
- IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} ##{chan} <#{nick}> #{msg}"
+ {:chat_recv, chan, {pk, msgbin, _sign}} ->
+ {ts, msg} = SData.term_unbin msgbin
+ nick = SApp.Identity.get_nick pk
+ IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} ##{chan} <#{nick} #{Shard.Keys.pk_display pk}> #{msg}"
handle_messages()
{:chat_send, _, _} ->
# do nothing
@@ -95,16 +91,10 @@ defmodule SCLI do
else
GenServer.call(state.room_pid, {:read_history, nil, 25})
|> Enum.each(fn {{pk, msgbin, _sign}, true} ->
- {ts, msg} = SData.term_unbin msgbin
- nick = case SApp.Identity.find_proc pk do
- nil ->
- SApp.Identity.default_nick pk
- pid ->
- info = GenServer.call(pid, :get_info)
- info.nick
- end
- IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} <#{nick} #{pk|>binary_part(0, 4)|>Base.encode16|>String.downcase}> #{msg}"
- end)
+ {ts, msg} = SData.term_unbin msgbin
+ nick = SApp.Identity.get_nick pk
+ IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} <#{nick} #{Shard.Keys.pk_display pk}> #{msg}"
+ end)
end
state
end
diff --git a/shard/lib/data/merklesearchtree.ex b/shard/lib/data/merklesearchtree.ex
index 49d54a5..e646774 100644
--- a/shard/lib/data/merklesearchtree.ex
+++ b/shard/lib/data/merklesearchtree.ex
@@ -301,8 +301,11 @@ defmodule SData.MerkleSearchTree do
case root do
nil -> []
_ ->
- %Page{ level: _, low: low, list: lst } = Store.get(s.store, root)
- last_aux(s, low, lst, top_bound, num)
+ case Store.get(s.store, root) do
+ nil -> []
+ %Page{ level: _, low: low, list: lst } ->
+ last_aux(s, low, lst, top_bound, num)
+ end
end
end
diff --git a/shard/lib/keys.ex b/shard/lib/keys.ex
index 0dc3154..de054e1 100644
--- a/shard/lib/keys.ex
+++ b/shard/lib/keys.ex
@@ -48,11 +48,21 @@ defmodule Shard.Keys do
Agent.get(__MODULE__, &(&1))
end
+ def get_any_identity() do
+ Agent.get(__MODULE__, fn _ ->
+ case list_identities() do
+ [x|_] -> x
+ [] -> new_identity()
+ end
+ end)
+ end
+
@doc"""
Generate a new keypair for a user identity, and start an Identity Shard for it.
"""
def new_identity() do
{pk, sk} = gen_keypair(Application.get_env(:shard, :identity_suffix))
+ Logger.info "New identity: #{pk|>Base.encode16}"
:dets.insert @key_db, {pk, sk}
SApp.Identity.start_link(pk)
pk
@@ -86,7 +96,7 @@ defmodule Shard.Keys do
If correct, returns {:ok, original_message}
"""
def open(pk, signed) do
- if check_suffix(pk, Application.get_env(:shard, :identity_suffix)) do
+ if valid_identity_pk? pk do
Sign.open(signed, pk)
else
{:error, :invalid_pk_suffix}
@@ -115,10 +125,24 @@ defmodule Shard.Keys do
Returns :ok if the signature was correct.
"""
def verify(pk, bin, sign) do
- if check_suffix(pk, Application.get_env(:shard, :identity_suffix)) do
+ if valid_identity_pk? pk do
Sign.verify_detached(sign, bin, pk)
else
{:error, :invalid_pk_suffix}
end
end
+
+ @doc"""
+ Check if a public key is a valid identity pk. Requirement: have the correct suffix.
+ """
+ def valid_identity_pk?(pk) do
+ check_suffix(pk, Application.get_env(:shard, :identity_suffix))
+ end
+
+ def pk_display(pk) do
+ pk
+ |> binary_part(0, 4)
+ |> Base.encode16
+ |> String.downcase
+ end
end