aboutsummaryrefslogtreecommitdiff
path: root/lib/identity.ex
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-07-03 15:42:17 +0200
committerAlex Auvolat <alex@adnab.me>2018-07-03 15:42:17 +0200
commit8f3009715ee9ccdd7ecb54fea1244a32a29b62c0 (patch)
tree416fb96c39effa4217864bb6a93243803ba8edba /lib/identity.ex
downloadshard-8f3009715ee9ccdd7ecb54fea1244a32a29b62c0.tar.gz
shard-8f3009715ee9ccdd7ecb54fea1244a32a29b62c0.zip
Initialize shard repo with code from somewhere
Diffstat (limited to 'lib/identity.ex')
-rw-r--r--lib/identity.ex32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/identity.ex b/lib/identity.ex
new file mode 100644
index 0000000..229d6c7
--- /dev/null
+++ b/lib/identity.ex
@@ -0,0 +1,32 @@
+defmodule Shard.Identity do
+ use Agent
+ require Salty.Sign.Ed25519, as: Sign
+
+ def start_link(_) do
+ Agent.start_link(__MODULE__, :init, [], name: __MODULE__)
+ end
+
+ def init() do
+ {:ok, pk, sk} = Sign.keypair
+ nick_suffix = pk
+ |> binary_part(0, 3)
+ |> Base.encode16
+ |> String.downcase
+ %{
+ keypair: {pk, sk},
+ nickname: "Anon" <> nick_suffix,
+ }
+ end
+
+ def get_keypair() do
+ Agent.get(__MODULE__, &(&1.keypair))
+ end
+
+ def get_nickname() do
+ Agent.get(__MODULE__, &(&1.nickname))
+ end
+
+ def set_nickname(newnick) do
+ Agent.update(__MODULE__, &(%{&1 | nickname: newnick}))
+ end
+end