diff options
author | Alex Auvolat <alex@adnab.me> | 2018-11-02 14:04:52 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-11-02 14:04:52 +0100 |
commit | 94b1f6b0ae387b7fcb0714a4c6e213212097a100 (patch) | |
tree | 33fa5f6f626b075c389b4bb977d9fbb5e7e05c37 /shardweb/lib/shard_uri.ex | |
parent | 3baa53f1da7f581619b066832b8303efbe9a46ba (diff) | |
download | shard-94b1f6b0ae387b7fcb0714a4c6e213212097a100.tar.gz shard-94b1f6b0ae387b7fcb0714a4c6e213212097a100.zip |
Directory stuff
Diffstat (limited to 'shardweb/lib/shard_uri.ex')
-rw-r--r-- | shardweb/lib/shard_uri.ex | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/shardweb/lib/shard_uri.ex b/shardweb/lib/shard_uri.ex new file mode 100644 index 0000000..71c0904 --- /dev/null +++ b/shardweb/lib/shard_uri.ex @@ -0,0 +1,42 @@ +defmodule ShardURI do + @moduledoc""" + Convert Shard manifests to and from strings. + """ + + def from_manifest(m) do + case m do + %SApp.Chat.Manifest{channel: chan} -> "shard:chat:#{chan}" + %SApp.Chat.PrivChat.Manifest{pk_list: pk_list} -> + "shard:privchat:#{pk_list|>Enum.map(&Base.encode16/1)|>Enum.join(",")}" + %SApp.Identity.Manifest{pk: pk} -> + "shard:identity:#{pk|>Base.encode16}" + %SApp.Directory.Manifest{owner: owner, public: true, name: name} -> + "shard:dir:pub:#{owner|>Base.encode16}:#{name}" + %SApp.Directory.Manifest{owner: owner, public: false, name: name} -> + "shard:dir:priv:#{owner|>Base.encode16}:#{name}" + end + end + + def to_manifest(p) do + case p do + "shard:chat:" <> chan -> + %SApp.Chat.Manifest{channel: chan} + "shard:privchat:" <> pklist -> + pklist + |> String.split(",") + |> Enum.map(&parse_pk/1) + |> SApp.Chat.PrivChat.Manifest.new() + "shard:identity:" <> pk -> + %SApp.Identity.Manifest{pk: parse_pk pk} + "shard:dir:pub:" <> <<pk::bytes-size(64)>> <> ":" <> name -> + %SApp.Directory.Manifest{owner: parse_pk(pk), public: true, name: name} + "shard:dir:priv:" <> <<pk::bytes-size(64)>> <> ":" <> name -> + %SApp.Directory.Manifest{owner: parse_pk(pk), public: false, name: name} + end + end + + def parse_pk(pkstr) do + 64 = byte_size pkstr + Base.decode16! pkstr + end +end |