This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): added required crypto and did stuff for ucan build
- Loading branch information
Showing
5 changed files
with
66 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters