Skip to content

Commit

Permalink
Create didplc.ex
Browse files Browse the repository at this point in the history
  • Loading branch information
mekaem committed Feb 17, 2024
1 parent fd8e114 commit 5b7f897
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions lib/hexpds/didplc.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5b7f897

Please sign in to comment.