Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
feat(plugins): added required crypto and did stuff for ucan build
Browse files Browse the repository at this point in the history
  • Loading branch information
madclaws committed Oct 28, 2023
1 parent 0efcf13 commit 6aacff3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
20 changes: 20 additions & 0 deletions lib/ex_ucan/plugins/ed25519/crypto.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule ExUcan.Plugins.Ed25519.Crypto do
@moduledoc """
Crypto functions related to `ExUcan.Plugins.Ed25519.Keypair`
"""
alias ExUcan.Plugins.Utils

@edwards_did_prefix <<0xED, 0x01>>

# TODO: doc
@spec did_to_publickey(did :: String.t()) :: binary()
def did_to_publickey(did) do
Utils.key_bytes_from_did(did, @edwards_did_prefix)
end

# TODO: doc
@spec publickey_to_did(pubkey :: binary()) :: String.t()
def publickey_to_did(pubkey) do
Utils.did_from_key_bytes(pubkey, @edwards_did_prefix)
end
end
22 changes: 12 additions & 10 deletions lib/ex_ucan/plugins/ed25519/keypair.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ defmodule ExUcan.Plugins.Ed25519.Keypair do
@moduledoc """
Encapsulates Ed25519 Keypair generation and other utilities
"""
alias ExUcan.Plugins.Ed25519.Crypto
# TODO: more doc..

# TODO: Need type doc
@type t :: %__MODULE__{
jwt_alg: String.t(),
secret_key: binary(),
public_key: binary(),
exportable: boolean()
}
jwt_alg: String.t(),
secret_key: binary(),
public_key: binary(),
exportable: boolean()
}

defstruct(
jwt_alg: "EdDSA",
Expand All @@ -19,10 +20,12 @@ defmodule ExUcan.Plugins.Ed25519.Keypair do
exportable: false
)


@spec create(boolean()) :: __MODULE__.t()
def create(exportable? \\ true)

def create(exportable?) do
{pub, priv} = :crypto.generate_key(:eddsa, :ed25519)

__MODULE__.__struct__(
jwt_alg: "EdDSA",
secret_key: priv,
Expand All @@ -31,9 +34,8 @@ defmodule ExUcan.Plugins.Ed25519.Keypair do
)
end

# @spec
def did() do

@spec did(__MODULE__.t()) :: String.t()
def did(keypair) do
Crypto.publickey_to_did(keypair.public_key)
end

end
31 changes: 31 additions & 0 deletions lib/ex_ucan/plugins/utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule ExUcan.Plugins.Utils do
@moduledoc """
Utilites related to plugins
"""

@base58_did_prefix "did:key:z"

# TODO: docs
@spec did_from_key_bytes(publickey_bytes :: binary(), prefix :: binary()) :: String.t()
def did_from_key_bytes(publickey_bytes, prefix) do
bytes = <<prefix::binary, publickey_bytes::binary>>
base58key = Base58.encode(bytes)
@base58_did_prefix <> base58key
end

# TODO: docs
@spec key_bytes_from_did(String.t(), binary()) :: {:ok, binary()} | {:error, String.t()}
def key_bytes_from_did("did:key:z" <> non_prefix_did, expected_prefix) do
bytes = Base58.decode(non_prefix_did)
<<a::size(8), b::size(8), pub::binary>> = bytes

if <<a, b>> == expected_prefix do
{:ok, pub}
else
{:error, "Expected prefix #{inspect(expected_prefix)}"}
end
end

def key_bytes_from_did(_did, _expected_prefix),
do: {:error, "Please use a base58-encoded DID formatted `did:key:z..."}
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule ExUcan.MixProject do
defp deps do
[
{:rustler, "~> 0.30.0", runtime: false},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false}
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:b58, "~> 1.0.2"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%{
"b58": {:hex, :b58, "1.0.3", "d300d6ae5a3de956a54b9e8220e924e4fee1a349de983df2340fe61e0e464202", [:mix], [], "hexpm", "af62a98a8661fd89978cf3a3a4b5b2ebe82209de6ac6164f0b112e36af72fc59"},
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"credo": {:hex, :credo, "1.7.1", "6e26bbcc9e22eefbff7e43188e69924e78818e2fe6282487d0703652bc20fd62", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e9871c6095a4c0381c89b6aa98bc6260a8ba6addccf7f6a53da8849c748a58a2"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
Expand Down

0 comments on commit 6aacff3

Please sign in to comment.