diff options
Diffstat (limited to 'shard/lib/keys.ex')
-rw-r--r-- | shard/lib/keys.ex | 28 |
1 files changed, 26 insertions, 2 deletions
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 |