AbtDid v0.3.7 AbtDid View Source

ArcBlock DID (decentralized identification) Authentication Protocol is an open protocol that provides a secure decentralized authentication mechanism by using asymmetric cryptography technology.

This module contains methods to generate the DID from secret key or publick key or publick key hash.

Link to this section Summary

Functions

Alias to pk_to_did. See pk_to_did for more information

Gets the DID type information from the DID

Gets the public key hash of this DID

Alias to pkhash_to_did. See pkhash_to_did for more information

Verifies if a DID is valid by checking the checksum

Verifies if a public key and a DID match with each other

Generates the DID from publick key

Generate the DID from a public key hash

Generates the DID from secret key

Link to this section Functions

Link to this function

data_to_did(did_type, data, opts \\ []) View Source
data_to_did(AbtDid.Type.t(), binary(), Keyword.t()) :: String.t()

Alias to pk_to_did. See pk_to_did for more information.

Link to this function

get_did_type(did) View Source
get_did_type(String.t()) :: AbtDid.Type.t()

Gets the DID type information from the DID.

Examples

iex> AbtDid.get_did_type("did:abt:z1muQ3xqHQK2uiACHyChikobsiY5kLqtShA")
%AbtDid.Type{hash_type: :sha3, key_type: :ed25519, role_type: :account}

iex> AbtDid.get_did_type("z1muQ3xqHQK2uiACHyChikobsiY5kLqtShA")
%AbtDid.Type{hash_type: :sha3, key_type: :ed25519, role_type: :account}
Link to this function

get_pubkey_hash(did, opts \\ [encode: true]) View Source
get_pubkey_hash(<<_::8, _::_*8>>, keyword()) :: binary()

Gets the public key hash of this DID.

Examples

iex> pk = <<136, 159, 157, 15, 85, 1, 98, 93, 76, 139, 60, 21, 243, 144, 249, 180, 60, 69, 140, 215, 195, 6, 33, 122, 117, 140, 241, 209, 47, 83, 173, 77>>
iex> did = AbtDid.pk_to_did(AbtDid.Type.validator, pk)
iex> AbtDid.get_pubkey_hash(did)
"BB6FD53B8B12E79CE94768B0349836AB9ED81D85"
Link to this function

hash_to_did(type, hash, opts) View Source
hash_to_did(atom(), binary() | String.t(), Keyword.t()) :: String.t()

Alias to pkhash_to_did. See pkhash_to_did for more information.

Verifies if a DID is valid by checking the checksum.

Examples

iex> AbtDid.is_valid?("did:abt:z1muQ3xqHQK2uiACHyChikobsiY5kLqtShA")
true

iex> AbtDid.is_valid?("z1muQ3xqHQK2uiACHyChikobsiY5kLqtShA")
true

iex> AbtDid.is_valid?("z2muQ3xqHQK2uiACHyChikobsiY5kLqtShA")
false

iex> AbtDid.is_valid?("z1muQ3xqHQK2uiACHyChikobsiY5kLqtSha")
false
Link to this function

match_pk?(did, pk) View Source
match_pk?(String.t(), binary()) :: boolean()

Verifies if a public key and a DID match with each other.

Link to this function

pk_to_did(did_type, pk, opts \\ []) View Source
pk_to_did(AbtDid.Type.t(), binary(), Keyword.t()) :: String.t()

Generates the DID from publick key.

Options:

:form: Determines the form of DID returned. :long - The returned DID will be prefixed by "did:abt:". :short - The retuned DID has only DID string.

:encode: Detemines whether or not encode the DID. true - The returned DID will be encoded as Base58. false - The returned DID will be in binary format and :form will be set as :short.

Link to this function

pkhash_to_did(role_type, pk_hash, opts \\ []) View Source
pkhash_to_did(:node, binary() | String.t(), Keyword.t()) :: String.t()
pkhash_to_did(:validator, binary() | String.t(), Keyword.t()) :: String.t()
pkhash_to_did(:tether, binary() | String.t(), Keyword.t()) :: String.t()
pkhash_to_did(:swap, binary() | String.t(), Keyword.t()) :: String.t()
pkhash_to_did(atom(), binary() | String.t(), Keyword.t()) :: String.t()

Generate the DID from a public key hash.

Options:

:form: Determines the form of DID returned. :long - The returned DID will be prefixed by "did:abt:". :short - The retuned DID has only DID string.

:encode: Detemines whether or not encode the DID. true - The returned DID will be encoded as Base58. false - The returned DID will be in binary format and :form will be set as :short.

Examples

iex> sk = "3E0F9A313300226D51E33D5D98A126E86396956122E97E32D31CEE2277380B83FF47B3022FA503EAA1E9FA4B20FA8B16694EA56096F3A2E9109714062B3486D9" |> Base.decode16!()
iex> did = AbtDid.sk_to_did(AbtDid.Type.validator, sk)
iex> pk_hash = AbtDid.get_pubkey_hash(did)
iex> AbtDid.pkhash_to_did(:validator, pk_hash)
"did:abt:zyt2vg6n8424c9xdXLGj1g27finM77ZN5KQL"

iex> pk = "FF47B3022FA503EAA1E9FA4B20FA8B16694EA56096F3A2E9109714062B3486D9" |> Base.decode16!()
iex> AbtDid.pk_to_did(AbtDid.Type.node, pk)
"did:abt:z89nF4GRYvgw5mqk8NqVVC7NeZLWKbcbQY7V"
iex> pk_hash = "D1B287B1ACB71A980568C99A3AB32A8ED1D9C1BB" |> Base.decode16!()
iex> AbtDid.pkhash_to_did(:node, pk_hash)
"did:abt:z89nF4GRYvgw5mqk8NqVVC7NeZLWKbcbQY7V"
Link to this function

sk_to_did(did_type, sk, opts \\ []) View Source
sk_to_did(AbtDid.Type.t(), binary(), Keyword.t()) :: String.t()

Generates the DID from secret key.

Options:

:form: Determines the form of DID returned. :long - The returned DID will be prefixed by "did:abt:". :short - The retuned DID has only DID string.

:encode: Detemines whether or not encode the DID. true - The returned DID will be encoded as Base58. false - The returned DID will be in binary format and :form will be set as :short.

Examples

iex> sk = "3E0F9A313300226D51E33D5D98A126E86396956122E97E32D31CEE2277380B83FF47B3022FA503EAA1E9FA4B20FA8B16694EA56096F3A2E9109714062B3486D9" |> Base.decode16!()
iex> AbtDid.sk_to_did(%AbtDid.Type{}, sk)
"did:abt:z1ioGHFYiEemfLa3hQjk4JTwWTQPu1g2YxP"

iex> sk = "3E0F9A313300226D51E33D5D98A126E86396956122E97E32D31CEE2277380B83FF47B3022FA503EAA1E9FA4B20FA8B16694EA56096F3A2E9109714062B3486D9" |> Base.decode16!()
iex> AbtDid.sk_to_did(AbtDid.Type.node, sk)
"did:abt:z89nF4GRYvgw5mqk8NqVVC7NeZLWKbcbQY7V"

iex> sk = "3E0F9A313300226D51E33D5D98A126E86396956122E97E32D31CEE2277380B83FF47B3022FA503EAA1E9FA4B20FA8B16694EA56096F3A2E9109714062B3486D9" |> Base.decode16!()
iex> AbtDid.sk_to_did(AbtDid.Type.validator, sk)
"did:abt:zyt2vg6n8424c9xdXLGj1g27finM77ZN5KQL"

iex> sk = "3E0F9A313300226D51E33D5D98A126E86396956122E97E32D31CEE2277380B83FF47B3022FA503EAA1E9FA4B20FA8B16694EA56096F3A2E9109714062B3486D9" |> Base.decode16!()
iex> AbtDid.sk_to_did(%AbtDid.Type{role_type: :node}, sk)
** (RuntimeError) The hash_type must be :sha2 and key_type must be :ed25519 if the role_type is :node or :validator.

iex> sk = "26954E19E8781905E2CF91A18AE4F36A954C142176EE1BC27C2635520C49BC55" |> Base.decode16!()
iex> AbtDid.sk_to_did(%AbtDid.Type{role_type: :validator, key_type: :secp256k1}, sk)
** (RuntimeError) The hash_type must be :sha2 and key_type must be :ed25519 if the role_type is :node or :validator.

iex> sk = "26954E19E8781905E2CF91A18AE4F36A954C142176EE1BC27C2635520C49BC55" |> Base.decode16!()
iex> AbtDid.sk_to_did(%AbtDid.Type{key_type: :secp256k1}, sk)
"did:abt:z1Ee1H8g248HqroacmEnZzMYgbhjz1Z2WSvv"

iex> sk = "26954E19E8781905E2CF91A18AE4F36A954C142176EE1BC27C2635520C49BC55" |> Base.decode16!()
iex> AbtDid.sk_to_did(%AbtDid.Type{key_type: :secp256k1}, sk, form: :short)
"z1Ee1H8g248HqroacmEnZzMYgbhjz1Z2WSvv"

iex> sk = "26954E19E8781905E2CF91A18AE4F36A954C142176EE1BC27C2635520C49BC55" |> Base.decode16!()
iex> AbtDid.sk_to_did(%AbtDid.Type{key_type: :secp256k1}, sk, encode: false)
<<0, 33, 228, 184, 246, 38, 116, 137, 126, 215, 93, 240, 247, 53, 110, 130, 198, 249, 166, 74, 92, 19, 243, 204, 12, 211>>
Link to this function

sk_to_wallet(did_type, sk, opts \\ []) View Source
sk_to_wallet(AbtDid.Type.t(), binary(), Keyword.t()) :: String.t()