From 5b7f89707f0fe72f8306fa357f4369383dfb675c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C9=B4=E1=B4=8F=E1=B4=A0=E1=B4=80?= <146671001+ovnanova@users.noreply.github.com> Date: Sat, 17 Feb 2024 12:01:35 -0800 Subject: [PATCH] Create didplc.ex --- lib/hexpds/didplc.ex | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 lib/hexpds/didplc.ex diff --git a/lib/hexpds/didplc.ex b/lib/hexpds/didplc.ex new file mode 100644 index 0000000..9cd4918 --- /dev/null +++ b/lib/hexpds/didplc.ex @@ -0,0 +1,68 @@ +defmodule Hexpds.DidPlc do + defmodule Operation do + defstruct [ + :rotationKeys, + :prev, + :verificationMethods, + :alsoKnownAs, + :services, + type: "plc_operation" + ] + + @type t :: %__MODULE__{ + rotationKeys: [Hexpds.K256.PrivateKey.t()], + prev: String.t() | nil, + verificationMethods: VerificationMethods.t(), + alsoKnownAs: [String.t()], + services: Services.t(), + type: String.t() + } + + defmodule VerificationMethods do + defstruct([:atproto]) + @type t :: %__MODULE__{atproto: Hexpds.K256.PrivateKey.t()} + end + + defmodule Services do + @type t :: %__MODULE__{atproto_pds: AtprotoPds.t()} + @derive Jason.Encoder + defstruct [:atproto_pds] + + defmodule AtprotoPds do + @type t :: %__MODULE__{endpoint: String.t(), type: String.t()} + @derive Jason.Encoder + defstruct [:endpoint, type: "AtprotoPersonalDataServer"] + end + end + + def genesis( + %Hexpds.K256.PrivateKey{} = rotationkey, + %Hexpds.K256.PrivateKey{} = signingkey, + handle, + pds + ) do + %__MODULE__{ + rotationKeys: [rotationkey], + prev: nil, + verificationMethods: %Operation.VerificationMethods{atproto: signingkey}, + alsoKnownAs: ["at://#{handle}"], + services: %Services{atproto_pds: %Services.AtprotoPds{endpoint: "https://#{pds}"}} + } + end + + def to_json(%__MODULE__{} = operation) do + encodekeys = fn k -> + k |> Hexpds.K256.PrivateKey.to_pubkey() |> Hexpds.K256.PublicKey.to_did_key() + end + + Jason.encode!(%{ + "type" => operation.type, + "rotationKeys" => Enum.map(operation.rotationKeys, encodekeys), + "prev" => operation.prev, + "verificationMethods" => %{atproto: encodekeys.(operation.verificationMethods.atproto)}, + "alsoKnownAs" => operation.alsoKnownAs, + "services" => operation.services + }) + end + end +end